当前位置: 首页>>代码示例>>Python>>正文


Python SymbolTable.getFromAnyEnclosingScope方法代码示例

本文整理汇总了Python中SymbolTable.SymbolTable.getFromAnyEnclosingScope方法的典型用法代码示例。如果您正苦于以下问题:Python SymbolTable.getFromAnyEnclosingScope方法的具体用法?Python SymbolTable.getFromAnyEnclosingScope怎么用?Python SymbolTable.getFromAnyEnclosingScope使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SymbolTable.SymbolTable的用法示例。


在下文中一共展示了SymbolTable.getFromAnyEnclosingScope方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TypeChecker

# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import getFromAnyEnclosingScope [as 别名]
class TypeChecker(NodeVisitor):
    def __init__(self):
        self.symbolTable = SymbolTable(None, 'root')
        self.declType = ''
        self.curFunc = None

    def visit_BinExpr(self, node):
        type1 = self.visit(node.left)
        type2 = self.visit(node.right)
        oper = node.op

        if ttype[oper][type1][type2] is None:
            print "Invalid binary expression {}. Line: {}".format(oper, node.line)
        return ttype[oper][type1][type2]

    def visit_Integer(self, node):
        return 'int'

    def visit_Float(self, node):
        return 'float'

    def visit_String(self, node):
        return 'string'

    def visit_Variable(self, node):
        definition = self.symbolTable.getFromAnyEnclosingScope(node.name)

        if definition is None:
            print "Undefined symbol {}. Line: {}".format(node.name, node.line)
        else:
            return definition.type

    def visit_Program(self, node):
        self.visit(node.segments)

    def visit_Declaration(self, node):
        self.declType = node.type
        self.visit(node.inits)
        self.declType = ''

    def visit_Init(self, node):
        initType = self.visit(node.expression)
        if initType == self.declType or (initType == "int" and self.declType == "float") or (
                        initType == "float" and self.declType == "int"):
            if self.symbolTable.get(node.name) is not None:
                print "Symbol {} in line {} is already defined in this scope".format(node.name, node.line)
            else:
                self.symbolTable.put(node.name, VariableSymbol(node.name, self.declType))
        else:
            print "Type mismatch in assignment of {} to {}. Line {}".format(initType, self.declType, node.line)

    def visit_Assignment(self, node):
        definition = self.symbolTable.getFromAnyEnclosingScope(node.id)
        type = self.visit(node.expression)

        if definition is None:
            print "Assignment to undefined symbol {}. Line {}".format(node.id, node.line)
        elif type != definition.type and (definition.type != "float" and definition != "int"):
            print "Type mismatch in assignment of {} to {}. Line {}.".format(type, definition.type, node.line)

    def visit_ParenExpression(self, node):
        return self.visit(node.expression)

    def visit_Fundef(self, node):
        if self.symbolTable.get(node.id) is not None:
            print "Function {} in line {} is already defined in this scope".format(node.id, node.line)
        else:
            func = FunctionSymbol(node.id, node.type, SymbolTable(self.symbolTable, node.id))
            self.symbolTable.put(node.id, func)
            self.curFunc = func
            self.symbolTable = func.table
            if node.args_list is not None:
                self.visit(node.args_list)
            func.setParamTypesFromTable()
            self.visit(node.compound_instr)
            self.symbolTable = self.symbolTable.getParentScope()
            self.curFunc = None

    def visit_CompoundInstruction(self, node):
        innerScope = SymbolTable(self.symbolTable, "innerScope")
        self.symbolTable = innerScope
        self.visit(node.segments)
        self.symbolTable = self.symbolTable.getParentScope()

    def visit_PrintInstruction(self, node):
        self.visit(node.expr_list)

    def visit_LabeledInstruction(self, node):
        self.visit(node.instruction)

    def visit_ChoiceInstruction(self, node):
        self.visit(node.condition)
        self.visit(node.instruction)
        self.visit(node.else_instruction)

    def visit_WhileInstruction(self, node):
        self.visit(node.condition)
        self.visit(node.instruction)

    def visit_RepeatInstruction(self, node):
#.........这里部分代码省略.........
开发者ID:zalon525,项目名称:kompilatory,代码行数:103,代码来源:TypeChecker.py


注:本文中的SymbolTable.SymbolTable.getFromAnyEnclosingScope方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。