about summary refs log tree commit diff stats
path: root/linux/tile/rpn.mu
blob: 95d5356a46aef8516b255f09482d4a99eabf0efa (plain) (blame)
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
fn evaluate functions: (addr handle function), bindings: (addr table), scratch: (addr line), end: (addr word), out: (addr value-stack) {
  var line/eax: (addr line) <- copy scratch
  var word-ah/eax: (addr handle word) <- get line, data
  var curr/eax: (addr word) <- lookup *word-ah
  var curr-stream-storage: (stream byte 0x10)
  var curr-stream/edi: (addr stream byte) <- address curr-stream-storage
  clear-value-stack out
  $evaluate:loop: {
    # precondition (should never hit)
    compare curr, 0
    break-if-=
    # update curr-stream
    emit-word curr, curr-stream
#?     print-string-to-real-screen "eval: "
#?     print-stream-to-real-screen curr-stream
#?     print-string-to-real-screen "\n"
    $evaluate:process-word: {
      ### if curr-stream is an operator, perform it
      ## numbers
      {
        var add?/eax: boolean <- stream-data-equal? curr-stream, "+"
        compare add?, 0
        break-if-=
        var _b/xmm0: float <- pop-number-from-value-stack out
        var b/xmm1: float <- copy _b
        var a/xmm0: float <- pop-number-from-value-stack out
        a <- add b
        push-number-to-value-stack out, a
        break $evaluate:process-word
      }
      {
        var sub?/eax: boolean <- stream-data-equal? curr-stream, "-"
        compare sub?, 0
        break-if-=
        var _b/xmm0: float <- pop-number-from-value-stack out
        var b/xmm1: float <- copy _b
        var a/xmm0: float <- pop-number-from-value-stack out
        a <- subtract b
        push-number-to-value-stack out, a
        break $evaluate:process-word
      }
      {
        var mul?/eax: boolean <- stream-data-equal? curr-stream, "*"
        compare mul?, 0
        break-if-=
        var _b/xmm0: float <- pop-number-from-value-stack out
        var b/xmm1: float <- copy _b
        var a/xmm0: float <- pop-number-from-value-stack out
        a <- multiply b
        push-number-to-value-stack out, a
        break $evaluate:process-word
      }
      {
        var div?/eax: boolean <- stream-data-equal? curr-stream, "/"
        compare div?, 0
        break-if-=
        var _b/xmm0: float <- pop-number-from-value-stack out
        var b/xmm1: float <- copy _b
        var a/xmm0: float <- pop-number-from-value-stack out
        a <- divide b
        push-number-to-value-stack out, a
        break $evaluate:process-word
      }
      {
        var sqrt?/eax: boolean <- stream-data-equal? curr-stream, "sqrt"
        compare sqrt?, 0
        break-if-=
        var a/xmm0: float <- pop-number-from-value-stack out
        a <- square-root a
        push-number-to-value-stack out, a
        break $evaluate:process-word
      }
      ## strings/arrays
      {
        var len?/eax: boolean <- stream-data-equal? curr-stream, "len"
        compare len?, 0
        break-if-=
#?         print-string 0, "is len\n"
        # pop target-val from out
        var out2/esi: (addr value-stack) <- copy out
        var top-addr/ecx: (addr int) <- get out2, top
        compare *top-addr, 0
        break-if-<=
#?         print-string 0, "stack has stuff\n"
        var data-ah/eax: (addr handle array value) <- get out2, data
        var data/eax: (addr array value) <- lookup *data-ah
        var top/edx: int <- copy *top-addr
        top <- decrement
        var dest-offset/edx: (offset value) <- compute-offset data, top
        var target-val/edx: (addr value) <- index data, dest-offset
        # check target-val is a string or array
        var target-type-addr/eax: (addr int) <- get target-val, type
        compare *target-type-addr, 1/string
        {
          break-if-!=
          # compute length
          var src-ah/eax: (addr handle array byte) <- get target-val, text-data
          var src/eax: (addr array byte) <- lookup *src-ah
          var result/ebx: int <- length src
          var result-f/xmm0: float <- convert result
          # save result into target-val
          var type-addr/eax: (addr int) <- get target-val, type
          copy-to *type-addr, 0/int
          var target-string-ah/eax: (addr handle array byte) <- get target-val, text-data
          clear-object target-string-ah
          var target/eax: (addr float) <- get target-val, number-data
          copy-to *target, result-f
          break $evaluate:process-word
        }
        compare *target-type-addr, 2/array
        {
          break-if-!=
          # compute length
          var src-ah/eax: (addr handle array value) <- get target-val, array-data
          var src/eax: (addr array value) <- lookup *src-ah
          var result/ebx: int <- length src
          var result-f/xmm0: float <- convert result
          # save result into target-val
          var type-addr/eax: (addr int) <- get target-val, type
          copy-to *type-addr, 0/int
          var target-array-ah/eax: (addr handle array value) <- get target-val, array-data
          clear-object target-array-ah
          var target/eax: (addr float) <- get target-val, number-data
          copy-to *target, result-f
          break $evaluate:process-word
        }
      }
      ## files
      {
        var open?/eax: boolean <- stream-data-equal? curr-stream, "open"
        compare open?, 0
        break-if-=
        # pop target-val from out
        var out2/esi: (addr value-stack) <- copy out
        var top-addr/ecx: (addr int) <- get out2, top
        compare *top-addr, 0
        break-if-<=
        var data-ah/eax: (addr handle array value) <- get out2, data
        var data/eax: (addr array value) <- lookup *data-ah
        var top/edx: int <- copy *top-addr
        top <- decrement
        var dest-offset/edx: (offset value) <- compute-offset data, top
        var target-val/edx: (addr value) <- index data, dest-offset
        # check target-val is a string
        var target-type-addr/eax: (addr int) <- get target-val, type
        compare *target-type-addr, 1/string
        break-if-!=
        # open target-val as a filename and save the handle in target-val
        var src-ah/eax: (addr handle array byte) <- get target-val, text-data
        var src/eax: (addr array byte) <- lookup *src-ah
        var result-ah/ecx: (addr handle buffered-file) <- get target-val, file-data
        open src, 0, result-ah  # write? = false
        # save result into target-val
        var type-addr/eax: (addr int) <- get target-val, type
        copy-to *type-addr, 3/file
        var target-string-ah/eax: (addr handle array byte) <- get target-val, text-data
        var filename-ah/ecx: (addr handle array byte) <- get target-val, filename
        copy-object target-string-ah, filename-ah
        clear-object target-string-ah
        break $evaluate:process-word
      }
      {
        var read?/eax: boolean <- stream-data-equal? curr-stream, "read"
        compare read?, 0
        break-if-=
        # pop target-val from out
        var out2/esi: (addr value-stack) <- copy out
        var top-addr/ecx: (addr int) <- get out2, top
        compare *top-addr, 0
        break-if-<=
        var data-ah/eax: (addr handle array value) <- get out2, data
        var data/eax: (addr array value) <- lookup *data-ah
        var top/edx: int <- copy *top-addr
        top <- decrement
        var dest-offset/edx: (offset value) <- compute-offset data, top
        var target-val/edx: (addr value) <- index data, dest-offset
        # check target-val is a file
        var target-type-addr/eax: (addr int) <- get target-val, type
        compare *target-type-addr, 3/file
        break-if-!=
        # read a line from the file and save in target-val
        # read target-val as a filename and save the handle in target-val
        var file-ah/eax: (addr handle buffered-file) <- get target-val, file-data
        var file/eax: (addr buffered-file) <- lookup *file-ah
        var s: (stream byte 0x100)
        var s-addr/ecx: (addr stream byte) <- address s
        read-line-buffered file, s-addr
        var target/eax: (addr handle array byte) <- get target-val, text-data
        stream-to-array s-addr, target
        # save result into target-val
        var type-addr/eax: (addr int) <- get target-val, type
        copy-to *type-addr, 1/string
        var target-file-ah/eax: (addr handle buffered-file) <- get target-val, file-data
        clear-object target-file-ah
        break $evaluate:process-word
      }
      {
        var slurp?/eax: boolean <- stream-data-equal? curr-stream, "slurp"
        compare slurp?, 0
        break-if-=
        # pop target-val from out
        var out2/esi: (addr value-stack) <- copy out
        var top-addr/ecx: (addr int) <- get out2, top
        compare *top-addr, 0
        break-if-<=
        var data-ah/eax: (addr handle array value) <- get out2, data
        var data/eax: (addr array value) <- lookup *data-ah
        var top/edx: int <- copy *top-addr
        top <- decrement
        var dest-offset/edx: (offset value) <- compute-offset data, top
        var target-val/edx: (addr value) <- index data, dest-offset
        # check target-val is a file
        var target-type-addr/eax: (addr int) <- get target-val, type
        compare *target-type-addr, 3/file
        break-if-!=
        # slurp all contents from file and save in target-val
        # read target-val as a filename and save the handle in target-val
        var file-ah/eax: (addr handle buffered-file) <- get target-val, file-data
        var file/eax: (addr buffered-file) <- lookup *file-ah
        var s: (stream byte 0x100)
        var s-addr/ecx: (addr stream byte) <- address s
        slurp file, s-addr
        var target/eax: (addr handle array byte) <- get target-val, text-data
        stream-to-array s-addr, target
        # save result into target-val
        var type-addr/eax: (addr int) <- get target-val, type
        copy-to *type-addr, 1/string
        var target-file-ah/eax: (addr handle buffered-file) <- get target-val, file-data
        clear-object target-file-ah
        break $evaluate:process-word
      }
      {
        var lines?/eax: boolean <- stream-data-equal? curr-stream, "lines"
        compare lines?, 0
        break-if-=
        # pop target-val from out
        var out2/esi: (addr value-stack) <- copy out
        var top-addr/ecx: (addr int) <- get out2, top
        compare *top-addr, 0
        break-if-<=
        var data-ah/eax: (addr handle array value) <- get out2, data
        var data/eax: (addr array value) <- lookup *data-ah
        var top/edx: int <- copy *top-addr
        top <- decrement
        var dest-offset/edx: (offset value) <- compute-offset data, top
        var target-val/edx: (addr value) <- index data, dest-offset
        # check target-val is a file
        var target-type-addr/eax: (addr int) <- get target-val, type
        compare *target-type-addr, 3/file
        break-if-!=
        # read all lines from file and save as an array of strings in target-val
        # read target-val as a filename and save the handle in target-val
        var file-ah/eax: (addr handle buffered-file) <- get target-val, file-data
        var file/eax: (addr buffered-file) <- lookup *file-ah
        var s: (stream byte 0x100)
        var s-addr/ecx: (addr stream byte) <- address s
        slurp file, s-addr
        var tmp-ah/eax: (addr handle array byte) <- get target-val, text-data
        stream-to-array s-addr, tmp-ah
        var tmp/eax: (addr array byte) <- lookup *tmp-ah
#?         enable-screen-type-mode
#?         print-string 0, tmp
        var h: (handle array (handle array byte))
        {
          var ah/edx: (addr handle array (handle array byte)) <- address h
          split-string tmp, 0xa, ah
        }
        var target/eax: (addr handle array value) <- get target-val, array-data
        save-lines h, target
        # save result into target-val
        var type-addr/eax: (addr int) <- get target-val, type
        copy-to *type-addr, 2/array
        var target-file-ah/eax: (addr handle buffered-file) <- get target-val, file-data
        var empty-file: (handle buffered-file)
        copy-handle empty-file, target-file-ah
        var target-text-ah/eax: (addr handle array byte) <- get target-val, text-data
        var empty-text: (handle array byte)
        copy-handle empty-text, target-text-ah
        break $evaluate:process-word
      }
      ## screens
      {
        var fake-screen?/eax: boolean <- stream-data-equal? curr-stream, "fake-screen"
        compare fake-screen?, 0
        break-if-=
        var out2/esi: (addr value-stack) <- copy out
        var top-addr/ecx: (addr int) <- get out2, top
        compare *top-addr, 0
        break-if-<=
        # pop width and height from out
        var nrows-f/xmm0: float <- pop-number-from-value-stack out2
        var nrows/edx: int <- convert nrows-f
        var ncols-f/xmm0: float <- pop-number-from-value-stack out2
        var ncols/ebx: int <- convert ncols-f
        # define a new screen with those dimensions
        var screen-h: (handle screen)
        var screen-ah/eax: (addr handle screen) <- address screen-h
        allocate screen-ah
        var screen/eax: (addr screen) <- lookup screen-h
        initialize-screen screen, nrows, ncols
        # push screen to stack
        var data-ah/eax: (addr handle array value) <- get out2, data
        var data/eax: (addr array value) <- lookup *data-ah
        var top/edx: int <- copy *top-addr
        increment *top-addr
        var dest-offset/edx: (offset value) <- compute-offset data, top
        var target-val/edx: (addr value) <- index data, dest-offset
        var type/eax: (addr int) <- get target-val, type
        copy-to *type, 4/screen
        var dest/eax: (addr handle screen) <- get target-val, screen-data
        copy-handle screen-h, dest
        break $evaluate:process-word
      }
      {
        var print?/eax: boolean <- stream-data-equal? curr-stream, "print"
        compare print?, 0
        break-if-=
        var out2/esi: (addr value-stack) <- copy out
        var top-addr/ecx: (addr int) <- get out2, top
        compare *top-addr, 0
        break-if-<=
        # pop string from out
        var top-addr/ecx: (addr int) <- get out2, top
        compare *top-addr, 0
        break-if-<=
        decrement *top-addr
        var data-ah/eax: (addr handle array value) <- get out2, data
        var _data/eax: (addr array value) <- lookup *data-ah
        var data/edi: (addr array value) <- copy _data
        var top/eax: int <- copy *top-addr
        var dest-offset/edx: (offset value) <- compute-offset data, top
        var s/esi: (addr value) <- index data, dest-offset
        # select target screen from top of out (but don't pop it)
        compare *top-addr, 0
        break-if-<=
        var top/eax: int <- copy *top-addr
        top <- decrement
        var dest-offset/edx: (offset value) <- compute-offset data, top
        var target-val/edx: (addr value) <- index data, dest-offset
        var type/eax: (addr int) <- get target-val, type
        compare *type, 4/screen
        break-if-!=
        # print string to target screen
        var dest-ah/eax: (addr handle screen) <- get target-val, screen-data
        var dest/eax: (addr screen) <- lookup *dest-ah
        var r/ecx: (addr int) <- get dest, cursor-row
        var c/edx: (addr int) <- get dest, cursor-col
        render-value-at dest, *r, *c, s, 0
        break $evaluate:process-word
      }
      {
        var move?/eax: boolean <- stream-data-equal? curr-stream, "move"
        compare move?, 0
        break-if-=
        var out2/esi: (addr value-stack) <- copy out
        # pop args
        var r-f/xmm0: float <- pop-number-from-value-stack out2
        var r/ecx: int <- convert r-f
        var c-f/xmm0: float <- pop-number-from-value-stack out2
        var c/edx: int <- convert c-f
        # select screen from top of out (but don't pop it)
        var top-addr/ebx: (addr int) <- get out2, top
        compare *top-addr, 0
        break-if-<=
        var data-ah/eax: (addr handle array value) <- get out2, data
        var _data/eax: (addr array value) <- lookup *data-ah
        var data/edi: (addr array value) <- copy _data
        var top/eax: int <- copy *top-addr
        top <- decrement
        var target-offset/eax: (offset value) <- compute-offset data, top
        var target-val/ebx: (addr value) <- index data, target-offset
        var type/eax: (addr int) <- get target-val, type
        compare *type, 4/screen
        break-if-!=
        var target-ah/eax: (addr handle screen) <- get target-val, screen-data
        var target/eax: (addr screen) <- lookup *target-ah
        move-cursor target, r, c
        break $evaluate:process-word
      }
      {
        var up?/eax: boolean <- stream-data-equal? curr-stream, "up"
        compare up?, 0
        break-if-=
        var out2/esi: (addr value-stack) <- copy out
        var top-addr/ebx: (addr int) <- get out2, top
        compare *top-addr, 0
        break-if-<=
        # pop args
        var d-f/xmm0: float <- pop-number-from-value-stack out2
        var d/ecx: int <- convert d-f
        # select screen from top of out (but don't pop it)
        compare *top-addr, 0
        break-if-<=
        var data-ah/eax: (addr handle array value) <- get out2, data
        var _data/eax: (addr array value) <- lookup *data-ah
        var data/edi: (addr array value) <- copy _data
        var top/eax: int <- copy *top-addr
        top <- decrement
        var target-offset/eax: (offset value) <- compute-offset data, top
        var target-val/ebx: (addr value) <- index data, target-offset
        var type/eax: (addr int) <- get target-val, type
        compare *type, 4/screen
        break-if-!=
        var target-ah/eax: (addr handle screen) <- get target-val, screen-data
        var _target/eax: (addr screen) <- lookup *target-ah
        var target/edi: (addr screen) <- copy _target
        var r/edx: (addr int) <- get target, cursor-row
        var c/eax: (addr int) <- get target, cursor-col
        var col/eax: int <- copy *c
        {
          compare d, 0
          break-if-<=
          compare *r, 1
          break-if-<=
          print-string target "│"
          decrement *r
          move-cursor target, *r, col
          d <- decrement
          loop
        }
        break $evaluate:process-word
      }
      {
        var down?/eax: boolean <- stream-data-equal? curr-stream, "down"
        compare down?, 0
        break-if-=
        var out2/esi: (addr value-stack) <- copy out
        var top-addr/ebx: (addr int) <- get out2, top
        compare *top-addr, 0
        break-if-<=
        # pop args
        var d-f/xmm0: float <- pop-number-from-value-stack out2
        var d/ecx: int <- convert d-f
        # select screen from top of out (but don't pop it)
        compare *top-addr, 0
        break-if-<=
        var data-ah/eax: (addr handle array value) <- get out2, data
        var _data/eax: (addr array value) <- lookup *data-ah
        var data/edi: (addr array value) <- copy _data
        var top/eax: int <- copy *top-addr
        top <- decrement
        var target-offset/eax: (offset value) <- compute-offset data, top
        var target-val/ebx: (addr value) <- index data, target-offset
        var type/eax: (addr int) <- get target-val, type
        compare *type, 4/screen
        break-if-!=
        var target-ah/eax: (addr handle screen) <- get target-val, screen-data
        var _target/eax: (addr screen) <- lookup *target-ah
        var target/edi: (addr screen) <- copy _target
        var bound-a/ebx: (addr int) <- get target, num-rows
        var bound/ebx: int <- copy *bound-a
        var r/edx: (addr int) <- get target, cursor-row
        var c/eax: (addr int) <- get target, cursor-col
        var col/eax: int <- copy *c
        {
          compare d, 0
          break-if-<=
          compare *r, bound
          break-if->=
          print-string target "│"
          increment *r
          move-cursor target, *r, col
          d <- decrement
          loop
        }
        break $evaluate:process-word
      }
      {
        var left?/eax: boolean <- stream-data-equal? curr-stream, "left"
        compare left?, 0
        break-if-=
        var out2/esi: (addr value-stack) <- copy out
        var top-addr/ebx: (addr int) <- get out2, top
        compare *top-addr, 0
        break-if-<=
        # pop args
        var d-f/xmm0: float <- pop-number-from-value-stack out2
        var d/ecx: int <- convert d-f
        # select screen from top of out (but don't pop it)
        compare *top-addr, 0
        break-if-<=
        var data-ah/eax: (addr handle array value) <- get out2, data
        var _data/eax: (addr array value) <- lookup *data-ah
        var data/edi: (addr array value) <- copy _data
        var top/eax: int <- copy *top-addr
        top <- decrement
        var target-offset/eax: (offset value) <- compute-offset data, top
        var target-val/ebx: (addr value) <- index data, target-offset
        var type/eax: (addr int) <- get target-val, type
        compare *type, 4/screen
        break-if-!=
        var target-ah/eax: (addr handle screen) <- get target-val, screen-data
        var _target/eax: (addr screen) <- lookup *target-ah
        var target/edi: (addr screen) <- copy _target
        var c/edx: (addr int) <- get target, cursor-col
        var r/eax: (addr int) <- get target, cursor-row
        var row/eax: int <- copy *r
        {
          compare d, 0
          break-if-<=
          compare *c, 1
          break-if-<=
          print-string target "─"
          decrement *c
          decrement *c  # second one to undo the print above
          move-cursor target, row, *c
          d <- decrement
          loop
        }
        break $evaluate:process-word
      }
      {
        var right?/eax: boolean <- stream-data-equal? curr-stream, "right"
        compare right?, 0
        break-if-=
        var out2/esi: (addr value-stack) <- copy out
        var top-addr/ebx: (addr int) <- get out2, top
        compare *top-addr, 0
        break-if-<=
        # pop args
        var _d/xmm0: float <- pop-number-from-value-stack out2
        var d/ecx: int <- convert _d
        # select screen from top of out (but don't pop it)
        compare *top-addr, 0
        break-if-<=
        var data-ah/eax: (addr handle array value) <- get out2, data
        var _data/eax: (addr array value) <- lookup *data-ah
        var data/edi: (addr array value) <- copy _data
        var top/eax: int <- copy *top-addr
        top <- decrement
        var target-offset/eax: (offset value) <- compute-offset data, top
        var target-val/ebx: (addr value) <- index data, target-offset
        var type/eax: (addr int) <- get target-val, type
        compare *type, 4/screen
        break-if-!=
        var target-ah/eax: (addr handle screen) <- get target-val, screen-data
        var _target/eax: (addr screen) <- lookup *target-ah
        var target/edi: (addr screen) <- copy _target
        var bound-a/ebx: (addr int) <- get target, num-rows
        var bound/ebx: int <- copy *bound-a
        var c/edx: (addr int) <- get target, cursor-col
        var r/eax: (addr int) <- get target, cursor-row
        var row/eax: int <- copy *r
        {
          compare d, 0
          break-if-<=
          compare *c, bound
          break-if->=
          print-string target "─"
          # no increment; the print took care of it
          move-cursor target, row, *c
          d <- decrement
          loop
        }
        break $evaluate:process-word
      }
      ## HACKS: we're trying to avoid turning this into Forth
      {
        var dup?/eax: boolean <- stream-data-equal? curr-stream, "dup"
        compare dup?, 0
        break-if-=
        # read src-val from out
        var out2/esi: (addr value-stack) <- copy out
        var top-addr/ecx: (addr int) <- get out2, top
        compare *top-addr, 0
        break-if-<=
        var data-ah/eax: (addr handle array value) <- get out2, data
        var data/eax: (addr array value) <- lookup *data-ah
        var top/ecx: int <- copy *top-addr
        top <- decrement
        var offset/edx: (offset value) <- compute-offset data, top
        var src-val/edx: (addr value) <- index data, offset
        # push a copy of it
        top <- increment
        var offset/ebx: (offset value) <- compute-offset data, top
        var target-val/ebx: (addr value) <- index data, offset
        copy-object src-val, target-val
        # commit
        var top-addr/ecx: (addr int) <- get out2, top
        increment *top-addr
        break $evaluate:process-word
      }
      {
        var swap?/eax: boolean <- stream-data-equal? curr-stream, "swap"
        compare swap?, 0
        break-if-=
        # read top-val from out
        var out2/esi: (addr value-stack) <- copy out
        var top-addr/ecx: (addr int) <- get out2, top
        compare *top-addr, 0
        break-if-<=
        var data-ah/eax: (addr handle array value) <- get out2, data
        var data/eax: (addr array value) <- lookup *data-ah
        var top/ecx: int <- copy *top-addr
        top <- decrement
        var offset/edx: (offset value) <- compute-offset data, top
        var top-val/edx: (addr value) <- index data, offset
        # read next val from out
        top <- decrement
        var offset/ebx: (offset value) <- compute-offset data, top
        var pen-top-val/ebx: (addr value) <- index data, offset
        # swap
        var tmp: value
        var tmp-a/eax: (addr value) <- address tmp
        copy-object top-val, tmp-a
        copy-object pen-top-val, top-val
        copy-object tmp-a, pen-top-val
        break $evaluate:process-word
      }
      ### if curr-stream defines a binding, save top of stack to bindings
      {
        var done?/eax: boolean <- stream-empty? curr-stream
        compare done?, 0/false
        break-if-!=
        var new-byte/eax: byte <- read-byte curr-stream
        compare new-byte, 0x3d/=
        break-if-!=
        # pop target-val from out
        var out2/esi: (addr value-stack) <- copy out
        var top-addr/ecx: (addr int) <- get out2, top
        compare *top-addr, 0
        break-if-<=
        var data-ah/eax: (addr handle array value) <- get out2, data
        var data/eax: (addr array value) <- lookup *data-ah
        var top/edx: int <- copy *top-addr
        top <- decrement
        var dest-offset/edx: (offset value) <- compute-offset data, top
        var target-val/edx: (addr value) <- index data, dest-offset
        # create binding from curr-stream to target-val
        var key-h: (handle array byte)
        var key/ecx: (addr handle array byte) <- address key-h
        stream-to-array curr-stream, key
        bind-in-table bindings, key, target-val
        break $evaluate:process-word
      }
      rewind-stream curr-stream
      ### if curr-stream is a known function name, call it appropriately
      {
        var callee-h: (handle function)
        var callee-ah/eax: (addr handle function) <- address callee-h
        find-function functions, curr-stream, callee-ah
        var callee/eax: (addr function) <- lookup *callee-ah
        compare callee, 0
        break-if-=
        perform-call callee, out, functions
        break $evaluate:process-word
      }
      ### if it's a name, push its value
      {
        compare bindings, 0
        break-if-=
        var tmp: (handle array byte)
        var curr-string-ah/edx: (addr handle array byte) <- address tmp
        stream-to-array curr-stream, curr-string-ah  # unfortunate leak
        var curr-string/eax: (addr array byte) <- lookup *curr-string-ah
        var val-storage: (handle value)
        var val-ah/edi: (addr handle value) <- address val-storage
        lookup-binding bindings, curr-string, val-ah
        var val/eax: (addr value) <- lookup *val-ah
        compare val, 0
        break-if-=
        push-value-stack out, val
        break $evaluate:process-word
      }
      ### if the word starts with a quote and ends with a quote, turn it into a string
      {
        var start/eax: byte <- stream-first curr-stream
        compare start, 0x22/double-quote
        break-if-!=
        var end/eax: byte <- stream-final curr-stream
        compare end, 0x22/double-quote
        break-if-!=
        var h: (handle array byte)
        var s/eax: (addr handle array byte) <- address h
        unquote-stream-to-array curr-stream, s  # leak
        push-string-to-value-stack out, *s
        break $evaluate:process-word
      }
      ### if the word starts with a '[' and ends with a ']', turn it into an array
      {
        var start/eax: byte <- stream-first curr-stream
        compare start, 0x5b/[
        break-if-!=
        var end/eax: byte <- stream-final curr-stream
        compare end, 0x5d/]
        break-if-!=
        # wastefully create a new input string to strip quotes
        var h: (handle array value)
        var input-ah/eax: (addr handle array byte) <- address h
        unquote-stream-to-array curr-stream, input-ah  # leak
        # wastefully parse input into int-array
        # TODO: support parsing arrays of other types
        var input/eax: (addr array byte) <- lookup *input-ah
        var h2: (handle array int)
        var int-array-ah/esi: (addr handle array int) <- address h2
        parse-array-of-decimal-ints input, int-array-ah  # leak
        var _int-array/eax: (addr array int) <- lookup *int-array-ah
        var int-array/esi: (addr array int) <- copy _int-array
        var len/ebx: int <- length int-array
        # push value-array of same size as int-array
        var h3: (handle array value)
        var value-array-ah/eax: (addr handle array value) <- address h3
        populate value-array-ah, len
        push-array-to-value-stack out, *value-array-ah
        # copy int-array into value-array
        var _value-array/eax: (addr array value) <- lookup *value-array-ah
        var value-array/edi: (addr array value) <- copy _value-array
        var i/eax: int <- copy 0
        {
          compare i, len
          break-if->=
          var src-addr/ecx: (addr int) <- index int-array, i
          var src/ecx: int <- copy *src-addr
          var src-f/xmm0: float <- convert src
          var dest-offset/edx: (offset value) <- compute-offset value-array, i
          var dest-val/edx: (addr value) <- index value-array, dest-offset
          var dest/edx: (addr float) <- get dest-val, number-data
          copy-to *dest, src-f
          i <- increment
          loop
        }
        break $evaluate:process-word
      }
      ### otherwise assume it's a literal number and push it
      {
        var n/eax: int <- parse-decimal-int-from-stream curr-stream
        var n-f/xmm0: float <- convert n
        push-number-to-value-stack out, n-f
      }
    }
    # termination check
    compare curr, end
    break-if-=
    # update
    var next-word-ah/edx: (addr handle word) <- get curr, next
    curr <- lookup *next-word-ah
    #
    loop
  }
  # process next line if necessary
  var line/eax: (addr line) <- copy scratch
  var next-line-ah/eax: (addr handle line) <- get line, next
  var next-line/eax: (addr line) <- lookup *next-line-ah
  compare next-line, 0
  break-if-=
  evaluate functions, bindings, next-line, end, out
}

fn test-evaluate {
  var line-storage: line
  var line/esi: (addr line) <- address line-storage
  var first-word-ah/eax: (addr handle word) <- get line-storage, data
  allocate-word-with first-word-ah, "3"
  append-word-with *first-word-ah, "=a"
  var next-line-ah/eax: (addr handle line) <- get line-storage, next
  allocate next-line-ah
  var next-line/eax: (addr line) <- lookup *next-line-ah
  var first-word-ah/eax: (addr handle word) <- get next-line, data
  allocate-word-with first-word-ah, "a"
  var functions-storage: (handle function)
  var functions/ecx: (addr handle function) <- address functions-storage
  var table-storage: table
  var table/ebx: (addr table) <- address table-storage
  initialize-table table, 0x10
  var stack-storage: value-stack
  var stack/edi: (addr value-stack) <- address stack-storage
  initialize-value-stack stack, 0x10
  evaluate functions, table, line, 0, stack
  var x-f/xmm0: float <- pop-number-from-value-stack stack
  var x/eax: int <- convert x-f
  check-ints-equal x, 3, "F - test-evaluate"
}

fn find-function first: (addr handle function), name: (addr stream byte), out: (addr handle function) {
  var curr/esi: (addr handle function) <- copy first
  $find-function:loop: {
    var _f/eax: (addr function) <- lookup *curr
    var f/ecx: (addr function) <- copy _f
    compare f, 0
    break-if-=
    var curr-name-ah/eax: (addr handle array byte) <- get f, name
    var curr-name/eax: (addr array byte) <- lookup *curr-name-ah
    var done?/eax: boolean <- stream-data-equal? name, curr-name
    compare done?, 0/false
    {
      break-if-=
      copy-handle *curr, out
      break $find-function:loop
    }
    curr <- get f, next
    loop
  }
}

fn perform-call _callee: (addr function), caller-stack: (addr value-stack), functions: (addr handle function) {
  var callee/ecx: (addr function) <- copy _callee
  # create bindings for args
  var table-storage: table
  var table/esi: (addr table) <- address table-storage
  initialize-table table, 0x10
  bind-args callee, caller-stack, table
  # obtain body
  var body-ah/eax: (addr handle line) <- get callee, body
  var body/eax: (addr line) <- lookup *body-ah
  # perform call
  var stack-storage: value-stack
  var stack/edi: (addr value-stack) <- address stack-storage
  initialize-value-stack stack, 0x10
#?   print-string-to-real-screen "about to enter recursive eval\n"
  evaluate functions, table, body, 0, stack
#?   print-string-to-real-screen "exited recursive eval\n"
  # pop target-val from out
  var top-addr/ecx: (addr int) <- get stack, top
  compare *top-addr, 0
  break-if-<=
  var data-ah/eax: (addr handle array value) <- get stack, data
  var data/eax: (addr array value) <- lookup *data-ah
  var top/edx: int <- copy *top-addr
  top <- decrement
  var dest-offset/edx: (offset value) <- compute-offset data, top
  var target-val/edx: (addr value) <- index data, dest-offset
  # stitch target-val into caller-stack
  push-value-stack caller-stack, target-val
}

# pop args from the caller-stack and bind them to successive args
# implies: function args are stored in reverse order
fn bind-args _callee: (addr function), _caller-stack: (addr value-stack), table: (addr table) {
  var callee/ecx: (addr function) <- copy _callee
  var curr-arg-ah/eax: (addr handle word) <- get callee, args
  var curr-arg/eax: (addr word) <- lookup *curr-arg-ah
  #
  var curr-key-storage: (handle array byte)
  var curr-key/edx: (addr handle array byte) <- address curr-key-storage
  {
    compare curr-arg, 0
    break-if-=
    # create binding
    word-to-string curr-arg, curr-key
    {
      # pop target-val from caller-stack
      var caller-stack/esi: (addr value-stack) <- copy _caller-stack
      var top-addr/ecx: (addr int) <- get caller-stack, top
      compare *top-addr, 0
      break-if-<=
      decrement *top-addr
      var data-ah/eax: (addr handle array value) <- get caller-stack, data
      var data/eax: (addr array value) <- lookup *data-ah
      var top/ebx: int <- copy *top-addr
      var dest-offset/ebx: (offset value) <- compute-offset data, top
      var target-val/ebx: (addr value) <- index data, dest-offset
      # create binding from curr-key to target-val
      bind-in-table table, curr-key, target-val
    }
    #
    var next-arg-ah/edx: (addr handle word) <- get curr-arg, next
    curr-arg <- lookup *next-arg-ah
    loop
  }
}

# Copy of 'simplify' that just tracks the maximum stack depth needed
# Doesn't actually need to simulate the stack, since every word has a predictable effect.
fn max-stack-depth first-word: (addr word), final-word: (addr word) -> _/edi: int {
  var curr-word/eax: (addr word) <- copy first-word
  var curr-depth/ecx: int <- copy 0
  var result/edi: int <- copy 0
  $max-stack-depth:loop: {
    $max-stack-depth:process-word: {
      # handle operators
      {
        var add?/eax: boolean <- word-equal? curr-word, "+"
        compare add?, 0
        break-if-=
        curr-depth <- decrement
        break $max-stack-depth:process-word
      }
      {
        var sub?/eax: boolean <- word-equal? curr-word, "-"
        compare sub?, 0
        break-if-=
        curr-depth <- decrement
        break $max-stack-depth:process-word
      }
      {
        var mul?/eax: boolean <- word-equal? curr-word, "*"
        compare mul?, 0
        break-if-=
        curr-depth <- decrement
        break $max-stack-depth:process-word
      }
      # otherwise it's an int (do we need error-checking?)
      curr-depth <- increment
      # update max depth if necessary
      {
        compare curr-depth, result
        break-if-<=
        result <- copy curr-depth
      }
    }
    # if curr-word == final-word break
    compare curr-word, final-word
    break-if-=
    # curr-word = curr-word->next
    var next-word-ah/edx: (addr handle word) <- get curr-word, next
    curr-word <- lookup *next-word-ah
    #
    loop
  }
  return result
}