finish refactoring

This commit is contained in:
Jan-Niclas Loosen
2025-12-11 01:19:53 +01:00
parent 5ce2c5a281
commit 99210f6e0d
2 changed files with 37 additions and 35 deletions

View File

@@ -59,13 +59,6 @@ def pretty_print(node, indent=0):
else:
print(f"{prefix} {key}: {value}")
# Exports the AST as a DOT file
def export_dot_file(dot_string: str, filename: str):
try:
Path(filename).write_text(dot_string, encoding="utf-8")
print(f"Saved DOT file as: {filename}")
except Exception as e:
print(f"Could not save DOT file: {e}")
if __name__ == "__main__":
print("\nTRIPLA Parser and TRIPLA to TRAM Compiler")
@@ -99,10 +92,17 @@ if __name__ == "__main__":
dot_str = ast.to_dot()
if cnsl.prompt_confirmation("Export AST as .dot file?"):
default = f"{path.stem}.dot"
cnsl = input(f"Filename [{default}]: ").strip()
if not cnsl:
cnsl = default
export_dot_file(dot_str, cnsl)
filename = input(f"Filename [{default}]: ").strip()
if not filename:
filename = default
try:
base_dir = Path(__file__).parent
out_path = base_dir / filename
with open(out_path, "w") as f:
f.write(dot_str)
print(f"Saved DOT file as: {out_path}")
except Exception as e:
print(f"Could not save DOT file: {e}")
# Display AST diagram
if cnsl.prompt_confirmation("Display AST diagram?"):
@@ -112,11 +112,13 @@ if __name__ == "__main__":
elif mode == 1:
tram_code = assemble(ast)
# Print TRAM code
if cnsl.prompt_confirmation("\nPrint TRAM code to console?"):
print("\nGenerated TRAM code:\n")
for instr in tram_code:
print(instr.toString())
# Save TRAM code
if cnsl.prompt_confirmation("Save TRAM code as .tram file?"):
base_dir = Path(__file__).parent / "tramcodes"
base_dir.mkdir(exist_ok=True)
@@ -127,13 +129,13 @@ if __name__ == "__main__":
filename = default
out_path = base_dir / filename
with open(out_path, "w") as f:
for instr in tram_code:
f.write(instr.toString() + "\n")
print(f"Saved TRAM code to: {out_path}")
# Display TRAM code in Visual TRAM UI
if cnsl.prompt_confirmation("Display TRAM code in Visual TRAM UI?"):
root = tk.Tk()
ui = MachineUI(root)