本文整理汇总了Python中SymbolTable.SymbolTable.getParentScope方法的典型用法代码示例。如果您正苦于以下问题:Python SymbolTable.getParentScope方法的具体用法?Python SymbolTable.getParentScope怎么用?Python SymbolTable.getParentScope使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SymbolTable.SymbolTable
的用法示例。
在下文中一共展示了SymbolTable.getParentScope方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TypeChecker
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import getParentScope [as 别名]
#.........这里部分代码省略.........
toReturn.append(id.value)
return toReturn
def makeClassContentName(self, className, objectName, fieldName):
return "__"+className+objectName+fieldName
def visit_Inits(self,node):
# print "visiting Inits"
toReturn=[]
for element in node.list:
toReturn.append(element.accept(self))
return toReturn
def visit_Init(self,node):
# print "visiting Init"
return [node.expression.accept(self),node.id]
def visit_Classinits(self,node):
# print "visiting Classinits"
toReturn=[]
for element in node.list:
toReturn.append(element.accept(self))
return toReturn
def visit_Classinit(self,node):
# print "visiting Classinit"
return node.id
def visit_Instructions(self,node):
# print "visiting Instructions"
self.symbolTable = SymbolTable(self.symbolTable,'instructions')
for element in node.list :
element.accept(self)
self.symbolTable = self.symbolTable.getParentScope()
def visit_PrintInstr(self,node):
# print "visiting PrintInstr"
if node.expression.accept(self) not in ['string','int','float']:
self.error("cannot print expression of that type",node.line)
def visit_LabeledInstr(self,node):
# print "visiting LabeledInstr"
node.instruction.accept(self)
def visit_Assignment(self,node):
# print "visiting Assignment"
try:
idType = node.access.accept(self)
exprType = node.expression.accept(self)
self.ttype['f'][idType][exprType]
except:
self.error("cannot assign "+exprType+" to "+idType,node.id.line)
def visit_ChoiceInstr(self,node):
# print "visiting ChoiceInstr"
node.condition.accept(self)
node.instruction.accept(self)
node.elseInstruction.accept(self)
def visit_Break(self,node):
# print "visiting Break"
pass
def visit_Continue(self,node):
# print "visiting Continue"
示例2: TypeChecker
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import getParentScope [as 别名]
#.........这里部分代码省略.........
for element in node.list :
element.accept(self)
def visit_Declaration(self,node):
#print "visiting Declaration"
declType = node.type
allInits = node.inits.accept(self)
for element in allInits:
[type,id] = element
if self.symbolTable.get(id.value) != None:
self.error("Symbol: "+id.value+", was previusly declared",id.line)
try:
self.ttype['f'][declType][type]
except:
self.error("cannot initialize symbol of type: "+declType+", with expression of type: "+type,id.value)
self.symbolTable.put(id.value,type)
def visit_Inits(self,node):
#print "visiting Inits"
toReturn=[]
for element in node.list:
toReturn.append(element.accept(self))
return toReturn
def visit_Init(self,node):
#print "visiting Init"
return [node.expression.accept(self),node.id]
def visit_Instructions(self,node):
#print "visiting Instructions"
self.symbolTable = SymbolTable(self.symbolTable,'instructions')
for element in node.list :
element.accept(self)
self.symbolTable = self.symbolTable.getParentScope()
def visit_PrintInstr(self,node):
#print "visiting PrintInstr"
if node.expression.accept(self) not in ['string','int','float']:
self.error("cannot print expression of that type",node.line)
def visit_LabeledInstr(self,node):
#print "visiting LabeledInstr"
node.instruction.accept(self)
def visit_Assignment(self,node):
#print "visiting Assignment"
if self.symbolTable.getIncludingParents(node.id.value) == None:
self.error("unknown symbol name: "+id.value,id.line)
try:
idType = node.id.accept(self)
exprType = node.expression.accept(self)
self.ttype['f'][idType][exprType]
except:
self.error("cannot assign "+exprType+" to "+idType,node.id.line)
def visit_ChoiceInstr(self,node):
#print "visiting ChoiceInstr"
node.condition.accept(self)
node.instruction.accept(self)
node.elseInstruction.accept(self)
def visit_Break(self,node):
#print "visiting Break"
pass
示例3: TypeChecker
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import getParentScope [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):
#.........这里部分代码省略.........
示例4: TypeChecker
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import getParentScope [as 别名]
class TypeChecker(NodeVisitor):
def visit_BinExpr(self, node):
if isinstance(node.left, str):
type1 = node.left
else:
type1 = self.visit(node.left) # type1 = node.left.accept(self)
if isinstance(node.right, str):
type2 = node.right
else:
type2 = self.visit(node.right) # type2 = node.right.accept(self)
op = node.op
if ttype[op][type1][type2] is None:
print("Wrong expression " + op + " in line: " + self.my_str(node.line))
return ttype[op][type1][type2]
def visit_UnaryExpr(self, node):
return self.visit(node.expr)
def visit_PrintInstruction(self, node):
self.visit(node.expr_list)
def visit_FunctionExpression(self, node):
function_definition = self.symbol_table.getGlobal(node.name)
if function_definition is None or not isinstance(function_definition, FunctionSymbol):
print("function " + node.name + " is not defined" + " in line: " + self.my_str(node.line))
else:
if node.expr is not None:
types = [self.visit(child) for child in node.expr.children]
declared_types = function_definition.args
if len(types) != len((declared_types)):
print("Wrong arguments in function " + node.name + " in line: " + self.my_str(node.line))
else:
for given_type, declared_type in zip(types, declared_types):
if given_type != declared_type:
print("Mismatching argument types Expected " + self.my_str(declared_type) + ", got " + self.my_str(given_type) + "in line: " + self.my_str(node.line))
elif function_definition.args != []:
print("Worng number of arguments in function: " + node.name + "in line: " + self.my_str(node.line))
return function_definition.type
def visit_Variable(self, node):
dec = self.symbol_table.getGlobal(node.name)
if dec is None:
print("Undefined symbol: " + node.name + "in line: " + self.my_str(node.line))
else:
return dec.type
def visit_WhileInstr(self, node):
self.is_in_loop = True
self.visit(node.condition)
self.visit(node.instruction)
self.is_in_loop = False
def visit_RepeatInstr(self, node):
self.is_in_loop = True
self.visit(node.condition)
self.visit(node.instructions)
self.is_in_loop = False
def visit_Return_instr(self, node):
if self.current_function is None:
print("Return placed outside of a function in line " + self.my_str(node.line))
else:
type = self.visit(node.expression)
if type != self.current_function.type:
print("Expected reutrn type " + self.my_str(self.current_function.type) + " actual" + self.my_str(type)+ "in line: " + self.my_str(self.my_str(node.line)))
def visit_Fundef(self, node):
if self.symbol_table.get(node.id):
print("Function " + node.id + "already defined" + "in line: " + self.my_str(node.line))
else:
function = FunctionSymbol(node.id, node.type, SymbolTable(self.symbol_table, node.id))
self.symbol_table.put(node.id, function)
self.current_function = function
self.symbol_table = self.current_function.symbol_table
if node.arg_list is not None:
self.visit(node.arg_list)
self.visit(node.compound_instr)
self.symbol_table = self.symbol_table.getParentScope()
self.current_function= None
def visit_Arg(self, node):
if self.symbol_table.get(node.id) is not None:
print("Double argument in function: " + node.id + "in line: " + self.my_str(node.line))
else:
self.symbol_table.put(node.id, VariableSymbol(node.id, node.type))
self.current_function.put_arg(node.type)
def visit_RelExpr(self, node):
type1 = self.visit(node.left) # type1 = node.left.accept(self)
type2 = self.visit(node.right) # type2 = node.right.accept(self)
# ...
#
def visit_Integer(self, node):
return 'int'
def visit_Float(self, node):
return 'float'
#.........这里部分代码省略.........
示例5: TypeChecker
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import getParentScope [as 别名]
class TypeChecker(NodeVisitor):
def __init__(self):
self.table = SymbolTable(None, "root")
self.actType = ""
self.isValid = True
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.table.getGlobal(node.name)
if definition is None:
self.isValid = False
print "Undefined symbol {} in line {}".format(node.name, node.line)
else:
return definition.type
def visit_BinExpr(self, node):
lhs = self.visit(node.lhs)
rhs = self.visit(node.rhs)
op = node.op
if ttype[op][lhs][rhs] is None:
self.isValid = False
print "Bad expression {} in line {}".format(node.op, node.line)
return ttype[op][lhs][rhs]
def visit_AssignmentInstruction(self, node):
definition = self.table.getGlobal(node.id)
type = self.visit(node.expr)
if definition is None:
self.isValid = False
print "Used undefined symbol {} in line {}".format(node.id, node.line)
elif type != definition.type and (definition.type != "float" and definition != "int"):
self.isValid = False
print "Bad assignment of {} to {} in line {}.".format(type, definition.type, node.line)
def visit_GroupedExpression(self, node):
return self.visit(node.interior)
def visit_FunctionExpression(self, node):
if self.table.get(node.name):
self.isValid = False
print "Function {} already defined. Line: {}".format(node.name, node.line)
else:
function = FunctionSymbol(node.name, node.retType, SymbolTable(self.table, node.name))
self.table.put(node.name, function)
self.actFunc = function
self.table = self.actFunc.table
if node.args is not None:
self.visit(node.args)
self.visit(node.body)
self.table = self.table.getParentScope()
self.actFunc = None
def visit_CompoundInstruction(self, node):
innerScope = SymbolTable(self.table, "innerScope")
self.table = innerScope
if node.declarations is not None:
self.visit(node.declarations)
self.visit(node.instructions)
self.table = self.table.getParentScope()
def visit_ArgumentList(self, node):
for arg in node.children:
self.visit(arg)
self.actFunc.extractParams()
def visit_Argument(self, node):
if self.table.get(node.name) is not None:
self.isValid = False
print "Argument {} already defined. Line: {}".format(node.name, node.line)
else:
self.table.put(node.name, VariableSymbol(node.name, node.type))
def visit_InvocationExpression(self, node):
funDef = self.table.getGlobal(node.name)
if funDef is None or not isinstance(funDef, FunctionSymbol):
self.isValid = False
print "Function {} not defined. Line: {}".format(node.name, node.line)
else:
if (node.args is None and funDef.params != []) or len(node.args.children) != len(funDef.params):
self.isValid = False
print "Invalid number of arguments in line {}. Expected {}".format(node.line, len(funDef.params))
else:
types = [self.visit(x) for x in node.args.children]
expectedTypes = funDef.params
for actual, expected in zip(types, expectedTypes):
if actual != expected and not (actual == "int" and expected == "float"):
self.isValid = False
print "Mismatching argument types in line {}. Expected {}, got {}".format(
node.line, expected, actual
)
return funDef.type
#.........这里部分代码省略.........