當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。