about summary refs log tree commit diff stats
path: root/js/baba-yaga/debug-interpreter.js
blob: 05d817b0b09ec1792a44f6ecb416b344a5fc888a (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
// debug-interpreter.js - Debug the interpreter JS bridge setup

import { createLexer } from './src/core/lexer.js';
import { createParser } from './src/core/parser.js';
import { createInterpreter } from './src/core/interpreter.js';

const code = `
  result : io.callJS "Math.abs" [-5];
`;

const lexer = createLexer(code);
const tokens = lexer.allTokens();
const parser = createParser(tokens);
const ast = parser.parse();

const host = {
  jsBridgeConfig: {
    allowedFunctions: new Set([
      'JSON.parse', 'JSON.stringify',
      'Math.abs', 'Math.floor', 'Math.ceil', 'Math.round',
      'Math.min', 'Math.max', 'Math.random',
      'console.log', 'console.warn', 'console.error',
      'Date.now', 'performance.now'
    ])
  },
  io: {
    out: (...args) => console.log('[OUT]', ...args),
    debug: (...args) => console.log('[DEBUG]', ...args)
  }
};

console.log('Host config:', host.jsBridgeConfig);

const interpreter = createInterpreter(ast, host);
console.log('Interpreter created');

// Let's see if we can access the bridge
console.log('Interpreter scope has io?', interpreter.scope.has('io'));
const ioObj = interpreter.scope.get('io');
console.log('IO object:', ioObj);
console.log('IO properties:', Array.from(ioObj.properties.keys()));

interpreter.interpret();
const result = interpreter.scope.get('result');
console.log('Final result:', result);