about summary refs log tree commit diff stats
path: root/js/baba-yaga/web/editor/test-integration.html
blob: 356b8cdb134bd48c12f3e2608a81dedc32a702ba (plain) (blame)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Baba Yaga Integration Test</title>
</head>
<body>
    <h1>Baba Yaga Integration Test</h1>
    
    <div id="status">Loading...</div>
    
    <div id="test-results"></div>
    
    <!-- Baba Yaga Language Components -->
    <script type="module">
        // Import Baba Yaga components and make them globally available
        import { createLexer, tokenTypes } from '../../lexer.js';
        import { createParser } from '../../parser.js';
        import { createInterpreter } from '../../interpreter.js';
        
        // Make them globally available
        window.createLexer = createLexer;
        window.createParser = createParser;
        window.createInterpreter = createInterpreter;
        window.tokenTypes = tokenTypes;
        
        console.log('Baba Yaga modules loaded and made globally available');
    </script>
    
    <script type="module">
        // Test the integration
        async function testIntegration() {
            const statusDiv = document.getElementById('status');
            const resultsDiv = document.getElementById('test-results');
            
            try {
                // Test 1: Check if modules are loaded
                statusDiv.textContent = 'Testing module loading...';
                
                if (typeof createLexer === 'undefined') {
                    throw new Error('createLexer not found');
                }
                if (typeof createParser === 'undefined') {
                    throw new Error('createParser not found');
                }
                if (typeof createInterpreter === 'undefined') {
                    throw new Error('createInterpreter not found');
                }
                
                resultsDiv.innerHTML += '<p>✅ All modules loaded successfully</p>';
                
                // Test 2: Test lexer
                statusDiv.textContent = 'Testing lexer...';
                const testCode = 'add : x y -> x + y;';
                const lexer = createLexer(testCode);
                const tokens = lexer.allTokens();
                
                resultsDiv.innerHTML += `<p>✅ Lexer working: ${tokens.length} tokens generated</p>`;
                
                // Test 3: Test parser
                statusDiv.textContent = 'Testing parser...';
                const parser = createParser(tokens);
                const ast = parser.parse();
                
                resultsDiv.innerHTML += `<p>✅ Parser working: AST generated with ${ast.body.length} statements</p>`;
                
                // Test 4: Test interpreter
                statusDiv.textContent = 'Testing interpreter...';
                const interpreter = createInterpreter(ast);
                const result = interpreter.interpret();
                
                resultsDiv.innerHTML += `<p>✅ Interpreter working: Result = ${JSON.stringify(result)}</p>`;
                
                // Test 5: Test more complex code
                statusDiv.textContent = 'Testing complex code...';
                const complexCode = `
factorial : n ->
  when n is
    0 then 1
    _ then n * (factorial (n - 1));

result : factorial 5;`;
                
                const complexLexer = createLexer(complexCode);
                const complexTokens = complexLexer.allTokens();
                const complexParser = createParser(complexTokens);
                const complexAST = complexParser.parse();
                const complexInterpreter = createInterpreter(complexAST);
                const complexResult = complexInterpreter.interpret();
                
                resultsDiv.innerHTML += `<p>✅ Complex code working: Result = ${JSON.stringify(complexResult)}</p>`;
                
                statusDiv.textContent = 'All tests passed! 🎉';
                statusDiv.style.color = 'green';
                
            } catch (error) {
                statusDiv.textContent = 'Test failed! ❌';
                statusDiv.style.color = 'red';
                resultsDiv.innerHTML += `<p style="color: red;">❌ Error: ${error.message}</p>`;
                console.error('Integration test failed:', error);
            }
        }
        
        // Run tests when page loads
        document.addEventListener('DOMContentLoaded', testIntegration);
    </script>
</body>
</html>