當前位置: 首頁>>代碼示例>>Python>>正文


Python SymbolTable.is_inside_loop方法代碼示例

本文整理匯總了Python中SymbolTable.SymbolTable.is_inside_loop方法的典型用法代碼示例。如果您正苦於以下問題:Python SymbolTable.is_inside_loop方法的具體用法?Python SymbolTable.is_inside_loop怎麽用?Python SymbolTable.is_inside_loop使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在SymbolTable.SymbolTable的用法示例。


在下文中一共展示了SymbolTable.is_inside_loop方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: TypeChecker

# 需要導入模塊: from SymbolTable import SymbolTable [as 別名]
# 或者: from SymbolTable.SymbolTable import is_inside_loop [as 別名]

#.........這裏部分代碼省略.........
        self.symbol_table.set_inside_loop(1)
        for item in node.body:
            self.visit(item)
        self.visit(node.condition)
        self.symbol_table.set_inside_loop(0)


    def visit_ReturnInstr(self, node):
        ret_type = self.get_return_type(node.expression)
        scope = self.symbol_table
        while scope and scope.name != "FunctionDef":
            scope = scope.get_parent_scope()
        if scope:
            fun_def = scope.get(scope.function_name)
            if fun_def:
                function_ret_type = fun_def.type.name
                scope.set_return_present(1)
                if ret_type and function_ret_type:
                    if function_ret_type == 'int' and ret_type == 'float':
                        print("Warning: Possible loss of precision: returning %s from function returning %s: line %s" %
                              (ret_type, function_ret_type, node.lineno))
                    elif function_ret_type == 'float' and ret_type == 'int':
                        pass
                    elif ret_type != function_ret_type:
                        print("Error: Improper returned type, expected %s, got %s: line %s" %
                              (function_ret_type, ret_type, node.lineno))
            else:
                # should not happen...
                print("something bad happened while parsing or checking")
        else:
            print("Error: return instruction outside a function: line %s" % node.lineno)

    def visit_ContinueInstr(self, node):
        if not self.symbol_table.is_inside_loop():
            print("Error: continue instruction outside a loop: line %s" % node.lineno)

    def visit_BreakInstr(self, node):
        if not self.symbol_table.is_inside_loop():
            print("Error: break instruction outside a loop: line %s" % node.lineno)

    def visit_CompoundInstr(self, node):
        self.symbol_table = self.symbol_table.push_scope("CompoundInstr")
        for item in node.declarations:
            self.visit(item)
        for item in node.instructions:
            self.visit(item)
        self.symbol_table = self.symbol_table.pop_scope()

    def visit_BinaryExpr(self, node):
        type_1 = self.get_return_type(node.left)
        if not type_1:
            print("Error: Usage of undeclared variable '%s': line %s" % (self.visit(node.left), node.lineno))
        type_2 = self.get_return_type(node.right)
        if not type_2:
            print("Error: Usage of undeclared variable '%s': line %s" % (self.visit(node.right), node.lineno))
        op = self.visit(node.op)
        ret = self.ttypes[op][type_1][type_2]
        if not ret:
            print("Error: Illegal operation, %s %s %s: line %s" % (type_1, op, type_2, node.left.lineno))
        return ret

    def visit_MethodCallExpr(self, node):
        name = self.visit(node.name)
        fun_def = self.symbol_table.get(name)
        if fun_def:
            if len(fun_def.args) != len(node.args):
開發者ID:margg,項目名稱:tk,代碼行數:70,代碼來源:TypeChecker.py


注:本文中的SymbolTable.SymbolTable.is_inside_loop方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。