예제 #1
0
    def visitFunctionDecl(self, ctx: GrammarParser.FunctionDeclContext):
        decl_info_list = [
            self.extract_decl(ctx.pureDecl(i))
            for i in range(len(ctx.pureDecl()))
        ]

        my_ast = AST.FunctionDeclaration(decl_info_list[0][0])
        my_ast.set_source_loc(source_from_ctx(ctx))

        # set type & arg_count of function
        my_st_entry = FuncEntry(decl_info_list[0][1])
        my_st_entry.arg_count = len(decl_info_list) - 1

        self.enter_scope(my_ast)

        # add variable decls for each argument & add arguments info to function's st entries
        for arg_nr in range(1, len(decl_info_list)):
            arg_ctx = ctx.pureDecl(arg_nr)
            arg = AST.Variable(decl_info_list[arg_nr][0])
            arg.set_source_loc(source_from_ctx(arg_ctx))
            decl = AST.Decl()
            decl.set_source_loc(source_from_ctx(arg_ctx))
            decl.add_child(arg)
            my_ast.add_child(decl)
            self._current_sym_table[arg.get_name()] = VarEntry(
                decl_info_list[arg_nr][1])
            my_st_entry.arg_types.append(decl_info_list[arg_nr][1])

        self.exit_scope(my_ast)

        # save function's st entry
        self._current_sym_table[decl_info_list[0][0]] = my_st_entry
        return my_ast
예제 #2
0
    def visitDecl(self, ctx):
        my_ast = AST.Decl()
        decl_info = self.extract_decl(ctx.pureDecl())
        var = AST.Variable(decl_info[0])
        var.set_source_loc(source_from_ctx(ctx))
        my_ast.add_child(var)

        self._current_sym_table[decl_info[0]] = VarEntry(decl_info[1], None)

        my_ast.set_source_loc(source_from_ctx(ctx))
        if ctx.ASSIGN_OP() is not None:
            assign = AST.AssignOp()
            assign.add_child(my_ast)
            assign.add_child(self.visit(ctx.expr()))
            assign.set_source_loc(source_from_ctx(ctx))
            return assign
        else:
            return my_ast