Skip to content

base

Classes:

Functions:

Builder

Builder()

Bases: ABC

Methods:

Source code in src/irx/builder/base.py
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
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,
    )
    self.runtime_feature_names = set()

activate_runtime_feature

activate_runtime_feature(feature_name: str) -> None
Source code in src/irx/builder/base.py
250
251
252
253
254
255
256
257
def activate_runtime_feature(self, feature_name: str) -> None:
    """
    title: Activate a native runtime feature for this compilation unit.
    parameters:
      feature_name:
        type: str
    """
    self.runtime_feature_names.add(feature_name)

build abstractmethod

build(expr: AST, output_file: str) -> None
Source code in src/irx/builder/base.py
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
@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/builder/base.py
231
232
233
234
235
236
237
def module(self) -> astx.Module:
    """
    title: Create a new ASTx Module.
    returns:
      type: astx.Module
    """
    return astx.Module()

run

run(
    *,
    capture_stderr: bool = True,
    raise_on_error: bool = True,
    debug: bool = False,
) -> CommandResult
Source code in src/irx/builder/base.py
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
def run(
    self,
    *,
    capture_stderr: bool = True,
    raise_on_error: bool = True,
    debug: bool = False,
) -> CommandResult:
    """
    title: Run the generated executable.
    parameters:
      capture_stderr:
        type: bool
        default: true
      raise_on_error:
        type: bool
        default: true
      debug:
        type: bool
        default: false
    returns:
      type: CommandResult
    """
    return run_command(
        [self.output_file],
        capture_stderr=capture_stderr,
        raise_on_error=raise_on_error,
        debug=debug,
    )

translate

translate(expr: AST) -> str
Source code in src/irx/builder/base.py
239
240
241
242
243
244
245
246
247
248
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

Bases: BaseVisitor

Methods:

translate

translate(expr: AST) -> str

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

Source code in src/irx/builder/base.py
176
177
178
179
180
181
182
183
184
185
186
187
188
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.")

visit

visit(node: AST) -> None
Source code in src/irx/builder/base.py
157
158
159
160
161
162
163
164
165
@dispatch
def visit(self, node: astx.AST) -> None:
    """
    title: Visit AST nodes.
    parameters:
      node:
        type: astx.AST
    """
    super().visit(node)

visit_child

visit_child(node: AST) -> None
Source code in src/irx/builder/base.py
167
168
169
170
171
172
173
174
def visit_child(self, node: astx.AST) -> None:
    """
    title: Forward a child AST node through the public visit dispatcher.
    parameters:
      node:
        type: astx.AST
    """
    self.visit(node)

CommandError

CommandError(result: CommandResult)

Bases: RuntimeError

Source code in src/irx/builder/base.py
65
66
67
68
69
70
71
72
73
74
75
76
def __init__(self, result: CommandResult) -> None:
    """
    title: Initialize CommandError.
    parameters:
      result:
        type: CommandResult
    """
    self.result: CommandResult = result
    super().__init__(
        f"Command {list(result.command)!r} failed "
        f"(exit {result.returncode}):\n{result.stderr.strip()}"
    )

CommandResult dataclass

CommandResult(
    stdout: str,
    stderr: str,
    returncode: int,
    command: Sequence[str],
)

Attributes:

success property

success: bool

run_command

run_command(
    command: Sequence[str],
    *,
    capture_stderr: bool = True,
    raise_on_error: bool = True,
    debug: bool = False,
) -> CommandResult
Source code in src/irx/builder/base.py
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
@typechecked
def run_command(
    command: Sequence[str],
    *,
    capture_stderr: bool = True,
    raise_on_error: bool = True,
    debug: bool = False,
) -> CommandResult:
    """
    title: Run a shell command and return a structured CommandResult.
    parameters:
      command:
        type: Sequence[str]
      capture_stderr:
        type: bool
        default: true
      raise_on_error:
        type: bool
        default: true
      debug:
        type: bool
        default: false
    returns:
      type: CommandResult
    raises:
      CommandError: When the command exits non-zero and raise_on_error=True.
    """
    if debug:
        logger.debug("run_command: %s", list(command))

    stderr_arg = subprocess.PIPE if capture_stderr else None

    try:
        proc = subprocess.run(
            command,
            check=False,
            stdout=subprocess.PIPE,
            stderr=stderr_arg,
            text=True,
        )
    except FileNotFoundError as exc:
        result = CommandResult(
            stdout="",
            stderr=str(exc),
            returncode=127,
            command=command,
        )
        if raise_on_error:
            raise CommandError(result) from exc
        return result

    result = CommandResult(
        stdout=proc.stdout or "",
        stderr=proc.stderr or "",
        returncode=proc.returncode,
        command=command,
    )

    if debug:
        logger.debug(
            "exit=%d stdout=%r stderr=%r",
            result.returncode,
            result.stdout[:200],
            result.stderr[:200],
        )

    if raise_on_error and not result.success:
        raise CommandError(result)

    return result