import streamlit as st # type: ignore[import] import sys import io # Import your core compiler pipeline logic safely from main import compile_code # Page styling st.set_page_config(page_title="Custom Mini-Compiler IDE", page_icon="💻", layout="wide") st.title("💻 Custom Mini-Compiler & CPU Architecture Simulator") st.markdown(""" This IDE tokenizes, parses, structurally analyzes, optimizes, and executes a custom programming language down to simulated hardware registers. --- """) # Layout split: Left side for code input, Right side for pipeline visualization col1, col2 = st.columns([1, 1]) with col1: st.subheader("📝 Source Code Editor") # Default presentation script default_code = """{\n int a = 10;\n int b = 20;\n float w = 3.3 + 3.2;\n int c = a + b;\n print(c);\n}""" user_code = st.text_area("Enter your custom source code here:", value=default_code, height=350) compile_btn = st.button("🚀 Compile and Run Code", type="primary", use_container_width=True) with col2: st.subheader("⚙️ Compiler Pipeline Terminal Output") if compile_btn: # Capture stdout console print statements directly from your main.py old_stdout = sys.stdout new_stdout = io.StringIO() sys.stdout = new_stdout try: # Run your exact core compiler execution path compile_code(user_code) output = new_stdout.getvalue() except Exception as e: output = f"Runtime Crash Error: {str(e)}" finally: sys.stdout = old_stdout # Display the terminal logs cleanly in a code block if output.strip(): st.code(output, language="text") else: st.info("Compilation completed silently. (No print functions were called inside the parsed AST block).") else: st.info("Click 'Compile and Run Code' to pass the source string through the execution toolchain.")