Spaces:
Runtime error
Runtime error
File size: 1,795 Bytes
753d525 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | from main import compile_code
print("=== Mini Compiler REPL ===")
print("Type 'exit' to quit. Type 'RUN' on a new line to compile multi-line blocks.")
print("---------------------------")
buffer = []
while True:
try:
# Prompt changes if we are inside a multi-line block
prompt = ".. " if buffer else ">> "
line = input(prompt)
if line.lower() == 'exit':
break
# If user types 'RUN', we compile everything in the buffer
if line.strip().upper() == 'RUN':
if buffer:
full_code = "\n".join(buffer)
print("\n--- Compiling ---")
compile_code(full_code)
print("-----------------\n")
buffer = [] # Clear buffer after run
continue
# Basic single-line execution for simple commands
if not buffer and not line.strip().endswith('{'):
if line.strip():
compile_code(line)
continue
# Otherwise, add to buffer (for blocks or multi-line logic)
buffer.append(line)
# Auto-run if braces are balanced and not empty (optional quality of life)
if line.strip() == '}':
# Check if braces are balanced
full_code = "\n".join(buffer)
if full_code.count('{') == full_code.count('}'):
print("\n--- Compiling Block ---")
compile_code(full_code)
print("-----------------------\n")
buffer = []
except KeyboardInterrupt:
print("\nExiting...")
break
except Exception as e:
print(f"REPL Error: {e}")
buffer = [] # Clear buffer on error to reset state |