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
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
|
# env is an alist of ((sym . val) (sym . val) ...)
# we never modify `_in-ah` or `env`
# ignore args past 'trace' on a first reading; they're for the environment not the language
# 'call-number' is just for showing intermediate progress; this is a _slow_ interpreter
# side-effects if not in a test (screen-cell != 0):
# prints intermediate states of the screen to real screen
# stops if a keypress is encountered
fn evaluate _in-ah: (addr handle cell), _out-ah: (addr handle cell), env-h: (handle cell), globals: (addr global-table), trace: (addr trace), screen-cell: (addr handle cell), keyboard-cell: (addr handle cell), call-number: int {
# stack overflow? # disable when enabling Really-debug-print
check-stack
{
var screen-cell/eax: (addr handle cell) <- copy screen-cell
compare screen-cell, 0
break-if-=
var screen-cell-addr/eax: (addr cell) <- lookup *screen-cell
compare screen-cell-addr, 0
break-if-=
# if screen-cell exists, we're probably not in a test
show-stack-state
}
# show intermediate progress on screen if necessary
# treat input at the real keyboard as interrupting
{
compare screen-cell, 0
break-if-=
var tmp/eax: int <- copy call-number
tmp <- and 0xf # every 16 calls to evaluate
compare tmp, 0
break-if-!=
var screen-cell/eax: (addr handle cell) <- copy screen-cell
var screen-cell-addr/eax: (addr cell) <- lookup *screen-cell
compare screen-cell-addr, 0
break-if-=
var screen-obj-ah/eax: (addr handle screen) <- get screen-cell-addr, screen-data
var screen-obj/eax: (addr screen) <- lookup *screen-obj-ah
compare screen-obj, 0
break-if-=
var y/ecx: int <- render-screen 0/screen, screen-obj, 0x70/xmin, 1/ymin
var key/eax: byte <- read-key 0/keyboard
compare key, 0
break-if-=
error trace, "key pressed; interrupting..."
}
# errors? skip
{
var error?/eax: boolean <- has-errors? trace
compare error?, 0/false
break-if-=
return
}
var in-ah/esi: (addr handle cell) <- copy _in-ah
#? dump-cell in-ah
#? {
#? var foo/eax: byte <- read-key 0/keyboard
#? compare foo, 0
#? loop-if-=
#? }
# trace "evaluate " in " in environment " env {{{
{
var should-trace?/eax: boolean <- should-trace? trace
compare should-trace?, 0/false
break-if-=
var stream-storage: (stream byte 0x300)
var stream/ecx: (addr stream byte) <- address stream-storage
write stream, "evaluate "
var nested-trace-storage: trace
var nested-trace/edi: (addr trace) <- address nested-trace-storage
initialize-trace nested-trace, 1/only-errors, 0x10/capacity, 0/visible
print-cell in-ah, stream, nested-trace
write stream, " in environment "
var env-ah/eax: (addr handle cell) <- address env-h
clear-trace nested-trace
print-cell env-ah, stream, nested-trace
trace trace, "eval", stream
}
# }}}
trace-lower trace
var in/eax: (addr cell) <- lookup *in-ah
{
var nil?/eax: boolean <- nil? in
compare nil?, 0/false
break-if-=
# nil is a literal
trace-text trace, "eval", "nil"
copy-object _in-ah, _out-ah
trace-higher trace
return
}
var in-type/ecx: (addr int) <- get in, type
compare *in-type, 1/number
{
break-if-!=
# numbers are literals
trace-text trace, "eval", "number"
copy-object _in-ah, _out-ah
trace-higher trace
return
}
compare *in-type, 3/stream
{
break-if-!=
# streams are literals
trace-text trace, "eval", "stream"
copy-object _in-ah, _out-ah
trace-higher trace
return
}
compare *in-type, 2/symbol
{
break-if-!=
trace-text trace, "eval", "symbol"
debug-print "a", 7/fg, 0/bg
lookup-symbol in, _out-ah, env-h, globals, trace, screen-cell, keyboard-cell
debug-print "z", 7/fg, 0/bg
trace-higher trace
return
}
compare *in-type, 5/screen
{
break-if-!=
trace-text trace, "eval", "screen"
copy-object _in-ah, _out-ah
trace-higher trace
return
}
compare *in-type, 6/keyboard
{
break-if-!=
trace-text trace, "eval", "keyboard"
copy-object _in-ah, _out-ah
trace-higher trace
return
}
# 'in' is a syntax tree
$evaluate:literal-function: {
# trees starting with "litfn" are literals
var expr/esi: (addr cell) <- copy in
var in/edx: (addr cell) <- copy in
var first-ah/ecx: (addr handle cell) <- get in, left
var first/eax: (addr cell) <- lookup *first-ah
var litfn?/eax: boolean <- litfn? first
compare litfn?, 0/false
break-if-=
trace-text trace, "eval", "literal function"
copy-object _in-ah, _out-ah
trace-higher trace
return
}
$evaluate:literal-macro: {
# trees starting with "litmac" are literals
var expr/esi: (addr cell) <- copy in
var in/edx: (addr cell) <- copy in
var first-ah/ecx: (addr handle cell) <- get in, left
var first/eax: (addr cell) <- lookup *first-ah
var litmac?/eax: boolean <- litmac? first
compare litmac?, 0/false
break-if-=
trace-text trace, "eval", "literal macro"
copy-object _in-ah, _out-ah
trace-higher trace
return
}
$evaluate:anonymous-function: {
# trees starting with "fn" are anonymous functions
var expr/esi: (addr cell) <- copy in
var in/edx: (addr cell) <- copy in
var first-ah/ecx: (addr handle cell) <- get in, left
var first/eax: (addr cell) <- lookup *first-ah
var fn?/eax: boolean <- fn? first
compare fn?, 0/false
break-if-=
# turn (fn ...) into (litfn env ...)
trace-text trace, "eval", "anonymous function"
var rest-ah/eax: (addr handle cell) <- get in, right
var tmp: (handle cell)
var tmp-ah/edi: (addr handle cell) <- address tmp
new-pair tmp-ah, env-h, *rest-ah
var litfn: (handle cell)
var litfn-ah/eax: (addr handle cell) <- address litfn
new-symbol litfn-ah, "litfn"
new-pair _out-ah, *litfn-ah, *tmp-ah
trace-higher trace
return
}
# builtins with "special" evaluation rules
$evaluate:quote: {
# trees starting with single quote create literals
var expr/esi: (addr cell) <- copy in
# if its first elem is not "'", break
var first-ah/ecx: (addr handle cell) <- get in, left
var rest-ah/edx: (addr handle cell) <- get in, right
var first/eax: (addr cell) <- lookup *first-ah
var quote?/eax: boolean <- symbol-equal? first, "'"
compare quote?, 0/false
break-if-=
#
trace-text trace, "eval", "quote"
copy-object rest-ah, _out-ah
trace-higher trace
return
}
$evaluate:backquote: {
# trees starting with single backquote create literals
var expr/esi: (addr cell) <- copy in
# if its first elem is not "'", break
var first-ah/ecx: (addr handle cell) <- get in, left
var rest-ah/edx: (addr handle cell) <- get in, right
var first/eax: (addr cell) <- lookup *first-ah
var backquote?/eax: boolean <- symbol-equal? first, "`"
compare backquote?, 0/false
break-if-=
#
trace-text trace, "eval", "backquote"
debug-print "`(", 7/fg, 0/bg
evaluate-backquote rest-ah, _out-ah, env-h, globals, trace, screen-cell, keyboard-cell, call-number
debug-print ")", 7/fg, 0/bg
trace-higher trace
return
}
$evaluate:define: {
# trees starting with "define" define globals
var expr/esi: (addr cell) <- copy in
# if its first elem is not "define", break
var first-ah/ecx: (addr handle cell) <- get in, left
var rest-ah/edx: (addr handle cell) <- get in, right
var first/eax: (addr cell) <- lookup *first-ah
var define?/eax: boolean <- symbol-equal? first, "define"
compare define?, 0/false
break-if-=
#
trace-text trace, "eval", "define"
trace-text trace, "eval", "evaluating second arg"
var rest/eax: (addr cell) <- lookup *rest-ah
var first-arg-ah/ecx: (addr handle cell) <- get rest, left
{
var first-arg/eax: (addr cell) <- lookup *first-arg-ah
var first-arg-type/eax: (addr int) <- get first-arg, type
compare *first-arg-type, 2/symbol
break-if-=
error trace, "first arg to define must be a symbol"
trace-higher trace
return
}
rest-ah <- get rest, right
rest <- lookup *rest-ah
var second-arg-ah/edx: (addr handle cell) <- get rest, left
debug-print "P", 4/fg, 0/bg
increment call-number
evaluate second-arg-ah, _out-ah, env-h, globals, trace, screen-cell, keyboard-cell, call-number
debug-print "Q", 4/fg, 0/bg
# errors? skip
{
var error?/eax: boolean <- has-errors? trace
compare error?, 0/false
break-if-=
trace-higher trace
return
}
trace-text trace, "eval", "saving global binding"
var first-arg/eax: (addr cell) <- lookup *first-arg-ah
var first-arg-data-ah/eax: (addr handle stream byte) <- get first-arg, text-data
var first-arg-data/eax: (addr stream byte) <- lookup *first-arg-data-ah
var tmp-string: (handle array byte)
var tmp-ah/edx: (addr handle array byte) <- address tmp-string
rewind-stream first-arg-data
stream-to-array first-arg-data, tmp-ah
var first-arg-data-string/eax: (addr array byte) <- lookup *tmp-ah
var out-ah/edi: (addr handle cell) <- copy _out-ah
assign-or-create-global globals, first-arg-data-string, *out-ah, trace
trace-higher trace
return
}
$evaluate:set: {
# trees starting with "set" mutate bindings
var expr/esi: (addr cell) <- copy in
# if its first elem is not "set", break
var first-ah/ecx: (addr handle cell) <- get in, left
var rest-ah/edx: (addr handle cell) <- get in, right
var first/eax: (addr cell) <- lookup *first-ah
var set?/eax: boolean <- symbol-equal? first, "set"
compare set?, 0/false
break-if-=
#
trace-text trace, "eval", "set"
trace-text trace, "eval", "evaluating second arg"
var rest/eax: (addr cell) <- lookup *rest-ah
var first-arg-ah/ecx: (addr handle cell) <- get rest, left
{
var first-arg/eax: (addr cell) <- lookup *first-arg-ah
var first-arg-type/eax: (addr int) <- get first-arg, type
compare *first-arg-type, 2/symbol
break-if-=
error trace, "first arg to set must be a symbol"
trace-higher trace
return
}
rest-ah <- get rest, right
rest <- lookup *rest-ah
var second-arg-ah/edx: (addr handle cell) <- get rest, left
debug-print "P", 4/fg, 0/bg
increment call-number
evaluate second-arg-ah, _out-ah, env-h, globals, trace, screen-cell, keyboard-cell, call-number
debug-print "Q", 4/fg, 0/bg
# errors? skip
{
var error?/eax: boolean <- has-errors? trace
compare error?, 0/false
break-if-=
trace-higher trace
return
}
trace-text trace, "eval", "mutating binding"
var first-arg/eax: (addr cell) <- lookup *first-arg-ah
var first-arg-data-ah/eax: (addr handle stream byte) <- get first-arg, text-data
var first-arg-data/eax: (addr stream byte) <- lookup *first-arg-data-ah
mutate-binding first-arg-data, _out-ah, env-h, globals, trace
trace-higher trace
return
}
$evaluate:and: {
var expr/esi: (addr cell) <- copy in
# if its first elem is not "and", break
var first-ah/ecx: (addr handle cell) <- get in, left
var rest-ah/edx: (addr handle cell) <- get in, right
var first/eax: (addr cell) <- lookup *first-ah
var and?/eax: boolean <- symbol-equal? first, "and"
compare and?, 0/false
break-if-=
#
trace-text trace, "eval", "and"
trace-text trace, "eval", "evaluating first arg"
var rest/eax: (addr cell) <- lookup *rest-ah
var first-arg-ah/ecx: (addr handle cell) <- get rest, left
debug-print "R2", 4/fg, 0/bg
increment call-number
evaluate first-arg-ah, _out-ah, env-h, globals, trace, screen-cell, keyboard-cell, call-number
debug-print "S2", 4/fg, 0/bg
# errors? skip
{
var error?/eax: boolean <- has-errors? trace
compare error?, 0/false
break-if-=
trace-higher trace
return
}
# if first arg is nil, short-circuit
var out-ah/eax: (addr handle cell) <- copy _out-ah
var out/eax: (addr cell) <- lookup *out-ah
var nil?/eax: boolean <- nil? out
compare nil?, 0/false
{
break-if-=
trace-higher trace
return
}
var rest/eax: (addr cell) <- lookup *rest-ah
rest-ah <- get rest, right
rest <- lookup *rest-ah
var second-ah/eax: (addr handle cell) <- get rest, left
debug-print "T2", 4/fg, 0/bg
increment call-number
evaluate second-ah, _out-ah, env-h, globals, trace, screen-cell, keyboard-cell, call-number
debug-print "U2", 4/fg, 0/bg
trace-higher trace
return
}
$evaluate:or: {
var expr/esi: (addr cell) <- copy in
# if its first elem is not "or", break
var first-ah/ecx: (addr handle cell) <- get in, left
var rest-ah/edx: (addr handle cell) <- get in, right
var first/eax: (addr cell) <- lookup *first-ah
var or?/eax: boolean <- symbol-equal? first, "or"
compare or?, 0/false
break-if-=
#
trace-text trace, "eval", "or"
trace-text trace, "eval", "evaluating first arg"
var rest/eax: (addr cell) <- lookup *rest-ah
var first-arg-ah/ecx: (addr handle cell) <- get rest, left
debug-print "R2", 4/fg, 0/bg
increment call-number
evaluate first-arg-ah, _out-ah, env-h, globals, trace, screen-cell, keyboard-cell, call-number
debug-print "S2", 4/fg, 0/bg
# errors? skip
{
var error?/eax: boolean <- has-errors? trace
compare error?, 0/false
break-if-=
trace-higher trace
return
}
# if first arg is not nil, short-circuit
var out-ah/eax: (addr handle cell) <- copy _out-ah
var out/eax: (addr cell) <- lookup *out-ah
var nil?/eax: boolean <- nil? out
compare nil?, 0/false
{
break-if-!=
trace-higher trace
return
}
var rest/eax: (addr cell) <- lookup *rest-ah
rest-ah <- get rest, right
rest <- lookup *rest-ah
var second-ah/eax: (addr handle cell) <- get rest, left
debug-print "T2", 4/fg, 0/bg
increment call-number
evaluate second-ah, _out-ah, env-h, globals, trace, screen-cell, keyboard-cell, call-number
debug-print "U2", 4/fg, 0/bg
# errors? skip
{
var error?/eax: boolean <- has-errors? trace
compare error?, 0/false
break-if-=
trace-higher trace
return
}
trace-higher trace
return
}
$evaluate:if: {
# trees starting with "if" are conditionals
var expr/esi: (addr cell) <- copy in
# if its first elem is not "if", break
var first-ah/ecx: (addr handle cell) <- get in, left
var rest-ah/edx: (addr handle cell) <- get in, right
var first/eax: (addr cell) <- lookup *first-ah
var if?/eax: boolean <- symbol-equal? first, "if"
compare if?, 0/false
break-if-=
#
trace-text trace, "eval", "if"
trace-text trace, "eval", "evaluating first arg"
var rest/eax: (addr cell) <- lookup *rest-ah
var first-arg-ah/ecx: (addr handle cell) <- get rest, left
var guard-h: (handle cell)
var guard-ah/esi: (addr handle cell) <- address guard-h
debug-print "R", 4/fg, 0/bg
increment call-number
evaluate first-arg-ah, guard-ah, env-h, globals, trace, screen-cell, keyboard-cell, call-number
debug-print "S", 4/fg, 0/bg
# errors? skip
{
var error?/eax: boolean <- has-errors? trace
compare error?, 0/false
break-if-=
trace-higher trace
return
}
rest-ah <- get rest, right
rest <- lookup *rest-ah
var branch-ah/edi: (addr handle cell) <- get rest, left
var guard-a/eax: (addr cell) <- lookup *guard-ah
var skip-to-third-arg?/eax: boolean <- nil? guard-a
compare skip-to-third-arg?, 0/false
{
break-if-=
trace-text trace, "eval", "skipping to third arg"
var rest/eax: (addr cell) <- lookup *rest-ah
rest-ah <- get rest, right
rest <- lookup *rest-ah
branch-ah <- get rest, left
}
debug-print "T", 4/fg, 0/bg
increment call-number
evaluate branch-ah, _out-ah, env-h, globals, trace, screen-cell, keyboard-cell, call-number
debug-print "U", 4/fg, 0/bg
trace-higher trace
return
}
$evaluate:while: {
# trees starting with "while" are loops
var expr/esi: (addr cell) <- copy in
# if its first elem is not "while", break
var first-ah/ecx: (addr handle cell) <- get in, left
var rest-ah/edx: (addr handle cell) <- get in, right
var first/eax: (addr cell) <- lookup *first-ah
var first-type/ecx: (addr int) <- get first, type
compare *first-type, 2/symbol
break-if-!=
var sym-data-ah/eax: (addr handle stream byte) <- get first, text-data
var sym-data/eax: (addr stream byte) <- lookup *sym-data-ah
var while?/eax: boolean <- stream-data-equal? sym-data, "while"
compare while?, 0/false
break-if-=
#
trace-text trace, "eval", "while"
var rest/eax: (addr cell) <- lookup *rest-ah
var first-arg-ah/ecx: (addr handle cell) <- get rest, left
rest-ah <- get rest, right
var guard-h: (handle cell)
var guard-ah/esi: (addr handle cell) <- address guard-h
$evaluate:while:loop-execution: {
{
var error?/eax: boolean <- has-errors? trace
compare error?, 0/false
break-if-!= $evaluate:while:loop-execution
}
trace-text trace, "eval", "loop termination check"
debug-print "V", 4/fg, 0/bg
increment call-number
evaluate first-arg-ah, guard-ah, env-h, globals, trace, screen-cell, keyboard-cell, call-number
debug-print "W", 4/fg, 0/bg
# errors? skip
{
var error?/eax: boolean <- has-errors? trace
compare error?, 0/false
break-if-=
trace-higher trace
return
}
var guard-a/eax: (addr cell) <- lookup *guard-ah
var done?/eax: boolean <- nil? guard-a
compare done?, 0/false
break-if-!=
evaluate-exprs rest-ah, _out-ah, env-h, globals, trace, screen-cell, keyboard-cell, call-number
# errors? skip
{
var error?/eax: boolean <- has-errors? trace
compare error?, 0/false
break-if-=
trace-higher trace
return
}
loop
}
trace-text trace, "eval", "loop terminated"
trace-higher trace
return
}
# trace "evaluate function call elements in " in {{{
{
var should-trace?/eax: boolean <- should-trace? trace
compare should-trace?, 0/false
break-if-=
var stream-storage: (stream byte 0x300)
var stream/ecx: (addr stream byte) <- address stream-storage
write stream, "evaluate function call elements in "
var nested-trace-storage: trace
var nested-trace/edi: (addr trace) <- address nested-trace-storage
initialize-trace nested-trace, 1/only-errors, 0x10/capacity, 0/visible
print-cell in-ah, stream, nested-trace
trace trace, "eval", stream
}
# }}}
trace-lower trace
var evaluated-list-storage: (handle cell)
var evaluated-list-ah/esi: (addr handle cell) <- address evaluated-list-storage
var curr-out-ah/edx: (addr handle cell) <- copy evaluated-list-ah
var curr/ecx: (addr cell) <- copy in
$evaluate-list:loop: {
allocate-pair curr-out-ah
var nil?/eax: boolean <- nil? curr
compare nil?, 0/false
break-if-!=
# eval left
var curr-out/eax: (addr cell) <- lookup *curr-out-ah
var left-out-ah/edi: (addr handle cell) <- get curr-out, left
var left-ah/esi: (addr handle cell) <- get curr, left
debug-print "A", 4/fg, 0/bg
increment call-number
evaluate left-ah, left-out-ah, env-h, globals, trace, screen-cell, keyboard-cell, call-number
debug-print "B", 4/fg, 0/bg
# errors? skip
{
var error?/eax: boolean <- has-errors? trace
compare error?, 0/false
break-if-=
trace-higher trace
trace-higher trace
return
}
#
curr-out-ah <- get curr-out, right
var right-ah/eax: (addr handle cell) <- get curr, right
var right/eax: (addr cell) <- lookup *right-ah
curr <- copy right
loop
}
trace-higher trace
var evaluated-list/eax: (addr cell) <- lookup *evaluated-list-ah
var function-ah/ecx: (addr handle cell) <- get evaluated-list, left
var args-ah/edx: (addr handle cell) <- get evaluated-list, right
debug-print "C", 4/fg, 0/bg
apply function-ah, args-ah, _out-ah, globals, trace, screen-cell, keyboard-cell, call-number
debug-print "Y", 4/fg, 0/bg
trace-higher trace
# trace "=> " _out-ah {{{
{
var should-trace?/eax: boolean <- should-trace? trace
compare should-trace?, 0/false
break-if-=
var stream-storage: (stream byte 0x200)
var stream/ecx: (addr stream byte) <- address stream-storage
write stream, "=> "
var nested-trace-storage: trace
var nested-trace/edi: (addr trace) <- address nested-trace-storage
initialize-trace nested-trace, 1/only-errors, 0x10/capacity, 0/visible
print-cell _out-ah, stream, nested-trace
trace trace, "eval", stream
}
# }}}
debug-print "Z", 4/fg, 0/bg
}
fn apply _f-ah: (addr handle cell), args-ah: (addr handle cell), out: (addr handle cell), globals: (addr global-table), trace: (addr trace), screen-cell: (addr handle cell), keyboard-cell: (addr handle cell), call-number: int {
var f-ah/eax: (addr handle cell) <- copy _f-ah
var _f/eax: (addr cell) <- lookup *f-ah
var f/esi: (addr cell) <- copy _f
# call primitive functions
{
var f-type/eax: (addr int) <- get f, type
compare *f-type, 4/primitive-function
break-if-!=
apply-primitive f, args-ah, out, globals, trace
return
}
# if it's not a primitive function it must be an anonymous function
# trace "apply anonymous function " f " in environment " env {{{
{
var should-trace?/eax: boolean <- should-trace? trace
compare should-trace?, 0/false
break-if-=
var stream-storage: (stream byte 0x200)
var stream/ecx: (addr stream byte) <- address stream-storage
write stream, "apply anonymous function "
var nested-trace-storage: trace
var nested-trace/edi: (addr trace) <- address nested-trace-storage
initialize-trace nested-trace, 1/only-errors, 0x10/capacity, 0/visible
print-cell _f-ah, stream, nested-trace
#? write stream, " in environment "
#? var callee-env-ah/eax: (addr handle cell) <- address callee-env-h
#? clear-trace nested-trace
#? print-cell callee-env-ah, stream, nested-trace
trace trace, "eval", stream
}
# }}}
trace-lower trace
{
var f-type/ecx: (addr int) <- get f, type
compare *f-type, 0/pair
break-if-!=
var first-ah/eax: (addr handle cell) <- get f, left
var first/eax: (addr cell) <- lookup *first-ah
var litfn?/eax: boolean <- litfn? first
compare litfn?, 0/false
break-if-=
var rest-ah/esi: (addr handle cell) <- get f, right
var rest/eax: (addr cell) <- lookup *rest-ah
var callee-env-ah/edx: (addr handle cell) <- get rest, left
rest-ah <- get rest, right
rest <- lookup *rest-ah
var params-ah/ecx: (addr handle cell) <- get rest, left
var body-ah/eax: (addr handle cell) <- get rest, right
debug-print "D", 7/fg, 0/bg
apply-function params-ah, args-ah, body-ah, out, *callee-env-ah, globals, trace, screen-cell, keyboard-cell, call-number
debug-print "Y", 7/fg, 0/bg
trace-higher trace
return
}
error trace, "unknown function"
}
fn apply-function params-ah: (addr handle cell), args-ah: (addr handle cell), body-ah: (addr handle cell), out: (addr handle cell), env-h: (handle cell), globals: (addr global-table), trace: (addr trace), screen-cell: (addr handle cell), keyboard-cell: (addr handle cell), call-number: int {
# push bindings for params to env
var new-env-h: (handle cell)
var new-env-ah/esi: (addr handle cell) <- address new-env-h
push-bindings params-ah, args-ah, env-h, new-env-ah, trace
# errors? skip
{
var error?/eax: boolean <- has-errors? trace
compare error?, 0/false
break-if-=
return
}
#
evaluate-exprs body-ah, out, new-env-h, globals, trace, screen-cell, keyboard-cell, call-number
}
fn evaluate-exprs _exprs-ah: (addr handle cell), out: (addr handle cell), env-h: (handle cell), globals: (addr global-table), trace: (addr trace), screen-cell: (addr handle cell), keyboard-cell: (addr handle cell), call-number: int {
# eval all exprs, writing result to `out` each time
var exprs-ah/ecx: (addr handle cell) <- copy _exprs-ah
$evaluate-exprs:loop: {
var exprs/eax: (addr cell) <- lookup *exprs-ah
# stop when exprs is nil
{
var exprs-nil?/eax: boolean <- nil? exprs
compare exprs-nil?, 0/false
break-if-!= $evaluate-exprs:loop
}
# evaluate each expression, writing result to `out`
{
var curr-ah/eax: (addr handle cell) <- get exprs, left
debug-print "E", 7/fg, 0/bg
increment call-number
evaluate curr-ah, out, env-h, globals, trace, screen-cell, keyboard-cell, call-number
debug-print "X", 7/fg, 0/bg
# errors? skip
{
var error?/eax: boolean <- has-errors? trace
compare error?, 0/false
break-if-=
return
}
}
#
exprs-ah <- get exprs, right
loop
}
# `out` contains result of evaluating final expression
}
# Bind params to corresponding args and add the bindings to old-env. Return
# the result in env-ah.
#
# We never modify old-env, but we point to it. This way other parts of the
# interpreter can continue using old-env, and everything works harmoniously
# even though no cells are copied around.
#
# env should always be a DAG (ignoring internals of values). It doesn't have
# to be a tree (some values may be shared), but there are also no cycles.
#
# Learn more: https://en.wikipedia.org/wiki/Persistent_data_structure
fn push-bindings _params-ah: (addr handle cell), _args-ah: (addr handle cell), old-env-h: (handle cell), env-ah: (addr handle cell), trace: (addr trace) {
var params-ah/edx: (addr handle cell) <- copy _params-ah
var args-ah/ebx: (addr handle cell) <- copy _args-ah
var _params/eax: (addr cell) <- lookup *params-ah
var params/esi: (addr cell) <- copy _params
{
var params-nil?/eax: boolean <- nil? params
compare params-nil?, 0/false
break-if-=
# nil is a literal
trace-text trace, "eval", "done with push-bindings"
copy-handle old-env-h, env-ah
return
}
# Params can only be symbols or pairs. Args can be anything.
# trace "pushing bindings from " params " to " args {{{
{
var should-trace?/eax: boolean <- should-trace? trace
compare should-trace?, 0/false
break-if-=
var stream-storage: (stream byte 0x200)
var stream/ecx: (addr stream byte) <- address stream-storage
write stream, "pushing bindings from "
var nested-trace-storage: trace
var nested-trace/edi: (addr trace) <- address nested-trace-storage
initialize-trace nested-trace, 1/only-errors, 0x10/capacity, 0/visible
print-cell params-ah, stream, nested-trace
write stream, " to "
clear-trace nested-trace
print-cell args-ah, stream, nested-trace
write stream, " onto "
var old-env-ah/eax: (addr handle cell) <- address old-env-h
clear-trace nested-trace
print-cell old-env-ah, stream, nested-trace
trace trace, "eval", stream
}
# }}}
trace-lower trace
var params-type/eax: (addr int) <- get params, type
compare *params-type, 2/symbol
{
break-if-!=
trace-text trace, "eval", "symbol; binding to all remaining args"
# create a new binding
var new-binding-storage: (handle cell)
var new-binding-ah/eax: (addr handle cell) <- address new-binding-storage
new-pair new-binding-ah, *params-ah, *args-ah
# push it to env
new-pair env-ah, *new-binding-ah, old-env-h
trace-higher trace
return
}
compare *params-type, 0/pair
{
break-if-=
error trace, "cannot bind a non-symbol"
trace-higher trace
return
}
var _args/eax: (addr cell) <- lookup *args-ah
var args/edi: (addr cell) <- copy _args
# params is now a pair, so args must be also
{
var args-nil?/eax: boolean <- nil? args
compare args-nil?, 0/false
break-if-=
error trace, "not enough args to bind"
return
}
var args-type/eax: (addr int) <- get args, type
compare *args-type, 0/pair
{
break-if-=
error trace, "args not in a proper list"
trace-higher trace
return
}
var intermediate-env-storage: (handle cell)
var intermediate-env-ah/edx: (addr handle cell) <- address intermediate-env-storage
var first-param-ah/eax: (addr handle cell) <- get params, left
var first-arg-ah/ecx: (addr handle cell) <- get args, left
push-bindings first-param-ah, first-arg-ah, old-env-h, intermediate-env-ah, trace
# errors? skip
{
var error?/eax: boolean <- has-errors? trace
compare error?, 0/false
break-if-=
trace-higher trace
return
}
var remaining-params-ah/eax: (addr handle cell) <- get params, right
var remaining-args-ah/ecx: (addr handle cell) <- get args, right
push-bindings remaining-params-ah, remaining-args-ah, *intermediate-env-ah, env-ah, trace
trace-higher trace
}
fn lookup-symbol sym: (addr cell), out: (addr handle cell), env-h: (handle cell), globals: (addr global-table), trace: (addr trace), screen-cell: (addr handle cell), keyboard-cell: (addr handle cell) {
# trace sym
{
var should-trace?/eax: boolean <- should-trace? trace
compare should-trace?, 0/false
break-if-=
var stream-storage: (stream byte 0x800) # pessimistically sized just for the large alist loaded from disk in `main`
var stream/ecx: (addr stream byte) <- address stream-storage
write stream, "look up "
var sym2/eax: (addr cell) <- copy sym
var sym-data-ah/eax: (addr handle stream byte) <- get sym2, text-data
var sym-data/eax: (addr stream byte) <- lookup *sym-data-ah
rewind-stream sym-data
write-stream stream, sym-data
write stream, " in "
var env-ah/eax: (addr handle cell) <- address env-h
var nested-trace-storage: trace
var nested-trace/edi: (addr trace) <- address nested-trace-storage
initialize-trace nested-trace, 1/only-errors, 0x10/capacity, 0/visible
print-cell env-ah, stream, nested-trace
trace trace, "eval", stream
}
trace-lower trace
var _env/eax: (addr cell) <- lookup env-h
var env/ebx: (addr cell) <- copy _env
# if env is not a list, error
{
var env-type/ecx: (addr int) <- get env, type
compare *env-type, 0/pair
break-if-=
error trace, "eval found a non-list environment"
trace-higher trace
return
}
# if env is nil, look up in globals
{
var env-nil?/eax: boolean <- nil? env
compare env-nil?, 0/false
break-if-=
debug-print "b", 7/fg, 0/bg
lookup-symbol-in-globals sym, out, globals, trace, screen-cell, keyboard-cell
debug-print "x", 7/fg, 0/bg
trace-higher trace
# trace "=> " out " (global)" {{{
{
var should-trace?/eax: boolean <- should-trace? trace
compare should-trace?, 0/false
break-if-=
var error?/eax: boolean <- has-errors? trace
compare error?, 0/false
break-if-!=
var stream-storage: (stream byte 0x200)
var stream/ecx: (addr stream byte) <- address stream-storage
write stream, "=> "
var nested-trace-storage: trace
var nested-trace/edi: (addr trace) <- address nested-trace-storage
initialize-trace nested-trace, 1/only-errors, 0x10/capacity, 0/visible
print-cell out, stream, nested-trace
write stream, " (global)"
trace trace, "eval", stream
}
# }}}
debug-print "y", 7/fg, 0/bg
return
}
# check car
var env-head-storage: (handle cell)
var env-head-ah/eax: (addr handle cell) <- address env-head-storage
{
var nested-trace-storage: trace
var nested-trace/edi: (addr trace) <- address nested-trace-storage
initialize-trace nested-trace, 1/only-errors, 0x10/capacity, 0/visible
car env, env-head-ah, nested-trace
}
var _env-head/eax: (addr cell) <- lookup *env-head-ah
var env-head/ecx: (addr cell) <- copy _env-head
# if car is not a list, abort
{
var env-head-type/eax: (addr int) <- get env-head, type
compare *env-head-type, 0/pair
break-if-=
error trace, "environment is not a list of (key . value) pairs"
trace-higher trace
return
}
# check key
var curr-key-storage: (handle cell)
var curr-key-ah/eax: (addr handle cell) <- address curr-key-storage
car env-head, curr-key-ah, trace
var curr-key/eax: (addr cell) <- lookup *curr-key-ah
# if key is not a symbol, abort
{
var curr-key-type/eax: (addr int) <- get curr-key, type
compare *curr-key-type, 2/symbol
break-if-=
error trace, "environment contains a binding for a non-symbol"
trace-higher trace
return
}
# if key matches sym, return val
var match?/eax: boolean <- cell-isomorphic? curr-key, sym, trace
compare match?, 0/false
{
break-if-=
var nested-trace-storage: trace
var nested-trace/edi: (addr trace) <- address nested-trace-storage
initialize-trace nested-trace, 1/only-errors, 0x10/capacity, 0/visible
cdr env-head, out, nested-trace
# trace "=> " out " (match)" {{{
{
var should-trace?/eax: boolean <- should-trace? trace
compare should-trace?, 0/false
break-if-=
var error?/eax: boolean <- has-errors? trace
compare error?, 0/false
break-if-!=
var stream-storage: (stream byte 0x800)
var stream/ecx: (addr stream byte) <- address stream-storage
write stream, "=> "
clear-trace nested-trace
print-cell out, stream, nested-trace
write stream, " (match)"
trace trace, "eval", stream
}
# }}}
trace-higher trace
return
}
# otherwise recurse
var env-tail-storage: (handle cell)
var env-tail-ah/eax: (addr handle cell) <- address env-tail-storage
cdr env, env-tail-ah, trace
lookup-symbol sym, out, *env-tail-ah, globals, trace, screen-cell, keyboard-cell
trace-higher trace
# trace "=> " out " (recurse)" {{{
{
var should-trace?/eax: boolean <- should-trace? trace
compare should-trace?, 0/false
break-if-=
var error?/eax: boolean <- has-errors? trace
compare error?, 0/false
break-if-!=
var stream-storage: (stream byte 0x200)
var stream/ecx: (addr stream byte) <- address stream-storage
write stream, "=> "
var nested-trace-storage: trace
var nested-trace/edi: (addr trace) <- address nested-trace-storage
initialize-trace nested-trace, 1/only-errors, 0x10/capacity, 0/visible
print-cell out, stream, nested-trace
write stream, " (recurse)"
trace trace, "eval", stream
}
# }}}
}
fn test-lookup-symbol-in-env {
# tmp = (a . 3)
var val-storage: (handle cell)
var val-ah/ecx: (addr handle cell) <- address val-storage
new-integer val-ah, 3
var key-storage: (handle cell)
var key-ah/edx: (addr handle cell) <- address key-storage
new-symbol key-ah, "a"
var env-storage: (handle cell)
var env-ah/ebx: (addr handle cell) <- address env-storage
new-pair env-ah, *key-ah, *val-ah
# env = ((a . 3))
var nil-storage: (handle cell)
var nil-ah/ecx: (addr handle cell) <- address nil-storage
allocate-pair nil-ah
new-pair env-ah, *env-ah, *nil-ah
# lookup sym(a) in env tmp
var tmp-storage: (handle cell)
var tmp-ah/edx: (addr handle cell) <- address tmp-storage
new-symbol tmp-ah, "a"
var in/eax: (addr cell) <- lookup *tmp-ah
var trace-storage: trace
var trace/edi: (addr trace) <- address trace-storage
initialize-trace trace, 1/only-errors, 0x10/capacity, 0/visible
lookup-symbol in, tmp-ah, *env-ah, 0/no-globals, trace, 0/no-screen, 0/no-keyboard
var result/eax: (addr cell) <- lookup *tmp-ah
var result-type/edx: (addr int) <- get result, type
check-ints-equal *result-type, 1/number, "F - test-lookup-symbol-in-env/0"
var result-value-addr/eax: (addr float) <- get result, number-data
var result-value/eax: int <- convert *result-value-addr
check-ints-equal result-value, 3, "F - test-lookup-symbol-in-env/1"
}
fn test-lookup-symbol-in-globals {
var globals-storage: global-table
var globals/edi: (addr global-table) <- address globals-storage
initialize-globals globals
# env = nil
var nil-storage: (handle cell)
var nil-ah/ecx: (addr handle cell) <- address nil-storage
allocate-pair nil-ah
# lookup sym(a), env
var tmp-storage: (handle cell)
var tmp-ah/ebx: (addr handle cell) <- address tmp-storage
new-symbol tmp-ah, "+"
var in/eax: (addr cell) <- lookup *tmp-ah
var trace-storage: trace
var trace/esi: (addr trace) <- address trace-storage
initialize-trace trace, 1/only-errors, 0x10/capacity, 0/visible
lookup-symbol in, tmp-ah, *nil-ah, globals, trace, 0/no-screen, 0/no-keyboard
var result/eax: (addr cell) <- lookup *tmp-ah
var result-type/edx: (addr int) <- get result, type
check-ints-equal *result-type, 4/primitive-function, "F - test-lookup-symbol-in-globals/0"
var result-value/eax: (addr int) <- get result, index-data
check-ints-equal *result-value, 1/add, "F - test-lookup-symbol-in-globals/1"
}
fn mutate-binding name: (addr stream byte), val: (addr handle cell), env-h: (handle cell), globals: (addr global-table), trace: (addr trace) {
# trace name
{
var should-trace?/eax: boolean <- should-trace? trace
compare should-trace?, 0/false
break-if-=
var stream-storage: (stream byte 0x800) # pessimistically sized just for the large alist loaded from disk in `main`
var stream/ecx: (addr stream byte) <- address stream-storage
write stream, "bind "
rewind-stream name
write-stream stream, name
write stream, " to "
var nested-trace-storage: trace
var nested-trace/edi: (addr trace) <- address nested-trace-storage
initialize-trace nested-trace, 1/only-errors, 0x10/capacity, 0/visible
print-cell val, stream, nested-trace
write stream, " in "
var env-ah/eax: (addr handle cell) <- address env-h
clear-trace nested-trace
print-cell env-ah, stream, nested-trace
trace trace, "eval", stream
}
trace-lower trace
var _env/eax: (addr cell) <- lookup env-h
var env/ebx: (addr cell) <- copy _env
# if env is not a list, abort
{
var env-type/ecx: (addr int) <- get env, type
compare *env-type, 0/pair
break-if-=
error trace, "eval found a non-list environment"
trace-higher trace
return
}
# if env is nil, look in globals
{
var env-nil?/eax: boolean <- nil? env
compare env-nil?, 0/false
break-if-=
debug-print "b", 3/fg, 0/bg
mutate-binding-in-globals name, val, globals, trace
debug-print "x", 3/fg, 0/bg
trace-higher trace
# trace "=> " val " (global)" {{{
{
var should-trace?/eax: boolean <- should-trace? trace
compare should-trace?, 0/false
break-if-=
var error?/eax: boolean <- has-errors? trace
compare error?, 0/false
break-if-!=
var stream-storage: (stream byte 0x200)
var stream/ecx: (addr stream byte) <- address stream-storage
write stream, "=> "
var nested-trace-storage: trace
var nested-trace/edi: (addr trace) <- address nested-trace-storage
initialize-trace nested-trace, 1/only-errors, 0x10/capacity, 0/visible
print-cell val, stream, nested-trace
write stream, " (global)"
trace trace, "eval", stream
}
# }}}
debug-print "y", 3/fg, 0/bg
return
}
# check car
var env-head-storage: (handle cell)
var env-head-ah/eax: (addr handle cell) <- address env-head-storage
var nested-trace-storage: trace
var nested-trace/edi: (addr trace) <- address nested-trace-storage
initialize-trace nested-trace, 1/only-errors, 0x10/capacity, 0/visible
car env, env-head-ah, nested-trace
var _env-head/eax: (addr cell) <- lookup *env-head-ah
var env-head/ecx: (addr cell) <- copy _env-head
# if car is not a list, abort
{
var env-head-type/eax: (addr int) <- get env-head, type
compare *env-head-type, 0/pair
break-if-=
error trace, "environment is not a list of (key . value) pairs"
trace-higher trace
return
}
# check key
var curr-key-storage: (handle cell)
var curr-key-ah/eax: (addr handle cell) <- address curr-key-storage
car env-head, curr-key-ah, trace
var curr-key/eax: (addr cell) <- lookup *curr-key-ah
# if key is not a symbol, abort
{
var curr-key-type/eax: (addr int) <- get curr-key, type
compare *curr-key-type, 2/symbol
break-if-=
error trace, "environment contains a binding for a non-symbol"
trace-higher trace
return
}
# if key matches name, return val
var curr-key-data-ah/eax: (addr handle stream byte) <- get curr-key, text-data
var curr-key-data/eax: (addr stream byte) <- lookup *curr-key-data-ah
var match?/eax: boolean <- streams-data-equal? curr-key-data, name
compare match?, 0/false
{
break-if-=
var dest/eax: (addr handle cell) <- get env-head, right
copy-object val, dest
trace-text trace, "eval", "=> done"
trace-higher trace
return
}
# otherwise recurse
var env-tail-storage: (handle cell)
var env-tail-ah/eax: (addr handle cell) <- address env-tail-storage
cdr env, env-tail-ah, trace
mutate-binding name, val, *env-tail-ah, globals, trace
trace-higher trace
}
fn car _in: (addr cell), out: (addr handle cell), trace: (addr trace) {
trace-text trace, "eval", "car"
trace-lower trace
var in/eax: (addr cell) <- copy _in
# if in is not a list, abort
{
var in-type/ecx: (addr int) <- get in, type
compare *in-type, 0/pair
break-if-=
error trace, "car on a non-list"
trace-higher trace
return
}
# if in is nil, abort
{
var in-nil?/eax: boolean <- nil? in
compare in-nil?, 0/false
break-if-=
error trace, "car on nil"
trace-higher trace
return
}
var in-left/eax: (addr handle cell) <- get in, left
copy-object in-left, out
trace-higher trace
return
}
fn cdr _in: (addr cell), out: (addr handle cell), trace: (addr trace) {
trace-text trace, "eval", "cdr"
trace-lower trace
var in/eax: (addr cell) <- copy _in
# if in is not a list, abort
{
var in-type/ecx: (addr int) <- get in, type
compare *in-type, 0/pair
break-if-=
error trace, "car on a non-list"
trace-higher trace
return
}
# if in is nil, abort
{
var in-nil?/eax: boolean <- nil? in
compare in-nil?, 0/false
break-if-=
error trace, "car on nil"
trace-higher trace
return
}
var in-right/eax: (addr handle cell) <- get in, right
copy-object in-right, out
trace-higher trace
return
}
fn cell-isomorphic? _a: (addr cell), _b: (addr cell), trace: (addr trace) -> _/eax: boolean {
trace-text trace, "eval", "cell-isomorphic?"
trace-lower trace
var a/esi: (addr cell) <- copy _a
var b/edi: (addr cell) <- copy _b
# if types don't match, return false
var a-type-addr/eax: (addr int) <- get a, type
var b-type-addr/ecx: (addr int) <- get b, type
var b-type/ecx: int <- copy *b-type-addr
compare b-type, *a-type-addr
{
break-if-=
trace-higher trace
trace-text trace, "eval", "=> false (type)"
return 0/false
}
# if types are number, compare number-data
# TODO: exactly comparing floats is a bad idea
compare b-type, 1/number
{
break-if-!=
var a-val-addr/eax: (addr float) <- get a, number-data
var b-val-addr/ecx: (addr float) <- get b, number-data
var a-val/xmm0: float <- copy *a-val-addr
compare a-val, *b-val-addr
{
break-if-=
trace-higher trace
trace-text trace, "eval", "=> false (numbers)"
return 0/false
}
trace-higher trace
trace-text trace, "eval", "=> true (numbers)"
return 1/true
}
$cell-isomorphic?:text-data: {
{
compare b-type, 2/symbol
break-if-=
compare b-type, 3/stream
break-if-=
break $cell-isomorphic?:text-data
}
var b-val-ah/eax: (addr handle stream byte) <- get b, text-data
var _b-val/eax: (addr stream byte) <- lookup *b-val-ah
var b-val/ecx: (addr stream byte) <- copy _b-val
var a-val-ah/eax: (addr handle stream byte) <- get a, text-data
var a-val/eax: (addr stream byte) <- lookup *a-val-ah
var tmp-array: (handle array byte)
var tmp-ah/edx: (addr handle array byte) <- address tmp-array
rewind-stream a-val
stream-to-array a-val, tmp-ah
var tmp/eax: (addr array byte) <- lookup *tmp-ah
var match?/eax: boolean <- stream-data-equal? b-val, tmp
trace-higher trace
{
compare match?, 0/false
break-if-=
trace-text trace, "eval", "=> true (symbols)"
}
{
compare match?, 0/false
break-if-!=
trace-text trace, "eval", "=> false (symbols)"
}
return match?
}
# if objects are primitive functions, compare index-data
compare b-type, 4/primitive
{
break-if-!=
var a-val-addr/eax: (addr int) <- get a, index-data
var b-val-addr/ecx: (addr int) <- get b, index-data
var a-val/eax: int <- copy *a-val-addr
compare a-val, *b-val-addr
{
break-if-=
trace-higher trace
trace-text trace, "eval", "=> false (primitives)"
return 0/false
}
trace-higher trace
trace-text trace, "eval", "=> true (primitives)"
return 1/true
}
# if objects are screens, check if they're the same object
compare b-type, 5/screen
{
break-if-!=
var a-val-addr/eax: (addr handle screen) <- get a, screen-data
var b-val-addr/ecx: (addr handle screen) <- get b, screen-data
var result/eax: boolean <- handle-equal? *a-val-addr, *b-val-addr
compare result, 0/false
return result
}
# if objects are keyboards, check if they have the same contents
compare b-type, 6/keyboard
{
break-if-!=
var a-val-addr/ecx: (addr handle gap-buffer) <- get a, keyboard-data
var _a/eax: (addr gap-buffer) <- lookup *a-val-addr
var a/ecx: (addr gap-buffer) <- copy _a
var b-val-addr/eax: (addr handle gap-buffer) <- get b, keyboard-data
var b/eax: (addr gap-buffer) <- lookup *b-val-addr
var result/eax: boolean <- gap-buffers-equal? a, b
return result
}
# if a is nil, b should be nil
{
# (assumes nil? returns 0 or 1)
var _b-nil?/eax: boolean <- nil? b
var b-nil?/ecx: boolean <- copy _b-nil?
var a-nil?/eax: boolean <- nil? a
# a == nil and b == nil => return true
{
compare a-nil?, 0/false
break-if-=
compare b-nil?, 0/false
break-if-=
trace-higher trace
trace-text trace, "eval", "=> true (nils)"
return 1/true
}
# a == nil => return false
{
compare a-nil?, 0/false
break-if-=
trace-higher trace
trace-text trace, "eval", "=> false (b != nil)"
return 0/false
}
# b == nil => return false
{
compare b-nil?, 0/false
break-if-=
trace-higher trace
trace-text trace, "eval", "=> false (a != nil)"
return 0/false
}
}
# a and b are pairs
var a-tmp-storage: (handle cell)
var a-tmp-ah/edx: (addr handle cell) <- address a-tmp-storage
var b-tmp-storage: (handle cell)
var b-tmp-ah/ebx: (addr handle cell) <- address b-tmp-storage
# if cars aren't equal, return false
car a, a-tmp-ah, trace
car b, b-tmp-ah, trace
{
var _a-tmp/eax: (addr cell) <- lookup *a-tmp-ah
var a-tmp/ecx: (addr cell) <- copy _a-tmp
var b-tmp/eax: (addr cell) <- lookup *b-tmp-ah
var result/eax: boolean <- cell-isomorphic? a-tmp, b-tmp, trace
compare result, 0/false
break-if-!=
trace-higher trace
trace-text trace, "eval", "=> false (car mismatch)"
return 0/false
}
# recurse on cdrs
cdr a, a-tmp-ah, trace
cdr b, b-tmp-ah, trace
var _a-tmp/eax: (addr cell) <- lookup *a-tmp-ah
var a-tmp/ecx: (addr cell) <- copy _a-tmp
var b-tmp/eax: (addr cell) <- lookup *b-tmp-ah
var result/eax: boolean <- cell-isomorphic? a-tmp, b-tmp, trace
trace-higher trace
return result
}
fn fn? _x: (addr cell) -> _/eax: boolean {
var x/esi: (addr cell) <- copy _x
var type/eax: (addr int) <- get x, type
compare *type, 2/symbol
{
break-if-=
return 0/false
}
var contents-ah/eax: (addr handle stream byte) <- get x, text-data
var contents/eax: (addr stream byte) <- lookup *contents-ah
var result/eax: boolean <- stream-data-equal? contents, "fn"
return result
}
fn litfn? _x: (addr cell) -> _/eax: boolean {
var x/esi: (addr cell) <- copy _x
var type/eax: (addr int) <- get x, type
compare *type, 2/symbol
{
break-if-=
return 0/false
}
var contents-ah/eax: (addr handle stream byte) <- get x, text-data
var contents/eax: (addr stream byte) <- lookup *contents-ah
var result/eax: boolean <- stream-data-equal? contents, "litfn"
return result
}
fn litmac? _x: (addr cell) -> _/eax: boolean {
var x/esi: (addr cell) <- copy _x
var type/eax: (addr int) <- get x, type
compare *type, 2/symbol
{
break-if-=
return 0/false
}
var contents-ah/eax: (addr handle stream byte) <- get x, text-data
var contents/eax: (addr stream byte) <- lookup *contents-ah
var result/eax: boolean <- stream-data-equal? contents, "litmac"
return result
}
fn test-evaluate-is-well-behaved {
var t-storage: trace
var t/esi: (addr trace) <- address t-storage
initialize-trace t, 0x100/max-depth, 0x10/capacity, 0/visible # we don't use trace UI
# env = nil
var env-storage: (handle cell)
var env-ah/ecx: (addr handle cell) <- address env-storage
allocate-pair env-ah
# eval sym(a), nil env
var tmp-storage: (handle cell)
var tmp-ah/edx: (addr handle cell) <- address tmp-storage
new-symbol tmp-ah, "a"
evaluate tmp-ah, tmp-ah, *env-ah, 0/no-globals, t, 0/no-screen, 0/no-keyboard, 0/call-number
# doesn't die
check-trace-contains t, "error", "unbound symbol: a", "F - test-evaluate-is-well-behaved"
}
fn test-evaluate-number {
# env = nil
var env-storage: (handle cell)
var env-ah/ecx: (addr handle cell) <- address env-storage
allocate-pair env-ah
# tmp = 3
var tmp-storage: (handle cell)
var tmp-ah/edx: (addr handle cell) <- address tmp-storage
new-integer tmp-ah, 3
var trace-storage: trace
var trace/edi: (addr trace) <- address trace-storage
initialize-trace trace, 1/only-errors, 0x10/capacity, 0/visible
evaluate tmp-ah, tmp-ah, *env-ah, 0/no-globals, trace, 0/no-screen, 0/no-keyboard, 0/call-number
#
var result/eax: (addr cell) <- lookup *tmp-ah
var result-type/edx: (addr int) <- get result, type
check-ints-equal *result-type, 1/number, "F - test-evaluate-number/0"
var result-value-addr/eax: (addr float) <- get result, number-data
var result-value/eax: int <- convert *result-value-addr
check-ints-equal result-value, 3, "F - test-evaluate-number/1"
}
fn test-evaluate-symbol {
# tmp = (a . 3)
var val-storage: (handle cell)
var val-ah/ecx: (addr handle cell) <- address val-storage
new-integer val-ah, 3
var key-storage: (handle cell)
var key-ah/edx: (addr handle cell) <- address key-storage
new-symbol key-ah, "a"
var env-storage: (handle cell)
var env-ah/ebx: (addr handle cell) <- address env-storage
new-pair env-ah, *key-ah, *val-ah
# env = ((a . 3))
var nil-storage: (handle cell)
var nil-ah/ecx: (addr handle cell) <- address nil-storage
allocate-pair nil-ah
new-pair env-ah, *env-ah, *nil-ah
# eval sym(a), env
var tmp-storage: (handle cell)
var tmp-ah/edx: (addr handle cell) <- address tmp-storage
new-symbol tmp-ah, "a"
var trace-storage: trace
var trace/edi: (addr trace) <- address trace-storage
initialize-trace trace, 1/only-errors, 0x10/capacity, 0/visible
evaluate tmp-ah, tmp-ah, *env-ah, 0/no-globals, trace, 0/no-screen, 0/no-keyboard, 0/call-number
var result/eax: (addr cell) <- lookup *tmp-ah
var result-type/edx: (addr int) <- get result, type
check-ints-equal *result-type, 1/number, "F - test-evaluate-symbol/0"
var result-value-addr/eax: (addr float) <- get result, number-data
var result-value/eax: int <- convert *result-value-addr
check-ints-equal result-value, 3, "F - test-evaluate-symbol/1"
}
fn test-evaluate-quote {
# env = nil
var nil-storage: (handle cell)
var nil-ah/ecx: (addr handle cell) <- address nil-storage
allocate-pair nil-ah
# eval `a, env
var tmp-storage: (handle cell)
var tmp-ah/edx: (addr handle cell) <- address tmp-storage
new-symbol tmp-ah, "'"
var tmp2-storage: (handle cell)
var tmp2-ah/ebx: (addr handle cell) <- address tmp2-storage
new-symbol tmp2-ah, "a"
new-pair tmp-ah, *tmp-ah, *tmp2-ah
var trace-storage: trace
var trace/edi: (addr trace) <- address trace-storage
initialize-trace trace, 1/only-errors, 0x10/capacity, 0/visible
evaluate tmp-ah, tmp-ah, *nil-ah, 0/no-globals, trace, 0/no-screen, 0/no-keyboard, 0/call-number
var result/eax: (addr cell) <- lookup *tmp-ah
var result-type/edx: (addr int) <- get result, type
check-ints-equal *result-type, 2/symbol, "F - test-evaluate-quote/0"
var sym?/eax: boolean <- symbol-equal? result, "a"
check sym?, "F - test-evaluate-quote/1"
}
fn test-evaluate-primitive-function {
var globals-storage: global-table
var globals/edi: (addr global-table) <- address globals-storage
initialize-globals globals
var nil-storage: (handle cell)
var nil-ah/ecx: (addr handle cell) <- address nil-storage
allocate-pair nil-ah
var add-storage: (handle cell)
var add-ah/ebx: (addr handle cell) <- address add-storage
new-symbol add-ah, "+"
# eval +, nil env
var tmp-storage: (handle cell)
var tmp-ah/esi: (addr handle cell) <- address tmp-storage
var trace-storage: trace
var trace/edx: (addr trace) <- address trace-storage
initialize-trace trace, 1/only-errors, 0x10/capacity, 0/visible
evaluate add-ah, tmp-ah, *nil-ah, globals, trace, 0/no-screen, 0/no-keyboard, 0/call-number
#
var result/eax: (addr cell) <- lookup *tmp-ah
var result-type/edx: (addr int) <- get result, type
check-ints-equal *result-type, 4/primitive-function, "F - test-evaluate-primitive-function/0"
var result-value/eax: (addr int) <- get result, index-data
check-ints-equal *result-value, 1/add, "F - test-evaluate-primitive-function/1"
}
fn test-evaluate-primitive-function-call {
var t-storage: trace
var t/edi: (addr trace) <- address t-storage
initialize-trace t, 0x100/max-depth, 0x100/capacity, 0/visible # we don't use trace UI
#
var nil-storage: (handle cell)
var nil-ah/ecx: (addr handle cell) <- address nil-storage
allocate-pair nil-ah
var one-storage: (handle cell)
var one-ah/edx: (addr handle cell) <- address one-storage
new-integer one-ah, 1
var add-storage: (handle cell)
var add-ah/ebx: (addr handle cell) <- address add-storage
new-symbol add-ah, "+"
# input is (+ 1 1)
var tmp-storage: (handle cell)
var tmp-ah/esi: (addr handle cell) <- address tmp-storage
new-pair tmp-ah, *one-ah, *nil-ah
new-pair tmp-ah, *one-ah, *tmp-ah
new-pair tmp-ah, *add-ah, *tmp-ah
#? dump-cell tmp-ah
#
var globals-storage: global-table
var globals/edx: (addr global-table) <- address globals-storage
initialize-globals globals
#
evaluate tmp-ah, tmp-ah, *nil-ah, globals, t, 0/no-screen, 0/no-keyboard, 0/call-number
#? dump-trace t
#
var result/eax: (addr cell) <- lookup *tmp-ah
var result-type/edx: (addr int) <- get result, type
check-ints-equal *result-type, 1/number, "F - test-evaluate-primitive-function-call/0"
var result-value-addr/eax: (addr float) <- get result, number-data
var result-value/eax: int <- convert *result-value-addr
check-ints-equal result-value, 2, "F - test-evaluate-primitive-function-call/1"
}
fn test-evaluate-backquote {
# env = nil
var nil-storage: (handle cell)
var nil-ah/ecx: (addr handle cell) <- address nil-storage
allocate-pair nil-ah
# eval `a, env
var tmp-storage: (handle cell)
var tmp-ah/edx: (addr handle cell) <- address tmp-storage
new-symbol tmp-ah, "`"
var tmp2-storage: (handle cell)
var tmp2-ah/ebx: (addr handle cell) <- address tmp2-storage
new-symbol tmp2-ah, "a"
new-pair tmp-ah, *tmp-ah, *tmp2-ah
clear-object tmp2-ah
var trace-storage: trace
var trace/edi: (addr trace) <- address trace-storage
initialize-trace trace, 1/only-errors, 0x10/capacity, 0/visible
evaluate tmp-ah, tmp2-ah, *nil-ah, 0/no-globals, trace, 0/no-screen, 0/no-keyboard, 0/call-number
var result/eax: (addr cell) <- lookup *tmp2-ah
var result-type/edx: (addr int) <- get result, type
check-ints-equal *result-type, 2/symbol, "F - test-evaluate-backquote/0"
var sym?/eax: boolean <- symbol-equal? result, "a"
check sym?, "F - test-evaluate-backquote/1"
}
fn evaluate-backquote _in-ah: (addr handle cell), _out-ah: (addr handle cell), env-h: (handle cell), globals: (addr global-table), trace: (addr trace), screen-cell: (addr handle cell), keyboard-cell: (addr handle cell), call-number: int {
# stack overflow? # disable when enabling Really-debug-print
#? dump-cell-from-cursor-over-full-screen _in-ah
check-stack
{
var screen-cell/eax: (addr handle cell) <- copy screen-cell
compare screen-cell, 0
break-if-=
var screen-cell-addr/eax: (addr cell) <- lookup *screen-cell
compare screen-cell-addr, 0
break-if-=
# if screen-cell exists, we're probably not in a test
show-stack-state
}
# errors? skip
{
var error?/eax: boolean <- has-errors? trace
compare error?, 0/false
break-if-=
return
}
trace-lower trace
var in-ah/esi: (addr handle cell) <- copy _in-ah
var in/eax: (addr cell) <- lookup *in-ah
{
var nil?/eax: boolean <- nil? in
compare nil?, 0/false
break-if-=
# nil is a literal
trace-text trace, "eval", "backquote nil"
copy-object _in-ah, _out-ah
trace-higher trace
return
}
var in-type/ecx: (addr int) <- get in, type
compare *in-type, 0/pair
{
break-if-=
# copy non-pairs directly
# TODO: streams might need to be copied
trace-text trace, "eval", "backquote atom"
copy-object _in-ah, _out-ah
trace-higher trace
return
}
# 'in' is a pair
debug-print "()", 4/fg, 0/bg
var in-ah/esi: (addr handle cell) <- copy _in-ah
var _in/eax: (addr cell) <- lookup *in-ah
var in/ebx: (addr cell) <- copy _in
var in-left-ah/ecx: (addr handle cell) <- get in, left
debug-print "10", 4/fg, 0/bg
# check for unquote
$macroexpand-iter:unquote: {
var in-left/eax: (addr cell) <- lookup *in-left-ah
var unquote?/eax: boolean <- symbol-equal? in-left, ","
compare unquote?, 0/false
break-if-=
trace-text trace, "eval", "unquote"
var rest-ah/eax: (addr handle cell) <- get in, right
increment call-number
debug-print ",", 3/fg, 0/bg
evaluate rest-ah, _out-ah, env-h, globals, trace, screen-cell, keyboard-cell, call-number
debug-print ",)", 3/fg, 0/bg
trace-higher trace
return
}
# check for unquote-splice in in-left
debug-print "11", 4/fg, 0/bg
var out-ah/edi: (addr handle cell) <- copy _out-ah
$macroexpand-iter:unquote-splice: {
#? dump-cell-from-cursor-over-full-screen in-left-ah
var in-left/eax: (addr cell) <- lookup *in-left-ah
{
debug-print "12", 4/fg, 0/bg
{
var in-left-is-nil?/eax: boolean <- nil? in-left
compare in-left-is-nil?, 0/false
}
break-if-!= $macroexpand-iter:unquote-splice
var in-left-type/ecx: (addr int) <- get in-left, type
debug-print "13", 4/fg, 0/bg
compare *in-left-type, 0/pair
break-if-!= $macroexpand-iter:unquote-splice
var in-left-left-ah/eax: (addr handle cell) <- get in-left, left
debug-print "14", 4/fg, 0/bg
var in-left-left/eax: (addr cell) <- lookup *in-left-left-ah
debug-print "15", 4/fg, 0/bg
var in-left-left-type/ecx: (addr int) <- get in-left-left, type
var left-is-unquote-splice?/eax: boolean <- symbol-equal? in-left-left, ",@"
debug-print "16", 4/fg, 0/bg
compare left-is-unquote-splice?, 0/false
}
break-if-=
debug-print "17", 4/fg, 0/bg
trace-text trace, "eval", "unquote-splice"
var in-unquote-payload-ah/eax: (addr handle cell) <- get in-left, right
increment call-number
evaluate in-unquote-payload-ah, out-ah, env-h, globals, trace, screen-cell, keyboard-cell, call-number
# errors? skip
{
var error?/eax: boolean <- has-errors? trace
compare error?, 0/false
break-if-=
trace-higher trace
return
}
# while (*out-ah != null) out-ah = cdr(out-ah)
{
var out/eax: (addr cell) <- lookup *out-ah
{
var done?/eax: boolean <- nil? out
compare done?, 0/false
}
break-if-!=
out-ah <- get out, right
loop
}
# append result of in-right
var in-right-ah/ecx: (addr handle cell) <- get in, right
evaluate-backquote in-right-ah, out-ah, env-h, globals, trace, screen-cell, keyboard-cell, call-number
trace-higher trace
return
}
debug-print "19", 4/fg, 0/bg
# otherwise continue copying
trace-text trace, "eval", "backquote: copy"
var out-ah/edi: (addr handle cell) <- copy _out-ah
allocate-pair out-ah
debug-print "20", 7/fg, 0/bg
#? dump-cell-from-cursor-over-full-screen out-ah
var out/eax: (addr cell) <- lookup *out-ah
var out-left-ah/edx: (addr handle cell) <- get out, left
debug-print "`(l", 3/fg, 0/bg
evaluate-backquote in-left-ah, out-left-ah, env-h, globals, trace, screen-cell, keyboard-cell, call-number
debug-print "`r)", 3/fg, 0/bg
# errors? skip
{
var error?/eax: boolean <- has-errors? trace
compare error?, 0/false
break-if-=
trace-higher trace
return
}
var in-right-ah/ecx: (addr handle cell) <- get in, right
var out-right-ah/edx: (addr handle cell) <- get out, right
debug-print "`r(", 3/fg, 0/bg
evaluate-backquote in-right-ah, out-right-ah, env-h, globals, trace, screen-cell, keyboard-cell, call-number
debug-print "`r)", 3/fg, 0/bg
trace-higher trace
}
fn test-evaluate-backquote-list {
var nil-storage: (handle cell)
var nil-ah/ecx: (addr handle cell) <- address nil-storage
allocate-pair nil-ah
var backquote-storage: (handle cell)
var backquote-ah/edx: (addr handle cell) <- address backquote-storage
new-symbol backquote-ah, "`"
# input is `(a b)
var a-storage: (handle cell)
var a-ah/ebx: (addr handle cell) <- address a-storage
new-symbol a-ah, "a"
var b-storage: (handle cell)
var b-ah/esi: (addr handle cell) <- address b-storage
new-symbol b-ah, "b"
var tmp-storage: (handle cell)
var tmp-ah/eax: (addr handle cell) <- address tmp-storage
new-pair tmp-ah, *b-ah, *nil-ah
new-pair tmp-ah, *a-ah, *tmp-ah
new-pair tmp-ah, *backquote-ah, *tmp-ah
#
var trace-storage: trace
var trace/edi: (addr trace) <- address trace-storage
initialize-trace trace, 1/only-errors, 0x10/capacity, 0/visible
evaluate tmp-ah, tmp-ah, *nil-ah, 0/no-globals, trace, 0/no-screen, 0/no-keyboard, 0/call-number
# result is (a b)
var result/eax: (addr cell) <- lookup *tmp-ah
{
var result-type/eax: (addr int) <- get result, type
check-ints-equal *result-type, 0/pair, "F - test-evaluate-backquote-list/0"
}
{
var a1-ah/eax: (addr handle cell) <- get result, left
var a1/eax: (addr cell) <- lookup *a1-ah
var check1/eax: boolean <- symbol-equal? a1, "a"
check check1, "F - test-evaluate-backquote-list/1"
}
var rest-ah/eax: (addr handle cell) <- get result, right
var rest/eax: (addr cell) <- lookup *rest-ah
{
var a2-ah/eax: (addr handle cell) <- get rest, left
var a2/eax: (addr cell) <- lookup *a2-ah
var check2/eax: boolean <- symbol-equal? a2, "b"
check check2, "F - test-evaluate-backquote-list/2"
}
var rest-ah/eax: (addr handle cell) <- get rest, right
var rest/eax: (addr cell) <- lookup *rest-ah
var check3/eax: boolean <- nil? rest
check check3, "F - test-evaluate-backquote-list/3"
}
fn test-evaluate-backquote-list-with-unquote {
var nil-h: (handle cell)
var nil-ah/eax: (addr handle cell) <- address nil-h
allocate-pair nil-ah
var backquote-h: (handle cell)
var backquote-ah/eax: (addr handle cell) <- address backquote-h
new-symbol backquote-ah, "`"
var unquote-h: (handle cell)
var unquote-ah/eax: (addr handle cell) <- address unquote-h
new-symbol unquote-ah, ","
var a-h: (handle cell)
var a-ah/eax: (addr handle cell) <- address a-h
new-symbol a-ah, "a"
var b-h: (handle cell)
var b-ah/eax: (addr handle cell) <- address b-h
new-symbol b-ah, "b"
# env = ((b . 3))
var val-h: (handle cell)
var val-ah/eax: (addr handle cell) <- address val-h
new-integer val-ah, 3
var env-h: (handle cell)
var env-ah/eax: (addr handle cell) <- address env-h
new-pair env-ah, b-h, val-h
new-pair env-ah, env-h, nil-h
# input is `(a ,b)
var tmp-h: (handle cell)
var tmp-ah/eax: (addr handle cell) <- address tmp-h
# tmp = cons(unquote, b)
new-pair tmp-ah, unquote-h, b-h
# tmp = cons(tmp, nil)
new-pair tmp-ah, tmp-h, nil-h
# tmp = cons(a, tmp)
new-pair tmp-ah, a-h, tmp-h
# tmp = cons(backquote, tmp)
new-pair tmp-ah, backquote-h, tmp-h
#
var trace-storage: trace
var trace/edi: (addr trace) <- address trace-storage
initialize-trace trace, 1/only-errors, 0x10/capacity, 0/visible
evaluate tmp-ah, tmp-ah, env-h, 0/no-globals, trace, 0/no-screen, 0/no-keyboard, 0/call-number
# result is (a 3)
var result/eax: (addr cell) <- lookup *tmp-ah
{
var result-type/eax: (addr int) <- get result, type
check-ints-equal *result-type, 0/pair, "F - test-evaluate-backquote-list-with-unquote/0"
}
{
var a1-ah/eax: (addr handle cell) <- get result, left
var a1/eax: (addr cell) <- lookup *a1-ah
var check1/eax: boolean <- symbol-equal? a1, "a"
check check1, "F - test-evaluate-backquote-list-with-unquote/1"
}
var rest-ah/eax: (addr handle cell) <- get result, right
var rest/eax: (addr cell) <- lookup *rest-ah
{
var a2-ah/eax: (addr handle cell) <- get rest, left
var a2/eax: (addr cell) <- lookup *a2-ah
var a2-value-addr/eax: (addr float) <- get a2, number-data
var a2-value/eax: int <- convert *a2-value-addr
check-ints-equal a2-value, 3, "F - test-evaluate-backquote-list-with-unquote/2"
}
var rest-ah/eax: (addr handle cell) <- get rest, right
var rest/eax: (addr cell) <- lookup *rest-ah
var check3/eax: boolean <- nil? rest
check check3, "F - test-evaluate-backquote-list-with-unquote/3"
}
fn test-evaluate-backquote-list-with-unquote-splice {
var nil-h: (handle cell)
var nil-ah/eax: (addr handle cell) <- address nil-h
allocate-pair nil-ah
var backquote-h: (handle cell)
var backquote-ah/eax: (addr handle cell) <- address backquote-h
new-symbol backquote-ah, "`"
var unquote-splice-h: (handle cell)
var unquote-splice-ah/eax: (addr handle cell) <- address unquote-splice-h
new-symbol unquote-splice-ah, ",@"
var a-h: (handle cell)
var a-ah/eax: (addr handle cell) <- address a-h
new-symbol a-ah, "a"
var b-h: (handle cell)
var b-ah/eax: (addr handle cell) <- address b-h
new-symbol b-ah, "b"
# env = ((b . (a 3)))
var val-h: (handle cell)
var val-ah/eax: (addr handle cell) <- address val-h
new-integer val-ah, 3
new-pair val-ah, val-h, nil-h
new-pair val-ah, a-h, val-h
var env-h: (handle cell)
var env-ah/eax: (addr handle cell) <- address env-h
new-pair env-ah, b-h, val-h
new-pair env-ah, env-h, nil-h
# input is `(a ,@b b)
var tmp-h: (handle cell)
var tmp-ah/eax: (addr handle cell) <- address tmp-h
# tmp = cons(b, nil)
new-pair tmp-ah, b-h, nil-h
# tmp2 = cons(unquote-splice, b)
var tmp2-h: (handle cell)
var tmp2-ah/ecx: (addr handle cell) <- address tmp2-h
new-pair tmp2-ah, unquote-splice-h, b-h
# tmp = cons(tmp2, tmp)
new-pair tmp-ah, tmp2-h, tmp-h
# tmp = cons(a, tmp)
new-pair tmp-ah, a-h, tmp-h
# tmp = cons(backquote, tmp)
new-pair tmp-ah, backquote-h, tmp-h
#? dump-cell-from-cursor-over-full-screen tmp-ah
#
var trace-storage: trace
var trace/edi: (addr trace) <- address trace-storage
initialize-trace trace, 1/only-errors, 0x10/capacity, 0/visible
evaluate tmp-ah, tmp-ah, env-h, 0/no-globals, trace, 0/no-screen, 0/no-keyboard, 0/call-number
# result is (a a 3 b)
#? dump-cell-from-cursor-over-full-screen tmp-ah
var result/eax: (addr cell) <- lookup *tmp-ah
{
var result-type/eax: (addr int) <- get result, type
check-ints-equal *result-type, 0/pair, "F - test-evaluate-backquote-list-with-unquote-splice/0"
}
{
var a1-ah/eax: (addr handle cell) <- get result, left
var a1/eax: (addr cell) <- lookup *a1-ah
var check1/eax: boolean <- symbol-equal? a1, "a"
check check1, "F - test-evaluate-backquote-list-with-unquote-splice/1"
}
var rest-ah/eax: (addr handle cell) <- get result, right
var rest/eax: (addr cell) <- lookup *rest-ah
{
var a2-ah/eax: (addr handle cell) <- get rest, left
var a2/eax: (addr cell) <- lookup *a2-ah
var check2/eax: boolean <- symbol-equal? a2, "a"
check check2, "F - test-evaluate-backquote-list-with-unquote-splice/2"
}
var rest-ah/eax: (addr handle cell) <- get rest, right
var rest/eax: (addr cell) <- lookup *rest-ah
{
var a3-ah/eax: (addr handle cell) <- get rest, left
var a3/eax: (addr cell) <- lookup *a3-ah
var a3-value-addr/eax: (addr float) <- get a3, number-data
var a3-value/eax: int <- convert *a3-value-addr
check-ints-equal a3-value, 3, "F - test-evaluate-backquote-list-with-unquote-splice/3"
}
var rest-ah/eax: (addr handle cell) <- get rest, right
var rest/eax: (addr cell) <- lookup *rest-ah
{
var a4-ah/eax: (addr handle cell) <- get rest, left
var a4/eax: (addr cell) <- lookup *a4-ah
var check4/eax: boolean <- symbol-equal? a4, "b"
check check4, "F - test-evaluate-backquote-list-with-unquote-splice/4"
}
var rest-ah/eax: (addr handle cell) <- get rest, right
var rest/eax: (addr cell) <- lookup *rest-ah
var check5/eax: boolean <- nil? rest
check check5, "F - test-evaluate-backquote-list-with-unquote-splice/5"
}
|