about summary refs log tree commit diff stats
path: root/shell/sandbox.mu
blob: 286ce6e893c5ed051e62c0e821a7c084e7d11cfa (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
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
type sandbox {
  data: (handle gap-buffer)
  value: (handle stream byte)
  trace: (handle trace)
  screen-var: (handle cell)
  keyboard-var: (handle cell)
  cursor-in-data?: boolean
  cursor-in-trace?: boolean
  cursor-in-keyboard?: boolean
}

fn initialize-sandbox _self: (addr sandbox), fake-screen-and-keyboard?: boolean {
  var self/esi: (addr sandbox) <- copy _self
  var data-ah/eax: (addr handle gap-buffer) <- get self, data
  allocate data-ah
  var data/eax: (addr gap-buffer) <- lookup *data-ah
  initialize-gap-buffer data, 0x1000/4KB
  #
  var value-ah/eax: (addr handle stream byte) <- get self, value
  populate-stream value-ah, 0x1000/4KB
  #
  {
    compare fake-screen-and-keyboard?, 0/false
    break-if-=
    var screen-ah/eax: (addr handle cell) <- get self, screen-var
    new-fake-screen screen-ah, 8/width, 3/height, 1/enable-pixel-graphics
    var keyboard-ah/eax: (addr handle cell) <- get self, keyboard-var
    new-fake-keyboard keyboard-ah, 0x10/keyboard-capacity
  }
  #
  var trace-ah/eax: (addr handle trace) <- get self, trace
  allocate trace-ah
  var trace/eax: (addr trace) <- lookup *trace-ah
  initialize-trace trace, 4/max-depth, 0x8000/lines, 0x80/visible
  var cursor-in-data?/eax: (addr boolean) <- get self, cursor-in-data?
  copy-to *cursor-in-data?, 1/true
}

## some helpers for tests

fn initialize-sandbox-with _self: (addr sandbox), s: (addr array byte) {
  var self/esi: (addr sandbox) <- copy _self
  var data-ah/eax: (addr handle gap-buffer) <- get self, data
  allocate data-ah
  var data/eax: (addr gap-buffer) <- lookup *data-ah
  initialize-gap-buffer-with data, s
  var value-ah/eax: (addr handle stream byte) <- get self, value
  populate-stream value-ah, 0x1000/4KB
  var trace-ah/eax: (addr handle trace) <- get self, trace
  allocate trace-ah
  var trace/eax: (addr trace) <- lookup *trace-ah
  initialize-trace trace, 3/max-depth, 0x8000/lines, 0x80/visible
  var cursor-in-data?/eax: (addr boolean) <- get self, cursor-in-data?
  copy-to *cursor-in-data?, 1/true
}

fn allocate-sandbox-with _out: (addr handle sandbox), s: (addr array byte) {
  var out/eax: (addr handle sandbox) <- copy _out
  allocate out
  var out-addr/eax: (addr sandbox) <- lookup *out
  initialize-sandbox-with out-addr, s
}

fn write-sandbox out: (addr stream byte), _self: (addr sandbox) {
  var self/eax: (addr sandbox) <- copy _self
  var data-ah/eax: (addr handle gap-buffer) <- get self, data
  var data/eax: (addr gap-buffer) <- lookup *data-ah
  {
    var len/eax: int <- gap-buffer-length data
    compare len, 0
    break-if-!=
    return
  }
  write out, "  (sandbox . "
  append-gap-buffer data, out
  write out, ")\n"
}

##

fn render-sandbox screen: (addr screen), _self: (addr sandbox), xmin: int, ymin: int, xmax: int, ymax: int {
  clear-rect screen, xmin, ymin, xmax, ymax, 0xc5/bg=blue-bg
  add-to xmin, 1/padding-left
  add-to ymin, 1/padding-top
  subtract-from xmax, 1/padding-right
  var self/esi: (addr sandbox) <- copy _self
  # data
  var data-ah/eax: (addr handle gap-buffer) <- get self, data
  var _data/eax: (addr gap-buffer) <- lookup *data-ah
  var data/edx: (addr gap-buffer) <- copy _data
  var x/eax: int <- copy xmin
  var y/ecx: int <- copy ymin
  y <- maybe-render-empty-screen screen, self, xmin, y
  y <- maybe-render-keyboard screen, self, xmin, y
  var cursor-in-sandbox?/ebx: (addr boolean) <- get self, cursor-in-data?
  x, y <- render-gap-buffer-wrapping-right-then-down screen, data, x, y, xmax, ymax, *cursor-in-sandbox?, 7/fg, 0xc5/bg=blue-bg
  y <- increment
  # trace
  var trace-ah/eax: (addr handle trace) <- get self, trace
  var _trace/eax: (addr trace) <- lookup *trace-ah
  var trace/edx: (addr trace) <- copy _trace
  var cursor-in-trace?/eax: (addr boolean) <- get self, cursor-in-trace?
  y <- render-trace screen, trace, xmin, y, xmax, ymax, *cursor-in-trace?
  # value
  $render-sandbox:value: {
    compare y, ymax
    break-if->=
    var value-ah/eax: (addr handle stream byte) <- get self, value
    var _value/eax: (addr stream byte) <- lookup *value-ah
    var value/esi: (addr stream byte) <- copy _value
    rewind-stream value
    var done?/eax: boolean <- stream-empty? value
    compare done?, 0/false
    break-if-!=
    var x/eax: int <- copy 0
    x, y <- draw-text-wrapping-right-then-down screen, "=> ", xmin, y, xmax, ymax, xmin, y, 7/fg, 0xc5/bg=blue-bg
    var x2/edx: int <- copy x
    var dummy/eax: int <- draw-stream-rightward screen, value, x2, xmax, y, 7/fg=grey, 0xc5/bg=blue-bg
  }
  y <- add 2  # padding
  y <- maybe-render-screen screen, self, xmin, y
  # render menu
  var cursor-in-data?/eax: (addr boolean) <- get self, cursor-in-data?
  compare *cursor-in-data?, 0/false
  {
    break-if-=
    render-sandbox-menu screen, self
    return
  }
  var cursor-in-trace?/eax: (addr boolean) <- get self, cursor-in-trace?
  compare *cursor-in-trace?, 0/false
  {
    break-if-=
    render-trace-menu screen
    return
  }
  var cursor-in-keyboard?/eax: (addr boolean) <- get self, cursor-in-keyboard?
  compare *cursor-in-keyboard?, 0/false
  {
    break-if-=
    render-keyboard-menu screen
    return
  }
}

fn clear-sandbox-output screen: (addr screen), _self: (addr sandbox), xmin: int, ymin: int, xmax: int, ymax: int {
  # render just enough of the sandbox to figure out what to erase
  var self/esi: (addr sandbox) <- copy _self
  var data-ah/eax: (addr handle gap-buffer) <- get self, data
  var _data/eax: (addr gap-buffer) <- lookup *data-ah
  var data/edx: (addr gap-buffer) <- copy _data
  var x/eax: int <- copy xmin
  var y/ecx: int <- copy ymin
  y <- maybe-render-empty-screen screen, self, xmin, y
  y <- maybe-render-keyboard screen, self, xmin, y
  var cursor-in-sandbox?/ebx: (addr boolean) <- get self, cursor-in-data?
  x, y <- render-gap-buffer-wrapping-right-then-down screen, data, x, y, xmax, ymax, *cursor-in-sandbox?, 3/fg, 0xc5/bg=blue-bg
  y <- increment
  clear-rect screen, xmin, y, xmax, ymax, 0xc5/bg=blue-bg
}

fn maybe-render-empty-screen screen: (addr screen), _self: (addr sandbox), xmin: int, ymin: int -> _/ecx: int {
  var self/esi: (addr sandbox) <- copy _self
  var screen-obj-cell-ah/eax: (addr handle cell) <- get self, screen-var
  var screen-obj-cell/eax: (addr cell) <- lookup *screen-obj-cell-ah
  compare screen-obj-cell, 0
  {
    break-if-!=
    return ymin
  }
  var screen-obj-cell-type/ecx: (addr int) <- get screen-obj-cell, type
  compare *screen-obj-cell-type, 5/screen
  {
    break-if-=
    return ymin  # silently give up on rendering the screen
  }
  var y/ecx: int <- copy ymin
  var screen-obj-ah/eax: (addr handle screen) <- get screen-obj-cell, screen-data
  var _screen-obj/eax: (addr screen) <- lookup *screen-obj-ah
  var screen-obj/edx: (addr screen) <- copy _screen-obj
  var x/eax: int <- draw-text-rightward screen, "screen:   ", xmin, 0x99/xmax, y, 0x17/fg, 0xc5/bg=blue-bg
  y <- render-empty-screen screen, screen-obj, x, y
  return y
}

fn maybe-render-screen screen: (addr screen), _self: (addr sandbox), xmin: int, ymin: int -> _/ecx: int {
  var self/esi: (addr sandbox) <- copy _self
  var screen-obj-cell-ah/eax: (addr handle cell) <- get self, screen-var
  var screen-obj-cell/eax: (addr cell) <- lookup *screen-obj-cell-ah
  compare screen-obj-cell, 0
  {
    break-if-!=
    return ymin
  }
  var screen-obj-cell-type/ecx: (addr int) <- get screen-obj-cell, type
  compare *screen-obj-cell-type, 5/screen
  {
    break-if-=
    return ymin  # silently give up on rendering the screen
  }
  var screen-obj-ah/eax: (addr handle screen) <- get screen-obj-cell, screen-data
  var _screen-obj/eax: (addr screen) <- lookup *screen-obj-ah
  var screen-obj/edx: (addr screen) <- copy _screen-obj
  {
    var screen-empty?/eax: boolean <- fake-screen-empty? screen-obj
    compare screen-empty?, 0/false
    break-if-=
    return ymin
  }
  var x/eax: int <- draw-text-rightward screen, "screen:   ", xmin, 0x99/xmax, ymin, 0x17/fg, 0xc5/bg=blue-bg
  var y/ecx: int <- copy ymin
  y <- render-screen screen, screen-obj, x, y
  return y
}

fn render-empty-screen screen: (addr screen), _target-screen: (addr screen), xmin: int, ymin: int -> _/ecx: int {
  var target-screen/esi: (addr screen) <- copy _target-screen
  var screen-y/edi: int <- copy ymin
  # screen
  var height/edx: (addr int) <- get target-screen, height
  var y/ecx: int <- copy 0
  {
    compare y, *height
    break-if->=
    set-cursor-position screen, xmin, screen-y
    var width/edx: (addr int) <- get target-screen, width
    var x/ebx: int <- copy 0
    {
      compare x, *width
      break-if->=
      draw-code-point-at-cursor screen, 0x20/space, 0x18/fg, 0/bg
      move-cursor-right screen
      x <- increment
      loop
    }
    y <- increment
    screen-y <- increment
    loop
  }
  return screen-y
}

fn render-screen screen: (addr screen), _target-screen: (addr screen), xmin: int, ymin: int -> _/ecx: int {
  var target-screen/esi: (addr screen) <- copy _target-screen
  var screen-y/edi: int <- copy ymin
  # text data
  {
    var height/edx: (addr int) <- get target-screen, height
    var y/ecx: int <- copy 0
    {
      compare y, *height
      break-if->=
      set-cursor-position screen, xmin, screen-y
      var width/edx: (addr int) <- get target-screen, width
      var x/ebx: int <- copy 0
      {
        compare x, *width
        break-if->=
        print-screen-cell-of-fake-screen screen, target-screen, x, y
        move-cursor-right screen
        x <- increment
        loop
      }
      y <- increment
      screen-y <- increment
      loop
    }
  }
  # pixel data
  {
    # screen top left pixels x y width height
    var tmp/eax: int <- copy xmin
    tmp <- shift-left 3/log2-font-width
    var left: int
    copy-to left, tmp
    tmp <- copy ymin
    tmp <- shift-left 4/log2-font-height
    var top: int
    copy-to top, tmp
    var pixels-ah/eax: (addr handle array byte) <- get target-screen, pixels
    var _pixels/eax: (addr array byte) <- lookup *pixels-ah
    var pixels/edi: (addr array byte) <- copy _pixels
    compare pixels, 0
    break-if-=
    var y/ebx: int <- copy 0
    var height-addr/edx: (addr int) <- get target-screen, height
    var height/edx: int <- copy *height-addr
    height <- shift-left 4/log2-font-height
    {
      compare y, height
      break-if->=
      var width-addr/edx: (addr int) <- get target-screen, width
      var width/edx: int <- copy *width-addr
      width <- shift-left 3/log2-font-width
      var x/eax: int <- copy 0
      {
        compare x, width
        break-if->=
        {
          var idx/ecx: int <- pixel-index target-screen, x, y
          var color-addr/ecx: (addr byte) <- index pixels, idx
          var color/ecx: byte <- copy-byte *color-addr
          var color2/ecx: int <- copy color
          compare color2, 0
          break-if-=
          var x2/eax: int <- copy x
          x2 <- add left
          var y2/ebx: int <- copy y
          y2 <- add top
          pixel screen, x2, y2, color2
        }
        x <- increment
        loop
      }
      y <- increment
      loop
    }
  }
  return screen-y
}

fn has-keyboard? _self: (addr sandbox) -> _/eax: boolean {
  var self/esi: (addr sandbox) <- copy _self
  var keyboard-obj-cell-ah/eax: (addr handle cell) <- get self, keyboard-var
  var keyboard-obj-cell/eax: (addr cell) <- lookup *keyboard-obj-cell-ah
  compare keyboard-obj-cell, 0
  {
    break-if-!=
    return 0/false
  }
  var keyboard-obj-cell-type/ecx: (addr int) <- get keyboard-obj-cell, type
  compare *keyboard-obj-cell-type, 6/keyboard
  {
    break-if-=
    return 0/false
  }
  var keyboard-obj-ah/eax: (addr handle gap-buffer) <- get keyboard-obj-cell, keyboard-data
  var _keyboard-obj/eax: (addr gap-buffer) <- lookup *keyboard-obj-ah
  var keyboard-obj/edx: (addr gap-buffer) <- copy _keyboard-obj
  compare keyboard-obj, 0
  {
    break-if-!=
    return 0/false
  }
  return 1/true
}

fn maybe-render-keyboard screen: (addr screen), _self: (addr sandbox), xmin: int, ymin: int -> _/ecx: int {
  var self/esi: (addr sandbox) <- copy _self
  var keyboard-obj-cell-ah/eax: (addr handle cell) <- get self, keyboard-var
  var keyboard-obj-cell/eax: (addr cell) <- lookup *keyboard-obj-cell-ah
  compare keyboard-obj-cell, 0
  {
    break-if-!=
    return ymin
  }
  var keyboard-obj-cell-type/ecx: (addr int) <- get keyboard-obj-cell, type
  compare *keyboard-obj-cell-type, 6/keyboard
  {
    break-if-=
    return ymin  # silently give up on rendering the keyboard
  }
  var keyboard-obj-ah/eax: (addr handle gap-buffer) <- get keyboard-obj-cell, keyboard-data
  var _keyboard-obj/eax: (addr gap-buffer) <- lookup *keyboard-obj-ah
  var keyboard-obj/edx: (addr gap-buffer) <- copy _keyboard-obj
  var y/ecx: int <- copy ymin
  y <- increment  # padding
  var x/eax: int <- draw-text-rightward screen, "keyboard: ", xmin, 0x99/xmax, y, 0x17/fg, 0xc5/bg=blue-bg
  var cursor-in-keyboard?/esi: (addr boolean) <- get self, cursor-in-keyboard?
  y <- render-keyboard screen, keyboard-obj, x, y, *cursor-in-keyboard?
  y <- increment  # padding
  return y
}

fn render-keyboard screen: (addr screen), _keyboard: (addr gap-buffer), xmin: int, ymin: int, render-cursor?: boolean -> _/ecx: int {
  var keyboard/esi: (addr gap-buffer) <- copy _keyboard
  var width/edx: int <- copy 0x10/keyboard-capacity
  var y/edi: int <- copy ymin
  # keyboard
  var x/eax: int <- copy xmin
  var xmax/ecx: int <- copy x
  xmax <- add 0x10
  var ymax/edx: int <- copy ymin
  ymax <- add 1
  clear-rect screen, x, y, xmax, ymax, 0/bg
  x <- render-gap-buffer screen, keyboard, x, y, render-cursor?, 3/fg, 0/bg
  y <- increment
  return y
}

fn print-screen-cell-of-fake-screen screen: (addr screen), _target: (addr screen), x: int, y: int {
  var target/ecx: (addr screen) <- copy _target
  var data-ah/eax: (addr handle array screen-cell) <- get target, data
  var data/eax: (addr array screen-cell) <- lookup *data-ah
  var index/ecx: int <- screen-cell-index target, x, y
  var offset/ecx: (offset screen-cell) <- compute-offset data, index
  var src-cell/esi: (addr screen-cell) <- index data, offset
  var src-grapheme/eax: (addr grapheme) <- get src-cell, data
  var src-color/ecx: (addr int) <- get src-cell, color
  var src-background-color/edx: (addr int) <- get src-cell, background-color
  draw-grapheme-at-cursor screen, *src-grapheme, *src-color, *src-background-color
}

fn render-sandbox-menu screen: (addr screen), _self: (addr sandbox) {
  var _width/eax: int <- copy 0
  var height/ecx: int <- copy 0
  _width, height <- screen-size screen
  var width/edx: int <- copy _width
  var y/ecx: int <- copy height
  y <- decrement
  var height/ebx: int <- copy y
  height <- increment
  clear-rect screen, 0/x, y, width, height, 0xc5/bg=blue-bg
  set-cursor-position screen, 0/x, y
  draw-text-rightward-from-cursor screen, " ^r ", width, 0/fg, 0x5c/bg=black
  draw-text-rightward-from-cursor screen, " run main  ", width, 7/fg, 0xc5/bg=blue-bg
  $render-sandbox-menu:render-ctrl-m: {
    var self/eax: (addr sandbox) <- copy _self
    var has-trace?/eax: boolean <- has-trace? self
    compare has-trace?, 0/false
    {
      break-if-=
      draw-text-rightward-from-cursor screen, " ^m ", width, 0/fg, 0x38/bg=trace
      draw-text-rightward-from-cursor screen, " to trace  ", width, 7/fg, 0xc5/bg=blue-bg
      break $render-sandbox-menu:render-ctrl-m
    }
    draw-text-rightward-from-cursor screen, " ^m ", width, 0/fg, 3/bg=keyboard
    draw-text-rightward-from-cursor screen, " to keyboard  ", width, 7/fg, 0xc5/bg=blue-bg
  }
  draw-text-rightward-from-cursor screen, " ^s ", width, 0/fg, 0x5c/bg=black
  draw-text-rightward-from-cursor screen, " run sandbox  ", width, 7/fg, 0xc5/bg=blue-bg
  draw-text-rightward-from-cursor screen, " ^a ", width, 0/fg, 0x5c/bg=black
  draw-text-rightward-from-cursor screen, " <<  ", width, 7/fg, 0xc5/bg=blue-bg
  draw-text-rightward-from-cursor screen, " ^b ", width, 0/fg, 0x5c/bg=black
  draw-text-rightward-from-cursor screen, " <word  ", width, 7/fg, 0xc5/bg=blue-bg
  draw-text-rightward-from-cursor screen, " ^f ", width, 0/fg, 0x5c/bg=black
  draw-text-rightward-from-cursor screen, " word>  ", width, 7/fg, 0xc5/bg=blue-bg
  draw-text-rightward-from-cursor screen, " ^e ", width, 0/fg, 0x5c/bg=black
  draw-text-rightward-from-cursor screen, " >>  ", width, 7/fg, 0xc5/bg=blue-bg
}

fn render-keyboard-menu screen: (addr screen) {
  var width/eax: int <- copy 0
  var height/ecx: int <- copy 0
  width, height <- screen-size screen
  var y/ecx: int <- copy height
  y <- decrement
  var height/edx: int <- copy y
  height <- increment
  clear-rect screen, 0/x, y, width, height, 0xc5/bg=blue-bg
  set-cursor-position screen, 0/x, y
  draw-text-rightward-from-cursor screen, " ^r ", width, 0/fg, 0x5c/bg=black
  draw-text-rightward-from-cursor screen, " run main  ", width, 7/fg, 0xc5/bg=blue-bg
  draw-text-rightward-from-cursor screen, " ^s ", width, 0/fg, 0x5c/bg=black
  draw-text-rightward-from-cursor screen, " run sandbox  ", width, 7/fg, 0xc5/bg=blue-bg
  draw-text-rightward-from-cursor screen, " ^m ", width, 0/fg, 7/bg
  draw-text-rightward-from-cursor screen, " to sandbox  ", width, 7/fg, 0xc5/bg=blue-bg
}

fn edit-sandbox _self: (addr sandbox), key: byte, globals: (addr global-table), data-disk: (addr disk), tweak-real-screen?: boolean {
  var self/esi: (addr sandbox) <- copy _self
  var g/edx: grapheme <- copy key
  # ctrl-s
  {
    compare g, 0x13/ctrl-s
    break-if-!=
    # if cursor is in trace, skip
    var cursor-in-trace?/eax: (addr boolean) <- get self, cursor-in-trace?
    compare *cursor-in-trace?, 0/false
    break-if-!=
    # minor gotcha here: any bindings created later in this iteration won't be
    # persisted until the next call to ctrl-s.
    store-state data-disk, self, globals
    #
    run-sandbox self, globals, tweak-real-screen?
    return
  }
  # ctrl-m
  {
    compare g, 0xd/ctrl-m
    break-if-!=
    # if cursor in data, switch to trace or fall through to keyboard
    {
      var cursor-in-data?/eax: (addr boolean) <- get self, cursor-in-data?
      compare *cursor-in-data?, 0/false
      break-if-=
      var has-trace?/eax: boolean <- has-trace? self
      compare has-trace?, 0/false
      {
        break-if-=
        var cursor-in-data?/eax: (addr boolean) <- get self, cursor-in-data?
        copy-to *cursor-in-data?, 0/false
        var cursor-in-trace?/eax: (addr boolean) <- get self, cursor-in-trace?
        copy-to *cursor-in-trace?, 1/false
        return
      }
      var has-keyboard?/eax: boolean <- has-keyboard? self
      compare has-keyboard?, 0/false
      {
        break-if-=
        var cursor-in-data?/eax: (addr boolean) <- get self, cursor-in-data?
        copy-to *cursor-in-data?, 0/false
        var cursor-in-keyboard?/eax: (addr boolean) <- get self, cursor-in-keyboard?
        copy-to *cursor-in-keyboard?, 1/false
        return
      }
      return
    }
    # if cursor in trace, switch to keyboard or fall through to data
    {
      var cursor-in-trace?/eax: (addr boolean) <- get self, cursor-in-trace?
      compare *cursor-in-trace?, 0/false
      break-if-=
      copy-to *cursor-in-trace?, 0/false
      var cursor-target/ecx: (addr boolean) <- get self, cursor-in-keyboard?
      var has-keyboard?/eax: boolean <- has-keyboard? self
      compare has-keyboard?, 0/false
      {
        break-if-!=
        cursor-target <- get self, cursor-in-data?
      }
      copy-to *cursor-target, 1/true
      return
    }
    # otherwise if cursor in keyboard, switch to data
    {
      var cursor-in-keyboard?/eax: (addr boolean) <- get self, cursor-in-keyboard?
      compare *cursor-in-keyboard?, 0/false
      break-if-=
      copy-to *cursor-in-keyboard?, 0/false
      var cursor-in-data?/eax: (addr boolean) <- get self, cursor-in-data?
      copy-to *cursor-in-data?, 1/true
      return
    }
    return
  }
  # if cursor in data, send key to data
  {
    var cursor-in-data?/eax: (addr boolean) <- get self, cursor-in-data?
    compare *cursor-in-data?, 0/false
    break-if-=
    var data-ah/eax: (addr handle gap-buffer) <- get self, data
    var data/eax: (addr gap-buffer) <- lookup *data-ah
    edit-gap-buffer data, g
    return
  }
  # if cursor in keyboard, send key to keyboard
  {
    var cursor-in-keyboard?/eax: (addr boolean) <- get self, cursor-in-keyboard?
    compare *cursor-in-keyboard?, 0/false
    break-if-=
    var keyboard-cell-ah/eax: (addr handle cell) <- get self, keyboard-var
    var keyboard-cell/eax: (addr cell) <- lookup *keyboard-cell-ah
    compare keyboard-cell, 0
    {
      break-if-!=
      return
    }
    var keyboard-cell-type/ecx: (addr int) <- get keyboard-cell, type
    compare *keyboard-cell-type, 6/keyboard
    {
      break-if-=
      return
    }
    var keyboard-ah/eax: (addr handle gap-buffer) <- get keyboard-cell, keyboard-data
    var keyboard/eax: (addr gap-buffer) <- lookup *keyboard-ah
    edit-gap-buffer keyboard, g
    return
  }
  # if cursor in trace, send key to trace
  {
    var cursor-in-trace?/eax: (addr boolean) <- get self, cursor-in-trace?
    compare *cursor-in-trace?, 0/false
    break-if-=
    var trace-ah/eax: (addr handle trace) <- get self, trace
    var trace/eax: (addr trace) <- lookup *trace-ah
    # if expanding the trace, first check if we need to run the sandbox again with a deeper trace
    {
      compare g, 0xa/newline
      break-if-!=
      {
        var need-rerun?/eax: boolean <- cursor-too-deep? trace
        compare need-rerun?, 0/false
      }
      break-if-=
#?       draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, "rerun", 7/fg 0/bg
      # save trace lines at various cached indices
      var save: trace-index-stash
      var save-addr/ecx: (addr trace-index-stash) <- address save
      save-indices trace, save-addr
      # rerun at higher depth
      var max-depth-addr/ecx: (addr int) <- get trace, max-depth
      increment *max-depth-addr
      run-sandbox self, globals, tweak-real-screen?
      # recompute cached indices
      recompute-all-visible-lines trace
      var save-addr/ecx: (addr trace-index-stash) <- address save
      restore-indices trace, save-addr
    }
    edit-trace trace, g
    return
  }
}

# hack: tweak-real-screen guards things there are no tests for
fn run-sandbox _self: (addr sandbox), globals: (addr global-table), tweak-real-screen?: boolean {
  var self/esi: (addr sandbox) <- copy _self
  var data-ah/ecx: (addr handle gap-buffer) <- get self, data
  var value-ah/eax: (addr handle stream byte) <- get self, value
  var _value/eax: (addr stream byte) <- lookup *value-ah
  var value/edx: (addr stream byte) <- copy _value
  var trace-ah/eax: (addr handle trace) <- get self, trace
  var _trace/eax: (addr trace) <- lookup *trace-ah
  var trace/ebx: (addr trace) <- copy _trace
  clear-trace trace
  {
    compare tweak-real-screen?, 0/false
    break-if-=
    clear-sandbox-output 0/screen, self, 0x56/sandbox-left-margin, 1/y, 0x80/screen-width, 0x2f/screen-height-without-menu
  }
  var screen-cell/eax: (addr handle cell) <- get self, screen-var
  clear-screen-cell screen-cell
  var keyboard-cell/esi: (addr handle cell) <- get self, keyboard-var
  rewind-keyboard-cell keyboard-cell  # don't clear keys from before
  {
    compare tweak-real-screen?, 0/false
    break-if-=
    set-cursor-position 0/screen, 0/x, 0/y  # for any debug prints during evaluation
  }
  run data-ah, value, globals, trace, screen-cell, keyboard-cell
}

fn run _in-ah: (addr handle gap-buffer), out: (addr stream byte), globals: (addr global-table), trace: (addr trace), screen-cell: (addr handle cell), keyboard-cell: (addr handle cell) {
  var in-ah/eax: (addr handle gap-buffer) <- copy _in-ah
  var in/eax: (addr gap-buffer) <- lookup *in-ah
  var read-result-h: (handle cell)
  var read-result-ah/esi: (addr handle cell) <- address read-result-h
  read-cell in, read-result-ah, trace
  var error?/eax: boolean <- has-errors? trace
  {
    compare error?, 0/false
    break-if-=
    return
  }
  macroexpand read-result-ah, globals, trace
  var error?/eax: boolean <- has-errors? trace
  {
    compare error?, 0/false
    break-if-=
    return
  }
  var nil-h: (handle cell)
  var nil-ah/eax: (addr handle cell) <- address nil-h
  allocate-pair nil-ah
  var eval-result-h: (handle cell)
  var eval-result-ah/edi: (addr handle cell) <- address eval-result-h
#?   set-cursor-position 0/screen, 0 0
#?   turn-on-debug-print
  debug-print "^", 4/fg, 0/bg
  evaluate read-result-ah, eval-result-ah, *nil-ah, globals, trace, screen-cell, keyboard-cell, 1/call-number
  debug-print "$", 4/fg, 0/bg
  var error?/eax: boolean <- has-errors? trace
  {
    compare error?, 0/false
    break-if-=
    return
  }
  # if there was no error and the read-result starts with "set" or "def", save
  # the gap buffer in the modified global, then create a new one for the next
  # command.
  maybe-stash-gap-buffer-to-global globals, read-result-ah, _in-ah
  clear-stream out
  print-cell eval-result-ah, out, trace
  mark-lines-dirty trace
}

fn read-evaluate-and-move-to-globals _in-ah: (addr handle gap-buffer), globals: (addr global-table), definition-name: (addr stream byte) {
  var in-ah/eax: (addr handle gap-buffer) <- copy _in-ah
  var in/eax: (addr gap-buffer) <- lookup *in-ah
  var read-result-h: (handle cell)
  var read-result-ah/esi: (addr handle cell) <- address read-result-h
  var trace-storage: trace
  var trace/edx: (addr trace) <- address trace-storage
  initialize-trace trace, 1/only-errors, 0x10/capacity, 0/visible
  read-cell in, read-result-ah, trace
  macroexpand read-result-ah, globals, trace
  var nil-storage: (handle cell)
  var nil-ah/eax: (addr handle cell) <- address nil-storage
  allocate-pair nil-ah
  var eval-result-storage: (handle cell)
  var eval-result/edi: (addr handle cell) <- address eval-result-storage
  debug-print "^", 4/fg, 0/bg
  evaluate read-result-ah, eval-result, *nil-ah, globals, trace, 0/no-screen-cell, 0/no-keyboard-cell, 1/call-number
  {
    var error?/eax: boolean <- has-errors? trace
    compare error?, 0/false
    break-if-=
    set-cursor-position 0/screen, 0x40/x, 0x18/y
    draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, "error when loading definition for ", 4/fg 0/bg
    rewind-stream definition-name
    draw-stream-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, definition-name, 3/fg 0/bg
    set-cursor-position 0/screen, 0x40/x, 0x19/y
    draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, "see trace in grey at top-left", 7/fg 0/bg
    dump-trace trace  # will print from 0, 0
    {
      loop
    }
  }
  debug-print "$", 4/fg, 0/bg
  move-gap-buffer-to-global globals, read-result-ah, _in-ah
}

fn test-run-integer {
  var sandbox-storage: sandbox
  var sandbox/esi: (addr sandbox) <- address sandbox-storage
  initialize-sandbox-with sandbox, "1"
  # eval
  edit-sandbox sandbox, 0x13/ctrl-s, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  # setup: screen
  var screen-on-stack: screen
  var screen/edi: (addr screen) <- address screen-on-stack
  initialize-screen screen, 0x80/width, 0x10/height, 0/no-pixel-graphics
  #
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  # skip one line of padding
  check-screen-row screen, 1/y, " 1    ", "F - test-run-integer/0"
  check-screen-row screen, 2/y, " ...  ", "F - test-run-integer/1"
  check-screen-row screen, 3/y, " => 1 ", "F - test-run-integer/2"
}

fn test-run-error-invalid-integer {
  var sandbox-storage: sandbox
  var sandbox/esi: (addr sandbox) <- address sandbox-storage
  initialize-sandbox-with sandbox, "1a"
  # eval
  edit-sandbox sandbox, 0x13/ctrl-s, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  # setup: screen
  var screen-on-stack: screen
  var screen/edi: (addr screen) <- address screen-on-stack
  initialize-screen screen, 0x80/width, 0x10/height, 0/no-pixel-graphics
  #
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  # skip one line of padding
  check-screen-row            screen,               1/y, " 1a             ", "F - test-run-error-invalid-integer/0"
  check-screen-row            screen,               2/y, " ...            ", "F - test-run-error-invalid-integer/1"
  check-screen-row-in-color   screen, 0xc/fg=error, 3/y, " invalid number ", "F - test-run-error-invalid-integer/2"
}

fn test-run-error-unknown-symbol {
  var sandbox-storage: sandbox
  var sandbox/esi: (addr sandbox) <- address sandbox-storage
  initialize-sandbox-with sandbox, "a"
  # eval
  edit-sandbox sandbox, 0x13/ctrl-s, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  # setup: screen
  var screen-on-stack: screen
  var screen/edi: (addr screen) <- address screen-on-stack
  initialize-screen screen, 0x80/width, 0x10/height, 0/no-pixel-graphics
  #
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  # skip one line of padding
  check-screen-row            screen,               1/y, " a                  ", "F - test-run-error-unknown-symbol/0"
  check-screen-row            screen,               2/y, " ...                ", "F - test-run-error-unknown-symbol/1"
  check-screen-row-in-color   screen, 0xc/fg=error, 3/y, " unbound symbol: a  ", "F - test-run-error-unknown-symbol/2"
}

fn test-run-with-spaces {
  var sandbox-storage: sandbox
  var sandbox/esi: (addr sandbox) <- address sandbox-storage
  initialize-sandbox-with sandbox, " 1 \n"
  # eval
  edit-sandbox sandbox, 0x13/ctrl-s, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  # setup: screen
  var screen-on-stack: screen
  var screen/edi: (addr screen) <- address screen-on-stack
  initialize-screen screen, 0x80/width, 0x10/height, 0/no-pixel-graphics
  #
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  # skip one line of padding
  check-screen-row screen, 1/y, "  1   ", "F - test-run-with-spaces/0"
  check-screen-row screen, 2/y, "      ", "F - test-run-with-spaces/1"
  check-screen-row screen, 3/y, " ...  ", "F - test-run-with-spaces/2"
  check-screen-row screen, 4/y, " => 1 ", "F - test-run-with-spaces/3"
}

fn test-run-quote {
  var sandbox-storage: sandbox
  var sandbox/esi: (addr sandbox) <- address sandbox-storage
  initialize-sandbox-with sandbox, "'a"
  # eval
  edit-sandbox sandbox, 0x13/ctrl-s, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  # setup: screen
  var screen-on-stack: screen
  var screen/edi: (addr screen) <- address screen-on-stack
  initialize-screen screen, 0x80/width, 0x10/height, 0/no-pixel-graphics
  #
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  # skip one line of padding
  check-screen-row screen, 1/y, " 'a   ", "F - test-run-quote/0"
  check-screen-row screen, 2/y, " ...  ", "F - test-run-quote/1"
  check-screen-row screen, 3/y, " => a ", "F - test-run-quote/2"
}

fn test-run-dotted-list {
  var sandbox-storage: sandbox
  var sandbox/esi: (addr sandbox) <- address sandbox-storage
  initialize-sandbox-with sandbox, "'(a . b)"
  # eval
  edit-sandbox sandbox, 0x13/ctrl-s, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  # setup: screen
  var screen-on-stack: screen
  var screen/edi: (addr screen) <- address screen-on-stack
  initialize-screen screen, 0x80/width, 0x10/height, 0/no-pixel-graphics
  #
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  # skip one line of padding
  check-screen-row screen, 1/y, " '(a . b)   ", "F - test-run-dotted-list/0"
  check-screen-row screen, 2/y, " ...        ", "F - test-run-dotted-list/1"
  check-screen-row screen, 3/y, " => (a . b) ", "F - test-run-dotted-list/2"
}

fn test-run-dot-and-list {
  var sandbox-storage: sandbox
  var sandbox/esi: (addr sandbox) <- address sandbox-storage
  initialize-sandbox-with sandbox, "'(a . (b))"
  # eval
  edit-sandbox sandbox, 0x13/ctrl-s, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  # setup: screen
  var screen-on-stack: screen
  var screen/edi: (addr screen) <- address screen-on-stack
  initialize-screen screen, 0x80/width, 0x10/height, 0/no-pixel-graphics
  #
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  # skip one line of padding
  check-screen-row screen, 1/y, " '(a . (b)) ", "F - test-run-dot-and-list/0"
  check-screen-row screen, 2/y, " ...        ", "F - test-run-dot-and-list/1"
  check-screen-row screen, 3/y, " => (a b)   ", "F - test-run-dot-and-list/2"
}

fn test-run-final-dot {
  var sandbox-storage: sandbox
  var sandbox/esi: (addr sandbox) <- address sandbox-storage
  initialize-sandbox-with sandbox, "'(a .)"
  # eval
  edit-sandbox sandbox, 0x13/ctrl-s, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  # setup: screen
  var screen-on-stack: screen
  var screen/edi: (addr screen) <- address screen-on-stack
  initialize-screen screen, 0x80/width, 0x10/height, 0/no-pixel-graphics
  #
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  # skip one line of padding
  check-screen-row screen, 1/y, " '(a .)               ", "F - test-run-final-dot/0"
  check-screen-row screen, 2/y, " ...                  ", "F - test-run-final-dot/1"
  check-screen-row screen, 3/y, " '. )' makes no sense ", "F - test-run-final-dot/2"
  # further errors may occur
}

fn test-run-double-dot {
  var sandbox-storage: sandbox
  var sandbox/esi: (addr sandbox) <- address sandbox-storage
  initialize-sandbox-with sandbox, "'(a . .)"
  # eval
  edit-sandbox sandbox, 0x13/ctrl-s, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  # setup: screen
  var screen-on-stack: screen
  var screen/edi: (addr screen) <- address screen-on-stack
  initialize-screen screen, 0x80/width, 0x10/height, 0/no-pixel-graphics
  #
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  # skip one line of padding
  check-screen-row screen, 1/y, " '(a . .)             ", "F - test-run-double-dot/0"
  check-screen-row screen, 2/y, " ...                  ", "F - test-run-double-dot/1"
  check-screen-row screen, 3/y, " '. .' makes no sense ", "F - test-run-double-dot/2"
  # further errors may occur
}

fn test-run-multiple-expressions-after-dot {
  var sandbox-storage: sandbox
  var sandbox/esi: (addr sandbox) <- address sandbox-storage
  initialize-sandbox-with sandbox, "'(a . b c)"
  # eval
  edit-sandbox sandbox, 0x13/ctrl-s, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  # setup: screen
  var screen-on-stack: screen
  var screen/edi: (addr screen) <- address screen-on-stack
  initialize-screen screen, 0x80/width, 0x10/height, 0/no-pixel-graphics
  #
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  # skip one line of padding
  check-screen-row screen, 1/y, " '(a . b c)                                           ", "F - test-run-multiple-expressions-after-dot/0"
  check-screen-row screen, 2/y, " ...                                                  ", "F - test-run-multiple-expressions-after-dot/1"
  check-screen-row screen, 3/y, " cannot have multiple expressions between '.' and ')' ", "F - test-run-multiple-expressions-after-dot/2"
  # further errors may occur
}

fn test-run-stream {
  var sandbox-storage: sandbox
  var sandbox/esi: (addr sandbox) <- address sandbox-storage
  initialize-sandbox-with sandbox, "[a b]"
  # eval
  edit-sandbox sandbox, 0x13/ctrl-s, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  # setup: screen
  var screen-on-stack: screen
  var screen/edi: (addr screen) <- address screen-on-stack
  initialize-screen screen, 0x80/width, 0x10/height, 0/no-pixel-graphics
  #
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  # skip one line of padding
  check-screen-row screen, 1/y, " [a b]    ", "F - test-run-stream/0"
  check-screen-row screen, 2/y, " ...      ", "F - test-run-stream/1"
  check-screen-row screen, 3/y, " => [a b] ", "F - test-run-stream/2"
}

fn test-run-move-cursor-into-trace {
  var sandbox-storage: sandbox
  var sandbox/esi: (addr sandbox) <- address sandbox-storage
  initialize-sandbox-with sandbox, "12"
  # eval
  edit-sandbox sandbox, 0x13/ctrl-s, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  # setup: screen
  var screen-on-stack: screen
  var screen/edi: (addr screen) <- address screen-on-stack
  initialize-screen screen, 0x80/width, 0x10/height, 0/no-pixel-graphics
  #
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  # skip one line of padding
  check-screen-row screen,                                  1/y, " 12    ", "F - test-run-move-cursor-into-trace/pre-0"
  check-background-color-in-screen-row screen, 7/bg=cursor, 1/y, "   |   ", "F - test-run-move-cursor-into-trace/pre-0/cursor"
  check-screen-row screen,                                  2/y, " ...   ", "F - test-run-move-cursor-into-trace/pre-1"
  check-background-color-in-screen-row screen, 7/bg=cursor, 2/y, "       ", "F - test-run-move-cursor-into-trace/pre-1/cursor"
  check-screen-row screen,                                  3/y, " => 12 ", "F - test-run-move-cursor-into-trace/pre-2"
  check-background-color-in-screen-row screen, 7/bg=cursor, 3/y, "       ", "F - test-run-move-cursor-into-trace/pre-2/cursor"
  # move cursor into trace
  edit-sandbox sandbox, 0xd/ctrl-m, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  #
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  # skip one line of padding
  check-screen-row screen,                                  1/y, " 12    ", "F - test-run-move-cursor-into-trace/trace-0"
  check-background-color-in-screen-row screen, 7/bg=cursor, 1/y, "       ", "F - test-run-move-cursor-into-trace/trace-0/cursor"
  check-screen-row screen,                                  2/y, " ...   ", "F - test-run-move-cursor-into-trace/trace-1"
  check-background-color-in-screen-row screen, 7/bg=cursor, 2/y, " |||   ", "F - test-run-move-cursor-into-trace/trace-1/cursor"
  check-screen-row screen,                                  3/y, " => 12 ", "F - test-run-move-cursor-into-trace/trace-2"
  check-background-color-in-screen-row screen, 7/bg=cursor, 3/y, "       ", "F - test-run-move-cursor-into-trace/trace-2/cursor"
  # move cursor into input
  edit-sandbox sandbox, 0xd/ctrl-m, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  #
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  # skip one line of padding
  check-screen-row screen,                                  1/y, " 12    ", "F - test-run-move-cursor-into-trace/input-0"
  check-background-color-in-screen-row screen, 7/bg=cursor, 1/y, "   |   ", "F - test-run-move-cursor-into-trace/input-0/cursor"
  check-screen-row screen,                                  2/y, " ...   ", "F - test-run-move-cursor-into-trace/input-1"
  check-background-color-in-screen-row screen, 7/bg=cursor, 2/y, "       ", "F - test-run-move-cursor-into-trace/input-1/cursor"
  check-screen-row screen,                                  3/y, " => 12 ", "F - test-run-move-cursor-into-trace/input-2"
  check-background-color-in-screen-row screen, 7/bg=cursor, 3/y, "       ", "F - test-run-move-cursor-into-trace/input-2/cursor"
}

fn has-trace? _self: (addr sandbox) -> _/eax: boolean {
  var self/esi: (addr sandbox) <- copy _self
  var trace-ah/eax: (addr handle trace) <- get self, trace
  var _trace/eax: (addr trace) <- lookup *trace-ah
  var trace/edx: (addr trace) <- copy _trace
  compare trace, 0
  {
    break-if-!=
    abort "null trace"
  }
  var first-free/ebx: (addr int) <- get trace, first-free
  compare *first-free, 0
  {
    break-if->
    return 0/false
  }
  return 1/true
}

fn test-run-expand-trace {
  var sandbox-storage: sandbox
  var sandbox/esi: (addr sandbox) <- address sandbox-storage
  initialize-sandbox-with sandbox, "12"
  # eval
  edit-sandbox sandbox, 0x13/ctrl-s, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  # setup: screen
  var screen-on-stack: screen
  var screen/edi: (addr screen) <- address screen-on-stack
  initialize-screen screen, 0x80/width, 0x10/height, 0/no-pixel-graphics
  #
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  # skip one line of padding
  check-screen-row screen,                                  1/y, " 12    ", "F - test-run-expand-trace/pre0-0"
  check-background-color-in-screen-row screen, 7/bg=cursor, 1/y, "   |   ", "F - test-run-expand-trace/pre0-0/cursor"
  check-screen-row screen,                                  2/y, " ...   ", "F - test-run-expand-trace/pre0-1"
  check-background-color-in-screen-row screen, 7/bg=cursor, 2/y, "       ", "F - test-run-expand-trace/pre0-1/cursor"
  check-screen-row screen,                                  3/y, " => 12 ", "F - test-run-expand-trace/pre0-2"
  check-background-color-in-screen-row screen, 7/bg=cursor, 3/y, "       ", "F - test-run-expand-trace/pre0-2/cursor"
  # move cursor into trace
  edit-sandbox sandbox, 0xd/ctrl-m, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  #
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  # skip one line of padding
  check-screen-row screen,                                  1/y, " 12    ", "F - test-run-expand-trace/pre1-0"
  check-background-color-in-screen-row screen, 7/bg=cursor, 1/y, "       ", "F - test-run-expand-trace/pre1-0/cursor"
  check-screen-row screen,                                  2/y, " ...   ", "F - test-run-expand-trace/pre1-1"
  check-background-color-in-screen-row screen, 7/bg=cursor, 2/y, " |||   ", "F - test-run-expand-trace/pre1-1/cursor"
  check-screen-row screen,                                  3/y, " => 12 ", "F - test-run-expand-trace/pre1-2"
  check-background-color-in-screen-row screen, 7/bg=cursor, 3/y, "       ", "F - test-run-expand-trace/pre1-2/cursor"
  # expand
  edit-sandbox sandbox, 0xa/newline, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  #
  clear-screen screen
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  # skip one line of padding
  check-screen-row screen,                                  1/y, " 12    ", "F - test-run-expand-trace/expand-0"
  check-background-color-in-screen-row screen, 7/bg=cursor, 1/y, "       ", "F - test-run-expand-trace/expand-0/cursor"
  check-screen-row screen,                                  2/y, " 1 toke", "F - test-run-expand-trace/expand-1"
  check-background-color-in-screen-row screen, 7/bg=cursor, 2/y, " ||||||", "F - test-run-expand-trace/expand-1/cursor"
  check-screen-row screen,                                  3/y, " ...   ", "F - test-run-expand-trace/expand-2"
  check-background-color-in-screen-row screen, 7/bg=cursor, 3/y, "       ", "F - test-run-expand-trace/expand-2/cursor"
  check-screen-row screen,                                  4/y, " 1 pars", "F - test-run-expand-trace/expand-2"
  check-background-color-in-screen-row screen, 7/bg=cursor, 4/y, "       ", "F - test-run-expand-trace/expand-2/cursor"
}

fn test-run-can-rerun-when-expanding-trace {
  var sandbox-storage: sandbox
  var sandbox/esi: (addr sandbox) <- address sandbox-storage
  # initialize sandbox with a max-depth of 3
  initialize-sandbox-with sandbox, "12"
  # eval
  edit-sandbox sandbox, 0x13/ctrl-s, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  # setup: screen
  var screen-on-stack: screen
  var screen/edi: (addr screen) <- address screen-on-stack
  initialize-screen screen, 0x80/width, 0x10/height, 0/no-pixel-graphics
  #
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  # skip one line of padding
  check-screen-row screen,                                  1/y, " 12    ", "F - test-run-can-rerun-when-expanding-trace/pre0-0"
  check-background-color-in-screen-row screen, 7/bg=cursor, 1/y, "   |   ", "F - test-run-can-rerun-when-expanding-trace/pre0-0/cursor"
  check-screen-row screen,                                  2/y, " ...   ", "F - test-run-can-rerun-when-expanding-trace/pre0-1"
  check-background-color-in-screen-row screen, 7/bg=cursor, 2/y, "       ", "F - test-run-can-rerun-when-expanding-trace/pre0-1/cursor"
  check-screen-row screen,                                  3/y, " => 12 ", "F - test-run-can-rerun-when-expanding-trace/pre0-2"
  check-background-color-in-screen-row screen, 7/bg=cursor, 3/y, "       ", "F - test-run-can-rerun-when-expanding-trace/pre0-2/cursor"
  # move cursor into trace
  edit-sandbox sandbox, 0xd/ctrl-m, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  #
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  # skip one line of padding
  check-screen-row screen,                                  1/y, " 12    ", "F - test-run-can-rerun-when-expanding-trace/pre1-0"
  check-background-color-in-screen-row screen, 7/bg=cursor, 1/y, "       ", "F - test-run-can-rerun-when-expanding-trace/pre1-0/cursor"
  check-screen-row screen,                                  2/y, " ...   ", "F - test-run-can-rerun-when-expanding-trace/pre1-1"
  check-background-color-in-screen-row screen, 7/bg=cursor, 2/y, " |||   ", "F - test-run-can-rerun-when-expanding-trace/pre1-1/cursor"
  check-screen-row screen,                                  3/y, " => 12 ", "F - test-run-can-rerun-when-expanding-trace/pre1-2"
  check-background-color-in-screen-row screen, 7/bg=cursor, 3/y, "       ", "F - test-run-can-rerun-when-expanding-trace/pre1-2/cursor"
  # expand
  edit-sandbox sandbox, 0xa/newline, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  #
  clear-screen screen
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  # skip one line of padding
  check-screen-row screen,                                  1/y, " 12    ", "F - test-run-can-rerun-when-expanding-trace/pre2-0"
  check-background-color-in-screen-row screen, 7/bg=cursor, 1/y, "       ", "F - test-run-can-rerun-when-expanding-trace/pre2-0/cursor"
  check-screen-row screen,                                  2/y, " 1 toke", "F - test-run-can-rerun-when-expanding-trace/pre2-1"
  check-background-color-in-screen-row screen, 7/bg=cursor, 2/y, " ||||||", "F - test-run-can-rerun-when-expanding-trace/pre2-1/cursor"
  check-screen-row screen,                                  3/y, " ...   ", "F - test-run-can-rerun-when-expanding-trace/pre2-2"
  check-background-color-in-screen-row screen, 7/bg=cursor, 3/y, "       ", "F - test-run-can-rerun-when-expanding-trace/pre2-2/cursor"
  check-screen-row screen,                                  4/y, " 1 pars", "F - test-run-can-rerun-when-expanding-trace/pre2-2"
  check-background-color-in-screen-row screen, 7/bg=cursor, 4/y, "       ", "F - test-run-can-rerun-when-expanding-trace/pre2-2/cursor"
  # move cursor down and expand
  edit-sandbox sandbox, 0x6a/j, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  edit-sandbox sandbox, 0xa/newline, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  #
  clear-screen screen
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  # screen looks same as if trace max-depth was really high
  check-screen-row screen,                                  1/y, " 12    ", "F - test-run-can-rerun-when-expanding-trace/expand-0"
  check-background-color-in-screen-row screen, 7/bg=cursor, 1/y, "       ", "F - test-run-can-rerun-when-expanding-trace/expand-0/cursor"
  check-screen-row screen,                                  2/y, " 1 toke", "F - test-run-can-rerun-when-expanding-trace/expand-1"
  check-background-color-in-screen-row screen, 7/bg=cursor, 2/y, "       ", "F - test-run-can-rerun-when-expanding-trace/expand-1/cursor"
  check-screen-row screen,                                  3/y, " 2 next", "F - test-run-can-rerun-when-expanding-trace/expand-2"
  check-background-color-in-screen-row screen, 7/bg=cursor, 3/y, " ||||||", "F - test-run-can-rerun-when-expanding-trace/expand-2/cursor"
  check-screen-row screen,                                  4/y, " ...   ", "F - test-run-can-rerun-when-expanding-trace/expand-3"
  check-background-color-in-screen-row screen, 7/bg=cursor, 4/y, "       ", "F - test-run-can-rerun-when-expanding-trace/expand-3/cursor"
  check-screen-row screen,                                  5/y, " 2 => 1", "F - test-run-can-rerun-when-expanding-trace/expand-4"
  check-background-color-in-screen-row screen, 7/bg=cursor, 5/y, "       ", "F - test-run-can-rerun-when-expanding-trace/expand-4/cursor"
}

fn test-run-preserves-trace-view-on-rerun {
  var sandbox-storage: sandbox
  var sandbox/esi: (addr sandbox) <- address sandbox-storage
  # initialize sandbox with a max-depth of 3
  initialize-sandbox-with sandbox, "7"
  # eval
  edit-sandbox sandbox, 0x13/ctrl-s, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  # setup: screen
  var screen-on-stack: screen
  var screen/edi: (addr screen) <- address screen-on-stack
  initialize-screen screen, 0x80/width, 0x10/height, 0/no-pixel-graphics
  #
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  # skip one line of padding
  check-screen-row screen,                                  1/y, " 7                     ", "F - test-run-preserves-trace-view-on-rerun/pre0-0"
  check-background-color-in-screen-row screen, 7/bg=cursor, 1/y, "  |                    ", "F - test-run-preserves-trace-view-on-rerun/pre0-0/cursor"
  check-screen-row screen,                                  2/y, " ...                   ", "F - test-run-preserves-trace-view-on-rerun/pre0-1"
  check-background-color-in-screen-row screen, 7/bg=cursor, 2/y, "                       ", "F - test-run-preserves-trace-view-on-rerun/pre0-1/cursor"
  check-screen-row screen,                                  3/y, " => 7                  ", "F - test-run-preserves-trace-view-on-rerun/pre0-2"
  check-background-color-in-screen-row screen, 7/bg=cursor, 3/y, "                       ", "F - test-run-preserves-trace-view-on-rerun/pre0-2/cursor"
  # move cursor into trace
  edit-sandbox sandbox, 0xd/ctrl-m, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  #
  check-screen-row screen,                                  1/y, " 7                     ", "F - test-run-preserves-trace-view-on-rerun/pre1-0"
  check-background-color-in-screen-row screen, 7/bg=cursor, 1/y, "                       ", "F - test-run-preserves-trace-view-on-rerun/pre1-0/cursor"
  check-screen-row screen,                                  2/y, " ...                   ", "F - test-run-preserves-trace-view-on-rerun/pre1-1"
  check-background-color-in-screen-row screen, 7/bg=cursor, 2/y, " |||                   ", "F - test-run-preserves-trace-view-on-rerun/pre1-1/cursor"
  check-screen-row screen,                                  3/y, " => 7                  ", "F - test-run-preserves-trace-view-on-rerun/pre1-2"
  check-background-color-in-screen-row screen, 7/bg=cursor, 3/y, "                       ", "F - test-run-preserves-trace-view-on-rerun/pre1-2/cursor"
  # expand
  edit-sandbox sandbox, 0xa/newline, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  clear-screen screen
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  #
  check-screen-row screen,                                  1/y, " 7                     ", "F - test-run-preserves-trace-view-on-rerun/pre2-0"
  check-background-color-in-screen-row screen, 7/bg=cursor, 1/y, "                       ", "F - test-run-preserves-trace-view-on-rerun/pre2-0/cursor"
  check-screen-row screen,                                  2/y, " 1 tokenize            ", "F - test-run-preserves-trace-view-on-rerun/pre2-1"
  check-background-color-in-screen-row screen, 7/bg=cursor, 2/y, " ||||||||||            ", "F - test-run-preserves-trace-view-on-rerun/pre2-1/cursor"
  check-screen-row screen,                                  3/y, " ...                   ", "F - test-run-preserves-trace-view-on-rerun/pre2-2"
  check-background-color-in-screen-row screen, 7/bg=cursor, 3/y, "                       ", "F - test-run-preserves-trace-view-on-rerun/pre2-2/cursor"
  check-screen-row screen,                                  4/y, " 1 parse               ", "F - test-run-preserves-trace-view-on-rerun/pre2-3"
  check-background-color-in-screen-row screen, 7/bg=cursor, 4/y, "                       ", "F - test-run-preserves-trace-view-on-rerun/pre2-3/cursor"
  check-screen-row screen,                                  5/y, " ...                   ", "F - test-run-preserves-trace-view-on-rerun/pre2-4"
  check-background-color-in-screen-row screen, 7/bg=cursor, 5/y, "                       ", "F - test-run-preserves-trace-view-on-rerun/pre2-4/cursor"
  check-screen-row screen,                                  6/y, " 1 macroexpand 7       ", "F - test-run-preserves-trace-view-on-rerun/pre2-5"
  check-background-color-in-screen-row screen, 7/bg=cursor, 6/y, "                       ", "F - test-run-preserves-trace-view-on-rerun/pre2-5/cursor"
  check-screen-row screen,                                  7/y, " ...                   ", "F - test-run-preserves-trace-view-on-rerun/pre2-6"
  check-background-color-in-screen-row screen, 7/bg=cursor, 7/y, "                       ", "F - test-run-preserves-trace-view-on-rerun/pre2-6/cursor"
  check-screen-row screen,                                  8/y, " 1 => 7                ", "F - test-run-preserves-trace-view-on-rerun/pre2-7"
  check-background-color-in-screen-row screen, 7/bg=cursor, 8/y, "                       ", "F - test-run-preserves-trace-view-on-rerun/pre2-7/cursor"
  # move cursor down below the macroexpand line and expand
  edit-sandbox sandbox, 0x6a/j, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  edit-sandbox sandbox, 0x6a/j, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  edit-sandbox sandbox, 0x6a/j, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  edit-sandbox sandbox, 0x6a/j, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  edit-sandbox sandbox, 0x6a/j, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  #
  check-screen-row screen,                                  1/y, " 7                     ", "F - test-run-preserves-trace-view-on-rerun/pre3-0"
  check-background-color-in-screen-row screen, 7/bg=cursor, 1/y, "                       ", "F - test-run-preserves-trace-view-on-rerun/pre3-0/cursor"
  check-screen-row screen,                                  2/y, " 1 tokenize            ", "F - test-run-preserves-trace-view-on-rerun/pre3-1"
  check-background-color-in-screen-row screen, 7/bg=cursor, 2/y, "                       ", "F - test-run-preserves-trace-view-on-rerun/pre3-1/cursor"
  check-screen-row screen,                                  3/y, " ...                   ", "F - test-run-preserves-trace-view-on-rerun/pre3-2"
  check-background-color-in-screen-row screen, 7/bg=cursor, 3/y, "                       ", "F - test-run-preserves-trace-view-on-rerun/pre3-2/cursor"
  check-screen-row screen,                                  4/y, " 1 parse               ", "F - test-run-preserves-trace-view-on-rerun/pre3-3"
  check-background-color-in-screen-row screen, 7/bg=cursor, 4/y, "                       ", "F - test-run-preserves-trace-view-on-rerun/pre3-3/cursor"
  check-screen-row screen,                                  5/y, " ...                   ", "F - test-run-preserves-trace-view-on-rerun/pre3-4"
  check-background-color-in-screen-row screen, 7/bg=cursor, 5/y, "                       ", "F - test-run-preserves-trace-view-on-rerun/pre3-4/cursor"
  check-screen-row screen,                                  6/y, " 1 macroexpand 7       ", "F - test-run-preserves-trace-view-on-rerun/pre3-5"
  check-background-color-in-screen-row screen, 7/bg=cursor, 6/y, "                       ", "F - test-run-preserves-trace-view-on-rerun/pre3-5/cursor"
  check-screen-row screen,                                  7/y, " ...                   ", "F - test-run-preserves-trace-view-on-rerun/pre3-6"
  check-background-color-in-screen-row screen, 7/bg=cursor, 7/y, " |||                   ", "F - test-run-preserves-trace-view-on-rerun/pre3-6/cursor"
  check-screen-row screen,                                  8/y, " 1 => 7                ", "F - test-run-preserves-trace-view-on-rerun/pre3-7"
  check-background-color-in-screen-row screen, 7/bg=cursor, 8/y, "                       ", "F - test-run-preserves-trace-view-on-rerun/pre3-7/cursor"
  # expand
  edit-sandbox sandbox, 0xa/newline, 0/no-globals, 0/no-disk, 0/no-tweak-screen
  clear-screen screen
  render-sandbox screen, sandbox, 0/x, 0/y, 0x80/width, 0x10/height
  # cursor line is expanded
  check-screen-row screen,                                  1/y, " 7                     ", "F - test-run-preserves-trace-view-on-rerun/expand-0"
  check-background-color-in-screen-row screen, 7/bg=cursor, 1/y, "                       ", "F - test-run-preserves-trace-view-on-rerun/expand-0/cursor"
  check-screen-row screen,                                  2/y, " 1 tokenize            ", "F - test-run-preserves-trace-view-on-rerun/expand-1"
  check-background-color-in-screen-row screen, 7/bg=cursor, 2/y, "                       ", "F - test-run-preserves-trace-view-on-rerun/expand-1/cursor"
  check-screen-row screen,                                  3/y, " ...                   ", "F - test-run-preserves-trace-view-on-rerun/expand-2"
  check-background-color-in-screen-row screen, 7/bg=cursor, 3/y, "                       ", "F - test-run-preserves-trace-view-on-rerun/expand-2/cursor"
  check-screen-row screen,                                  4/y, " 1 parse               ", "F - test-run-preserves-trace-view-on-rerun/expand-3"
  check-background-color-in-screen-row screen, 7/bg=cursor, 4/y, "                       ", "F - test-run-preserves-trace-view-on-rerun/expand-3/cursor"
  check-screen-row screen,                                  5/y, " ...                   ", "F - test-run-preserves-trace-view-on-rerun/expand-4"
  check-background-color-in-screen-row screen, 7/bg=cursor, 5/y, "                       ", "F - test-run-preserves-trace-view-on-rerun/expand-4/cursor"
  check-screen-row screen,                                  6/y, " 1 macroexpand 7       ", "F - test-run-preserves-trace-view-on-rerun/expand-5"
  check-background-color-in-screen-row screen, 7/bg=cursor, 6/y, "                       ", "F - test-run-preserves-trace-view-on-rerun/expand-5/cursor"
  check-screen-row screen,                                  7/y, " 2 macroexpand-iter 7  ", "F - test-run-preserves-trace-view-on-rerun/expand-6"
  check-background-color-in-screen-row screen, 7/bg=cursor, 7/y, " ||||||||||||||||||||  ", "F - test-run-preserves-trace-view-on-rerun/expand-6/cursor"
  check-screen-row screen,                                  8/y, " ...                   ", "F - test-run-preserves-trace-view-on-rerun/expand-7"
  check-background-color-in-screen-row screen, 7/bg=cursor, 8/y, "                       ", "F - test-run-preserves-trace-view-on-rerun/expand-7/cursor"
}