control keys, dobackslash
[watForth.git] / forth.wat
1 (module
2 (type $FUNCSIGi (func (result i32)))
3 (type $FUNCSIGii (func (param i32)))
4 (type $FUNCSIGiii (func))
5 (type $FUNCSIGiv (func (param i32 i32) (result i32)))
6 (type $FUNCSIG$v (func (param i32) (result i32)))
7 (type $FUNCSIG$vi (func (param i32 i32) (result i32)))
8 (import "env" "pop" (func $pop (result i32)))
9 (import "env" "push" (func $push (param i32)))
10 (import "env" "rinit" (func $rinit))
11 (import "env" "rpop" (func $rpop (result i32)))
12 (import "env" "rpush" (func $rpush (param i32)))
13 (import "env" "sys_read" (func $sys_read (param i32 i32) (result i32)))
14 (import "env" "sys_fetch" (func $sys_fetch (param i32 i32) (result i32)))
15 (import "env" "sys_listen" (func $sys_listen (param i32) (result i32)))
16 (import "env" "sys_write" (func $sys_write (param i32 i32 i32) (result i32)))
17 (import "env" "sys_echo" (func $sys_echo (param i32)))
18 (import "env" "sys_echochar" (func $sys_echochar (param i32)))
19 (import "env" "sys_reflect" (func $sys_reflect (param i32)))
20 (import "env" "vocab_get" (func $vocab_get (param i32 i32) (result i32)))
21 (import "env" "vocab_set" (func $vocab_set (param i32 i32 i32) (result i32)))
22 (import "env" "is_whitespace" (func $is_whitespace (param i32) (result i32)))
23 (import "env" "sys_parsenum" (func $sys_parsenum (param i32 i32 i32) (result i32)))
24 (import "env" "sys_stack" (func $sys_stack))
25 (import "env" "sys_words" (func $sys_words))
26 (table (;0;) 0 anyfunc)
27 (memory $0 1)
28 (; String Belt ;) ;; 0x0000 Size: 8192
29 (global $wordbelt i32 (i32.const 8192)) ;; 0x2000 Size: 4096
30 (global $inbuf i32 (i32.const 12288)) ;; 0x3000 Size: 2048
31 (global $inbuf_size i32 (i32.const 12292))
32 (global $inbuf_data i32 (i32.const 12296))
33 (global $kvars i32 (i32.const 14336)) ;; 0x3800 Size: 2048
34 (data (i32.const 12288) "\fc\07\00\00") ;; 2044 len
35 (data (i32.const 14336) "\28\41\00\00") ;; MODE
36 (data (i32.const 14340) "\04\42\00\00") ;; HERE
37 (data (i32.const 14344) "\00\40\00\00") ;; START
38 (data (i32.const 14348) "\0a\00\00\00") ;; BASE
39 (data (i32.const 14352) "\00\00\00\00") ;; STRINGBELT_TAIL
40 (data (i32.const 14356) "\00\00\00\00") ;; STRINGBELT_HEAD
41 (data (i32.const 14360) "\00\20\00\00") ;; WORDBELT_TAIL
42 (data (i32.const 14364) "\00\20\00\00") ;; WORDBELT_HEAD
43 (data (i32.const 14368) "\00\00\00\00") ;; CHANNEL
44 (; Quit ;)
45 (data (i32.const 16384) "\03\00\00\00") ;; RINIT xt
46 (data (i32.const 16388) "\10\40\00\00") ;; INTERPRET xt
47 (data (i32.const 16392) "\12\00\00\00") ;; JMP xt
48 (data (i32.const 16396) "\00\40\00\00") ;; quit location (16384)
49 (; Interpret ;)
50 (data (i32.const 16400) "\74\40\00\00") ;; WORD xt (16500)
51 (data (i32.const 16404) "\06\00\00\00") ;; DUP
52 (data (i32.const 16408) "\0e\00\00\00") ;; JZ:
53 (data (i32.const 16412) "\38\40\00\00") ;; INTERP-END addr (16444)
54 (data (i32.const 16416) "\02\00\00\00") ;; LIT xt
55 (data (i32.const 16420) "\00\38\00\00") ;; MODE addr (14336)
56 (data (i32.const 16424) "\0a\00\00\00") ;; @ (fetch) xt
57 (data (i32.const 16428) "\0c\00\00\00") ;; EXECUTE xt
58 (data (i32.const 16432) "\0d\00\00\00") ;; NOOP xt
59 (data (i32.const 16436) "\01\00\00\00") ;; RET
60 (data (i32.const 16440) "\10\00\00\00") ;; DROP <-- INTERP-END
61 (data (i32.const 16444) "\10\00\00\00") ;; DROP
62 (data (i32.const 16448) "\19\00\00\00") ;; BYE
63 (; Word ;)
64 (data (i32.const 16500) "\14\00\00\00") ;; WORDSTART
65 (data (i32.const 16504) "\05\00\00\00") ;; KEY <-- KEYLOOP
66 (data (i32.const 16508) "\06\00\00\00") ;; DUP
67 (data (i32.const 16512) "\18\00\00\00") ;; J-1: 18
68 (data (i32.const 16516) "\f0\40\00\00") ;; addr of WORDEND
69 (data (i32.const 16520) "\11\00\00\00") ;; WS?
70 (data (i32.const 16524) "\0f\00\00\00") ;; JNZ:
71 (data (i32.const 16528) "\bc\40\00\00") ;; addr of KEYDROP
72 (data (i32.const 16532) "\02\00\00\00") ;; LIT
73 (data (i32.const 16536) "\5c\00\00\00") ;; 92 (\ character)
74 (data (i32.const 16540) "\25\00\00\00") ;; =?
75 (data (i32.const 16544) "\0e\00\00\00") ;; JZ:
76 (data (i32.const 16548) "\e4\40\00\00") ;; addr of DOCHAR
77 (data (i32.const 16552) "\10\00\00\00") ;; DROP
78 (data (i32.const 16556) "\94\41\00\00") ;; DO-BACKSLASH (continue using this wbuf we started)
79 (data (i32.const 16560) "\0d\00\00\00") ;; NOOP
80 (data (i32.const 16564) "\12\00\00\00") ;; JMP:
81 (data (i32.const 16568) "\74\40\00\00") ;; addr of KEYLOOP-1 (get a new wbuf, call to wbuf+1 ate ours)
82 (data (i32.const 16572) "\10\00\00\00") ;; DROP <-- KEYDROP
83 (data (i32.const 16576) "\12\00\00\00") ;; JMP:
84 (data (i32.const 16580) "\78\40\00\00") ;; addr of KEYLOOP
85 (data (i32.const 16584) "\05\00\00\00") ;; KEY <-- WORDLOOP
86 (data (i32.const 16588) "\11\00\00\00") ;; WS?
87 (data (i32.const 16592) "\0f\00\00\00") ;; JNZ:
88 (data (i32.const 16596) "\f0\40\00\00") ;; addr of WORDEND
89 (data (i32.const 16600) "\06\00\00\00") ;; DUP
90 (data (i32.const 16604) "\18\00\00\00") ;; J-1:
91 (data (i32.const 16608) "\f0\40\00\00") ;; addr of WORDEND
92 (data (i32.const 16612) "\13\00\00\00") ;; WORDPUTC <-- DOCHAR
93 (data (i32.const 16616) "\12\00\00\00") ;; JMP:
94 (data (i32.const 16620) "\c8\40\00\00") ;; addr of WORDLOOP
95 (data (i32.const 16624) "\10\00\00\00") ;; DROP <-- WORDEND
96 (data (i32.const 16628) "\17\00\00\00") ;; WORDFINISH
97 (data (i32.const 16632) "\01\00\00\00") ;; RET
98 (; Exec Mode ;)
99 (data (i32.const 16680) "\1e\00\00\00") ;; DUP2
100 (data (i32.const 16684) "\15\00\00\00") ;; DICT_GET
101 (data (i32.const 16688) "\06\00\00\00") ;; DUP
102 (data (i32.const 16692) "\0e\00\00\00") ;; JZ:
103 (data (i32.const 16696) "\4c\41\00\00") ;; donum -1 (16716)
104 (data (i32.const 16700) "\1f\00\00\00") ;; ROT
105 (data (i32.const 16704) "\20\00\00\00") ;; DROP2
106 (data (i32.const 16708) "\0c\00\00\00") ;; EXECUTE
107 (data (i32.const 16712) "\01\00\00\00") ;; RET
108 (data (i32.const 16716) "\10\00\00\00") ;; DROP (xt from dictionary)
109 (data (i32.const 16720) "\16\00\00\00") ;; NUMBER <-- donum, pushes NUM, UNPARSED
110 (data (i32.const 16724) "\06\00\00\00") ;; DUP
111 (data (i32.const 16728) "\0f\00\00\00") ;; JNZ:
112 (data (i32.const 16732) "\68\41\00\00") ;; donum_err (16744)
113 (data (i32.const 16736) "\10\00\00\00") ;; DROP
114 (data (i32.const 16740) "\01\00\00\00") ;; RET
115 (data (i32.const 16744) "\10\00\00\00") ;; PARSE_ERR <-- donum_err
116 (data (i32.const 16748) "\10\00\00\00") ;; ( DROP DROP )
117 (data (i32.const 16752) "\19\00\00\00") ;; BYE
118 (; Do Backslash ;)
119 (data (i32.const 16788) "\05\00\00\00") ;; KEY
120 (data (i32.const 16792) "\02\00\00\00") ;; LIT
121 (data (i32.const 16796) "\20\00\00\00") ;; 32 (space)
122 (data (i32.const 16800) "\25\00\00\00") ;; =?
123 (data (i32.const 16804) "\0f\00\00\00") ;; JNZ:
124 (data (i32.const 16808) "\bc\41\00\00") ;; addr of keypump
125 (data (i32.const 16812) "\cc\40\00\00") ;; WORDLOOP + 1
126 (data (i32.const 16816) "\28\41\00\00") ;; EXECUTE-MODE
127 (data (i32.const 16820) "\01\00\00\00") ;; RET
128 (data (i32.const 16828) "\18\00\00\00") ;; j-1: <-- keypump
129 (data (i32.const 16832) "\e0\41\00\00") ;; addr of end
130 (data (i32.const 16836) "\05\00\00\00") ;; KEY
131 (data (i32.const 16840) "\02\00\00\00") ;; LIT
132 (data (i32.const 16844) "\0a\00\00\00") ;; 10 (line feed)
133 (data (i32.const 16848) "\25\00\00\00") ;; =?
134 (data (i32.const 16852) "\0e\00\00\00") ;; JZ:
135 (data (i32.const 16856) "\bc\41\00\00") ;; addr of keypump
136 (data (i32.const 16860) "\10\00\00\00") ;; DROP
137 (data (i32.const 16864) "\01\00\00\00") ;; RET
138
139 (export "memory" (memory $0))
140 (export "main" (func $main))
141 (func $main (result i32)
142 call $interpret
143 )
144 (func $interpret (result i32)
145 (local $here i32)
146 (local $eax i32)
147 (local $esi i32)
148 (local $inbuf_head i32)
149 (local $stringbelt_tail i32)
150 (local $stringbelt_head i32)
151 (local $wordbelt_tail i32)
152 (local $wordbelt_head i32)
153 (local $channel i32)
154 i32.const 14340
155 i32.load
156 set_local $here
157 i32.const 14344
158 i32.load
159 set_local $esi
160 get_global $inbuf_data
161 set_local $inbuf_head
162 i32.const 14352
163 i32.load
164 set_local $stringbelt_tail
165 i32.const 14356
166 i32.load
167 set_local $stringbelt_head
168 i32.const 14360
169 i32.load
170 set_local $wordbelt_tail
171 i32.const 14364
172 i32.load
173 set_local $wordbelt_head
174 i32.const 14368
175 i32.load
176 set_local $channel
177 block $bye
178 loop $next
179 call $sys_stack
180 get_local $esi
181 call $sys_reflect
182 get_local $esi
183 get_local $esi
184 i32.const 4
185 i32.add
186 set_local $esi
187 i32.load
188 set_local $eax
189 loop $execloop
190 block $default block $op0 block $ret block $lit block $rinit
191 block $word block $key block $dup block $plus block $noop2 block $emit
192 block $fetch block $set block $execute block $noop block $jz block $jnz
193 block $drop block $wsbool block $jmp block $wordputc block $wordstart
194 block $dictget block $parsenum block $wordfinish block $jneg1 block $swap
195 block $words block $here block $dictset block $dup2 block $rot block $drop2
196 block $comma block $subtract block $keychan block $sethere block $eqbool
197 block $echostring
198 get_local $eax
199 br_table $op0 $ret (;2;)$lit $rinit (;4;)$word $key (;6;)$dup $plus
200 (;8;)$jmp $emit (;10;)$fetch $set (;12;)$execute $noop (;14;)$jz $jnz
201 (;16;)$drop $wsbool (;18;)$jmp $wordputc (;20;)$wordstart $dictget
202 (;22;)$parsenum $wordfinish (;24;)$jneg1 $bye (;26;)$swap $words
203 (;28;)$here $dictset (;30;)$dup2 $rot (;32;)$drop2 $comma
204 (;34;)$subtract $keychan (;36;)$sethere $eqbool (;38;)$echostring $default
205 end ;; echostring
206 get_local $channel
207 call $pop
208 set_local $eax
209 call $pop
210 get_local $eax
211 call $sys_write
212 br $next
213 end ;; eqbool
214 block $equiv
215 call $pop
216 call $pop
217 tee_local $eax
218 i32.eq
219 get_local $eax
220 call $push
221 br_if $equiv
222 i32.const 0
223 call $push
224 br $next
225 end
226 i32.const 1
227 call $push
228 br $next
229 end ;; sethere
230 call $pop
231 set_local $here
232 br $next
233 end ;; keychan
234 call $pop
235 set_local $channel
236 br $next
237 end ;; subtract
238 call $pop
239 set_local $eax
240 call $pop
241 get_local $eax
242 i32.sub
243 call $push
244 br $next
245 end ;; comma
246 get_local $here
247 call $pop
248 i32.store
249 get_local $here
250 i32.const 4
251 i32.add
252 set_local $here
253 br $next
254 end ;; drop2
255 call $pop
256 call $pop
257 drop
258 drop
259 br $next
260 end ;; rot
261 call $pop
262 set_local $eax
263 call $pop
264 call $pop
265 get_local $eax
266 call $push
267 call $push
268 call $push
269 br $next
270 end ;; dup2
271 get_local $esi
272 call $rpush
273 call $pop
274 set_local $eax
275 call $pop
276 tee_local $esi
277 call $push
278 get_local $eax
279 call $push
280 get_local $esi
281 call $push
282 get_local $eax
283 call $push
284 call $rpop
285 set_local $esi
286 br $next
287 end ;; dictset
288 call $pop
289 call $rpush
290 call $pop
291 set_local $eax
292 call $pop
293 get_local $eax
294 call $rpop
295 call $vocab_set
296 drop
297 br $next
298 end ;; here
299 get_local $here
300 call $push
301 br $next
302 end ;; words
303 call $sys_words
304 br $next
305 end ;; swap
306 call $pop
307 call $pop
308 set_local $eax
309 call $push
310 get_local $eax
311 call $push
312 br $next
313 end ;; jneg1
314 block $jneg1if
315 call $pop
316 i32.const -1
317 i32.eq
318 br_if $jneg1if
319 get_local $esi
320 i32.const 4
321 i32.add
322 set_local $esi
323 br $next
324 end
325 get_local $esi
326 i32.load
327 set_local $esi
328 br $next
329 end ;; wordfinish
330 get_local $wordbelt_tail
331 get_local $wordbelt_head
332 get_local $wordbelt_tail
333 i32.const 4
334 i32.add
335 i32.sub
336 tee_local $eax (; n bytes ;)
337 i32.store
338 (; align to 32-bit ;)
339 get_local $wordbelt_head
340 i32.const 3
341 i32.add
342 i32.const 12284
343 i32.and
344 set_local $wordbelt_head
345 (; /align ;)
346 get_local $wordbelt_tail
347 i32.const 4
348 i32.add
349 call $push
350 get_local $eax
351 call $push
352 br $next
353 end ;; parsenum
354 call $pop
355 call $rpush
356 call $pop
357 tee_local $eax
358 call $rpop
359 i32.const 14348 (; load BASE ;)
360 i32.load
361 call $sys_parsenum
362 get_local $eax
363 i32.load
364 call $push
365 call $push
366 br $next
367 end ;; dictget
368 call $pop
369 set_local $eax
370 call $pop
371 get_local $eax
372 call $vocab_get
373 call $push
374 br $next
375 end ;; wordstart
376 block $wbhasspace
377 get_local $wordbelt_head
378 get_global $inbuf
379 i32.const 8
380 i32.sub
381 i32.le_u
382 br_if $wbhasspace
383 get_global $wordbelt
384 set_local $wordbelt_head
385 end
386 get_local $wordbelt_head
387 get_local $wordbelt_head
388 tee_local $wordbelt_tail
389 i32.const 0
390 i32.store
391 i32.const 4
392 i32.add
393 set_local $wordbelt_head
394 br $next
395 end ;; wordputc
396 block $wbhasspace2
397 get_local $wordbelt_head
398 get_global $inbuf
399 i32.lt_u
400 br_if $wbhasspace2
401 get_global $wordbelt
402 tee_local $wordbelt_head
403 get_local $wordbelt_tail
404 i32.load
405 i32.store
406 get_local $wordbelt_head
407 i32.const 4
408 i32.add
409 set_local $wordbelt_head
410 get_local $wordbelt_tail
411 i32.const 4
412 i32.add
413 set_local $wordbelt_tail
414 loop $copywordtostart
415 get_local $wordbelt_head
416 get_local $wordbelt_tail
417 i32.load16_u
418 i32.store16
419 get_local $wordbelt_head
420 i32.const 2
421 i32.add
422 set_local $wordbelt_head
423 get_local $wordbelt_tail
424 i32.const 2
425 i32.add
426 tee_local $wordbelt_tail
427 get_global $inbuf
428 i32.le_u
429 br_if $copywordtostart
430 end
431 get_global $wordbelt
432 set_local $wordbelt_tail
433 end
434 get_local $wordbelt_head
435 call $pop
436 i32.store16
437 get_local $wordbelt_head
438 i32.const 2
439 i32.add
440 set_local $wordbelt_head
441 br $next
442 end ;; jmp
443 get_local $esi
444 i32.load
445 set_local $esi
446 br $next
447 end ;; wsbool
448 call $pop
449 tee_local $eax
450 call $is_whitespace
451 get_local $eax
452 call $push
453 call $push
454 call $sys_stack
455 br $next
456 end ;; drop
457 call $pop
458 drop
459 br $next
460 end ;; jnz
461 block $jnzif
462 call $pop
463 i32.eqz
464 br_if $jnzif
465 get_local $esi
466 i32.load
467 set_local $esi
468 br $next
469 end
470 get_local $esi
471 i32.const 4
472 i32.add
473 set_local $esi
474 br $next
475 end ;; jz
476 block $jzif
477 call $pop
478 i32.eqz
479 br_if $jzif
480 get_local $esi
481 i32.const 4
482 i32.add
483 set_local $esi
484 br $next
485 end
486 get_local $esi
487 i32.load
488 set_local $esi
489 br $next
490 end ;; noop
491 br $next
492 end ;; execute
493 call $pop
494 tee_local $eax
495 i32.const 256
496 i32.lt_u
497 br_if $execloop
498 get_local $esi
499 call $rpush
500 get_local $eax
501 set_local $esi
502 br $next
503 end ;; set
504 call $pop
505 set_local $eax
506 call $pop
507 get_local $eax
508 i32.store
509 br $next
510 end ;; fetch
511 call $pop
512 i32.load
513 call $push
514 br $next
515 end ;; emit (.)
516 call $pop
517 call $sys_echo
518 br $next
519 end ;; noop2
520 br $next
521 end ;; plus
522 call $pop
523 call $pop
524 i32.add
525 call $push
526 br $next
527 end ;; dup
528 call $pop
529 tee_local $eax
530 get_local $eax
531 call $push
532 call $push
533 br $next
534 end ;; key
535 loop $key_loop
536 block $key_read
537 get_global $inbuf_size
538 i32.load
539 get_local $inbuf_head
540 get_global $inbuf_data
541 i32.sub
542 i32.le_u
543 br_if $key_read
544 get_local $inbuf_head
545 i32.load16_u
546 call $push
547 get_local $inbuf_head
548 i32.const 2
549 i32.add
550 set_local $inbuf_head
551 br $next
552 end ;; key_read
553 get_local $channel
554 get_global $inbuf
555 call $sys_read
556 block $nullread
557 get_global $inbuf_size
558 i32.load
559 i32.eqz
560 br_if $nullread
561 br $key_loop
562 end ;; nullread
563 i32.const -1 ;; <- keyval sent if sz == 0
564 call $push
565 br $next
566 end ;; key_loop
567 end ;; word
568 br $next
569 end ;; rinit
570 call $rinit
571 br $next
572 end ;; lit
573 get_local $esi
574 get_local $esi
575 i32.const 4
576 i32.add
577 set_local $esi
578 i32.load
579 call $push
580 br $next
581 end ;; ret
582 call $rpop
583 set_local $esi
584 br $next
585 end ;; op0
586 get_local $esi
587 call $sys_reflect
588 br $bye
589 end ;; default
590 get_local $esi
591 call $rpush
592 get_local $eax
593 set_local $esi
594 br $next
595 end ;; execloop
596 end ;; nextl
597 end ;; bye
598 i32.const 14340
599 get_local $here
600 i32.store
601 i32.const 14352
602 get_local $stringbelt_tail
603 i32.store
604 i32.const 14356
605 get_local $stringbelt_head
606 i32.store
607 i32.const 14360
608 get_local $wordbelt_tail
609 i32.store
610 i32.const 14364
611 get_local $wordbelt_head
612 i32.store
613 i32.const 14368
614 get_local $channel
615 i32.store
616 i32.const 0
617 )
618 )