Skip to content

main

Classes:

Functions:

ArxMain dataclass

ArxMain(
    input_files: list[str] = list(),
    output_file: str = "",
    is_lib: bool = False,
    link_mode: Literal["auto", "pie", "no-pie"] = "auto",
)

Methods:

compile

compile(show_llvm_ir: bool = False) -> bool
Source code in src/arx/main.py
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
def compile(self, show_llvm_ir: bool = False) -> bool:
    """
    title: Compile the given input file.
    parameters:
      show_llvm_ir:
        type: bool
    returns:
      type: bool
    """
    tree_ast = self._get_codegen_astx()
    ir = LLVMLiteIR()
    self.output_file = self._resolve_output_file()
    emits_executable = not self.is_lib and self._has_main_entry(tree_ast)
    ir.build(
        tree_ast,
        output_file=self.output_file,
        link=emits_executable,
        link_mode=self.link_mode,
    )
    return emits_executable

run

run(**kwargs: Any) -> None
Source code in src/arx/main.py
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
def run(self, **kwargs: Any) -> None:
    """
    title: Compile the given source code.
    parameters:
      kwargs:
        type: Any
        variadic: keyword
    """
    self.input_files = kwargs.get("input_files", [])
    output_file = kwargs.get("output_file")
    self.output_file = output_file.strip() if output_file else ""
    self.is_lib = kwargs.get("is_lib", False)
    link_mode = str(kwargs.get("link_mode", "auto")).strip().lower()
    if link_mode not in {"auto", "pie", "no-pie"}:
        raise ValueError(
            "Invalid link mode. Expected one of: auto, pie, no-pie."
        )
    self.link_mode = cast(
        Literal["auto", "pie", "no-pie"],
        link_mode,
    )

    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()

    emits_executable = self.compile()
    if kwargs.get("run"):
        if emits_executable is False:
            raise ValueError(
                "`--run` requires `fn main` (or disable `--lib`)."
            )
        self.run_binary()

run_binary

run_binary() -> None
Source code in src/arx/main.py
265
266
267
268
269
270
271
272
273
274
def run_binary(self) -> None:
    """
    title: Run the generated binary.
    """
    binary_path = Path(self.output_file)
    if not binary_path.is_absolute():
        binary_path = Path.cwd() / binary_path
    result = subprocess.run([str(binary_path)], check=False)
    if result.returncode != 0:
        raise SystemExit(result.returncode)

run_shell

run_shell() -> None
Source code in src/arx/main.py
259
260
261
262
263
def run_shell(self) -> None:
    """
    title: Open arx in shell mode.
    """
    raise Exception("Arx Shell is not implemented yet.")

show_ast

show_ast() -> None
Source code in src/arx/main.py
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
def show_ast(self) -> None:
    """
    title: Print the AST for the given input file.
    """
    tree_ast = self._get_astx()
    try:
        print(repr(tree_ast))
    except Exception:
        try:
            if hasattr(tree_ast, "to_json"):
                print(tree_ast.to_json())
                return
        except Exception:
            pass

        if isinstance(tree_ast, astx.AST):
            print(self._format_ast_fallback(tree_ast))
            return

        # Fallback for nodes whose repr visualizer path is not supported.
        print(str(tree_ast))

show_llvm_ir

show_llvm_ir() -> None
Source code in src/arx/main.py
251
252
253
254
255
256
257
def show_llvm_ir(self) -> None:
    """
    title: Compile into LLVM IR the given input file.
    """
    tree_ast = self._get_codegen_astx()
    ir = LLVMLiteIR()
    print(ir.translator.translate(tree_ast))

show_tokens

show_tokens() -> None
Source code in src/arx/main.py
239
240
241
242
243
244
245
246
247
248
249
def show_tokens(self) -> None:
    """
    title: 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
Source code in src/arx/main.py
20
21
22
23
24
25
26
27
28
29
def get_module_name_from_file_path(filepath: str) -> str:
    """
    title: Return the module name from the source file name.
    parameters:
      filepath:
        type: str
    returns:
      type: str
    """
    return filepath.rsplit(os.sep, maxsplit=1)[-1].replace(".x", "")