コード例 #1
0
ファイル: TypeChecker.py プロジェクト: Callisto210/Compilers
    def visit_Function(self, node):
        if self.table.get(node.name) is None:
            fun = FunctionSymbol(node.name, node.type,
                                 SymbolTable(self.table, node.name))
            self.table.put(node.name, fun)
            self.actualFun = fun
            self.table = fun.table
            numOfParameter = 0
            for arg in node.parameters.children:
                self.visit(
                    arg
                )  #Wojtku, nie musisz pisac explicite visit_Parameter. Program sam wnioskuje, ktora funkcje odwiedzic, a jak nie to odwiedza defaultowa
                if self.table.get(arg.name) != None:
                    print "Error: Variable '{}' already declared: line {}".format(
                        arg.name, node.line)
                varSym = VariableSymbol(arg.name, self.visit(arg))
                self.table.put(arg.name, varSym)
                fun.put(str(numOfParameter), varSym)
                numOfParameter = numOfParameter + 1
                #Zamiast nazywac parametry funkcji bedziemy je numerowac

            self.visit(node.body)
            if self.actualFun.returnExists == False:
                print "Error: Missing return statement in function '{}' returning int: line {}".format(
                    node.name, node.body.endline)
            self.table = self.table.getParentScope()
            self.actualFun = None

        else:
            print "Error: Redefinition of function '{}': line {}".format(
                node.name, node.line)
コード例 #2
0
    def visit_FuncDecl(self, node):
        func_name = node.func_name
        func_symbol = FunctionSymbol(func_name)
        self.current_scope.insert(func_symbol)

        #print("ENTER scope: %s" % func_name)
        function_scope = SymbolTable(
            scope_name=func_name,
            scope_level=self.current_scope.scope_level + 1,
            enclosing_scope=self.current_scope
        )
        self.current_scope.children[func_name] = function_scope
        self.current_scope = function_scope

        params = []
        for parameter in node.parameters:
            params.append(self.visit(parameter))

        func_symbol.params = params

        for child in node.children:
            self.visit(child)

        #print(function_scope)
        #print("LEAVE scope: %s" % func_name)
        self.current_scope = self.current_scope.enclosing_scope
コード例 #3
0
ファイル: TypeChecker.py プロジェクト: zalon525/kompilatory
 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
コード例 #4
0
 def visit_FunctionExpression(self, node):
     if self.table.get(node.name) is not None:
         raise MyException("Error: Redefinition of function '{}': line {}".format(node.id, node.line))
     else:
         function = FunctionSymbol(node.retType, node.name, SymbolTable(self.table, node.name))
         self.table.put(node.name, function)
         self.table = function.symbolTable
         self.currentFun = function
         if node.args is not None:
             self.visit(node.args)
         function.extractParams()
         self.visit(node.body)
         self.table = self.table.getParentScope()
         self.currentFun = None
コード例 #5
0
    def visit_FunctionDecl(self, node):
        func_name = node.func_name

        type_name = node.return_type.value
        type_symbol = self.current_scope.lookup(str.upper(type_name))
        func_symbol = FunctionSymbol(func_name, type_symbol)

        self.current_scope.insert(func_symbol)

        print('ENTER scope: %s' % func_name)

        # Scope for parameters and local variables
        function_scope = ScopedSymbolTable(
            scope_name=func_name,
            scope_level=self.current_scope.scope_level + 1,
            enclosing_scope=self.current_scope)

        self.current_scope = function_scope

        # Insert parameters into the function_scope
        for param in node.params:
            param_type = self.current_scope.lookup(
                str.upper(param.type_node.value))
            param_name = param.var_node.value
            var_symbol = VarSymbol(param_name, param_type)
            self.current_scope.insert(var_symbol)
            func_symbol.params.append(var_symbol)

        self.visit(node.block_node)
        print(function_scope)
        self.current_scope = self.current_scope.enclosing_scope
        print('LEAVE scope: %s' % func_name)
コード例 #6
0
    def visit_FunDef(self, node):
        s = self.symbol_table.current_scope
        symbol = s.find(node.name)
        if isinstance(symbol, FunctionSymbol):
            message = "Redefinition of function '{}'".format(node.name)
            self.log_error(message, node.lineno)
        elif isinstance(symbol, VariableSymbol):
            message = "Variable identifier '{}' used as function name".format(
                node.name)
            self.log_error(message, node.lineno)
        else:
            args_type = [arg.arg_type for arg in node.args]
            fun_symbol = FunctionSymbol(node.name, node.return_type, args_type)
            self.symbol_table.current_scope.put(node.name, fun_symbol)

            self.symbol_table.push_scope(ScopeType.FUNCTION, fun_symbol)
            self.visit(node.args)
            self.visit(node.statements)

            if not fun_symbol.has_return:
                message = "Missing return statement in function '{}' returning {}".format(
                    node.name, node.return_type)
                self.log_error(message, node.end_lineno)

            self.symbol_table.pop_scope()
コード例 #7
0
 def visit_FunctionDefinition(self, node, tab):
     fun_name = self.findVariable(tab, node.id)
     if not fun_name is None:
         print "Error: Symbol {0} declared before, line {1}".format(node.id, node.arglist.line)
     else:
         tab.put(node.id, FunctionSymbol(node.id, node.type, node.arglist))
         tab = tab.pushScope(node.id)
         tab.put(node.id, FunctionSymbol(node.id, node.type, node.arglist))
         self.actFunc = node
         self.hasReturn = False
         self.visit(node.arglist, tab)
         self.visit(node.compound_instr, tab, True)
         if self.hasReturn == False:
             print "Error: Missing return instruction for {0} function, line {1}".format(node.id, node.arglist.line)
         self.hasReturn = False
         self.actFunc = None
         tab = tab.popScope()
コード例 #8
0
 def visit_FunctionExpression(self, node):
     if self.table.get(node.name):
         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
コード例 #9
0
 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
コード例 #10
0
 def visit_FunctionExpression(self, node):
     if self.table.get(node.name):
         print_message("Redefinition of function '{}'".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)
         if self.actFunc.returnPresent is False:
             print_message(
                 "Missing return statement in function '{}' returning {}".
                 format(node.name, node.retType), node.line)
         self.table = self.table.getParentScope()
         self.actFunc = None
コード例 #11
0
 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