decoder compiles

This commit is contained in:
Arnav Sacheti
2025-10-19 00:06:45 -07:00
parent 0c66453ba0
commit bb1ac6bde9
21 changed files with 335 additions and 416 deletions

View File

@@ -1,16 +1,18 @@
from __future__ import annotations
from textwrap import indent
from types import EllipsisType
from typing import Self
from .body import Body, SupportsStr
class IfBody(Body):
def __init__(self, *, indent: int = 4) -> None:
def __init__(self) -> None:
super().__init__()
# (None means 'else')
self._branches: list[tuple[SupportsStr | None, Body]] = []
self._has_else = False
self._indent = " " * indent
# --- Item access: if/else-if via condition; else via Ellipsis/None ---
def __getitem__(self, condition: SupportsStr | EllipsisType | None) -> Body:
@@ -63,7 +65,7 @@ class IfBody(Body):
) -> bool:
return False
def cm(self, condition: SupportsStr | None) -> "IfBody._BranchCtx":
def cm(self, condition: SupportsStr | None) -> IfBody._BranchCtx:
"""Use with: with ifb.cm('cond') as b: ... ; use None for else."""
return IfBody._BranchCtx(self, condition)
@@ -79,6 +81,12 @@ class IfBody(Body):
out.append("else begin")
body_str = str(body)
if body_str:
out.extend(self._indent + ln for ln in body_str.splitlines())
out.extend(indent(ln, " ") for ln in body_str.splitlines())
out.append("end")
return "\n".join(out)
def __len__(self) -> int:
return len(self._branches)
def __bool__(self) -> bool:
return bool(self._branches)