本文整理匯總了Python中SymbolTable.SymbolTable.getDirect方法的典型用法代碼示例。如果您正苦於以下問題:Python SymbolTable.getDirect方法的具體用法?Python SymbolTable.getDirect怎麽用?Python SymbolTable.getDirect使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類SymbolTable.SymbolTable
的用法示例。
在下文中一共展示了SymbolTable.getDirect方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: TypeChecker
# 需要導入模塊: from SymbolTable import SymbolTable [as 別名]
# 或者: from SymbolTable.SymbolTable import getDirect [as 別名]
#.........這裏部分代碼省略.........
return value.type
else:
print("Undefined variable {0} at {1}:{2}".format(node.name, node.pos[0], node.pos[1]))
return None
def visit_If(self, node):
condition_type = node.condition.accept(self)
if condition_type != 'int':
print("Condition must evaluate to integer at {0}:{1}".format(node.condition.pos[0], node.condition.pos[1]))
node.block_if.accept(self)
if node.block_else != None:
node.block_else.accept(self)
def visit_While(self, node):
condition_type = node.condition.accept(self)
if condition_type != 'int':
print("Condition must evaluate to integer at {0}:{1}".format(node.condition.pos[0], node.condition.pos[1]))
old_breakable = self.breakable
self.breakable = True
node.body.accept(self)
self.breakable = old_breakable
def visit_Repeat(self, node):
condition_type = node.condition.accept(self)
if condition_type != 'int':
print("Condition must evaluate to integer at {0}:{1}".format(node.condition.pos[0], node.condition.pos[1]))
old_breakable = self.breakable
self.breakable = True
node.body.accept(self)
self.breakable = old_breakable
def visit_Fundef(self, node):
symbol = self.scope.getDirect(node.name)
if symbol is None:
self.scope.put(node.name, node)
else:
print("Symbol {0} already defined at {1}:{2}. First defined at {3}:{4}".format(
node.name, node.pos[0], node.pos[1], symbol.pos[0], symbol.pos[1]))
# Create new scope for function
self.scope = SymbolTable(self.scope, node.name)
# Leave information about return type of the function
self.return_type = node.return_type
self.returned = False
for argument in node.arguments:
argument.accept(self)
node.body.accept(self)
if not self.returned:
print("No return statement found in function {0} defined at {1}:{2}".format(node.name, node.pos[0], node.pos[1]))
# Clear information about return type
self.return_type = None
self.returned = False
# Get the hell out of function scope, after its done
self.scope = self.scope.parent
def visit_Arg(self, node):
self.scope.put(node.id, node)
return node.type
def visit_Declaration(self, node):
for init in node.inits:
init.type = node.type
defined_var = self.scope.getDirect(init.name.name)
if defined_var is not None: