implemented TODO for proper depth tracking for py blocks

This commit is contained in:
IgorCielniak
2025-12-30 11:09:40 +00:00
parent 2319785702
commit b186ae2826

63
main.py
View File

@@ -2569,35 +2569,82 @@ class Compiler:
def _load_with_imports(self, path: Path, seen: Optional[Set[Path]] = None) -> str:
if seen is None:
seen = set()
path = path.resolve()
if path in seen:
return ""
seen.add(path)
try:
contents = path.read_text()
except FileNotFoundError as exc:
raise ParseError(f"cannot import {path}: {exc}") from exc
lines: List[str] = []
in_py_block = False
brace_depth = 0
string_char = None # "'" or '"'
escape = False
def scan_line(line: str):
nonlocal brace_depth, string_char, escape
for ch in line:
if string_char:
if escape:
escape = False
elif ch == "\\":
escape = True
elif ch == string_char:
string_char = None
else:
if ch in ("'", '"'):
string_char = ch
elif ch == "{":
brace_depth += 1
elif ch == "}":
brace_depth -= 1
for idx, line in enumerate(contents.splitlines()):
stripped = line.strip()
# TODO: implement proper depth tracking for {} in py blocks
# Detect :py block start/end
if stripped.startswith(":py") and "{" in stripped:
# Detect :py { block start
if not in_py_block and stripped.startswith(":py") and "{" in stripped:
in_py_block = True
if in_py_block and "}" in stripped:
brace_depth = 0
string_char = None
escape = False
scan_line(line)
# Edge case: empty block on same line
if brace_depth == 0:
in_py_block = False
# Only process import as file import if not in :py block
if not in_py_block and stripped.startswith("import "):
lines.append(line)
continue
if in_py_block:
scan_line(line)
if brace_depth == 0:
in_py_block = False
lines.append(line)
continue
# Process imports only outside python blocks
if stripped.startswith("import "):
target = stripped.split(None, 1)[1].strip()
if not target:
raise ParseError(f"empty import target in {path}:{idx + 1}")
target_path = (path.parent / target).resolve()
lines.append(self._load_with_imports(target_path, seen))
continue
lines.append(line)
return "\n".join(lines) + "\n"
lines.append(line)
return "\n".join(lines) + "\n"
def run_nasm(asm_path: Path, obj_path: Path, debug: bool = False) -> None:
cmd = ["nasm", "-f", "elf64"]