finish refactoring task 1 and 2

This commit is contained in:
Jan-Niclas Loosen
2026-03-08 17:21:09 +01:00
parent 605eaf3278
commit 438447e6de
4 changed files with 65 additions and 79 deletions

View File

@@ -11,7 +11,9 @@ import cfg_build
import lib.console as cnsl
import syntax
import triplayacc as yacc
from cfa.to_dot import analysis_to_dot, run_all_analyses
from cfa.LiveVariables import LiveVariables
from cfa.ReachedUses import ReachedUses
from cfa.to_dot import analysis_to_dot
from cfg.CFG import CFG
from vistram.tram import *
from vistram.vistram import MachineUI
@@ -64,12 +66,11 @@ def pretty_print(node, indent=0):
else:
print(f"{prefix} {key}: {value}")
def print_analysis_reports(cfg, analyses: dict, ru_edges: dict[int, list[int]]):
"""Print compact Live Variables and Reached Uses reports to console."""
# Print compact Live Variables and Reached Uses reports to console.
def print_analysis_reports(cfg, analyses: dict):
lv = analyses["lv"]
ru = analyses["ru"]
_ = ru
ru_edges = ru.reached_uses_by_node()
node_by_id = {n.id: n for n in cfg.nodes()}
def node_text(nid: int) -> str:
@@ -102,7 +103,6 @@ def print_analysis_reports(cfg, analyses: dict, ru_edges: dict[int, list[int]]):
if not has_ru:
print("(no reached uses)")
if __name__ == "__main__":
print("\nTRIPLA Parser and TRIPLA to TRAM Compiler")
@@ -211,28 +211,25 @@ if __name__ == "__main__":
print("Rendered CFG diagram.")
elif mode == 3:
# Reset global CFG builder state so each analysis run is clean
# Reset the global CFG builder state so each analysis run is clean
cfg_build.FUNCTIONS.clear()
cfg_build.CURRENT_FUNCTION = None
cfg = make_cfg(ast)
analysis_name = "Live Variables + Reached Uses"
analysis_name = "Live Variables + Reached Uses analyses"
print(f"\nRunning {analysis_name}")
try:
_analyses, annotations, ru_edges = run_all_analyses(cfg)
lv = LiveVariables(cfg)
ru = ReachedUses(cfg)
analyses = {"lv": lv, "ru": ru}
except Exception as exc:
print(f"Analysis failed: {exc}")
continue
print(
f"Done. {len(annotations)} LV annotation node(s), "
f"{len(ru_edges)} RU definition node(s)."
)
dot_str = analysis_to_dot(cfg, annotations, analysis_name, ru_edges)
if cnsl.prompt_confirmation("\nPrint analysis reports to console?"):
print_analysis_reports(cfg, _analyses, ru_edges)
print_analysis_reports(cfg, analyses)
dot_str = analysis_to_dot(cfg, analyses, analysis_name)
if cnsl.prompt_confirmation("\nExport annotated CFG as .dot file?"):
default = f"{path.stem}_analysis.dot"