implemented TODO for proper depth tracking for py blocks
This commit is contained in:
63
main.py
63
main.py
@@ -2569,35 +2569,82 @@ class Compiler:
|
|||||||
def _load_with_imports(self, path: Path, seen: Optional[Set[Path]] = None) -> str:
|
def _load_with_imports(self, path: Path, seen: Optional[Set[Path]] = None) -> str:
|
||||||
if seen is None:
|
if seen is None:
|
||||||
seen = set()
|
seen = set()
|
||||||
|
|
||||||
path = path.resolve()
|
path = path.resolve()
|
||||||
if path in seen:
|
if path in seen:
|
||||||
return ""
|
return ""
|
||||||
seen.add(path)
|
seen.add(path)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
contents = path.read_text()
|
contents = path.read_text()
|
||||||
except FileNotFoundError as exc:
|
except FileNotFoundError as exc:
|
||||||
raise ParseError(f"cannot import {path}: {exc}") from exc
|
raise ParseError(f"cannot import {path}: {exc}") from exc
|
||||||
|
|
||||||
lines: List[str] = []
|
lines: List[str] = []
|
||||||
|
|
||||||
in_py_block = False
|
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()):
|
for idx, line in enumerate(contents.splitlines()):
|
||||||
stripped = line.strip()
|
stripped = line.strip()
|
||||||
# TODO: implement proper depth tracking for {} in py blocks
|
|
||||||
# Detect :py block start/end
|
# Detect :py { block start
|
||||||
if stripped.startswith(":py") and "{" in stripped:
|
if not in_py_block and stripped.startswith(":py") and "{" in stripped:
|
||||||
in_py_block = True
|
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
|
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()
|
target = stripped.split(None, 1)[1].strip()
|
||||||
if not target:
|
if not target:
|
||||||
raise ParseError(f"empty import target in {path}:{idx + 1}")
|
raise ParseError(f"empty import target in {path}:{idx + 1}")
|
||||||
|
|
||||||
target_path = (path.parent / target).resolve()
|
target_path = (path.parent / target).resolve()
|
||||||
lines.append(self._load_with_imports(target_path, seen))
|
lines.append(self._load_with_imports(target_path, seen))
|
||||||
continue
|
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:
|
def run_nasm(asm_path: Path, obj_path: Path, debug: bool = False) -> None:
|
||||||
cmd = ["nasm", "-f", "elf64"]
|
cmd = ["nasm", "-f", "elf64"]
|
||||||
|
|||||||
Reference in New Issue
Block a user