-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
73 lines (64 loc) · 1.8 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<script src="./mustard/tree-sitter.js"></script>
<script type="module">
import * as art from "./build/debug.js";
const Parser = window.TreeSitter;
Parser.init().then(async () => {
const parser = new Parser();
const mustard = await Parser.Language.load(
"./mustard/tree-sitter-mustard.wasm"
);
parser.setLanguage(mustard);
const engine = art.newEngine();
const scope = art.newTopLevelScope(engine);
document.body.innerText = art.add(1, 2);
const tree = parser.parse(`
let foo = 1
let bar = 2
let abc = foo + bar
{
abc
}
`);
const pe = (x, y) => {
if (x.length != y.length) {
return false;
}
for (let i = 0; i < x.length; i++) {
if (x[i] != y[i]) {
return false;
}
}
return true;
};
const c = tree.rootNode;
const walk = (p, c, s) => {
// console.log(p, c, s);
if (pe(p, [])) {
s = art.newTopLevelScope(engine);
}
if (
pe([...p, c.type], [
"document",
"statement",
"declaration_statement",
"let_declaration",
])
) {
const name = c.childForFieldName("ident").text
const expr = c.childForFieldName("value").text
console.log(name, expr)
}
if (c.children) {
c.children.filter(n => n.isNamed()).forEach((x) => walk([...p, c.type], x, s));
}
};
walk([], c);
const e = art.evaluate(scope, tree);
});
</script>
</head>
<body></body>
</html>