本文整理匯總了Python中SymbolTable.SymbolTable.get_declared_var方法的典型用法代碼示例。如果您正苦於以下問題:Python SymbolTable.get_declared_var方法的具體用法?Python SymbolTable.get_declared_var怎麽用?Python SymbolTable.get_declared_var使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類SymbolTable.SymbolTable
的用法示例。
在下文中一共展示了SymbolTable.get_declared_var方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: TypeChecker
# 需要導入模塊: from SymbolTable import SymbolTable [as 別名]
# 或者: from SymbolTable.SymbolTable import get_declared_var [as 別名]
#.........這裏部分代碼省略.........
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))
elif expression_ret_type:
TypeChecker.check_type_consistency(node, declared_type, expression_ret_type)
def visit_IfInstr(self, node):
self.visit(node.condition)