First implementation of the compiler

This commit is contained in:
Jan-Niclas Loosen
2025-12-08 00:03:18 +01:00
parent 88b8de363d
commit 4e6f93b353
11 changed files with 886 additions and 32 deletions

View File

@@ -1,18 +1,26 @@
import triplayacc as yacc
import triplalex as lex
import syntax
from pathlib import Path
from graphviz import Source
from vistram.tram import *
import tkinter as tk
from vistram.vistram import MachineUI
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import lib.console as cnsl
import helpers.console as cnsl
import os
import matplotlib
matplotlib.use("TkAgg")
# Assembles the AST into TRAM code
def assemble(ast):
code = ast.code({}, 0)
return code + [halt()]
# Renders a diagram of the AST
def render_diagram(dot_string: str):
# Set DPI for PNG
@@ -60,41 +68,77 @@ def export_dot_file(dot_string: str, filename: str):
print(f"Could not save DOT file: {e}")
if __name__ == "__main__":
print("\nTRIPLA Parser Tool")
print("\nTRIPLA Parser and TRIPLA to TRAM Compiler")
while True:
choice = cnsl.prompt_choice("\nSelect action:", ["Parse .tripla", "Exit"])
mode = cnsl.prompt_choice("\nSelect action:", ["Parse .tripla", "Compile .tripla", "Exit"])
if choice == 1:
if mode == 2:
print("\nBye Bye.")
break
programs = cnsl.search_programs()
base = Path(__file__).parent / "triplaprograms"
programs = sorted([f for f in base.glob("*.tripla")])
if not programs:
print("\nNo .tripla files found.")
continue
idx = cnsl.prompt_choice("\nSelect program to parse:", [p.name for p in programs])
idx = cnsl.prompt_choice("\nSelect TRIPLA program:", [p.name for p in programs])
path = programs[idx]
source = path.read_text()
ast = yacc.parser.parse(source)
# Pretty print
if cnsl.prompt_confirmation("\nPretty-print AST?"):
print("")
pretty_print(ast)
if mode == 0:
# Pretty print
if cnsl.prompt_confirmation("\nPretty-print AST?"):
print("")
pretty_print(ast)
# Export DOT
dot_str = ast.to_dot()
if cnsl.prompt_confirmation("Export AST as .dot file?"):
default = f"{path.stem}.dot"
cnsl = input(f"Filename [{default}]: ").strip()
if not cnsl:
cnsl = default
export_dot_file(dot_str, cnsl)
# Display AST diagram
if cnsl.prompt_confirmation("Display AST diagram?"):
render_diagram(dot_str)
print("Rendered AST diagram.")
elif mode == 1:
tram_code = assemble(ast)
if cnsl.prompt_confirmation("\nPrint TRAM code to console?"):
print("\nGenerated TRAM code:\n")
for instr in tram_code:
print(instr.toString())
if cnsl.prompt_confirmation("Save TRAM code as .tram file?"):
base_dir = Path(__file__).parent / "tramcodes"
base_dir.mkdir(exist_ok=True)
default = f"{path.stem}.tram"
filename = input(f"Filename [{default}]: ").strip()
if not filename:
filename = default
out_path = base_dir / filename
with open(out_path, "w") as f:
for instr in tram_code:
f.write(instr.toString() + "\n")
print(f"Saved TRAM code to: {out_path}")
if cnsl.prompt_confirmation("Display TRAM code in Visual TRAM UI?"):
root = tk.Tk()
ui = MachineUI(root)
ui.machine.initial_program = tram_code
ui.machine.reset()
root.mainloop()
# Export DOT
dot_str = ast.to_dot()
if cnsl.prompt_confirmation("Export AST as .dot file?"):
default = f"{path.stem}.dot"
cnsl = input(f"Filename [{default}]: ").strip()
if not cnsl:
cnsl = default
export_dot_file(dot_str, cnsl)
# Display AST diagram
if cnsl.prompt_confirmation("Display AST diagram?"):
render_diagram(dot_str)
print("Rendered AST diagram.")