First try
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import triplayacc as yacc
|
||||
import cfg_build
|
||||
import syntax
|
||||
|
||||
from pathlib import Path
|
||||
@@ -10,9 +11,12 @@ from vistram.vistram import MachineUI
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
import matplotlib.image as mpimg
|
||||
import helpers.console as cnsl
|
||||
import lib.console as cnsl
|
||||
import os
|
||||
|
||||
from cfg.CFG import CFG
|
||||
from cfg.CFG_Node import (CFG_START, CFG_END)
|
||||
|
||||
import matplotlib
|
||||
matplotlib.use("TkAgg")
|
||||
|
||||
@@ -21,6 +25,17 @@ def assemble(ast):
|
||||
code = ast.code({}, 0)
|
||||
return code + [halt()]
|
||||
|
||||
def make_cfg(ast):
|
||||
start = CFG_START()
|
||||
end = CFG_END()
|
||||
|
||||
last = ast.cfa(start, end)
|
||||
if last is not None:
|
||||
last.add_child(end)
|
||||
|
||||
return CFG(start, end)
|
||||
|
||||
|
||||
# Renders a diagram of the AST
|
||||
def render_diagram(dot_string: str):
|
||||
# Set DPI for PNG
|
||||
@@ -64,9 +79,9 @@ if __name__ == "__main__":
|
||||
print("\nTRIPLA Parser and TRIPLA to TRAM Compiler")
|
||||
|
||||
while True:
|
||||
mode = cnsl.prompt_choice("\nSelect action:", ["Parse .tripla", "Compile .tripla", "Exit"])
|
||||
mode = cnsl.prompt_choice("\nSelect action:", ["Parse .tripla", "Compile .tripla", "CFG for .tripla", "Exit"])
|
||||
|
||||
if mode == 2:
|
||||
if mode == 3:
|
||||
print("\nBye Bye.")
|
||||
break
|
||||
|
||||
@@ -143,4 +158,26 @@ if __name__ == "__main__":
|
||||
ui.machine.reset()
|
||||
root.mainloop()
|
||||
|
||||
elif mode == 2:
|
||||
cfg = make_cfg(ast)
|
||||
|
||||
dot_str = cfg.to_dot()
|
||||
if cnsl.prompt_confirmation("\nExport CFG as .dot file?"):
|
||||
default = f"{path.stem}_cfg.dot"
|
||||
filename = input(f"Filename [{default}]: ").strip()
|
||||
if not filename:
|
||||
filename = default
|
||||
|
||||
out_path = Path(__file__).parent / filename
|
||||
with open(out_path, "w") as f:
|
||||
f.write(dot_str)
|
||||
|
||||
print(f"Saved CFG DOT file as: {out_path}")
|
||||
|
||||
if cnsl.prompt_confirmation("Display CFG diagram?"):
|
||||
render_diagram(dot_str)
|
||||
print("Rendered CFG diagram.")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user