LocalVariableAST.py revision 7839
17839Snilay@cs.wisc.edu#
27839Snilay@cs.wisc.edu# Copyright (c) 2011 Mark D. Hill and David A. Wood
37839Snilay@cs.wisc.edu# All rights reserved.
47839Snilay@cs.wisc.edu#
57839Snilay@cs.wisc.edu# Redistribution and use in source and binary forms, with or without
67839Snilay@cs.wisc.edu# modification, are permitted provided that the following conditions are
77839Snilay@cs.wisc.edu# met: redistributions of source code must retain the above copyright
87839Snilay@cs.wisc.edu# notice, this list of conditions and the following disclaimer;
97839Snilay@cs.wisc.edu# redistributions in binary form must reproduce the above copyright
107839Snilay@cs.wisc.edu# notice, this list of conditions and the following disclaimer in the
117839Snilay@cs.wisc.edu# documentation and/or other materials provided with the distribution;
127839Snilay@cs.wisc.edu# neither the name of the copyright holders nor the names of its
137839Snilay@cs.wisc.edu# contributors may be used to endorse or promote products derived from
147839Snilay@cs.wisc.edu# this software without specific prior written permission.
157839Snilay@cs.wisc.edu#
167839Snilay@cs.wisc.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
177839Snilay@cs.wisc.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
187839Snilay@cs.wisc.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
197839Snilay@cs.wisc.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
207839Snilay@cs.wisc.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
217839Snilay@cs.wisc.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
227839Snilay@cs.wisc.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
237839Snilay@cs.wisc.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
247839Snilay@cs.wisc.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
257839Snilay@cs.wisc.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
267839Snilay@cs.wisc.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
277839Snilay@cs.wisc.edu#
287839Snilay@cs.wisc.edu
297839Snilay@cs.wisc.edufrom slicc.ast.StatementAST import StatementAST
307839Snilay@cs.wisc.edufrom slicc.symbols import Var
317839Snilay@cs.wisc.edu
327839Snilay@cs.wisc.educlass LocalVariableAST(StatementAST):
337839Snilay@cs.wisc.edu    def __init__(self, slicc, type_ast, ident):
347839Snilay@cs.wisc.edu        super(LocalVariableAST, self).__init__(slicc)
357839Snilay@cs.wisc.edu        self.type_ast = type_ast
367839Snilay@cs.wisc.edu        self.ident    = ident
377839Snilay@cs.wisc.edu
387839Snilay@cs.wisc.edu    def __repr__(self):
397839Snilay@cs.wisc.edu        return "[LocalVariableAST: %r %r]" % (self.type_ast, self.ident)
407839Snilay@cs.wisc.edu
417839Snilay@cs.wisc.edu    @property
427839Snilay@cs.wisc.edu    def name(self):
437839Snilay@cs.wisc.edu        return self.var_name
447839Snilay@cs.wisc.edu
457839Snilay@cs.wisc.edu    def generate(self, code):
467839Snilay@cs.wisc.edu        type = self.type_ast.type;
477839Snilay@cs.wisc.edu        ident = "%s" % self.ident;
487839Snilay@cs.wisc.edu
497839Snilay@cs.wisc.edu        # Add to symbol table
507839Snilay@cs.wisc.edu        v = Var(self.symtab, self.ident, self.location, type, ident,
517839Snilay@cs.wisc.edu                self.pairs)
527839Snilay@cs.wisc.edu        self.symtab.newSymbol(v)
537839Snilay@cs.wisc.edu        code += "%s* %s" % (type.c_ident, ident)
547839Snilay@cs.wisc.edu        return type
55