91 lines
2.6 KiB
Python
91 lines
2.6 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
import os
|
|
from pathlib import Path
|
|
|
|
# Add the current directory to Python path so we can import our modules
|
|
sys.path.insert(0, '/home/janniclas/Projekte/Construction-of-Compilers/Project-02-03-04')
|
|
|
|
import triplayacc as yacc
|
|
import cfg_build
|
|
import syntax
|
|
from cfg.CFG import CFG
|
|
from cfg.CFG_Node import (CFG_START, CFG_END)
|
|
|
|
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, ast)
|
|
|
|
def generate_dot_files():
|
|
base = Path("triplaprograms")
|
|
programs = sorted([f for f in base.glob("*.tripla")])
|
|
|
|
output_dir = Path("mistraltests/after")
|
|
output_dir.mkdir(exist_ok=True, parents=True)
|
|
|
|
for path in programs:
|
|
try:
|
|
print(f"Processing {path.name}...")
|
|
|
|
# Reset the global FUNCTIONS registry
|
|
cfg_build.FUNCTIONS.clear()
|
|
|
|
source = path.read_text()
|
|
ast = yacc.parser.parse(source)
|
|
|
|
# Create CFG
|
|
cfg = make_cfg(ast)
|
|
|
|
# Generate dot file
|
|
dot_str = cfg.to_dot()
|
|
|
|
# Save to file
|
|
output_path = output_dir / f"{path.stem}_cfg_after.dot"
|
|
with open(output_path, "w") as f:
|
|
f.write(dot_str)
|
|
|
|
print(f"Saved: {output_path}")
|
|
|
|
except Exception as e:
|
|
print(f"Error processing {path.name}: {e}")
|
|
continue
|
|
|
|
def compare_dot_files():
|
|
"""Compare before and after dot files to ensure they're the same"""
|
|
before_dir = Path("mistraltests/before")
|
|
after_dir = Path("mistraltests/after")
|
|
|
|
before_files = sorted(before_dir.glob("*_cfg_before.dot"))
|
|
after_files = sorted(after_dir.glob("*_cfg_after.dot"))
|
|
|
|
print("\nComparing dot files...")
|
|
|
|
mismatches = []
|
|
|
|
for before_file, after_file in zip(before_files, after_files):
|
|
before_content = before_file.read_text()
|
|
after_content = after_file.read_text()
|
|
|
|
if before_content == after_content:
|
|
print(f"✓ {before_file.stem} - MATCH")
|
|
else:
|
|
print(f"✗ {before_file.stem} - MISMATCH")
|
|
mismatches.append((before_file, after_file))
|
|
|
|
if mismatches:
|
|
print(f"\n{mismatches} files have mismatches!")
|
|
for before_file, after_file in mismatches:
|
|
print(f" {before_file.name} vs {after_file.name}")
|
|
else:
|
|
print("\nAll dot files match! The refactoring was successful.")
|
|
|
|
if __name__ == "__main__":
|
|
generate_dot_files()
|
|
compare_dot_files() |