本文整理汇总了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):
#.........这里部分代码省略.........