Skip to content

base

Classes:

Functions:

Builder

Builder()

Bases: ABC

Methods:

Source code in src/irx/builders/base.py
84
85
86
87
88
89
90
91
92
93
94
95
96
97
def __init__(self) -> None:
    """
    title: 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
Source code in src/irx/builders/base.py
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
@abstractmethod
def build(
    self,
    expr: astx.AST,
    output_file: str,  # noqa: F841, RUF100
) -> None:
    """
    title: Transpile ASTx to LLVM-IR and build an executable file.
    parameters:
      expr:
        type: astx.AST
      output_file:
        type: str
    """
    ...

module

module() -> Module
Source code in src/irx/builders/base.py
 99
100
101
102
103
104
105
def module(self) -> astx.Module:
    """
    title: Create a new ASTx Module.
    returns:
      type: astx.Module
    """
    return astx.Module()

run

run() -> str
Source code in src/irx/builders/base.py
134
135
136
137
138
139
140
def run(self) -> str:
    """
    title: Run the generated executable.
    returns:
      type: str
    """
    return run_command([self.output_file])

translate

translate(expr: AST) -> str
Source code in src/irx/builders/base.py
107
108
109
110
111
112
113
114
115
116
def translate(self, expr: astx.AST) -> str:
    """
    title: Transpile ASTx to LLVM-IR.
    parameters:
      expr:
        type: astx.AST
    returns:
      type: str
    """
    return self.translator.translate(expr)

BuilderVisitor

Methods:

translate

translate(expr: AST) -> str

self.visit(expr) return str(self.result)

Source code in src/irx/builders/base.py
48
49
50
51
52
53
54
55
56
57
58
59
60
def translate(self, expr: astx.AST) -> str:
    """
    title: Translate an ASTx expression to string.
    parameters:
      expr:
        type: astx.AST
    returns:
      type: str
    examples: |-
      self.visit(expr)
      return str(self.result)
    """
    raise Exception("Not implemented yet.")

run_command

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

Raises CalledProcessError if the command exits with a non-zero status. parameters: command: type: Sequence[str] returns: type: str

Source code in src/irx/builders/base.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
@typechecked
def run_command(command: Sequence[str]) -> str:
    """
    title: Run a shell command and return its stdout as a string.
    summary: >-
      Raises CalledProcessError if the command exits with a non-zero status.
    parameters:
      command:
        type: Sequence[str]
    returns:
      type: str
    """
    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