Skip to content

base

Define the public irx API.

Classes:

Functions:

  • run_command

    Run a shell command and return its stdout as a string.

Builder

Builder()

Bases: ABC

ASTx Builder.

Methods:

  • build

    Transpile ASTx to LLVM-IR and build an executable file.

  • module

    Create a new ASTx Module.

  • run

    Run the generated executable.

  • translate

    Transpile ASTx to LLVM-IR.

Source code in src/irx/builders/base.py
61
62
63
64
65
66
67
68
69
70
71
72
def __init__(self) -> None:
    """Initialize Builder object."""
    self.translator = BuilderVisitor()
    self.tmp_path = ""
    self.output_file = ""
    self.sh_args: Dict[str, Any] = dict(
        _in=sys.stdin,
        _out=sys.stdout,
        _err=sys.stderr,
        _env=os.environ,
        # _new_session=True,
    )

build abstractmethod

build(expr: AST, output_file: str) -> None

Transpile ASTx to LLVM-IR and build an executable file.

Source code in src/irx/builders/base.py
82
83
84
85
86
87
88
89
@abstractmethod
def build(
    self,
    expr: astx.AST,
    output_file: str,  # noqa: F841, RUF100
) -> None:
    """Transpile ASTx to LLVM-IR and build an executable file."""
    ...

module

module() -> Module

Create a new ASTx Module.

Source code in src/irx/builders/base.py
74
75
76
def module(self) -> astx.Module:
    """Create a new ASTx Module."""
    return astx.Module()

run

run() -> str

Run the generated executable.

Source code in src/irx/builders/base.py
91
92
93
def run(self) -> str:
    """Run the generated executable."""
    return run_command([self.output_file])

translate

translate(expr: AST) -> str

Transpile ASTx to LLVM-IR.

Source code in src/irx/builders/base.py
78
79
80
def translate(self, expr: astx.AST) -> str:
    """Transpile ASTx to LLVM-IR."""
    return self.translator.translate(expr)

BuilderVisitor

Builder translator visitor.

Methods:

  • translate

    Translate an ASTx expression to string.

translate

translate(expr: AST) -> str

Translate an ASTx expression to string.

Example of how it could be implemented:

self.visit(expr)
return str(self.result)
Source code in src/irx/builders/base.py
39
40
41
42
43
44
45
46
47
48
def translate(self, expr: astx.AST) -> str:
    """
    Translate an ASTx expression to string.

    Example of how it could be implemented:

        self.visit(expr)
        return str(self.result)
    """
    raise Exception("Not implemented yet.")

run_command

run_command(command: Sequence[str]) -> str

Run a shell command and return its stdout as a string.

Raises CalledProcessError if the command exits with a non-zero status.

Source code in src/irx/builders/base.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@typechecked
def run_command(command: Sequence[str]) -> str:
    """
    Run a shell command and return its stdout as a string.

    Raises CalledProcessError if the command exits with a non-zero status.
    """
    try:
        result = subprocess.run(
            command, check=True, capture_output=True, text=True
        )
        output = result.stdout
    except subprocess.CalledProcessError as e:
        output = str(e.returncode)

    return output