diff options
Diffstat (limited to 'js/baba-yaga/web/editor/js/main.js')
-rw-r--r-- | js/baba-yaga/web/editor/js/main.js | 225 |
1 files changed, 225 insertions, 0 deletions
diff --git a/js/baba-yaga/web/editor/js/main.js b/js/baba-yaga/web/editor/js/main.js new file mode 100644 index 0000000..29dda7c --- /dev/null +++ b/js/baba-yaga/web/editor/js/main.js @@ -0,0 +1,225 @@ +/** + * main.js - Main entry point for the Baba Yaga Structural Editor + * Initializes the editor when the page loads + */ + +// Wait for DOM to be ready +document.addEventListener('DOMContentLoaded', () => { + console.log('Baba Yaga Structural Editor initializing...'); + + try { + // Initialize the main editor + const container = document.querySelector('.editor-container'); + if (!container) { + throw new Error('Editor container not found'); + } + + // Create and initialize the editor + const editor = new BabaYagaEditor(container); + + // Store reference globally for debugging + window.babaYagaEditor = editor; + + console.log('Baba Yaga Structural Editor initialized successfully'); + + // Add some helpful console commands for development + window.babaYagaEditorCommands = { + getAST: () => editor.getAST(), + getCode: () => editor.getCode(), + parse: () => editor.parseCode(), + format: () => editor.formatCode(), + run: () => editor.runCode(), + retryLanguageMode: () => editor.retryLanguageMode(), + initLanguageMode: () => window.initBabaYagaMode(), + forceRefresh: () => editor.forceRefreshEditor(), + refresh: () => editor.refreshEditor(), + showAST: () => { + const ast = editor.getAST(); + console.log('Current AST:', ast); + return ast; + }, + showCode: () => { + const code = editor.getCode(); + console.log('Current Code:', code); + return code; + } + }; + + console.log('Development commands available: window.babaYagaEditorCommands'); + + } catch (error) { + console.error('Failed to initialize Baba Yaga Structural Editor:', error); + + // Show error message to user + const errorDiv = document.createElement('div'); + errorDiv.style.cssText = ` + position: fixed; + top: 50%; + left: 50%; + transform: translate(-50%, -50%); + background: #d73a49; + color: white; + padding: 2rem; + border-radius: 8px; + font-family: monospace; + max-width: 80%; + text-align: center; + z-index: 10000; + `; + errorDiv.innerHTML = ` + <h2>Initialization Error</h2> + <p>${error.message}</p> + <p>Check the console for more details.</p> + <button onclick="this.parentElement.remove()" style="margin-top: 1rem; padding: 0.5rem 1rem; border: none; border-radius: 4px; cursor: pointer;">Close</button> + `; + document.body.appendChild(errorDiv); + } +}); + +// Add some global error handling +window.addEventListener('error', (event) => { + console.error('Global error:', event.error); +}); + +window.addEventListener('unhandledrejection', (event) => { + console.error('Unhandled promise rejection:', event.reason); +}); + +// Add some helpful utility functions +window.babaYagaUtils = { + // Parse Baba Yaga code manually + parseCode: (code) => { + try { + // Try to use the real Baba Yaga parser if available + if (typeof createLexer !== 'undefined' && typeof createParser !== 'undefined') { + try { + const lexer = createLexer(code); + const tokens = lexer.allTokens(); + const parser = createParser(tokens); + return parser.parse(); + } catch (parserError) { + console.warn('Real parser failed, falling back to basic parsing:', parserError); + } + } + + // Basic parsing for demonstration + const lines = code.split('\n').filter(line => line.trim()); + const ast = { + type: 'Program', + body: [] + }; + + lines.forEach((line, index) => { + const trimmed = line.trim(); + if (trimmed && !trimmed.startsWith('//')) { + if (trimmed.includes(':')) { + const [name, ...rest] = trimmed.split(':'); + const value = rest.join(':').trim(); + + if (value.includes('->')) { + ast.body.push({ + type: 'FunctionDeclaration', + name: name.trim(), + params: window.babaYagaUtils.parseFunctionParams(value), + body: window.babaYagaUtils.parseFunctionBody(value), + line: index + 1 + }); + } else { + ast.body.push({ + type: 'VariableDeclaration', + name: name.trim(), + value: value, + line: index + 1 + }); + } + } + } + }); + + return ast; + } catch (error) { + console.error('Parse error:', error); + throw error; + } + }, + + parseFunctionParams: (value) => { + const arrowIndex = value.indexOf('->'); + if (arrowIndex === -1) return []; + + const beforeArrow = value.substring(0, arrowIndex).trim(); + if (!beforeArrow) return []; + + return beforeArrow.split(/\s+/).filter(p => p.trim()); + }, + + parseFunctionBody: (value) => { + const arrowIndex = value.indexOf('->'); + if (arrowIndex === -1) return ''; + + return value.substring(arrowIndex + 2).trim(); + }, + + // Generate code from AST + generateCode: (ast) => { + if (!ast || ast.type !== 'Program') return ''; + + const lines = []; + + ast.body.forEach(node => { + switch (node.type) { + case 'FunctionDeclaration': + lines.push(window.babaYagaUtils.generateFunctionCode(node)); + break; + case 'VariableDeclaration': + lines.push(window.babaYagaUtils.generateVariableCode(node)); + break; + default: + lines.push(`// Unknown node type: ${node.type}`); + } + }); + + return lines.join('\n'); + }, + + generateFunctionCode: (node) => { + let code = `${node.name} : `; + + if (node.params && node.params.length > 0) { + code += node.params.join(' '); + } + + code += ' -> '; + code += node.body || ''; + + return code; + }, + + generateVariableCode: (node) => { + return `${node.name} : ${node.value};`; + }, + + // Validate Baba Yaga syntax + validateSyntax: (code) => { + try { + const ast = window.babaYagaUtils.parseCode(code); + return { valid: true, ast }; + } catch (error) { + return { valid: false, error: error.message }; + } + }, + + // Format Baba Yaga code + formatCode: (code) => { + try { + const ast = window.babaYagaUtils.parseCode(code); + return window.babaYagaUtils.generateCode(ast); + } catch (error) { + console.error('Format error:', error); + return code; // Return original code if formatting fails + } + } +}; + +console.log('Baba Yaga utilities available: window.babaYagaUtils'); +console.log('Try: window.babaYagaUtils.parseCode("add : x y -> x + y;")'); |