diff --git a/Project-02-03/main.py b/Project-02-03/main.py index e152db4..58da03a 100644 --- a/Project-02-03/main.py +++ b/Project-02-03/main.py @@ -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) diff --git a/Project-02-03/tramcodes/homework.tram b/Project-02-03/tramcodes/homework.tram index 101989a..1694e6f 100644 --- a/Project-02-03/tramcodes/homework.tram +++ b/Project-02-03/tramcodes/homework.tram @@ -1,47 +1,47 @@ - GOTO L13 -L14: NOP + GOTO L1 +L2: NOP LOAD 0 0 CONST 0 EQ - IFZERO L16 - GOTO L18 -L19: NOP + IFZERO L4 + GOTO L6 +L7: NOP LOAD 0 0 LOAD 1 1 MUL LOAD 1 0 ADD RETURN -L18: NOP +L6: NOP CONST 2 LOAD 1 0 MUL LOAD 0 0 - INVOKE 2 L19 0 - GOTO L17 -L16: NOP + INVOKE 2 L7 0 + GOTO L5 +L4: NOP LOAD 0 0 CONST 1 ADD -L17: NOP +L5: NOP RETURN -L15: NOP +L3: NOP LOAD 1 0 CONST 0 GT - IFZERO L22 - GOTO L23 -L20: NOP + IFZERO L10 + GOTO L11 +L8: NOP LOAD 1 0 CONST 0 GT - IFZERO L21 + IFZERO L9 POP -L23: NOP +L11: NOP LOAD 0 0 LOAD 0 0 LOAD 1 0 - INVOKE 2 L14 1 + INVOKE 2 L2 1 MUL STORE 0 0 LOAD 0 0 @@ -51,21 +51,21 @@ L23: NOP SUB STORE 1 0 LOAD 1 0 - GOTO L20 -L22: NOP + GOTO L8 +L10: NOP CONST None -L21: NOP +L9: NOP POP LOAD 0 0 CONST 42 ADD RETURN -L13: NOP +L1: NOP CONST 1 CONST 2 - INVOKE 2 L14 0 + INVOKE 2 L2 0 POP CONST 3 CONST 3 - INVOKE 2 L15 0 + INVOKE 2 L3 0 HALT