-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
360 lines (299 loc) · 11.4 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>watasu - example</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono&display=swap" rel="stylesheet">
<link rel="stylesheet" href="./assets/style.css">
</head>
<body>
<main>
<section>
<section>
<h2>Watasu (渡す) Interactive Example</h2>
<p>This is an interactive example of what "Watasu" is able to do. Everything here is using the default
version of the "Watasu" library and simply visualizes the data that is available.</p>
<small>The instruction set can be found on the right side. <a href="#instructions">Click
here</a>.</small>
</section>
<section>
<div>
<button id="btn-load-program">Load Program (tokenize & parse)</button>
</div>
<hr>
<textarea id="code-input" rows="20" placeholder="code..."></textarea>
</section>
<section>
<h2>Context</h2>
<div>
<button id="btn-run">Run ▶</button>
<button id="btn-step">Step ↷</button>
</div>
<pre>Position: <code id="context-position-container">No position</code></pre>
<div style="max-height: 30vh; overflow-y: auto; overflow-x: hidden;">
<pre><code id="context-container">No context</code></pre>
</div>
</section>
</section>
<section>
<section>
<h2>Tokens</h2>
<small>Click to highlight token in editor.</small>
<div style="max-height: 30vh; overflow-y: auto; overflow-x: hidden;">
<ol id="tokens-container">
<p>No tokens</p>
</ol>
</div>
</section>
<section>
<h2>AST</h2>
<small>Click "Show" to highlight node in editor.</small>
<div style="max-height: 30vh; overflow-y: auto; overflow-x: hidden;">
<ul id="ast-container" class="tree">No AST</ul>
</div>
</section>
<section id="instructions">
<h2>Instructions</h2>
<ul>
<li><code>set(variable: dataReference, value: *)</code></li>
<li><code>print(...args: *)</code></li>
<li><code>inc(variable: dataReference)</code></li>
<li><code>dec(variable: dataReference)</code></li>
<li><code>not(variable: *)</code></li>
<li><code>lower(a: *, b: *)</code></li>
<li><code>greater(a: *, b: *)</code></li>
<li><code>while(condition: *) { block }</code></li>
<li><code>if(condition: *) { block }</code></li>
<li>more can be easily added through the API</li>
</ul>
</section>
</section>
</main>
</body>
<script>
document.body.onload = () => {
document.getElementById('code-input').value = `set(sum, 0);
print("the current sum is ", sum);
inc(sum);
set(sometext, tostring(sum));
while(lower(sum, 10)) {
inc(sum);
print("sum", sum);
}
if(greater(sum, 5)) {
while(greater(sum, 5)) {
dec(sum);
}
}
if(not(lower(sum, 5))) {
print("decrement success");
}
print("done");`
}
</script>
<script>
function doInBackground(func) {
// force work from task queue in browser
setTimeout(func, 1);
}
function nowMs() {
return window.performance.now();
}
function measurePrint(name, func) {
const start = nowMs();
func();
const diff = nowMs() - start;
console.log(`Call "${name}" took ${diff.toFixed(2)}ms`);
}
</script>
<script type="module">
import Watasu, {WatasuParser} from './dist/watasu.js';
const codeInput = document.getElementById('code-input');
const contextContainer = document.getElementById('context-container');
const contextPositionContainer = document.getElementById('context-position-container');
const tokensContainer = document.getElementById('tokens-container');
const astContainer = document.getElementById('ast-container');
const btnLoadProgram = document.getElementById('btn-load-program');
btnLoadProgram.onclick = loadProgram;
const btnRun = document.getElementById('btn-run');
btnRun.onclick = run;
const btnStep = document.getElementById('btn-step');
btnStep.onclick = step;
const watasu = new Watasu();
watasu.registerCall('set', function set(name, value) {
if (name.name === WatasuParser.ASTNodeNames.DATA_REFERENCE) {
this._context[name.value] = this.resolveParameterValue(value);
} else {
throw new Error('invalid set call');
}
})
watasu.registerCall('inc', function inc(name) {
if (name.name === WatasuParser.ASTNodeNames.DATA_REFERENCE) {
this._context[name.value]++;
} else {
throw new Error('invalid inc call');
}
})
watasu.registerCall('dec', function dec(name) {
if (name.name === WatasuParser.ASTNodeNames.DATA_REFERENCE) {
this._context[name.value]--;
} else {
throw new Error('invalid dec call');
}
})
watasu.registerCall('print', function print(...args) {
console.log(...this.resolveParametersValues(args));
})
watasu.registerCall('tostring', function tostring(value) {
return `${this.resolveParameterValue(value)}`;
})
watasu.registerCall('while', function whileFunc(condition) {
// if not true, don't even start
if (!this.resolveParameterValue(condition)) {
return;
}
this._position.deeper();
this._position.pushReturn(() => {
if (this.resolveParameterValue(condition)) {
// no reset required, false will trigger a reset
return false;
} else {
// no higher required, true will trigger a higher
return true;
}
});
})
watasu.registerCall('if', function ifFunc(condition) {
// if not true, don't even start
if (!this.resolveParameterValue(condition)) {
return;
}
this._position.deeper();
this._position.pushReturn(() => true);
})
watasu.registerCall('lower', function lower(a, b) {
return this.resolveParameterValue(a) < this.resolveParameterValue(b);
})
watasu.registerCall('greater', function greater(a, b) {
return this.resolveParameterValue(a) > this.resolveParameterValue(b);
})
watasu.registerCall('not', function not(condition) {
return !this.resolveParameterValue(condition);
})
function showToken(start, end) {
codeInput.focus();
codeInput.setSelectionRange(start, end);
}
function loadProgram() {
measurePrint("load program", () => {
watasu.loadProgram(codeInput.value);
});
// Render tokens
const tokens = watasu.tokens;
tokensContainer.innerHTML = null;
for (const token of tokens) {
let li = document.createElement('li');
li.innerHTML = `<code>${token.name}</code> with <code>${token.value}</code>`;
li.onclick = () => {
showToken(token.position, token.position + token.value.length);
}
tokensContainer.appendChild(li);
}
// Render ast nodes
const ast = watasu.ast;
astContainer.innerHTML = null;
const generateASTNodeLink = (astNode) => {
const child = document.createElement('li');
switch (astNode.name) {
case 'functionCall':
child.innerHTML = `call <code>${astNode.value.name}</code>`;
break;
case 'dataBoolean':
case 'dataNumber':
case 'dataReference':
child.innerHTML = `${astNode.name}=<code>${astNode.value}</code>`;
break;
case 'dataString':
child.innerHTML = `${astNode.name}=<code>"${astNode.value}"</code>`;
break;
}
const showBtn = document.createElement('button');
showBtn.innerText = 'Show';
showBtn.onclick = () => {
showToken(astNode.range.start, astNode.range.end);
}
child.appendChild(showBtn);
return child;
}
const generateASTBlock = (block) => {
let items = [];
if (!(Symbol.iterator in Object(block))) {
return items;
}
for (const astNode of block) {
const child = generateASTNodeLink(astNode);
console.log(astNode.value.block);
if (astNode.value.block) {
const subItems = generateASTBlock(astNode.value.block.value);
if (subItems.length > 0) {
child.appendChild(...subItems);
}
}
items.push(child);
}
return items;
};
const generateASTTree = (name, ast) => {
const root = document.createElement('li');
root.innerText = name;
const list = document.createElement('ul');
for (const astNode of ast) {
const item = generateASTNodeLink(astNode);
const itemList = document.createElement('ul');
if (astNode.value.parameters) {
itemList.appendChild(generateASTTree('Parameters', astNode.value.parameters))
}
if (astNode.value.block) {
itemList.appendChild(generateASTTree('Block', astNode.value.block.value))
}
item.appendChild(itemList);
list.appendChild(item);
}
root.appendChild(list);
return root;
};
astContainer.appendChild(generateASTTree('Root', ast));
const end = document.createElement('li');
end.innerText = "End.";
astContainer.appendChild(end);
}
function updateProgramContext() {
contextPositionContainer.innerText = JSON.stringify(watasu._runner._position._path);
contextContainer.innerText = JSON.stringify(watasu._runner._context, null, 2);
}
function step() {
measurePrint("step program", () => {
watasu.step();
});
if (watasu._runner.isDone) {
alert('Done.');
}
const currentNode = watasu._runner._lastNode;
showToken(currentNode.range.start, currentNode.range.end);
updateProgramContext();
}
function run() {
measurePrint("run program", () => {
while (!watasu._runner.isDone) {
watasu.step();
}
});
updateProgramContext();
alert('done.');
}
</script>
</html>