Skip to content

Main

Arx main module.

ArxMain

The main class for calling Arx compiler.

Source code in src/arx/main.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class ArxMain:
    """The main class for calling Arx compiler."""

    input_files: List[str]
    output_file: str
    is_lib: bool

    def run(self, *args: Any, **kwargs: Any) -> None:
        """Compile the given source code."""
        self.input_files = kwargs.get("input_files", [])
        self.output_file = kwargs.get("output_file", "")
        # is_lib now is the only available option
        self.is_lib = kwargs.get("is_lib", True) or True

        if kwargs.get("show_ast"):
            return self.show_ast()

        if kwargs.get("show_tokens"):
            return self.show_tokens()

        if kwargs.get("show_llvm_ir"):
            return self.show_llvm_ir()

        if kwargs.get("shell"):
            return self.run_shell()

        self.compile()

    def show_ast(self) -> None:
        """Print the AST for the given input file."""
        lexer = Lexer()
        parser = Parser()
        tree_ast = ast.BlockAST()

        for input_file in self.input_files:
            ArxIO.file_to_buffer(input_file)
            module_name = get_module_name_from_file_path(input_file)
            module_ast = parser.parse(lexer.lex(), module_name)
            tree_ast.nodes.append(module_ast)

        printer = ASTtoOutput()
        printer.emit_ast(tree_ast)

    def show_tokens(self) -> None:
        """Print the AST for the given input file."""
        lexer = Lexer()

        for input_file in self.input_files:
            ArxIO.file_to_buffer(input_file)
            tokens = lexer.lex()
            for token in tokens:
                print(token)

    def show_llvm_ir(self) -> None:
        """Compile into LLVM IR the given input file."""
        self.compile(show_llvm_ir=True)

    def run_shell(self) -> None:
        """Open arx in shell mode."""
        raise Exception("Arx Shell is not implemented yet.")

    def compile(self, show_llvm_ir: bool = False) -> None:
        """Compile the given input file."""
        lexer = Lexer()
        parser = Parser()

        tree_ast: ast.BlockAST = ast.BlockAST()

        for input_file in self.input_files:
            ArxIO.file_to_buffer(input_file)
            module_name = get_module_name_from_file_path(input_file)

            module_ast = parser.parse(lexer.lex(), module_name)
            tree_ast.nodes.append(module_ast)

        # todo: now the object generator should work for all files together
        obj_gen = ObjectGenerator("input_file", self.output_file, self.is_lib)
        obj_gen.evaluate(tree_ast, show_llvm_ir)

compile(show_llvm_ir=False)

Compile the given input file.

Source code in src/arx/main.py
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
def compile(self, show_llvm_ir: bool = False) -> None:
    """Compile the given input file."""
    lexer = Lexer()
    parser = Parser()

    tree_ast: ast.BlockAST = ast.BlockAST()

    for input_file in self.input_files:
        ArxIO.file_to_buffer(input_file)
        module_name = get_module_name_from_file_path(input_file)

        module_ast = parser.parse(lexer.lex(), module_name)
        tree_ast.nodes.append(module_ast)

    # todo: now the object generator should work for all files together
    obj_gen = ObjectGenerator("input_file", self.output_file, self.is_lib)
    obj_gen.evaluate(tree_ast, show_llvm_ir)

run(*args, **kwargs)

Compile the given source code.

Source code in src/arx/main.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def run(self, *args: Any, **kwargs: Any) -> None:
    """Compile the given source code."""
    self.input_files = kwargs.get("input_files", [])
    self.output_file = kwargs.get("output_file", "")
    # is_lib now is the only available option
    self.is_lib = kwargs.get("is_lib", True) or True

    if kwargs.get("show_ast"):
        return self.show_ast()

    if kwargs.get("show_tokens"):
        return self.show_tokens()

    if kwargs.get("show_llvm_ir"):
        return self.show_llvm_ir()

    if kwargs.get("shell"):
        return self.run_shell()

    self.compile()

run_shell()

Open arx in shell mode.

Source code in src/arx/main.py
77
78
79
def run_shell(self) -> None:
    """Open arx in shell mode."""
    raise Exception("Arx Shell is not implemented yet.")

show_ast()

Print the AST for the given input file.

Source code in src/arx/main.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def show_ast(self) -> None:
    """Print the AST for the given input file."""
    lexer = Lexer()
    parser = Parser()
    tree_ast = ast.BlockAST()

    for input_file in self.input_files:
        ArxIO.file_to_buffer(input_file)
        module_name = get_module_name_from_file_path(input_file)
        module_ast = parser.parse(lexer.lex(), module_name)
        tree_ast.nodes.append(module_ast)

    printer = ASTtoOutput()
    printer.emit_ast(tree_ast)

show_llvm_ir()

Compile into LLVM IR the given input file.

Source code in src/arx/main.py
73
74
75
def show_llvm_ir(self) -> None:
    """Compile into LLVM IR the given input file."""
    self.compile(show_llvm_ir=True)

show_tokens()

Print the AST for the given input file.

Source code in src/arx/main.py
63
64
65
66
67
68
69
70
71
def show_tokens(self) -> None:
    """Print the AST for the given input file."""
    lexer = Lexer()

    for input_file in self.input_files:
        ArxIO.file_to_buffer(input_file)
        tokens = lexer.lex()
        for token in tokens:
            print(token)

get_module_name_from_file_path(filepath)

Return the module name from the source file name.

Source code in src/arx/main.py
15
16
17
def get_module_name_from_file_path(filepath: str) -> str:
    """Return the module name from the source file name."""
    return filepath.split(os.sep)[-1].replace(".arx", "")