Implement --build and --save flags

This commit is contained in:
2026-05-01 23:35:35 -05:00
parent a8e51967ed
commit 5b2b41f01b

View File

@@ -1,6 +1,7 @@
import argparse import argparse
import subprocess import subprocess
import sys import sys
import os
from pathlib import Path from pathlib import Path
from .transpiler import transpile from .transpiler import transpile
@@ -8,18 +9,49 @@ from .transpiler import transpile
def main(): def main():
parser = argparse.ArgumentParser(description="Tamil Python compiler") parser = argparse.ArgumentParser(description="Tamil Python compiler")
parser.add_argument("file", help="Tamil Python file") parser.add_argument("file", help="Tamil Python file to run")
parser.add_argument("--build", action="store_true", help="Build only") parser.add_argument(
"--build",
action="store_true",
help="Build to Python file only, don't run"
)
parser.add_argument(
"--save",
action="store_true",
help="Save generated .py file for debugging"
)
args = parser.parse_args() args = parser.parse_args()
code = Path(args.file).read_text(encoding="utf-8") file_path = Path(args.file)
generated = transpile(code) if not file_path.exists():
out = Path(args.file).with_suffix(".generated.py") print(f"Error: File not found: {args.file}", file=sys.stderr)
out.write_text(generated, encoding="utf-8") sys.exit(1)
print(f"Generated: {out.name}")
with open(file_path, "r", encoding="utf-8") as f:
code = f.read()
generated_code = transpile(code)
generated_path = file_path.with_suffix(".generated.py")
# Always save generated file
generated_path.write_text(generated_code, encoding="utf-8")
print(f"Generated: {generated_path.name}")
# Run if not --build
if not args.build: if not args.build:
subprocess.run([sys.executable, str(out)], capture_output=True) result = subprocess.run(
[sys.executable, str(generated_path)],
capture_output=True,
text=True
)
if result.returncode != 0:
print(result.stderr, file=sys.stderr)
sys.exit(result.returncode)
# Delete original if not --save
if not args.save and not args.build:
file_path.unlink()
if __name__ == "__main__": if __name__ == "__main__":