Extend main for export

This commit is contained in:
Jan-Niclas Loosen
2025-11-22 18:32:29 +01:00
parent cb0c7ac2e0
commit ddb6eee609
2 changed files with 16 additions and 4 deletions

View File

@@ -100,8 +100,20 @@ if __name__ == "__main__":
print("")
pretty_print(ast)
# Show AST graph
# Export DOT
dot_str = ast.to_dot()
if prompt_yesno("Export AST as .dot file?"):
default = f"{path.stem}.dot"
out = input(f"Filename [{default}]: ").strip()
if not out:
out = default
try:
Path(out).write_text(dot_str, encoding="utf-8")
print(f"Saved DOT file as: {out}")
except Exception as e:
print(f"Could not save DOT file: {e}")
# Display AST diagram
if prompt_yesno("Display AST diagram?"):
dot_str = ast.to_dot()
render_ast_from_string(dot_str)
print("Rendered AST diagram.")

View File

@@ -16,7 +16,7 @@ reserved = {
'false': 'FALSE'
}
# List of token names. This is always requiredy
# List of token names.
tokens = [
'ID',
'CONST',