-
Notifications
You must be signed in to change notification settings - Fork 0
/
host.html
350 lines (289 loc) · 7.11 KB
/
host.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>boing interpreter</title>
<style>
html, body {
margin: 0;
height: 100%;
width: 100%;
font-family: Arial, Helvetica, sans-serif;
font-size: x-large;
color: #272727;
}
.left_content {
margin: auto;
overflow: auto;
height: 95%;
width: 48%;
padding: 1%;
float: left;
}
.right_content {
margin: auto;
overflow: auto;
width: 47%;
height: 95%;
padding: 1%;
float: right;
border-left: 3px solid #000000;
}
#script {
min-width: 100%;
max-width: 100%;
height: 50%;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
#console_output {
min-width: 100%;
max-width: 100%;
height: 20%;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
#console_input {
min-width: 80%;
max-width: 80%;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
</style>
</head>
<body>
<div class="left_content">
<b>script:</b>
<hr>
<textarea id="script">l<I 20{
p"hello world" nI
}</textarea>
<br>
<button id="run_button"">run</button>
<button onclick="getElementById('script').value = window.location.href + '?s=' + encodeURIComponent(getElementById('script').value)">convert to sharable url</button>
<select id="script_template" onchange="update_selected()">
<option value="0">select demo</option>
<option value="1">hello loop</option>
<option value="2">eval and stack</option>
<option value="3">tables</option>
<option value="4">mandelbrot</option>
<option value="5">mouse draw</option>
<option value="6">draw random lines</option>
</select>
<br><br>
<b>console:</b>
<textarea id="console_output" readonly></textarea>
<textarea rows="1" id="console_input" placeholder="type here"></textarea>
<button>enter</button>
</div>
<div class="right_content">
<b>screen area:</b>
<hr>
<canvas id="screen" oncontextmenu="event.preventDefault()"></canvas>
</div>
<script type="text/javascript">
var boing = null;
var templates = [];
var script_run = null;
var urlp = new URLSearchParams(window.location.search);
var new_script = urlp.get("s");
if(new_script)
{
console.log("setting script textarea to s query parameter");
var script = document.getElementById("script");
script.value = new_script;
}
function update_selected()
{
var select = document.getElementById("script_template");
if(select.selectedIndex != 0)
{
document.getElementById("script").value = templates[Number(select.options[select.selectedIndex].value) -1];
}
console.log("selected " + select.options[select.selectedIndex].value);
}
var Module = {
preRun: [],
postRun: [],
print: (function() {
var console_output = document.getElementById("console_output");
if(console_output)
console_output.value = "";
return function(text) {
if(arguments.length > 1)
text = Array.prototype.slice.call(arguments).join(" ");
console.log(text);
if(console_output)
{
console_output.value += text + "\n";
console_output.scrollTop = console_output.scrollHeight;
}
};
})(),
printErr: function(text) {
var console_output = document.getElementById("console_output");
console.error(text);
if(console_output)
{
console_output.value += "ERROR: " + text + "\n";
console_output.scrollTop = console_output.scrollHeight;
}
},
canvas: (function() {
var canvas = document.getElementById("screen");
canvas.addEventListener("webglcontextlost", function(e) {console.error("webgl ctx lost"); e.preventDefault();}, false);
return canvas;
})(),
setStatus: (function(text) {
})(),
onRuntimeInitialized: function() {
console.log("initialized");
boing = Module.ccall("init_host", "number", null);
console.log("boing return value: " + boing);
script_run = Module.cwrap("script_run", null, ["number", "string"]);
console.log("script_run: " + script_run);
document.getElementById("run_button").onclick = function(){
var script_text = document.getElementById("script").value;
var console_output = document.getElementById("console_output");
console_output.value = "";
console.log("running script: '" + script_text + "'");
script_run(boing, script_text);
};
}
};
console.log("loading...");
</script>
<script async type="text/javascript" src="boing.js"></script>
<script type="text/javascript">
// just a bunch of literals for templates
var templates = [
`l<I 20{
p"hello world" nI
}`,
`# test preserving a new scope stack
wVAR "test"
wSCRIPT
{
p("the variable is " VAR) # first time, should print 0, after should print hello
wVAR "hello"
}
wSTACK k-1
e(SCRIPT [] STACK FALSE) # print 0
e(SCRIPT [] STACK FALSE) # print hello
pVAR # print test`,
`wTABLE[
["hello" 3]
["hello1" 5]
["hello2" 66]
["hello3" 89]
]
ptTABLE"hello5"
t(TABLE "hello5" "value")
ptTABLE"hello5"
wTABLE_T[]
t(TABLE_T "row1" "test")
t(TABLE_T "row2" "test1")
t(TABLE_T "row3" "test2")
t(TABLE_T "row4" "test3")
t(TABLE_T "row5" "test4")`,
`#!../boing
wWIDTH 80
wHEIGHT 24
wMAXITERS 70
wSCALEFACTOR '0.04'
wXOFFSET '0.0'
wYOFFSET '0.0F'
wBENCHMARK 1
wPIXTYPES[" " "." "^" "~" "+" "*" "C" "O" "=" "$" "%" "#" "@"]
wXITER 0
wYITER 0
wITER 0
wX 0
wY 0
wXX 0
wYY 0
wTX 0
wCALCPIX
{
/(*(iARGS 0 12) MAXITERS)
}
p"mandelbrot:"
l(<YITER HEIGHT)
{
wLINE "" #the line to print
wXITER 0
l(<XITER WIDTH)
{
wXX +(*(-(XITER /(WIDTH 2)) SCALEFACTOR) XOFFSET)
wYY +(*(-(YITER /(HEIGHT 2)) *(SCALEFACTOR 2)) XOFFSET)
#l(<(&(<(+(*(X X) *(Y Y)) 4) ITER) +(MAXITERS 1)))
l(&( <( +( *(X X) *(Y Y) ) 4) <(ITER MAXITERS)))
{
wTX +(-(*(X X) *(Y Y)) XX)
wY +(*(2 X Y) YY)
wX TX
#wITER +(ITER 1)
nITER
}
wLINE +(LINE i(PIXTYPES eCALCPIX[ITER]))
wITER 0
wX 0
wY 0
#wXITER+(XITER 1) # no inc op yet
nXITER
}
pLINE
#wYITER+(YITER 1) # no inc op yet
nYITER
}`,
`wWINDOW xSCREEN_CREATE_WINDOW["boing" 640 480 TRUE]
wQUIT 0
wMOUSE_STATUS 0
wLOOP_FUNC
{
wEVENT 0
l(xSCREEN_POLL_EVENT[EVENT])
{
0f(= tEVENT"type" SCREEN_QUIT)
{
nQUIT
}
f(= tEVENT"type" SCREEN_MOUSEBUTTONDOWN)
{
wMOUSE_STATUS 1
}
f(= tEVENT"type" SCREEN_MOUSEBUTTONUP)
{
wMOUSE_STATUS 0
}
f(= tEVENT"type" SCREEN_MOUSEMOTION)
{
0fMOUSE_STATUS
{
xSCREEN_DRAW_COLOR[WINDOW tEVENT"x" tEVENT"y" tEVENT"x" tEVENT"y"]
xSCREEN_DRAW_RECT[WINDOW xSCREEN_RECT[-(tEVENT"x" 5) -(tEVENT"y" 5) 10 10]]
}
}
}
# xSCREEN_WINDOW_PRESENT[WINDOW]
xSCREEN_DELAY[16]
}
xOS_EMSCRIPTEN_SET_LOOP[LOOP_FUNC]`,
`wWINDOW xSCREEN_CREATE_WINDOW["hello from boing" 640 480 TRUE]
xSCREEN_WINDOW_CLEAR[WINDOW]
xSCREEN_WINDOW_PRESENT[WINDOW]
l(< I 180)
{
xSCREEN_DRAW_COLOR[WINDOW g(255) g(255) g(255) g(255)]
xSCREEN_DRAW_LINE[WINDOW g(640) g(480) g(640) g(480)]
xSCREEN_WINDOW_PRESENT[WINDOW]
nI
}
`
];
</script>
</body>
</html>