当前位置: 首页>>代码示例>>Python>>正文


Python SymbolTable.getGlobal方法代码示例

本文整理汇总了Python中SymbolTable.SymbolTable.getGlobal方法的典型用法代码示例。如果您正苦于以下问题:Python SymbolTable.getGlobal方法的具体用法?Python SymbolTable.getGlobal怎么用?Python SymbolTable.getGlobal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SymbolTable.SymbolTable的用法示例。


在下文中一共展示了SymbolTable.getGlobal方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TypeChecker

# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import getGlobal [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

#.........这里部分代码省略.........
开发者ID:atryda12,项目名称:Compilation-Theory,代码行数:103,代码来源:TypeChecker.py

示例2: TypeChecker

# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import getGlobal [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'
#.........这里部分代码省略.........
开发者ID:WiktorJ,项目名称:Compilation-Theory,代码行数:103,代码来源:TypeChecker.py

示例3: TypeChecker

# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import getGlobal [as 别名]
class TypeChecker(NodeVisitor):
    def __init__(self):
        self.table = SymbolTable(None, "root")
        self.current_type = ""
        self.current_func = None
        self.current_loop = None
        self.errors = False

    def visit_BinExpr(self, node):
        left = self.visit(node.left)
        right = self.visit(node.right)
        op = node.op
        t = types_table[op][left][right]
        if t is None:
            self.errors = True
            print "Cannot perform operation {0} between types {1} and {2} at line {3}".format(
                op, left, right, node.lineno
            )
        return t

    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.id)
        if definition is None:
            self.errors = True
            print "Undefined symbol {0} in line {1}.".format(node.id, node.lineno)
        else:
            return definition.type

    def visit_Fundef(self, node):
        f = self.table.get(node.id)
        if f:
            self.errors = True
            if type(f) == FunctionSymbol:
                print "Function {0} is already defined at line {1}. Redefinition at line {2}.".format(
                    node.id, f.lineno, node.lineno
                )
            else:
                print "Name {0} is already defined at line {1}. Redefinition at line {2}.".format(
                    node.id, f.lineno, node.lineno
                )
        else:
            f = FunctionSymbol(node.id, node.t, SymbolTable(self.table, node.id), node.lineno)
            self.table.put(node.id, f)
            self.current_func = f
            globalTable = self.table
            self.table = self.current_func.table
            if node.args_list is not None:
                self.visit(node.args_list)
            self.visit(node.comp_instr)
            self.table = globalTable
            self.current_func = None

    def visit_Assignment(self, node):
        definition = self.table.getGlobal(node.var)
        t = self.visit(node.expr)
        if definition is None:
            self.errors = True
            print "Undefined symbol: {0} at line {1}".format(node.var, node.lineno)
        elif t != definition.type:
            warning = False
            if definition.type == "float" and t == "int":
                warning = True
            self.errors = self.errors or not warning
            print "{0}Wrong assignment type for symbol: {1}: symbol's type is {2}," \
                  " tried to assign type {3} at line {4}.".format("Warning: " if warning else "",
                                                                  node.var, definition.type,
                                                                  t if t is not None else "undefined",
                                                                  node.lineno)

    def visit_Declaration(self, node):
        self.current_type = node.declaration_type
        self.visit(node.inits)
        self.current_type = ""

    def visit_Init(self, node):
        t = self.visit(node.expression)
        if t != self.current_type:
            warning = False
            if t == "int" and self.current_type == "float":
                warning = True
            self.errors = self.errors or not warning
            print "{0}Initialization to symbol {1}: symbol's type is {2}," \
                  " tried to assign type {3} at line {4}".format("Warning: " if Warning else "",
                                                                 node.var_name, self.current_type,
                                                                 t, node.lineno)
        definition = self.table.get(node.var_name)
        if definition is not None:
            self.errors = True
            print "Variable {0} is already defined at line {1}. Redefinition at line {2}.".format(
                node.var_name, definition.lineno, node.lineno
            )
#.........这里部分代码省略.........
开发者ID:salceson,项目名称:kompilatory,代码行数:103,代码来源:TypeChecker.py


注:本文中的SymbolTable.SymbolTable.getGlobal方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。