~kameliya/piret

ref: 1f7f0673d7dd05a316e0d9dcef634bbb3f46fdff piret/piretc.pi -rw-r--r-- 20.3 KiB
1f7f0673 — Arlen Christian Mart Cuss SUCCESSFUL BOOTSTRAP 8 years ago
                                                                                
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
;; read

;; lex

(type node (union (string symbol int (list node))))

(defn
  is-symbol-char
  (string bool)
  (s) ()
  (member s (list "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m"
                  "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
                  "+" "*" "-" "0" "1" "2" "3" "4" "5" "6" "7" "8" "9")))

(defn
  is-numeric
  (string bool)
  (s) ()
  (member s (list "0" "1" "2" "3" "4" "5" "6" "7" "8" "9")))

(defn
  list
  (string int (tuple (maybe node) int))
  (src i)
  ((r (list node))
   (ok bool)
   (tmp (tuple (maybe string) int))
   (p (maybe string))
   (tmp1 (tuple (maybe node) int)))
    
  (set i (+ 1 i))
  (set r (list))
  (set ok true)
  (while ok
    (set tmp (call peek src i))
    (set p (get-just (index tmp 0)))
    (set i (index tmp 1))

    (if (eq p ")")
      (set ok false)
      (do
        (set tmp1 (call lex src i))
        (set i (index tmp1 1))
        (set r (concat r (list (get-just (index tmp1 0))))))))

  (tuple (just r) (+ 1 i)))

(defn
  symbol
  (string int (tuple (maybe node) int))
  (src i)
  ((s string))
  (set s (index src i))
  (set i (+ 1 i))
  (while (call is-symbol-char (index src i))
    (set s (concat s (index src i)))
    (set i (+ 1 i)))
  (tuple (just (symbol s)) i))

(defn
  string
  (string int (tuple (maybe node) int))
  (src i)
  ((s string))
  (set i (+ 1 i))
  (set s "")
  (while (neq (index src i) "\"")
    (if (eq (index src i) "\\")
      (set i (+ 1 i)))
    (set s (concat s (index src i)))
    (set i (+ 1 i)))
  (tuple (just s) (+ 1 i)))

(defn
  integer
  (string int (tuple (maybe node) int))
  (src i)
  ((n int))
  (set n (- (ord (index src i)) 48))
  (set i (+ 1 i))
  (while (call is-numeric (index src i))
    (set n (+ (* n 10) (- (ord (index src i)) 48)))
    (set i (+ 1 i)))
  (tuple (just n) i))

(defn
  peek
  (string int (tuple (maybe string) int))
  (src i)
  ()
  (while (member (index src i) (list " " "	" "
" "
" ";"))
    (if (eq (index src i) ";")
      (while (neq (index src i) "
")
        (set i (+ 1 i)))
      (set i (+ 1 i))))
  (if (eq nil (index src i))
    (tuple nothing i)
    (tuple (just (index src i)) i)))

(defn
  lex
  (string int (tuple (maybe node) int))
  (src i)
  ((tmp (tuple (maybe string) int))
   (p string))
  (set tmp (call peek src i))
  (if (eq nothing (index tmp 0))
    (tuple nothing i)
    (do
      (set p (get-just (index tmp 0)))
      (set i (index tmp 1))
      (cond
        (eq p "(")
         (call list src i)
        (call is-numeric p)
         (call integer src i)
        (call is-symbol-char p)
         (call symbol src i)
        (eq p "\"")
         (call string src i)
        true
         (do
           (print "unknown character: " (ord p))
           (panic "unknown character"))))))

(defn
  lex-all
  (string (list node))
  (src)
  ((r (list node))
   (i int)
   (tmp (tuple (maybe node) int))
   (n (maybe node)))
  (set r (list))
  (set i 0)
  (while true
    (set tmp (call lex src i))
    (set n (index tmp 0))
    (set i (index tmp 1))
    (if (neq n nothing)
      (set r (concat r (list (get-just n))))
      (return r))))


;; resolve top-level definitions

;(defn
  ;resolve)

(defn
  procsym
  (symbol string)
  (osym)
  ((i int)
   (sym string))
  (set i 0)
  (set sym
    (if (is-symbol osym)
      (string osym)
      osym))
  (while (neq nil (index sym i))
    (if (eq (index sym i) "-")
      (set sym (string-with sym i "_")))
    (set i (+ 1 i)))
  sym)

(def new-var-counter)

(defn
  new-var
  (string)
  ()
  ((i int))
  (set i new-var-counter)
  (set new-var-counter (+ new-var-counter 1))
  (concat "v" (string i)))

(defn
  process-list
  (node string)
  (value)
  ((name string)
   (args (node list))
   (v string)
   (w string)
   (vn string)
   (rvn string)
   (i integer))
  (set name (index value 0))
  (set args (from value 1))
  (cond
    (eq name (symbol "is-list"))
     (do
       (set v (call process (index args 0)))
       (set vn (call new-var))
       (puts "struct piret_val ")
       (puts vn)
       (puts " = ")
       (puts v)
       (puts ";
")
       (concat "piret_bool(" (concat vn ".kind == PIRET_LIST)")))
    (eq name (symbol "is-symbol"))
     (do
       (set v (call process (index args 0)))
       (set vn (call new-var))
       (puts "struct piret_val ")
       (puts vn)
       (puts " = ")
       (puts v)
       (puts ";
")
       (concat "piret_bool(" (concat vn ".kind == PIRET_SYMBOL)")))
    (eq name (symbol "is-string"))
     (do
       (set v (call process (index args 0)))
       (set vn (call new-var))
       (puts "struct piret_val ")
       (puts vn)
       (puts " = ")
       (puts v)
       (puts ";
")
       (concat "piret_bool(" (concat vn ".kind == PIRET_STRING)")))
    (eq name (symbol "is-integer"))
     (do
       (set v (call process (index args 0)))
       (set vn (call new-var))
       (puts "struct piret_val ")
       (puts vn)
       (puts " = ")
       (puts v)
       (puts ";
")
       (concat "piret_bool(" (concat vn ".kind == PIRET_INT)")))
    (eq name (symbol "member"))
     (do
       (set v (call process (index args 0)))
       (set w (call process (index args 1)))
       (set vn (call new-var))
       (puts "  struct piret_val ")
       (puts vn)
       (puts " = piret_member(")
       (puts v)
       (puts ", ")
       (puts w)
       (puts ");
")
       vn)
    (eq name (symbol "set"))
     (do
       (set v (call process (index args 1)))
       (puts "  ")
       (puts (call procsym (index args 0)))
       (puts " = ")
       (puts v)
       (puts ";
")
      (call procsym (index args 0)))   
    (or (member name (list (symbol "+") (symbol "-") (symbol "*") (symbol "/"))))
     (do
       (set v (call process (index args 0)))
       (set w (call process (index args 1)))
       (puts "if ((")
       (puts v)
       (puts ").kind != PIRET_INT || (")
       (puts w)
       (puts ").kind != PIRET_INT) {
    piret_panic(\"attempted to ")
       (puts name)
       (puts " non-int\");
  }")
       (concat "piret_int(("
         (concat v
         (concat ").integer "
         (concat (string name)
         (concat " ("
         (concat w ").integer)")))))))
    (or (eq name (symbol "list")) (eq name (symbol "tuple")))
     (do
       (set vn (call new-var))
       (puts "struct piret_val ")
       (puts vn)
       (puts ";
  ")
       (puts vn)
       (puts ".kind = ")
       (puts
         (if (eq name (symbol "list")) "PIRET_LIST" "PIRET_TUPLE"))
       (puts ";
  ")
       (puts vn)
       (puts ".list.length = ")
       (puts (string (count args)))
       (puts ";
  ")
       (puts vn)
       (puts ".list.items = piret_malloc(sizeof(*")
       (puts vn)
       (puts ".list.items) * ")
       (puts (string (count args)))
       (puts ");
  ")
       (set i 0)
       (while (neq nil (index args i))
         (set v (call process (index args i)))
         (puts "  ")
         (puts vn)
         (puts ".list.items[")
         (puts (string i))
         (puts "] = ")
         (puts v)
         (puts ";
")
         (set i (+ 1 i)))
       vn)
    (eq name (symbol "while"))
     (do
       (set v (index args 0))
       (set w (from args 1))
       (puts "while (1) {
")
       (set v (call process v))
       (puts "  if ((")
       (puts v)
       (puts ").kind != PIRET_BOOL) {
    piret_panic(\"used non-bool as while condition\");
  }
  if (!(")
       (puts v)
       (puts ").integer) { break; }
")
       (set i 0)
       (while (neq nil (index w i))
         (call process (index w i))
         (set i (+ i 1)))
       (puts "}")
       "piret_nil()")
    (eq name (symbol "call"))
     (do
       (set v (call procsym (index args 0)))
       (set w (list))
       (set i 1)
       (while (neq nil (index args i))
         (set w (concat w (list (concat "(" (concat (call process (index args i)) ")")))))
         (set i (+ i 1)))
       (set vn (call new-var))
       (puts "struct piret_val ")
       (puts vn)
       (puts " = piret_user_")
       (puts v)
       (puts "(")
       (set i 0)
       (while (neq nil (index w i))
         (if (neq i 0) (puts ", "))
         (puts (index w i))
         (set i (+ i 1)))
       (puts ");
")
       vn)
    (eq name (symbol "just"))
     (do
       (set v (call process (index args 0)))
       (set vn (call new-var))
       (puts "  struct piret_val ")
       (puts vn)
       (puts " = piret_just(")
       (puts v)
       (puts ");
")
       vn)
    (eq name (symbol "index"))
     (do
       (set v (call process (index args 0)))
       (set w (call process (index args 1)))
       (set vn (call new-var))
       (puts "  struct piret_val ")
       (puts vn)
       (puts " = piret_index(")
       (puts v)
       (puts ", ")
       (puts w)
       (puts ");
")
       vn)
    (eq name (symbol "concat"))
     (do
       (set v (call process (index args 0)))
       (set w (call process (index args 1)))
       (set vn (call new-var))
       (puts "  struct piret_val ")
       (puts vn)
       (puts " = piret_concat(")
       (puts v)
       (puts ", ")
       (puts w)
       (puts ");
")
       vn)
    (eq name (symbol "symbol"))
     (do
       (set v (call process (index args 0)))
       (set vn (call new-var))
       (puts "  struct piret_val ")
       (puts vn)
       (puts " = piret_symbol_from_string(")
       (puts v)
       (puts ");
")
       vn)
    (eq name (symbol "string"))
     (do
       (set v (call process (index args 0)))
       (set vn (call new-var))
       (puts "  struct piret_val ")
       (puts vn)
       (puts " = piret_string_from_symbol(")
       (puts v)
       (puts ");
")
       vn)
    (eq name (symbol "neq"))
     (do
       (set v (call process (index args 0)))
       (set w (call process (index args 1)))
       (concat "piret_bool(piret_compare("
         (concat v
         (concat ", "
         (concat w ") != 0)")))))
    (eq name (symbol "eq"))
     (do
       (set v (call process (index args 0)))
       (set w (call process (index args 1)))
       (concat "piret_bool(piret_compare("
         (concat v
         (concat ", "
         (concat w ") == 0)")))))
    (eq name (symbol "ord"))
     (do
       (set v (call process (index args 0)))
       (set vn (call new-var))
       (puts "  if ((")
       (puts v)
       (puts ").kind != PIRET_STRING) {
    piret_panic(\"tried to ord non-string\");
  }
  struct piret_val ")
       (puts vn)
       (puts " = piret_int((")
       (puts v)
       (puts ").string[0]);
")
       vn)
    (eq name (symbol "if"))
     (do
       (set vn (call new-var))
       ; cond
       (set v (call process (index args 0)))
       (puts "  struct piret_val ")
       (puts vn)
       (puts ";
  ")
       (puts vn)
       (puts " = ")
       (puts v)
       (puts ";
  if (")
       (puts vn)
       (puts ".kind != PIRET_BOOL) {
    piret_panic(\"used non-bool as if condition\");
  }
  if (")
       (puts vn)
       (puts ".integer) {
    ")
       ; iftrue
       (set v (call process (index args 1)))
       (puts vn)
       (puts " = ")
       (puts v)
       (puts ";
  } else {
    ")
       ; iffalse
       (set v (call process (index args 2)))
       (puts vn)
       (puts " = ")
       (puts v)
       (puts ";
  }
")
       vn)
    (eq name (symbol "do"))
     (do
       (set w nil)
       (set i 0)
       (while (neq nil (index args i))
         (set w (call process (index args i)))
         (set i (+ i 1)))
       w)
    (eq name (symbol "get-just"))
     (do
       (set v (call process (index args 0)))
       (set vn (call new-var))
       (puts "  struct piret_val ")
       (puts vn)
       (puts " = piret_get_just(")
       (puts v)
       (puts ");
")
       vn)
    (eq name (symbol "cond"))
     (do
       (set rvn (call new-var))
       (puts "  struct piret_val ")
       (puts rvn)
       (puts " = piret_nil();
")
       (set i 0)
       (while (neq nil (index args i))
         (set v (call process (index args i))) ; co
         (set vn (call new-var))
         (puts "  struct piret_val ")
         (puts vn)
         (puts " = ")
         (puts v)
         (puts ";
  if (")
         (puts vn)
         (puts ".kind != PIRET_BOOL) {
    piret_panic(\"cond has non-bool arm\");
  }

  if (")
         (puts vn)
         (puts ".integer) {
")
         (set v (call process (index args (+ i 1)))) ; re
         (puts "    ")
         (puts rvn)
         (puts " = ")
         (puts v)
         (puts ";
    goto piret_cond_")
         (puts rvn)
         (puts "_done;
  }
")
         (set i (+ i 2)))
       (puts "piret_cond_")
       (puts rvn)
       (puts "_done:
")
       rvn)
    (eq name (symbol "ord"))
     (do
       (set v (call process (index args 0)))
       (set vn (call new-var))
       (puts "  if ((")
       (puts v)
       (puts ").kind != PIRET_STRING) {
    piret_panic(\"tried to ord non-string\");
  }
  struct piret_val ")
       (puts vn)
       (puts " = piret_int((")
       (puts v)
       (puts ").string[0]);
")
       vn)
    (eq name (symbol "print"))
     (do
       (set i 0)
       (while (neq nil (index args i))
         (if (is-string (index args i))
           (do
             (puts "  printf(\"")
             (puts (index args i))
             (puts "\");
"))
           (do
             (set v (call process (index args i)))
             (puts "  piret_print(")
             (puts v)
             (puts ");
")))
         (set i (+ i 1)))
       (puts "  printf(\"\\\n\");
")
       "piret_nil()")
    (eq name (symbol "panic"))
     (do
       (set v (call process (index args 0)))
       (set vn (call new-var))
       (puts "  struct piret_val ")
       (puts vn)
       (puts " = ")
       (puts v)
       (puts ";
  if (")
       (puts vn)
       (puts ".kind != PIRET_STRING) {
    piret_panic(\"tried to panic with a non-string\");
  }
  piret_panic(")
       (puts vn)
       (puts ".string);
")
       "piret_nil()")
    (eq name (symbol "string-with"))
     (do
       (set v (call process (index args 0)))
       (set i (call process (index args 1)))
       (set w (call process (index args 2)))
       (set vn (call new-var))
       (puts "  struct piret_val ")
       (puts vn)
       (puts " = piret_string_with(")
       (puts v)
       (puts ", ")
       (puts i)
       (puts ", ")
       (puts w)
       (puts ");
")
       vn)
    (eq name (symbol "from"))
     (do
       (set v (call process (index args 0)))
       (set w (call process (index args 1)))
       (set vn (call new-var))
       (puts "  struct piret_val ")
       (puts vn)
       (puts " = piret_from(")
       (puts v)
       (puts ", ")
       (puts w)
       (puts ");
")
       vn)
    (eq name (symbol "puts"))
     (do
       (set v (call process (index args 0)))
       (set vn (call new-var))
       (puts "  struct piret_val ")
       (puts vn)
       (puts " = ")
       (puts v)
       (puts ";
  if (")
       (puts vn)
       (puts ".kind != PIRET_STRING && ")
       (puts vn)
       (puts ".kind != PIRET_SYMBOL) {
    printf(\"piret puts %d\\n\", ")
       (puts vn)
       (puts ".kind);
    piret_panic(\"tried to puts non-string\");
  }
  printf(\"%s\", ")
       (puts vn)
       (puts ".string);
")
       "piret_nil()")
    (eq name (symbol "or"))
     (do
       (set i 0)
       (set w "piret_bool(")
       (while (neq nil (index args i))
         (set v (call process (index args i)))
         (set vn (call new-var))
         (puts "  struct piret_val ")
         (puts vn)
         (puts " = ")
         (puts v)
         (puts ";
  if (")
         (puts vn)
         (puts ".kind != PIRET_BOOL) {
    piret_panic(\"tried to or non-boolean\");
  }
")
         (if (neq i 0) (set w (concat w " || ")))
         (set w (concat w (concat vn ".integer")))
         (set i (+ i 1)))
       (concat w ")"))
    (eq name (symbol "count"))
     (do
       (set v (call process (index args 0)))
       (set vn (call new-var))
       (puts "  struct piret_val ")
       (puts vn)
       (puts " = ")
       (puts v)
       (puts ";
  if (")
       (puts vn)
       (puts ".kind != PIRET_LIST) {
    piret_panic(\"tried to count non-list\");
  }
")
       (concat "piret_int(" (concat vn ".list.length)")))
    (eq name (symbol "read"))
     (do
       (set v (call process (index args 0)))
       (set vn (call new-var))
       (puts "  struct piret_val ")
       (puts vn)
       (puts " = piret_read(")
       (puts v)
       (puts ");
")
       vn)
    (eq name (symbol "argv"))
     (do
       (set v (call process (index args 0)))
       (set vn (call new-var))
       (puts "  struct piret_val ")
       (puts vn)
       (puts " = piret_string(argv[(")
       (puts v)
       (puts ").integer]);
")
       vn)
    (eq name (symbol "return"))
     (do
       (set v (call process (index args 0)))
       (puts "  return ")
       (puts v)
       (puts ";
")
       "piret_nil()")
    true
     (do
       (print value)
       (panic "unknown directive in process-list"))))

(defn
  escape-string
  (string string)
  (str)
  ((nstr string)
   (i int))
  (set nstr "")
  (set i 0)
  (while (neq nil (index str i))
    (if (eq (index str i) "\\")
      (set nstr (concat nstr "\\\\"))
      (if (eq (index str i) "\"")
        (set nstr (concat nstr "\\\""))
        (if (eq (index str i) "
")
          (set nstr (concat nstr "\\r"))
          (if (eq (index str i) "
")
            (set nstr (concat nstr "\\n"))
            (set nstr (concat nstr (index str i)))))))
    (set i (+ i 1)))
  nstr)

(defn
  process
  (node string)
  (value)
  ()
  (cond
    (is-list value)
     (call process-list value)
    (eq value (symbol "false"))
     "piret_bool(0)"
    (eq value (symbol "true"))
     "piret_bool(1)"
    (eq value (symbol "nil"))
     "piret_nil()"
    (eq value (symbol "nothing"))
     "piret_nothing()"
    (is-symbol value)
     (call procsym value)
    (is-string value)
     (concat "piret_string(\"" (concat (call escape-string value) "\")"))
    (is-integer value)
     (concat "piret_int(" (concat (string value) ")"))
    (eq nil value)
     "piret_nil()"
    true
     (do
       (print "value is " value)
       (panic "unknown in process"))))

(defn
  main
  (argcv int)
  ()
  ((source string)
   (lexed (list node))
   (i int)
   (n node)
   (name string)
   (ty string)
   (arglist (list symbol))
   (varlist (list node))
   (rest (list node))
   (j int)
   (last string))
  (set new-var-counter 0)
  (set source (read (argv 1)))
  (set lexed (call lex-all source))
  (puts (read "piretc-user.c"))
  (set i 0)
  (while (neq nil (index lexed i))
    (set n (index lexed i))
    (cond
      (eq (symbol "defn") (index n 0))
       (do
         (set name (string (index n 1)))
         (set ty "int ")
         (if (neq name "main")
           (do
             (set name (concat "piret_user_" name))
             (set ty "struct piret_val ")))
         (puts ty)
         (puts (call procsym name))
         (puts "();
"))
      (eq (symbol "def") (index n 0))
       (do
         (puts "struct piret_val ")
         (puts (call procsym (index n 1)))
         (puts ";
")))
    (set i (+ i 1)))
  (set i 0)
  (while (neq nil (index lexed i))
    (set n (index lexed i))
    (set i (+ i 1))
    (if (eq (symbol "defn") (index n 0))
      (do
        (set name (index n 1))
        (set arglist (index n 3))
        (set varlist (index n 4))
        (set rest (from n 5))
        (if (eq name (symbol "main"))
          (puts "int main(int argc, char **argv) {
")
          (do
            (puts "struct piret_val piret_user_")
            (puts (call procsym name))
            (puts "(")
            (set j 0)
            (while (neq nil (index arglist j))
              (if (neq 0 j) (puts ", "))
              (puts "struct piret_val ")
              (puts (call procsym (index arglist j)))
              (set j (+ j 1)))
            (puts ") {
")))
        (set j 0)
        (puts "  ")
        (while (neq nil (index varlist j))
          (if (neq j 0) (puts " "))
          (puts "struct piret_val ")
          (puts (call procsym (index (index varlist j) 0)))
          (puts ";")
          (set j (+ 1 j)))
        (puts "
")
        ; do main processing
        (set last nil)
        (set j 0)
        (while (neq nil (index rest j))
          (set last (call process (index rest j)))
          (set j (+ j 1)))

        (if (neq nil last)
          (if (eq name (symbol "main"))
            (do
              (puts "  if ((")
              (puts last)
              (puts ").kind != PIRET_INT) {
    piret_panic(\"main() return not int\");
  }
  return (")
              (puts last)
              (puts ").integer;
"))
            (do
              (puts "  return ")
              (puts last)
              (puts ";
"))))
         (puts "}

")

               )))
  0)

; vim: set ft=lisp et sw=2: