about summary refs log blame commit diff stats
path: root/010vm.cc
blob: c1cec40638fe95c376884bab5daf18f7135eb269 (plain) (tree)
dcf743
# Some unsafe methods not intended to be used directly in SubX, only through
# Mu after proper type-checking.

== code

stream-empty?:  # s: (addr stream _) -> result/eax: boolean
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    # . save registers
    51/push-ecx
    56/push-esi
    # result = false
    b8/copy-to-eax 0/imm32/false
    # esi = s
    8b/-> *(ebp+8) 6/r32/esi
    # return s->read >= s->write
    8b/-> *esi 1/r32/ecx
    39/compare-with *(esi+4) 1/r32/ecx
    0f 9d/set-if->= %al
$stream-empty?:end:
    # . restore registers
    5e/pop-to-esi
    59/pop-to-ecx
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

stream-full?:  # s: (addr stream _) -> result/eax: boolean
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    # . save registers
    51/push-ecx
    56/push-esi
    # result = false
    b8/copy-to-eax 0/imm32/false
    # esi = s
    8b/-> *(ebp+8) 6/r32/esi
    # return s->write >= s->size
    8b/-> *(esi+8) 1/r32/ecx
    39/compare-with *esi 1/r32/ecx
    0f 9d/set-if->= %al
$stream-full?:end:
    # . restore registers
    5e/pop-to-esi
    59/pop-to-ecx
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

write-to-stream:  # s: (addr stream _), in: (addr byte), n: int
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    # . save registers
    50/push-eax
    51/push-ecx
    52/push-edx
    53/push-ebx
    57/push-edi
    # edi = s
    8b/-> *(ebp+8) 7/r32/edi
    # var swrite/edx: int = s->write
    8b/-> *edi 2/r32/edx
    # if (swrite + n > s->size) abort
    8b/-> *(ebp+0x10) 1/r32/ecx
    01/add-to %ecx 2/r32/edx
    3b/compare 1/r32/ecx *(edi+8)
    0f 8f/jump-if-> $write-to-stream:abort/disp32
    # var out/edx: (addr byte) = s->data + s->write
    8d/copy-address *(edi+edx+0xc) 2/r32/edx
    # var outend/ebx: (addr byte) = out + n
    8b/-> *(ebp+0x10) 3/r32/ebx
    8d/copy-address *(edx+ebx) 3/r32/ebx
    # eax = in
    8b/-> *(ebp+0xc) 0/r32/eax
    # var inend/ecx: (addr byte) = in + n
    8b/-> *(ebp+0x10) 1/r32/ecx
    8d/copy-address *(eax+ecx) 1/r32/ecx
    #
    (_append-4  %edx %ebx  %eax %ecx)  # => eax
    # s->write += n
    8b/-> *(ebp+0x10) 1/r32/ecx
    01/add-to *edi 1/r32/ecx
$write-to-stream:end:
    # . restore registers
    5f/pop-to-edi
    5b/pop-to-ebx
    5a/pop-to-edx
    59/pop-to-ecx
    58/pop-to-eax
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

$write-to-stream:abort:
    (abort "write-to-stream: stream full")
    # never gets here

read-from-stream:  # s: (addr stream _), out: (addr byte), n: int
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    # . save registers
    50/push-eax
    51/push-ecx
    52/push-edx
    53/push-ebx
    56/push-esi
    # esi = s
    8b/-> *(ebp+8) 6/r32/esi
    # var sread/edx: int = s->read
    8b/-> *(esi+4) 2/r32/edx
    # if (sread + n > s->write) abort
    8b/-> *(ebp+0x10) 1/r32/ecx
    01/add-to %ecx 2/r32/edx
    3b/compare 1/r32/ecx *esi
    0f 8f/jump-if-> $read-from-stream:abort/disp32
    # var in/edx: (addr byte) = s->data + s->read
    8d/copy-address *(esi+edx+0xc) 2/r32/edx
    # var inend/ebx: (addr byte) = in + n
    8b/-> *(ebp+0x10) 3/r32/ebx
    8d/copy-address *(edx+ebx) 3/r32/ebx
    # eax = out
    8b/-> *(ebp+0xc) 0/r32/eax
    # var outend/ecx: (addr byte) = out + n
    8b/-> *(ebp+0x10) 1/r32/ecx
    8d/copy-address *(eax+ecx) 1/r32/ecx
    #
    (_append-4  %eax %ecx  %edx %ebx)  # => eax
    # s->read += n
    8b/-> *(ebp+0x10) 1/r32/ecx
    01/add-to *(esi+4) 1/r32/ecx
$read-from-stream:end:
    # . restore registers
    5e/pop-to-esi
    5b/pop-to-ebx
    5a/pop-to-edx
    59/pop-to-ecx
    58/pop-to-eax
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

$read-from-stream:abort:
    (abort "read-from-stream: stream empty")
    # never gets here

stream-first:  # s: (addr stream byte) -> result/eax: byte
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    # . save registers
    51/push-ecx
    56/push-esi
    # result = false
    b8/copy-to-eax 0/imm32
    # esi = s
    8b/-> *(ebp+8) 6/r32/esi
    # var idx/ecx: int = s->read
    8b/-> *(esi+4) 1/r32/ecx
    # if idx >= s->write return 0
    3b/compare-with 1/r32/ecx *esi
    7d/jump-if->= $stream-first:end/disp8
    # result = s->data[idx]
    8a/byte-> *(esi+ecx+0xc) 0/r32/AL
$stream-first:end:
    # . restore registers
    5e/pop-to-esi
    59/pop-to-ecx
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

stream-final:  # s: (addr stream byte) -> result/eax: byte
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    # . save registers
    51/push-ecx
    56/push-esi
    # result = false
    b8/copy-to-eax 0/imm32
    # esi = s
    8b/-> *(ebp+8) 6/r32/esi
    # var max/ecx: int = s->write
    8b/-> *esi 1/r32/ecx
    # if s->read >= max return 0
    39/compare-with *(esi+4) 1/r32/ecx
    7d/jump-if->= $stream-final:end/disp8
    # var idx/ecx: int = max - 1
    49/decrement-ecx
    # result = s->data[idx]
    8a/byte-> *(esi+ecx+0xc) 0/r32/AL
$stream-final:end:
    # . restore registers
    5e/pop-to-esi
    59/pop-to-ecx
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

# compare all the data in two streams (ignoring the read pointer)
streams-data-equal?:  # a: (addr stream byte), b: (addr array byte) -> result/eax: boolean
    # pseudocode:
    #   awrite = a->write
    #   if (awrite != b->write) return false
    #   i = 0
    #   curra = a->data
    #   currb = b->data
    #   while i < awrite
    #     i1 = *curra
    #     i2 = *currb
    #     if (c1 != c2) return false
    #     i+=4, curra+=4, currb+=4
    #   return true
    #
    # registers:
    #   i: ecx
    #   awrite: edx
    #   curra: esi
    #   currb: edi
    #   i1: eax
    #   i2: ebx
    #
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    # . save registers
    51/push-ecx
    52/push-edx
    53/push-ebx
    56/push-esi
    57/push-edi
    # esi = a
    8b/-> *(ebp+8) 6/r32/esi
    # edi = b
    8b/-> *(ebp+0xc) 7/r32/edi
    # var awrite/edx: int = a->write
    8b/-> *esi 2/r32/edx
$streams-data-equal?:sizes:
    # if (awrite != b->write) return false
    39/compare *edi 2/r32/edx
    75/jump-if-!= $streams-data-equal?:false/disp8
    # var curra/esi: (addr byte) = a->data
    81 0/subop/add %esi 0xc/imm32
    # var currb/edi: (addr byte) = b->data
    81 0/subop/add %edi 0xc/imm32
    # var i/ecx: int = 0
    31/xor-with %ecx 1/r32/ecx
    # var vala/eax: int
    31/xor-with %eax 0/r32/eax
    # var valb/ebx: int
    31/xor-with %ebx 3/r32/ebx
$streams-data-equal?:loop:
    {
      # if (i >= awrite) return true
      39/compare %ecx 2/r32/edx
      7d/jump-if->= $streams-data-equal?:true/disp8
      # var vala/eax: int = *curra
      8a/byte-> *esi 0/r32/eax
      # var valb/ebx: int = *currb
      8a/byte-> *edi 3/r32/ebx
      # if (vala != valb) return false
      39/compare %eax 3/r32/ebx
      75/jump-if-!= $streams-data-equal?:false/disp8
      # i++
      41/increment-ecx
      # curra++
      46/increment-esi
      # currb++
      47/increment-edi
      eb/jump loop/disp8
    }
$streams-data-equal?:true:
    b8/copy-to-eax 1/imm32
    eb/jump $streams-data-equal?:end/disp8
$streams-data-equal?:false:
    b8/copy-to-eax 0/imm32
$streams-data-equal?:end:
    # . restore registers
    5f/pop-to-edi
    5e/pop-to-esi
    5b/pop-to-ebx
    5a/pop-to-edx
    59/pop-to-ecx
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

# helper for tests
check-streams-data-equal:  # s: (addr stream _), expected: (addr array _), msg: (addr array byte)
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    # . save registers
    50/push-eax
    #
    (streams-data-equal? *(ebp+8) *(ebp+0xc))  # => eax
    (check-ints-equal %eax 1 *(ebp+0x10))
$check-streams-data-equal:end:
    # . restore registers
    58/pop-to-eax
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return
; 2016-09-09 18:32:52 -0700 3309' href='/akkartik/mu/commit/010vm.cc?h=hlt&id=af023b323bb6fdb09851cb673401fca2abe983b6'>af023b32 ^
c6034af3 ^
4593828f ^
af023b32 ^
32597844 ^
af023b32 ^


4593828f ^
3cfa56a9 ^



c6034af3 ^

ae256ea1 ^
af023b32 ^



ae256ea1 ^
4593828f ^
af023b32 ^



ae256ea1 ^

51b53b13 ^
32597844 ^
af023b32 ^
51b53b13 ^
dcf7436e ^

b24eb476 ^
36594a43 ^
53a569b8 ^
dcf7436e ^

3c435756 ^
dcf7436e ^
257b8a29 ^
dcf7436e ^

9a81d746 ^


b24eb476 ^
dcf7436e ^
363be37f ^


36594a43 ^

363be37f ^
795f5244 ^
363be37f ^
f1a6f323 ^
795f5244 ^
12f304a3 ^
fc9b09f8 ^
795f5244 ^
12f304a3 ^
795f5244 ^
12f304a3 ^
795f5244 ^
12f304a3 ^
db5c9550 ^
5497090a ^
795f5244 ^
12f304a3 ^
f1a6f323 ^
36594a43 ^
c6034af3 ^
6c96a437 ^

3adc9e08 ^
c6034af3 ^


6324f6af ^
53a569b8 ^
c6034af3 ^
dcf7436e ^

db5c9550 ^




b291f85b ^
db5c9550 ^




72d5d018 ^


db5c9550 ^

dcf7436e ^
a3d9828c ^
db5c9550 ^
3adc9e08 ^
f1a6f323 ^
7858a06a ^


dcf7436e ^

1848b18f ^





36594a43 ^
2199940a ^

9a81d746 ^
2199940a ^
36594a43 ^
363be37f ^
795f5244 ^
f1a6f323 ^
795f5244 ^
f1a6f323 ^
26785f2a ^


286d7620 ^

26785f2a ^
53a569b8 ^
13ba3def ^

795f5244 ^
53a569b8 ^
d3902ff0 ^
363be37f ^
36594a43 ^
363be37f ^
36594a43 ^
2429c65c ^
























093036e8 ^



2429c65c ^




7284d503 ^
3c435756 ^
dcf7436e ^
df40cb6e ^



af16d897 ^


6c96a437 ^
8e1d1add ^
dcf7436e ^
8d09d030 ^
9c30b383 ^
1f59be84 ^
8d09d030 ^

c4e143d6 ^




ce2e604e ^
c4e143d6 ^



32597844 ^
c4e143d6 ^
32597844 ^
c4e143d6 ^
f8b56b79 ^




08cf048f ^
8d09d030 ^

ae256ea1 ^

f8b56b79 ^
dcf7436e ^
8d09d030 ^
06462ccd ^
ae256ea1 ^
1f7e3c05 ^
08cf048f ^
93d4cc93 ^
























ae256ea1 ^

93d4cc93 ^
ae256ea1 ^

af023b32 ^


c157066c ^
af023b32 ^
57d01f21 ^
af023b32 ^





ae256ea1 ^
af023b32 ^

ae256ea1 ^

c6034af3 ^

caa4275c ^




6c96a437 ^
caa4275c ^

ae256ea1 ^
caa4275c ^
3377364a ^
c6034af3 ^

4593828f ^





af023b32 ^

4593828f ^



c9f920da ^
4593828f ^
c9f920da ^
4593828f ^
af023b32 ^
c6034af3 ^

3cfa56a9 ^
















93d4cc93 ^
3cfa56a9 ^
93d4cc93 ^

3cfa56a9 ^












































3cfa56a9 ^




3cfa56a9 ^




4593828f ^




ae256ea1 ^

caa4275c ^

6c96a437 ^
f86778e3 ^
ae256ea1 ^
6c96a437 ^
caa4275c ^



f86778e3 ^
caa4275c ^
3377364a ^
c6034af3 ^



11d10eb0 ^



6c96a437 ^
11d10eb0 ^




c6034af3 ^
11d10eb0 ^
c6034af3 ^




ae256ea1 ^



c6034af3 ^
376b333a ^



















e9eb8be0 ^












3473c63a ^
6c96a437 ^
e9eb8be0 ^





6c96a437 ^
e9eb8be0 ^





5f05e954 ^
9a81d746 ^
e9eb8be0 ^











6c96a437 ^
e9eb8be0 ^




f28f2636 ^
e9eb8be0 ^
6cc999ae ^

e9eb8be0 ^



6c96a437 ^
e9eb8be0 ^








6c96a437 ^
e9eb8be0 ^


6c96a437 ^
e9eb8be0 ^

6c96a437 ^
e9eb8be0 ^




acc4792d ^
e9eb8be0 ^

6c96a437 ^
e9eb8be0 ^




6c96a437 ^
e9eb8be0 ^





acc4792d ^


6c96a437 ^
acc4792d ^




6c96a437 ^
acc4792d ^





9f95c745 ^
acc4792d ^
8d09d030 ^
acc4792d ^
c4e143d6 ^
9f95c745 ^
6c96a437 ^
acc4792d ^
dcf7436e ^
acc4792d ^
8d09d030 ^

acc4792d ^

d41955c1 ^
acc4792d ^

dcf7436e ^
b954ac38 ^

15c44292 ^
b954ac38 ^


6cc999ae ^
8a3d101e ^
b954ac38 ^
af023b32 ^
b954ac38 ^
6808ff7d ^

6cc999ae ^
af023b32 ^


8a3d101e ^


af023b32 ^




8a3d101e ^
93d4cc93 ^




8a3d101e ^
ae256ea1 ^

15c44292 ^
6808ff7d ^
0c0bc3ae ^
b954ac38 ^
c4e143d6 ^
b954ac38 ^
6808ff7d ^

c4e143d6 ^
af023b32 ^

6b3fc927 ^
c4e143d6 ^


af023b32 ^




c4e143d6 ^
93d4cc93 ^




c4e143d6 ^
9cc389ce ^

2cdf4a04 ^
f3760b0f ^
d75103c0 ^
ed09f738 ^
c157066c ^
ed09f738 ^

c4e143d6 ^







af023b32 ^



c4e143d6 ^


af023b32 ^




c4e143d6 ^
93d4cc93 ^




c4e143d6 ^










af023b32 ^



c4e143d6 ^


af023b32 ^




c4e143d6 ^
93d4cc93 ^




c4e143d6 ^


a284b697 ^

4082acd2 ^













5e29d0ac ^

4082acd2 ^





dbf64731 ^
3dc3c72c ^
2efceef6 ^



4082acd2 ^
2efceef6 ^
3dc3c72c ^
2efceef6 ^
4082acd2 ^


6f7e2f57 ^
3dc3c72c ^
6f7e2f57 ^





3dc3c72c ^
6f7e2f57 ^



4082acd2 ^

d91de4b8 ^
385ff136 ^
5feb36ff ^
385ff136 ^
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
                                                   
                       
                                                                
                                   

                                                  
                                       
 




                                                                              
                           
 
                     

                                                                       
               
              
                            
                      
           

  


                                                                                              

                                                                      
                                                                      


                                    
                                    
                                                         
                                                
                                                         

                                                    
                           

                
                  


                              



                                                                             
                
                         
              
                  
                                                                                                                                               
               
                   
                       
                           
                                                        
             
               

                                              
                                                              


                          
                                                                              
                                                                      
                  




                                      
               
                                       
                        
                                  


                                                                                                      
                                                  



                                                                                 

  
                    



                                       
                 
                                           



                                                                                  

  
                           
       
                                                                                                                     
 

                                                                          
                        
                     
               

                
                                                                          
                                                                          
                                                                          

             


                                                                             
                         
                       


                                       

                    
                                      
                                  
                        
                            
                                                                         
                                              
                                                                                                      
                                                                           
                                                
                                                                           
                                                
                                                                               
                                                    
                                                                       
                                           
                                                                       
                                            
                                
 
                       

                                                                                         
                                 


                       
                              
              
                       

                     




                                                                                
  




                                                                              


                     

  
                  
              
                    
                           
                         


                                

  





                                      
       

                                                                             
                                                                               
                        
                      
                                          
                                    
                             
                                    
                                 


                                                                             

                                                                         
                              
                

                                                                         
                                                   
                   
                                   
                                                                                            
                     
                                                                    
 
























                                                                              



                                                                                                             




                                       
            
 
       



                           


                                                              
                                                                                                                                                                                   
                                                                  
 
                                                                            
                                                                                                 
                              

                      




                                                           
                                                  



                                       
                                    
                                  
                                    
                     




                                                                              
                        

                                            

                                                  
                                                          
   
 
 
                                               
                                      
                                 
























                                                               

 
       

                                                         


                                                
                                              
                                           
                                                                                                                         





                                                                    
   

                                                         

 

                                        




                                          
                                                      

                                                                                                                                             
   
                                                        
                                 

 





                                                                 

 



                                                            
                        
                                                              
                          
                                                                 
               

 
















                                                                                
                                                              
                                     

                                                                                         












































                                                                     




                                                                             




                              




                                                                   

 

                                                   
                                              
                                                                
                     
                                                    



                                                                                                                                                                           
                        
                                                        
                              



                     



                       
                                                




                                     
              
              




                         



                             
 



















                                                     












                                             
                                                         
                                                  





                                                             
                                                   





                                         
                       
                                                                                      











                                                              
                                                                                 




                                                                 
                                                                          
                                                                     

                                                                              



                                       
                                           








                                                    
                                                         


                                                
                                                      

                                                                    
                                                   




                                                                 
                                                    

                                       
                                                   




                                               
                                                      





                                                  


                                           
                                                   




                                            
                                                      





                                             
                                    
                              
                    
             
                                                   
                              
                                                  
                                                                                                  
   
             

                   

                                          
                                 

                       
 

                                       
                                                                                         


                   
                                               
                             
                    
                      
                   

 
                                               


                                  


             




                                
   




                                               
             

 
                                         
                                                       
                                                           
                    
                  
                   

 
                                             

                 
                        


             




                                
   




                                               
             

 
                                            
                               
                                
      
                       

 







                                                       



                                                   


             




                                
   




                                               










                                                              



                                                                  


             




                                               
   




                                               


             

                                                       













                                                   

                                                





                                              
                            
                                              



                                      
   
                                       
                              
                              


                                 
                                        
                                           





                                                        
                                              



                                                 

 
                        
                  
                
                 
//: A Mu program is a book of 'recipes' (functions)
:(before "End Globals")
//: Each recipe is stored at a specific page number, or ordinal.
map<recipe_ordinal, recipe> Recipe;
//: You can also refer to each recipe by its name.
map<string, recipe_ordinal> Recipe_ordinal;
recipe_ordinal Next_recipe_ordinal = 1;

//: Ordinals are like numbers, except you can't do arithmetic on them. Ordinal
//: 1 is not less than 2, it's just different. Phone numbers are ordinals;
//: adding two phone numbers is meaningless. Here each recipe does something
//: incommensurable with any other recipe.
:(after "Types")
typedef int recipe_ordinal;

:(before "End Types")
// Recipes are lists of instructions. To perform or 'run' a recipe, the
// computer runs its instructions.
struct recipe {
  string name;
  vector<instruction> steps;
  // End recipe Fields
  recipe();
};

:(before "struct recipe")
// Each instruction is either of the form:
//   product1, product2, product3, ... <- operation ingredient1, ingredient2, ingredient3, ...
// or just a single 'label' starting with a non-alphanumeric character
//   +label
// Labels don't do anything, they're just named locations in a recipe.
struct instruction {
  bool is_label;
  string label;  // only if is_label
  string name;  // only if !is_label
  string old_name;  // before our automatic rewrite rules
  string original_string;  // for error messages
  recipe_ordinal operation;  // get(Recipe_ordinal, name)
  vector<reagent> ingredients;  // only if !is_label
  vector<reagent> products;  // only if !is_label
  // End instruction Fields
  instruction();
  void clear();
  bool is_empty();
};

:(before "struct instruction")
// Ingredients and products are a single species -- a reagent. Reagents refer
// either to numbers or to locations in memory along with 'type' tags telling
// us how to interpret them. They also can contain arbitrary other lists of
// properties besides types, but we're getting ahead of ourselves.
struct reagent {
  string original_string;
  string name;
  type_tree* type;
  vector<pair<string, string_tree*> > properties;  // can't be a map because the string_tree sometimes needs to be NULL, which can be confusing
  double value;
  bool initialized;
  // End reagent Fields
  reagent(const string& s);
  reagent() :type(NULL), value(0), initialized(false) {}
  ~reagent();
  void clear();
  reagent(const reagent& original);
  reagent& operator=(const reagent& original);
  void set_value(double v) { value = v;  initialized = true; }
};

:(before "struct reagent")
// Types can range from a simple type ordinal, to arbitrarily complex trees of
// type parameters, like (map (address array character) (list number))
struct type_tree {
  bool atom;
  string name;  // only if atom
  type_ordinal value;  // only if atom
  type_tree* left;  // only if !atom
  type_tree* right;  // only if !atom
  ~type_tree();
  type_tree(const type_tree& original);
  // atomic type ordinal
  explicit type_tree(string name);
  type_tree(string name, type_ordinal v) :atom(true), name(name), value(v), left(NULL), right(NULL) {}
  // tree of type ordinals
  type_tree(type_tree* l, type_tree* r) :atom(false), value(0), left(l), right(r) {}
  type_tree& operator=(const type_tree& original);
  bool operator==(const type_tree& other) const;
  bool operator!=(const type_tree& other) const { return !operator==(other); }
  bool operator<(const type_tree& other) const;
  bool operator>(const type_tree& other) const { return other.operator<(*this); }
};

struct string_tree {
  bool atom;
  string value;  // only if atom
  string_tree* left;  // only if !atom
  string_tree* right;  // only if !atom
  ~string_tree();
  string_tree(const string_tree& original);
  // atomic string
  explicit string_tree(string v) :atom(true), value(v), left(NULL), right(NULL) {}
  // tree of strings
  string_tree(string_tree* l, string_tree* r) :atom(false), left(l), right(r) {}
};

// End type_tree Definition
:(code)
type_tree::type_tree(string name) :atom(true), name(name), value(get(Type_ordinal, name)), left(NULL), right(NULL) {}

:(before "End Globals")
// Locations refer to a common 'memory'. Each location can store a number.
map<int, double> Memory;
:(before "End Setup")
Memory.clear();

:(after "Types")
// Mu types encode how the numbers stored in different parts of memory are
// interpreted. A location tagged as a 'character' type will interpret the
// value 97 as the letter 'a', while a different location of type 'number'
// would not.
//
// Unlike most computers today, Mu stores types in a single big table, shared
// by all the Mu programs on the computer. This is useful in providing a
// seamless experience to help understand arbitrary Mu programs.
typedef int type_ordinal;
:(before "End Globals")
map<string, type_ordinal> Type_ordinal;
map<type_ordinal, type_info> Type;
type_ordinal Next_type_ordinal = 1;
:(code)
void setup_types() {
  Type.clear();  Type_ordinal.clear();
  put(Type_ordinal, "literal", 0);
  Next_type_ordinal = 1;
  // Mu Types Initialization
  type_ordinal number = put(Type_ordinal, "number", Next_type_ordinal++);
  get_or_insert(Type, number).name = "number";
  put(Type_ordinal, "location", number);  // synonym of number to indicate we only care about its size
  type_ordinal address = put(Type_ordinal, "address", Next_type_ordinal++);
  get_or_insert(Type, address).name = "address";
  type_ordinal boolean = put(Type_ordinal, "boolean", Next_type_ordinal++);
  get_or_insert(Type, boolean).name = "boolean";
  type_ordinal character = put(Type_ordinal, "character", Next_type_ordinal++);
  get_or_insert(Type, character).name = "character";
  // Array types are a special modifier to any other type. For example,
  // array:number or array:address:boolean.
  type_ordinal array = put(Type_ordinal, "array", Next_type_ordinal++);
  get_or_insert(Type, array).name = "array";
  // End Mu Types Initialization
}
void teardown_types() {
  for (map<type_ordinal, type_info>::iterator p = Type.begin();  p != Type.end();  ++p) {
    for (int i = 0;  i < SIZE(p->second.elements);  ++i)
      p->second.elements.clear();
  }
  Type_ordinal.clear();
}
:(before "End One-time Setup")
setup_types();
atexit(teardown_types);

:(before "End Types")
// You can construct arbitrary new types. New types are either 'containers'
// with multiple 'elements' of other types, or 'exclusive containers' containing
// one of multiple 'variants'. (These are similar to C structs and unions,
// respectively, though exclusive containers implicitly include a tag element
// recording which variant they should be interpreted as.)
//
// For example, storing bank balance and name for an account might require a
// container, but if bank accounts may be either for individuals or groups,
// with different properties for each, that may require an exclusive container
// whose variants are individual-account and joint-account containers.
enum kind_of_type {
  PRIMITIVE,
  CONTAINER,
  EXCLUSIVE_CONTAINER
};

struct type_info {
  string name;
  kind_of_type kind;
  vector<reagent> elements;
  // End type_info Fields
  type_info() :kind(PRIMITIVE) {
    // End type_info Constructor
  }
};

enum primitive_recipes {
  IDLE = 0,
  COPY,
  // End Primitive Recipe Declarations
  MAX_PRIMITIVE_RECIPES,
};
:(code)
//: It's all very well to construct recipes out of other recipes, but we need
//: to know how to do *something* out of the box. For the following
//: recipes there are only codes, no entries in the book, because Mu just knows
//: what to do for them.
void setup_recipes() {
  Recipe.clear();  Recipe_ordinal.clear();
  put(Recipe_ordinal, "idle", IDLE);
  // Primitive Recipe Numbers
  put(Recipe_ordinal, "copy", COPY);
  // End Primitive Recipe Numbers
}
//: We could just reset the recipe table after every test, but that gets slow
//: all too quickly. Instead, initialize the common stuff just once at
//: startup. Later layers will carefully undo each test's additions after
//: itself.
:(before "End One-time Setup")
setup_recipes();
assert(MAX_PRIMITIVE_RECIPES < 200);  // level 0 is primitives; until 199
Next_recipe_ordinal = 200;
put(Recipe_ordinal, "main", Next_recipe_ordinal++);
// End Load Recipes
:(before "End Commandline Parsing")
assert(Next_recipe_ordinal < 1000);  // recipes being tested didn't overflow into test space
:(before "End Setup")
Next_recipe_ordinal = 1000;  // consistent new numbers for each test

//: One final detail: tests can modify our global tables of recipes and types,
//: so we need some way to clean up after each test is done so it doesn't
//: influence later ones.
:(before "End Globals")
map<string, recipe_ordinal> Recipe_ordinal_snapshot;
map<recipe_ordinal, recipe> Recipe_snapshot;
map<string, type_ordinal> Type_ordinal_snapshot;
map<type_ordinal, type_info> Type_snapshot;
:(before "End One-time Setup")
save_snapshots();
:(before "End Setup")
restore_snapshots();

:(code)
void save_snapshots() {
  Recipe_ordinal_snapshot = Recipe_ordinal;
  Recipe_snapshot = Recipe;
  Type_ordinal_snapshot = Type_ordinal;
  Type_snapshot = Type;
  // End save_snapshots
}

void restore_snapshots() {
  Recipe = Recipe_snapshot;
  Recipe_ordinal = Recipe_ordinal_snapshot;
  restore_non_recipe_snapshots();
}
// when running sandboxes in the edit/ app we'll want to restore everything except recipes defined in the app
void restore_non_recipe_snapshots() {
  Type_ordinal = Type_ordinal_snapshot;
  Type = Type_snapshot;
  // End restore_snapshots
}

//:: Helpers

:(code)
recipe::recipe() {
  // End recipe Constructor
}

instruction::instruction() :is_label(false), operation(IDLE) {
  // End instruction Constructor
}
void instruction::clear() { is_label=false;  label.clear();  name.clear();  old_name.clear();  operation=IDLE;  ingredients.clear();  products.clear();  original_string.clear(); }
bool instruction::is_empty() { return !is_label && name.empty(); }

// Reagents have the form <name>:<type>:<type>:.../<property>/<property>/...
reagent::reagent(const string& s) :original_string(s), type(NULL), value(0), initialized(false) {
  // Parsing reagent(string s)
  istringstream in(s);
  in >> std::noskipws;
  // name and type
  istringstream first_row(slurp_until(in, '/'));
  first_row >> std::noskipws;
  name = slurp_until(first_row, ':');
  string_tree* type_names = parse_property_list(first_row);
  // End Parsing Reagent Type Property(type_names)
  type = new_type_tree(type_names);
  delete type_names;
  // special cases
  if (is_integer(name) && type == NULL)
    type = new type_tree("literal");
  if (name == "_" && type == NULL)
    type = new type_tree("literal");
  // other properties
  slurp_properties(in, properties);
  // End Parsing reagent
}

void slurp_properties(istream& in, vector<pair<string, string_tree*> >& out) {
  while (has_data(in)) {
    istringstream row(slurp_until(in, '/'));
    row >> std::noskipws;
    string key = slurp_until(row, ':');
    string_tree* value = parse_property_list(row);
    out.push_back(pair<string, string_tree*>(key, value));
  }
}

string_tree* parse_property_list(istream& in) {
  skip_whitespace_but_not_newline(in);
  if (!has_data(in)) return NULL;
  string_tree* first = new string_tree(slurp_until(in, ':'));
  if (!has_data(in)) return first;
  string_tree* rest = parse_property_list(in);
  if (!has_data(in) && rest->atom)
    return new string_tree(first, new string_tree(rest, NULL));
  return new string_tree(first, rest);
}
:(before "End Unit Tests")
void test_parse_property_list_atom() {
  istringstream in("a");
  string_tree* x = parse_property_list(in);
  CHECK(x->atom);
  delete x;
}
void test_parse_property_list_list() {
  istringstream in("a:b");
  string_tree* x = parse_property_list(in);
  CHECK(!x->atom);
  CHECK(x->left->atom);
  CHECK_EQ(x->left->value, "a");
  CHECK(!x->right->atom);
  CHECK(x->right->left->atom);
  CHECK_EQ(x->right->left->value, "b");
  CHECK(x->right->right == NULL);
  delete x;
}

:(code)
type_tree* new_type_tree(const string_tree* properties) {
  if (!properties) return NULL;
  if (properties->atom) {
    const string& type_name = properties->value;
    int value = 0;
    if (contains_key(Type_ordinal, type_name))
      value = get(Type_ordinal, type_name);
    else if (is_integer(type_name))  // sometimes types will contain non-type tags, like numbers for the size of an array
      value = 0;
    else if (properties->value == "->")  // used in recipe types
      value = 0;
    else
      value = -1;  // should never happen; will trigger errors later
    return new type_tree(type_name, value);
  }
  return new type_tree(new_type_tree(properties->left),
                       new_type_tree(properties->right));
}

//: avoid memory leaks for the type tree

reagent::reagent(const reagent& other) {
  original_string = other.original_string;
  name = other.name;
  value = other.value;
  initialized = other.initialized;
  for (int i = 0;  i < SIZE(other.properties);  ++i) {
    properties.push_back(pair<string, string_tree*>(other.properties.at(i).first,
                                                    other.properties.at(i).second ? new string_tree(*other.properties.at(i).second) : NULL));
  }
  type = other.type ? new type_tree(*other.type) : NULL;
  // End reagent Copy Constructor
}

type_tree::type_tree(const type_tree& original) {
  atom = original.atom;
  name = original.name;
  value = original.value;
  left = original.left ? new type_tree(*original.left) : NULL;
  right = original.right ? new type_tree(*original.right) : NULL;
}

type_tree& type_tree::operator=(const type_tree& original) {
  atom = original.atom;
  name = original.name;
  value = original.value;
  if (left) delete left;
  left = original.left ? new type_tree(*original.left) : NULL;
  if (right) delete right;
  right = original.right ? new type_tree(*original.right) : NULL;
  return *this;
}

bool type_tree::operator==(const type_tree& other) const {
  if (atom != other.atom) return false;
  if (atom)
    return name == other.name && value == other.value;
  return (left == other.left || *left == *other.left)
      && (right == other.right || *right == *other.right);
}

// only constraint we care about: if a < b then !(b < a)
bool type_tree::operator<(const type_tree& other) const {
  if (atom != other.atom) return atom > other.atom;  // atoms before non-atoms
  if (atom) return name < other.name;  // sort atoms in lexical order
  // first location in one that's missing in the other makes that side 'smaller'
  if (left && !other.left) return false;
  if (!left && other.left) return true;
  if (right && !other.right) return false;
  if (!right && other.right) return true;
  // now if either pointer is unequal neither side can be null
  // if one side is equal that's easy
  if (left == other.left || *left == *other.left) return right && *right < *other.right;
  if (right == other.right || *right == *other.right) return left && *left < *other.left;
  // if the two sides criss-cross, pick the side with the smaller lhs
  if ((left == other.right || *left == *other.right)
      && (right == other.left || *right == *other.left))
    return *left < *other.left;
  // now the hard case: both sides are not equal
  // make sure we stay consistent between (a < b) and (b < a)
  // just return the side with the smallest of the 4 branches
  if (*left < *other.left && *left < *other.right) return true;
  if (*right < *other.left && *right < *other.right) return true;
  return false;
}
:(before "End Unit Tests")
// These unit tests don't always use valid types.
void test_compare_atom_types() {
  reagent a("a:address"), b("b:boolean");
  CHECK(*a.type < *b.type);
  CHECK(!(*b.type < *a.type));
}
void test_compare_equal_atom_types() {
  reagent a("a:address"), b("b:address");
  CHECK(!(*a.type < *b.type));
  CHECK(!(*b.type < *a.type));
}
void test_compare_atom_with_non_atom() {
  reagent a("a:address:number"), b("b:boolean");
  CHECK(!(*a.type < *b.type));
  CHECK(*b.type < *a.type);
}
void test_compare_lists_with_identical_structure() {
  reagent a("a:address:address"), b("b:address:boolean");
  CHECK(*a.type < *b.type);
  CHECK(!(*b.type < *a.type));
}
void test_compare_identical_lists() {
  reagent a("a:address:boolean"), b("b:address:boolean");
  CHECK(!(*a.type < *b.type));
  CHECK(!(*b.type < *a.type));
}
void test_compare_list_with_extra_element() {
  reagent a("a:address:address"), b("b:address:address:number");
  CHECK(*a.type < *b.type);
  CHECK(!(*b.type < *a.type));
}
void test_compare_list_with_smaller_left_but_larger_right() {
  reagent a("a:address:number"), b("b:character:array");
  CHECK(*a.type < *b.type);
  CHECK(!(*b.type < *a.type));
}
void test_compare_list_with_smaller_left_but_larger_right_identical_types() {
  reagent a("a:address:boolean"), b("b:boolean:address");
  CHECK(*a.type < *b.type);
  CHECK(!(*b.type < *a.type));
}

:(code)
string_tree::string_tree(const string_tree& original) {
  atom = original.atom;
  value = original.value;
  left = original.left ? new string_tree(*original.left) : NULL;
  right = original.right ? new string_tree(*original.right) : NULL;
}

reagent& reagent::operator=(const reagent& other) {
  original_string = other.original_string;
  for (int i = 0;  i < SIZE(properties);  ++i)
    if (properties.at(i).second) delete properties.at(i).second;
  properties.clear();
  for (int i = 0;  i < SIZE(other.properties);  ++i)
    properties.push_back(pair<string, string_tree*>(other.properties.at(i).first, other.properties.at(i).second ? new string_tree(*other.properties.at(i).second) : NULL));
  name = other.name;
  value = other.value;
  initialized = other.initialized;
  if (type) delete type;
  type = other.type ? new type_tree(*other.type) : NULL;
  // End reagent Copy Operator
  return *this;
}

reagent::~reagent() {
  clear();
}

void reagent::clear() {
  for (int i = 0;  i < SIZE(properties);  ++i) {
    if (properties.at(i).second) {
      delete properties.at(i).second;
      properties.at(i).second = NULL;
    }
  }
  delete type;
  type = NULL;
}
type_tree::~type_tree() {
  delete left;
  delete right;
}
string_tree::~string_tree() {
  delete left;
  delete right;
}

void append(type_tree*& base, type_tree* extra) {
  if (!base) {
    base = extra;
    return;
  }
  type_tree* curr = base;
  while (curr->right) curr = curr->right;
  curr->right = extra;
}

void append(string_tree*& base, string_tree* extra) {
  if (!base) {
    base = extra;
    return;
  }
  string_tree* curr = base;
  while (curr->right) curr = curr->right;
  curr->right = extra;
}

string slurp_until(istream& in, char delim) {
  ostringstream out;
  char c;
  while (in >> c) {
    if (c == delim) {
      // drop the delim
      break;
    }
    out << c;
  }
  return out.str();
}

bool has_property(const reagent& x, const string& name) {
  for (int i = 0;  i < SIZE(x.properties);  ++i) {
    if (x.properties.at(i).first == name) return true;
  }
  return false;
}

string_tree* property(const reagent& r, const string& name) {
  for (int p = 0;  p != SIZE(r.properties);  ++p) {
    if (r.properties.at(p).first == name)
      return r.properties.at(p).second;
  }
  return NULL;
}

:(before "End Globals")
extern const string Ignore(",");  // commas are ignored in Mu except within [] strings
:(code)
void skip_whitespace_but_not_newline(istream& in) {
  while (true) {
    if (!has_data(in)) break;
    else if (in.peek() == '\n') break;
    else if (isspace(in.peek())) in.get();
    else if (Ignore.find(in.peek()) != string::npos) in.get();
    else break;
  }
}

void dump_memory() {
  for (map<int, double>::iterator p = Memory.begin();  p != Memory.end();  ++p) {
    cout << p->first << ": " << no_scientific(p->second) << '\n';
  }
}

//:: Helpers for converting various values to string
//: Use to_string() in trace(), and try to keep it stable from run to run.
//: Use debug_string() while debugging, and throw everything into it.
//: Use inspect() only for emitting a canonical format that can be parsed back
//: into the value.

string to_string(const recipe& r) {
  ostringstream out;
  out << "recipe " << r.name << " [\n";
  for (int i = 0;  i < SIZE(r.steps);  ++i)
    out << "  " << to_string(r.steps.at(i)) << '\n';
  out << "]\n";
  return out.str();
}

string debug_string(const recipe& x) {
  ostringstream out;
  out << "- recipe " << x.name << '\n';
  // Begin debug_string(recipe x)
  for (int index = 0;  index < SIZE(x.steps);  ++index) {
    const instruction& inst = x.steps.at(index);
    out << "inst: " << to_string(inst) << '\n';
    out << "  ingredients\n";
    for (int i = 0;  i < SIZE(inst.ingredients);  ++i)
      out << "    " << debug_string(inst.ingredients.at(i)) << '\n';
    out << "  products\n";
    for (int i = 0;  i < SIZE(inst.products);  ++i)
      out << "    " << debug_string(inst.products.at(i)) << '\n';
  }
  return out.str();
}

string to_original_string(const instruction& inst) {
  if (inst.is_label) return inst.label;
  ostringstream out;
  for (int i = 0;  i < SIZE(inst.products);  ++i) {
    if (i > 0) out << ", ";
    out << inst.products.at(i).original_string;
  }
  if (!inst.products.empty()) out << " <- ";
  out << inst.name << ' ';
  for (int i = 0;  i < SIZE(inst.ingredients);  ++i) {
    if (i > 0) out << ", ";
    out << inst.ingredients.at(i).original_string;
  }
  return out.str();
}

string to_string(const instruction& inst) {
  if (inst.is_label) return inst.label;
  ostringstream out;
  for (int i = 0;  i < SIZE(inst.products);  ++i) {
    if (i > 0) out << ", ";
    out << to_string(inst.products.at(i));
  }
  if (!inst.products.empty()) out << " <- ";
  out << inst.name << ' ';
  for (int i = 0;  i < SIZE(inst.ingredients);  ++i) {
    if (i > 0) out << ", ";
    out << to_string(inst.ingredients.at(i));
  }
  return out.str();
}

string to_string(const reagent& r) {
  if (is_dummy(r)) return "_";
  ostringstream out;
  out << "{";
  out << r.name << ": " << names_to_string(r.type);
  if (!r.properties.empty()) {
    for (int i = 0;  i < SIZE(r.properties);  ++i)
      out << ", \"" << r.properties.at(i).first << "\": " << to_string(r.properties.at(i).second);
  }
  out << "}";
  return out.str();
}

// special name for ignoring some products
bool is_dummy(const reagent& x) {
  return x.name == "_";
}

string debug_string(const reagent& x) {
  ostringstream out;
  out << x.name << ": " << x.value << ' ' << to_string(x.type) << " -- " << to_string(x);
  return out.str();
}

string to_string(const string_tree* property) {
  if (!property) return "()";
  ostringstream out;
  dump(property, out);
  return out.str();
}

void dump(const string_tree* x, ostream& out) {
  if (!x) return;
  if (x->atom) {
    out << '"' << x->value << '"';
    return;
  }
  out << '(';
  const string_tree* curr = x;
  while (curr && !curr->atom) {
    dump(curr->left, out);
    if (curr->right) out << ' ';
    curr = curr->right;
  }
  // check for dotted list; should never happen
  if (curr) {
    out << ". ";
    dump(curr, out);
  }
  out << ')';
}

string to_string(const type_tree* type) {
  // abbreviate a single-node tree to just its contents
  if (!type) return "NULLNULLNULL";  // should never happen
  ostringstream out;
  dump(type, out);
  return out.str();
}

void dump(const type_tree* x, ostream& out) {
  if (!x) return;
  if (x->atom) {
    dump(x->value, out);
    return;
  }
  out << '(';
  const type_tree* curr = x;
  while (curr && !curr->atom) {
    dump(curr->left, out);
    if (curr->right) out << ' ';
    curr = curr->right;
  }
  // check for dotted list; should never happen
  if (curr) {
    out << ". ";
    dump(curr, out);
  }
  out << ')';
}

void dump(type_ordinal type, ostream& out) {
  if (contains_key(Type, type))
    out << get(Type, type).name;
  else
    out << "?" << type;
}

string names_to_string(const type_tree* type) {
  // abbreviate a single-node tree to just its contents
  if (!type) return "()";  // should never happen
  ostringstream out;
  dump_names(type, out);
  return out.str();
}

void dump_names(const type_tree* x, ostream& out) {
  if (!x) return;
  if (x->atom) {
    out << '"' << x->name << '"';
    return;
  }
  out << '(';
  const type_tree* curr = x;
  while (curr && !curr->atom) {
    dump_names(curr->left, out);
    if (curr->right) out << ' ';
    curr = curr->right;
  }
  // check for dotted list; should never happen
  if (curr) {
    out << ". ";
    dump_names(curr, out);
  }
  out << ')';
}

string names_to_string_without_quotes(const type_tree* type) {
  // abbreviate a single-node tree to just its contents
  if (!type) return "NULLNULLNULL";  // should never happen
  ostringstream out;
  dump_names_without_quotes(type, out);
  return out.str();
}

void dump_names_without_quotes(const type_tree* x, ostream& out) {
  if (!x) return;
  if (x->atom) {
    out << x->name;
    return;
  }
  out << '(';
  const type_tree* curr = x;
  while (curr && !curr->atom) {
    dump_names_without_quotes(curr->left, out);
    if (curr->right) out << ' ';
    curr = curr->right;
  }
  // check for dotted list; should never happen
  if (curr) {
    out << ". ";
    dump_names_without_quotes(curr, out);
  }
  out << ')';
}

//: helper to print numbers without excessive precision

:(before "End Types")
struct no_scientific {
  double x;
  explicit no_scientific(double y) :x(y) {}
};

:(code)
ostream& operator<<(ostream& os, no_scientific x) {
  if (!isfinite(x.x)) {
    // Infinity or NaN
    os << x.x;
    return os;
  }
  ostringstream tmp;
  // more accurate, but too slow
//?   tmp.precision(308);  // for 64-bit numbers
  tmp << std::fixed << x.x;
  os << trim_floating_point(tmp.str());
  return os;
}

string trim_floating_point(const string& in) {
  if (in.empty()) return "";
  if (in.find('.') == string::npos) return in;
  int length = SIZE(in);
  while (length > 1) {
    if (in.at(length-1) != '0') break;
    --length;
  }
  if (in.at(length-1) == '.') --length;
  if (length == 0) return "0";
  return in.substr(0, length);
}

void test_trim_floating_point() {
  CHECK_EQ(trim_floating_point(""), "");
  CHECK_EQ(trim_floating_point(".0"), "0");
  CHECK_EQ(trim_floating_point("1.5000"), "1.5");
  CHECK_EQ(trim_floating_point("1.000001"), "1.000001");
  CHECK_EQ(trim_floating_point("23.000000"), "23");
  CHECK_EQ(trim_floating_point("23.0"), "23");
  CHECK_EQ(trim_floating_point("23."), "23");
  CHECK_EQ(trim_floating_point("23"), "23");
  CHECK_EQ(trim_floating_point("230"), "230");
  CHECK_EQ(trim_floating_point("3.000000"), "3");
  CHECK_EQ(trim_floating_point("3.0"), "3");
  CHECK_EQ(trim_floating_point("3."), "3");
  CHECK_EQ(trim_floating_point("3"), "3");
}

:(before "End Includes")
#include <utility>
using std::pair;
#include <math.h>