#!/usr/bin/env python3

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

def test_systematic():
    
    tests = [
        # Test 1: Use exact object structure but with simple identifiers
        ("Exact object structure, simple names", '''
tool test takes x returns result {
    a = x + 1
    b = x + 2  
    result = {
        subtotal: x,
        tax: a,
        total: b
    }
}
'''),
        
        # Test 2: Use exact variable names but different object structure
        ("Exact variable names, simple object", '''
tool process_order takes order returns order_result {
    tax_amount = order + 0.08
    total = order + tax_amount
    order_result = {
        value: total
    }
}
'''),
        
        # Test 3: Exact code but break it into parts
        ("Part 1 - just statements", '''
tool process_order takes order returns order_result {
    tax_amount = order + 0.08
    total = order + tax_amount
}
'''),
        
        ("Part 2 - just object assignment", '''
tool process_order takes order returns order_result {
    order_result = {
        subtotal: order,
        tax: tax_amount,
        total: total  
    }
}
'''),
        
        # Test 4: The exact failing case standalone (tool only)
        ("Exact failing case (tool only)", '''
tool process_order takes order returns order_result {
    tax_amount = order + 0.08
    total = order + tax_amount
    order_result = {
        subtotal: order,
        tax: tax_amount,
        total: total
    }
}
'''),
    ]
    
    for name, code in tests:
        print(f"=== {name} ===")
        print(repr(code))
        
        try:
            ast = parser.parse(code)
            if ast and ast[0] == 'program' and len(ast) > 1:
                statements = ast[1]
                print(f"Success! Found {len(statements)} statements")
                for i, stmt in enumerate(statements):
                    if stmt[0] == 'tool':
                        tool_body = stmt[4]
                        if tool_body[0] == 'block':
                            tool_statements = tool_body[1]
                            print(f"  Tool body has {len(tool_statements)} statements")
                    else:
                        print(f"  Statement {i}: {stmt[0]}")
            else:
                print("Failed to parse or no statements found")
                print("AST:", ast)
        except Exception as e:
            print(f"Parser error: {e}")
        
        print()

if __name__ == "__main__":
    test_systematic()