finish refactoring
This commit is contained in:
@@ -59,13 +59,6 @@ def pretty_print(node, indent=0):
|
|||||||
else:
|
else:
|
||||||
print(f"{prefix} {key}: {value}")
|
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__":
|
if __name__ == "__main__":
|
||||||
print("\nTRIPLA Parser and TRIPLA to TRAM Compiler")
|
print("\nTRIPLA Parser and TRIPLA to TRAM Compiler")
|
||||||
@@ -99,10 +92,17 @@ if __name__ == "__main__":
|
|||||||
dot_str = ast.to_dot()
|
dot_str = ast.to_dot()
|
||||||
if cnsl.prompt_confirmation("Export AST as .dot file?"):
|
if cnsl.prompt_confirmation("Export AST as .dot file?"):
|
||||||
default = f"{path.stem}.dot"
|
default = f"{path.stem}.dot"
|
||||||
cnsl = input(f"Filename [{default}]: ").strip()
|
filename = input(f"Filename [{default}]: ").strip()
|
||||||
if not cnsl:
|
if not filename:
|
||||||
cnsl = default
|
filename = default
|
||||||
export_dot_file(dot_str, cnsl)
|
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
|
# Display AST diagram
|
||||||
if cnsl.prompt_confirmation("Display AST diagram?"):
|
if cnsl.prompt_confirmation("Display AST diagram?"):
|
||||||
@@ -112,11 +112,13 @@ if __name__ == "__main__":
|
|||||||
elif mode == 1:
|
elif mode == 1:
|
||||||
tram_code = assemble(ast)
|
tram_code = assemble(ast)
|
||||||
|
|
||||||
|
# Print TRAM code
|
||||||
if cnsl.prompt_confirmation("\nPrint TRAM code to console?"):
|
if cnsl.prompt_confirmation("\nPrint TRAM code to console?"):
|
||||||
print("\nGenerated TRAM code:\n")
|
print("\nGenerated TRAM code:\n")
|
||||||
for instr in tram_code:
|
for instr in tram_code:
|
||||||
print(instr.toString())
|
print(instr.toString())
|
||||||
|
|
||||||
|
# Save TRAM code
|
||||||
if cnsl.prompt_confirmation("Save TRAM code as .tram file?"):
|
if cnsl.prompt_confirmation("Save TRAM code as .tram file?"):
|
||||||
base_dir = Path(__file__).parent / "tramcodes"
|
base_dir = Path(__file__).parent / "tramcodes"
|
||||||
base_dir.mkdir(exist_ok=True)
|
base_dir.mkdir(exist_ok=True)
|
||||||
@@ -127,13 +129,13 @@ if __name__ == "__main__":
|
|||||||
filename = default
|
filename = default
|
||||||
|
|
||||||
out_path = base_dir / filename
|
out_path = base_dir / filename
|
||||||
|
|
||||||
with open(out_path, "w") as f:
|
with open(out_path, "w") as f:
|
||||||
for instr in tram_code:
|
for instr in tram_code:
|
||||||
f.write(instr.toString() + "\n")
|
f.write(instr.toString() + "\n")
|
||||||
|
|
||||||
print(f"Saved TRAM code to: {out_path}")
|
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?"):
|
if cnsl.prompt_confirmation("Display TRAM code in Visual TRAM UI?"):
|
||||||
root = tk.Tk()
|
root = tk.Tk()
|
||||||
ui = MachineUI(root)
|
ui = MachineUI(root)
|
||||||
|
|||||||
@@ -1,47 +1,47 @@
|
|||||||
GOTO L13
|
GOTO L1
|
||||||
L14: NOP
|
L2: NOP
|
||||||
LOAD 0 0
|
LOAD 0 0
|
||||||
CONST 0
|
CONST 0
|
||||||
EQ
|
EQ
|
||||||
IFZERO L16
|
IFZERO L4
|
||||||
GOTO L18
|
GOTO L6
|
||||||
L19: NOP
|
L7: NOP
|
||||||
LOAD 0 0
|
LOAD 0 0
|
||||||
LOAD 1 1
|
LOAD 1 1
|
||||||
MUL
|
MUL
|
||||||
LOAD 1 0
|
LOAD 1 0
|
||||||
ADD
|
ADD
|
||||||
RETURN
|
RETURN
|
||||||
L18: NOP
|
L6: NOP
|
||||||
CONST 2
|
CONST 2
|
||||||
LOAD 1 0
|
LOAD 1 0
|
||||||
MUL
|
MUL
|
||||||
LOAD 0 0
|
LOAD 0 0
|
||||||
INVOKE 2 L19 0
|
INVOKE 2 L7 0
|
||||||
GOTO L17
|
GOTO L5
|
||||||
L16: NOP
|
L4: NOP
|
||||||
LOAD 0 0
|
LOAD 0 0
|
||||||
CONST 1
|
CONST 1
|
||||||
ADD
|
ADD
|
||||||
L17: NOP
|
L5: NOP
|
||||||
RETURN
|
RETURN
|
||||||
L15: NOP
|
L3: NOP
|
||||||
LOAD 1 0
|
LOAD 1 0
|
||||||
CONST 0
|
CONST 0
|
||||||
GT
|
GT
|
||||||
IFZERO L22
|
IFZERO L10
|
||||||
GOTO L23
|
GOTO L11
|
||||||
L20: NOP
|
L8: NOP
|
||||||
LOAD 1 0
|
LOAD 1 0
|
||||||
CONST 0
|
CONST 0
|
||||||
GT
|
GT
|
||||||
IFZERO L21
|
IFZERO L9
|
||||||
POP
|
POP
|
||||||
L23: NOP
|
L11: NOP
|
||||||
LOAD 0 0
|
LOAD 0 0
|
||||||
LOAD 0 0
|
LOAD 0 0
|
||||||
LOAD 1 0
|
LOAD 1 0
|
||||||
INVOKE 2 L14 1
|
INVOKE 2 L2 1
|
||||||
MUL
|
MUL
|
||||||
STORE 0 0
|
STORE 0 0
|
||||||
LOAD 0 0
|
LOAD 0 0
|
||||||
@@ -51,21 +51,21 @@ L23: NOP
|
|||||||
SUB
|
SUB
|
||||||
STORE 1 0
|
STORE 1 0
|
||||||
LOAD 1 0
|
LOAD 1 0
|
||||||
GOTO L20
|
GOTO L8
|
||||||
L22: NOP
|
L10: NOP
|
||||||
CONST None
|
CONST None
|
||||||
L21: NOP
|
L9: NOP
|
||||||
POP
|
POP
|
||||||
LOAD 0 0
|
LOAD 0 0
|
||||||
CONST 42
|
CONST 42
|
||||||
ADD
|
ADD
|
||||||
RETURN
|
RETURN
|
||||||
L13: NOP
|
L1: NOP
|
||||||
CONST 1
|
CONST 1
|
||||||
CONST 2
|
CONST 2
|
||||||
INVOKE 2 L14 0
|
INVOKE 2 L2 0
|
||||||
POP
|
POP
|
||||||
CONST 3
|
CONST 3
|
||||||
CONST 3
|
CONST 3
|
||||||
INVOKE 2 L15 0
|
INVOKE 2 L3 0
|
||||||
HALT
|
HALT
|
||||||
|
|||||||
Reference in New Issue
Block a user