-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_test.ts
More file actions
91 lines (77 loc) · 2.55 KB
/
debug_test.ts
File metadata and controls
91 lines (77 loc) · 2.55 KB
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
import { OneScript } from './src/OneScript.ts';
import { Node } from 'web-tree-sitter'
import { OSCompiler } from './src/OneScriptCompiler.ts';
import { OSMachine } from './src/OneScriptVirtualMachine.ts';
function printAST(node: Node, indent: string = '') {
console.log(`${indent}${node.type}: ${node.text}`);
for (const child of node.children) {
if (child) {
printAST(child, indent + ' ');
}
}
}
function printAllChildrenInfo(node: Node) {
console.log('\n=== All Children info (including anonymous) ===');
node.children.forEach((child, index) => {
if (child) {
console.log(`Child ${index}: type="${child.type}", text="${child.text}"`);
} else {
console.log(`Child ${index}: null`);
}
});
}
function printNamedChildrenInfo(node: Node) {
console.log('\n=== Named Children info ===');
node.namedChildren.forEach((child, index) => {
if (child) {
console.log(`Named Child ${index}: type="${child.type}", text="${child.text}"`);
} else {
console.log(`Named Child ${index}: null`);
}
});
}
async function testIfElse() {
const oneScript = new OneScript('./wasm/tree-sitter-onescript.wasm');
// Given
const source =
`
а = 1;
в = 0;
Если а = 0 Тогда
в = 1
Иначе
в = 2
КонецЕсли;
`;
console.log('=== Source code ===');
console.log(source);
// Parse and compile
const tree = await oneScript.GetTree(source);
if (!tree) {
console.error('Failed to parse source');
return;
}
console.log('\n=== AST ===');
printAST(tree.rootNode);
// Find if_statement node
const ifStatementNode = tree.rootNode.children.find(child => child && child.type === 'if_statement');
if (ifStatementNode) {
printAllChildrenInfo(ifStatementNode);
printNamedChildrenInfo(ifStatementNode);
}
// Compile
const compiler = new OSCompiler();
const instructions = compiler.compile(tree.rootNode);
console.log('\n=== Instructions ===');
instructions.forEach((instr, index) => {
console.log(`${index}: ${instr.code} ${instr.arg ? '(' + instr.arg + ')' : ''}`);
});
// Run
const machine = new OSMachine();
machine.run(instructions);
const vars = machine.dumpVariables();
console.log('\n=== Variables ===');
console.log('Variables:', vars);
console.log('Expected в = 2, Actual в =', vars['в']);
}
testIfElse().catch(console.error);