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


Python SymbolTable.set_return_present方法代码示例

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


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

示例1: TypeChecker

# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import set_return_present [as 别名]

#.........这里部分代码省略.........
            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)
        self.visit(node.body)
        if node.else_body:
            self.visit(node.else_body)

    def visit_WhileInstr(self, node):
        self.visit(node.condition)
        self.symbol_table.set_inside_loop(1)
        self.visit(node.body)
        self.symbol_table.set_inside_loop(0)

    def visit_RepeatInstr(self, node):
        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):
开发者ID:margg,项目名称:tk,代码行数:70,代码来源:TypeChecker.py


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