#!/usr/bin/env python3
"""
Alloy Language Interpreter Demo
A simple demonstration of the working Alloy language features.
"""

import sys
import os

# Add the src directory to the Python path
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'src'))

from alloy import parser, Interpreter

def run_alloy_code(code, description):
    """Run Alloy code and display results"""
    print(f"\n📝 {description}")
    print(f"Code: {code.strip()}")
    print("─" * 40)
    
    try:
        ast = parser.parse(code)
        interpreter = Interpreter()
        interpreter.interpret(ast)
        
        # Show relevant variables
        for var_name in ['result', 'x', 'y', 'z', 'numbers', 'sum']:
            try:
                value = interpreter.global_env.get(var_name)
                if isinstance(value, tuple) and len(value) == 2:
                    print(f"{var_name} = {value[0]}")
                elif value is not None:
                    print(f"{var_name} = {value}")
            except:
                pass
                
    except Exception as e:
        print(f"Error: {e}")

def main():
    print("🚀 Alloy Language Interpreter Demo")
    print("=" * 50)
    
    # Demo 1: Basic arithmetic tool
    run_alloy_code('''
    tool add takes a, b returns result {
        result = a + b
        return result
    }
    
    sum = add(15, 27)
    ''', "Tool Definition & Function Call")
    
    # Demo 2: Variable operations
    run_alloy_code('''
    x = 100
    y = 200  
    z = x + y
    ''', "Variable Assignments & Arithmetic")
    
    # Demo 3: Array handling
    run_alloy_code('''
    numbers = [10, 20, 30, 40, 50]
    ''', "Array Literals")
    
    print("\n" + "=" * 50)
    print("🎉 Demo complete! The Alloy interpreter is working for basic operations.")

if __name__ == "__main__":
    main() 