Skip to content

main

Arx main module.

Classes:

  • ArxMain

    The main class for calling Arx compiler.

Functions:

ArxMain dataclass

ArxMain(
    input_files: list[str] = list(),
    output_file: str = "",
    is_lib: bool = False,
)

The main class for calling Arx compiler.

Methods:

  • compile

    Compile the given input file.

  • run

    Compile the given source code.

  • run_shell

    Open arx in shell mode.

  • show_ast

    Print the AST for the given input file.

  • show_llvm_ir

    Compile into LLVM IR the given input file.

  • show_tokens

    Print the AST for the given input file.

compile

compile(show_llvm_ir: bool = False) -> None

Compile the given input file.

Source code in src/arx/main.py
89
90
91
92
93
def compile(self, show_llvm_ir: bool = False) -> None:
    """Compile the given input file."""
    tree_ast = self._get_astx()
    ir = LLVMLiteIR()
    ir.build(tree_ast, output_file=self.output_file)

run

run(*args: Any, **kwargs: Any) -> None

Compile the given source code.

Source code in src/arx/main.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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

run_shell() -> None

Open arx in shell mode.

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

show_ast

show_ast() -> None

Print the AST for the given input file.

Source code in src/arx/main.py
64
65
66
67
def show_ast(self) -> None:
    """Print the AST for the given input file."""
    tree_ast = self._get_astx()
    print(repr(tree_ast))

show_llvm_ir

show_llvm_ir() -> None

Compile into LLVM IR the given input file.

Source code in src/arx/main.py
79
80
81
82
83
def show_llvm_ir(self) -> None:
    """Compile into LLVM IR the given input file."""
    tree_ast = self._get_astx()
    ir = LLVMLiteIR()
    print(ir.translator.translate(tree_ast))

show_tokens

show_tokens() -> None

Print the AST for the given input file.

Source code in src/arx/main.py
69
70
71
72
73
74
75
76
77
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

get_module_name_from_file_path(filepath: str) -> str

Return the module name from the source file name.

Source code in src/arx/main.py
17
18
19
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", "")