本文整理汇总了Python中SymbolTable.SymbolTable.pop_scope方法的典型用法代码示例。如果您正苦于以下问题:Python SymbolTable.pop_scope方法的具体用法?Python SymbolTable.pop_scope怎么用?Python SymbolTable.pop_scope使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SymbolTable.SymbolTable
的用法示例。
在下文中一共展示了SymbolTable.pop_scope方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TypeChecker
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import pop_scope [as 别名]
#.........这里部分代码省略.........
self.add_ttype('>', 'string', 'string', 'string')
self.add_ttype('<=', 'string', 'string', 'string')
self.add_ttype('>=', 'string', 'string', 'string')
def add_ttype(self, operation, operand1, operand2, returned):
self.ttypes[operation][operand1][operand2] = returned
def visit_Name(self, node):
return node.name
def visit_CheckedName(self, node):
if self.symbol_table.get(node.name):
return node.name
else:
print("Error: Usage of undeclared variable '%s': line %s" % (node.name, node.lineno))
return node.name
def visit_Operator(self, node):
return node.op
def visit_Integer(self, node):
return 'int'
def visit_Float(self, node):
return 'float'
def visit_String(self, node):
return 'string'
def visit_Program(self, node):
self.symbol_table = self.symbol_table.push_scope("Program")
for item in node.body:
self.visit(item)
self.symbol_table = self.symbol_table.pop_scope()
def visit_Declaration(self, node):
for init in node.inits:
name = self.visit(init.name)
var = self.symbol_table.get_declared_var(name)
if var:
if isinstance(var, FunctionDefSymbol):
print("Error: Function identifier '%s' used as a variable: line %d" % (name, node.lineno))
else:
print("Error: Variable '%s' already declared: line %d" % (name, node.lineno))
else:
self.symbol_table.put(name, VariableSymbol(init.name, node.var_type))
self.visit(init)
def visit_Initializer(self, node):
expression_ret_type = self.get_return_type(node.expression)
declared_type = self.get_return_type(node.name)
if expression_ret_type and declared_type:
TypeChecker.check_type_consistency(node, declared_type, expression_ret_type)
def visit_PrintInstr(self, node):
for item in node.expr_list:
self.visit(item)
def visit_LabeledInstr(self, node):
self.visit(node.instruction)
def visit_Assignment(self, node):
declared_type = self.get_return_type(node.target)
expression_ret_type = self.get_return_type(node.value)
if not declared_type:
print("Error: Variable '%s' undefined in current scope: line %d" % (self.visit(node.target), node.lineno))