#!/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_identifier_conflict():
    
    tests = [
        # Test the exact conflict
        ("Simple identifier conflict", '''
tool test takes x returns result {
    a = x
    result = a
}
'''),
        
        ("Break the pattern - use expression", '''
tool test takes x returns result {
    a = x + 0
    result = a
}
'''),
        
        ("Break the pattern - different variable", '''
tool test takes x returns result {
    a = x
    b = a
}
'''),
        
        ("Break the pattern - use function call", '''
tool test takes x returns result {
    a = x
    result = func(a)
}
'''),
        
        ("Another conflict pattern", '''
tool test takes x returns result {
    val = x
    result = val
}
'''),
        
        ("Test with 'result' variable", '''
tool test takes x returns y {
    result = x
    y = result
}
'''),
    ]
    
    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]
                if len(statements) > 0 and statements[0][0] == 'tool':
                    print(f"SUCCESS: Tool parsed")
                else:
                    print(f"ERROR: No tool statement found")
            else:
                print("ERROR: Failed to parse")
        except Exception as e:
            print(f"PARSER ERROR: {e}")
        
        print()

if __name__ == "__main__":
    test_identifier_conflict()