This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenyalios.ss
2591 lines (2482 loc) · 96.2 KB
/
enyalios.ss
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
;; A simple compiler for a restricted subset of Digamma, similar in nature to Eprime, but
;; containing fixes for things I've learned since having started Eprime, as well as
;; some simpler experiments.
;; Two major things:
;; generate pretty C code, and avoid strings with C code in them
;; (can be used for generating FFI code too!)
;; remove unecessary walks through functions; just generate code.
;; (meaning, no tail-call?, no hof?, &c.).
;; my idea wrt "just generate it" is to wrap functions in
;; while-blocks, and break on all expressions that would return.
;; Basic testing shows that gcc creates similar code for:
;; int
;; foo()
;; {
;; while(1) {
;; if(3 < 4)
;; return 1;
;; else
;; return 0;
;; }
;; }
;; and
;; int
;; foo()
;; {
;; if(3 < 4)
;; return 1;
;; else
;; return 0;
;; }
;; I still have to find a nice way of generating HOFs, but I don't
;; think that it will be too big a deal to turn:
;; (define (foo f x) ... (f x 1) ...)
;; into:
;; SExp *
;; foo(SExp *(*f)(SExp *, SExp *), SExp *x)
;; {
;; ...
;; }
;; More cond/if and/or/not related notes:
;; * if Cond used a nested approach, an item that needed vars could easily be supported
;; - support both: if no and/or/not is detected, use linear cond expansion; if and/or/not is detected, use nested cond expansion
;; - make if support vars first
;; - SSA?
;; * Fix apply:
;; ** don't use list with apply
;; - (+ x y) -> fplus(list(2,x,y));
;; - (apply + (some-fn)) -> fplus(some_fn());
(load "experiments/sr.ss")
(define *procedures* {
;; [ c-proc min max ]
;; if max != min && max == 0:
;; 1 .. N args are accepted
;; should write these to a list. The proc
;; handles unpacking.
:display ["f_princ" 1 2]
:newline ["newline" 0 1]
:read ["f_read" 0 1]
:write ["f_write" 1 2]
:format ["format" 1 0]
:read-char ["fread_char" 0 1]
:write-char ["fwrite_char" 0 1]
:read-buffer ["fread_buffer" 1 2]
:quit ["fquit" 0 0]
:write-buffer ["fwrite_buffer" 1 2]
:read-string #t
:write-string #t
:eval ["__seval" 1 1]
:sys/getpid ["f_sysgetpid" 0 1]
:sys/getppid ["f_sysgetppid" 0 1]
:sys/fork ["f_fork" 0 1]
:sys/time ["f_time" 0 1]
:sys/stat ["f_stat" 1 2]
:sys/getenv ["f_getenv" 1 0]
:announce ["f_announce" 3 0]
:accept ["f_accept" 1 0]
:listen ["f_listen" 1 0]
:hangup ["f_hangup" 1 0]
:ls ["f_ls" 1 0]
:close ["f_close" 1 0]
:open ["f_open" 2 0]
})
(define *varprimitives* {
:list "list"
:vector "vector"
;:dict "dict"
;:string "string"
})
(define *primitives* {
:car ["car" #f 1 1]
:cdr ["cdr" #f 1 1]
:cons ["cons" #f 2 2]
:eq? ["eqp" #f 2 2]
:< ["flt" #f 1 -1]
:> ["fgt" #f 1 -1]
:<= ["flte" #f 1 -1]
:>= ["fgte" #f 1 -1]
:= ["fnumeq" #f 1 -1]
:+ ["fplus" #f 1 -1]
:* ["fmult" #f 1 -1]
:/ ["fdivd" #f 1 -1]
:- ["fsubt" #f 1 -1]
:numerator ["fnumerator" #f 1 1]
:denomenator ["fdenomenator" #f 1 1]
:length ["flength" #f 1 1]
:exact? ["fexactp" #f 1 1]
:inexact? ["finexactp" #f 1 1]
:real? ["frealp" #f 1 1]
:integer? ["fintegerp" #f 1 1]
:complex? ["fcomplexp" #f 1 1]
:rational? ["frationalp" #f 1 1]
:rationalize ["frationalize" #f 2 2]
:numerator ["fnum" #f 1 1]
:denomenator ["fden" #f 1 1]
:type ["ftype" #f 1 1]
:gcd ["fgcd" #f 1 -1]
:lcm ["flcm" #f 1 -1]
:ceil ["fceil" #f 1 1]
:floor ["ffloor" #f 1 1]
:truncate ["ftrunc" #f 1 1]
:round ["fround" #f 1 1]
:inexact->exact ["fin2ex" #f 1 1]
:quotient ["fquotient" #f 2 2]
:modulo ["fmodulo" #f 2 2]
:remainder ["fremainder" #f 2 2]
:& ["fbitand" #f 2 2]
:| ["fbitor" #f 2 2]
:^ ["fbitxor" #f 2 2]
:~ ["fbitnot" #f 1 1]
:<< ["fbitshl" #f 2 2]
:>> ["fbitshr" #f 2 2]
:make-vector ["fmkvector" #f 1 -1]
:make-string ["fmakestring" #f 1 -1]
:type ["ftype" #f 1 1]
:append ["fappend" #f 2 -1]
:first ["ffirst" #f 1 1]
:rest ["frest" #f 1 1]
:ccons ["fccons" #f 2 2]
:nth ["fnth" #f 2 3]
:keys ["fkeys" #f 1 1]
:partial-key? ["fpartial_key" #f 2 2]
:cset! ["fcset" #f 3 3]
:dict-set! ["fdset" #f 3 3] ;; to be optimized away below
:vector-set! ["fvset" #f 3 3] ;; to be optimized away below
:vector-ref ["fvref" #f 2 2] ;; to be optimized away below
:type? ["typep" #f 2 2] ;; to be optimized away below
:string ["fstring" #f 0 -1]
:dict ["fdict" #f 0 -1]
:empty? ["fempty" #f 1 1]
:gensym ["fgensym" #f 0 1]
:imag-part ["fimag_part" #f 1 1]
:real-part ["freal_part" #f 1 1]
:make-rectangular ["fmake_rect" #f 2 2]
:make-polar ["fmake_pole" #f 2 2]
:magnitude ["fmag" #f 1 1]
:argument ["fimag_part" #f 1 1]
:conjugate! ["fconjugate_bang" #f 1 1]
:conjugate ["fconjugate" #f 1 1]
:polar->rectangular ["fpol2rect" #f 1 1]
:rectangular->polar ["frect2pol" #f 1 1]
:sin ["fsin" #f 1 1]
:cos ["fcos" #f 1 1]
:tan ["ftan" #f 1 1]
:asin ["fasin" #f 1 1]
:acos ["facos" #f 1 1]
:atan ["fatan" #f 1 1]
:atan2 ["fatan2" #f 2 2]
:cosh ["fcosh" #f 1 1]
:sinh ["fsinh" #f 1 1]
:tanh ["ftanh" #f 1 1]
:exp ["fexp" #f 1 1]
:ln ["fln" #f 1 1]
:abs ["fnabs" #f 1 1]
:sqrt ["fsqrt" #f 1 1]
:exp2 ["fexp2" #f 1 1]
:expm1 ["fexpm1" #f 1 1]
:log2 ["flog2" #f 1 1]
:log10 ["flog10" #f 1 1]
:string-append ["fstringappend" #f 0 -1]
;:apply #t
:assq ["assq" #f 2 2]
:memq ["memq" #f 2 2]
;:defrec #t
;:set-rec! #t
;:dict ["fdict" #f 0 -1]
:make-dict ["makedict" #f 0 0]
:dict-has? ["fdicthas" #f 2 2]
:coerce ["fcoerce" #f 2 2]
:error ["f_error" #f 1 1]
:cupdate ["fcupdate" #f 3 3]
:cslice ["fcslice" #f 3 3]
:not ["fnot" #f 1 1] ;; to be optimized away
;:tconc! ["tconc"]
;;:make-tconc #t
;;:tconc-list #t
;;:tconc->pair #t
;;:tconc-splice! ["tconc_splice"]
})
(define *profiling* #f)
(define *ulambdas* {})
(define *usyntax* {})
;; Out-of-Bounds lambdas
;; these are lambdas that are defined
;; somewhere in the code, and need to be lifted.
(define *ooblambdas* '())
;; Includes; these are handled by their respective IL systems
;; Not checked in any way shape or form; SRFI-0 would help
;; here. A user can use the config language to select which
;; includes they want to use, based on the output IL
(define *includes* '())
(define *debug* #f)
(define (show x (prefix "x: "))
(if *debug*
(begin
(display prefix)
(write x)
(newline))
#v)
x)
(define (p x) (display "x: ") (write x) (newline) x)
(define (enyalios@primitive? o)
(dict-has? *primitives* o))
(define (enyalios@procedure? o)
(dict-has? *procedures* o))
(define (enyalios@ulambda? o)
(dict-has? *ulambdas* o))
(define (enyalios@usyntax? o)
(dict-has? *usyntax* o))
(define (enyalios@var-prim? o)
(or
(eq? o 'list)
(eq? o 'vector)
(eq? o 'dict)
(eq? o 'string)))
(define (enyalios@parameter-call? o lparams)
(not (eq? (memq o (nth lparams "parameters" '())) #f)))
(define (member*? x l)
(cond
(null? l) #f
(pair? (car l)) (or (member*? x (car l)) (member*? x (cdr l)))
(eq? (car l) x) #t
else (member*? x (cdr l))))
(define (count-arities p req opt)
(cond
(null? p) (list req opt)
(symbol? (car p)) (count-arities (cdr p) (+ 1 req) opt)
(pair? (car p)) (count-arities (cdr p) req (+ 1 opt))))
(define (shadow-params params d)
(if (null? params)
d
(if (pair? (car params))
(shadow-params
(cdr params)
(append
d
(list (gensym (caar params)))))
(shadow-params
(cdr params)
(append
d
(list (gensym (car params))))))))
(define (set-arity! name params)
(let ((arities (count-arities params 0 0)))
(cset! *ulambdas* name
(vector
name
params
(car arities)
(cadr arities)
(shadow-params params '())
'()))))
(define (fixed-helper args idx max-len rewrites lparams)
(cond
(>= idx max-len)
'()
(null? args)
(cons '(c-nil) (fixed-helper args (+ idx 1) max-len rewrites lparams))
else
(cons
(cadr (generate-code (car args) '() #f rewrites lparams))
(fixed-helper (cdr args) (+ idx 1) max-len rewrites lparams))))
(define (compile-primitive block name tail? rewrites lparams)
(let ((prim (nth *primitives* (car block)))
(args (cdr block)))
;(display "compile-primitive ")
;(display (car block))
;(display " ; len(args) == ")
;(display (length args))
;(display "and primitive lens == ")
;(display (format "~a, ~a~%" (nth prim 2) (nth prim 3)))
(cond
(and
(= (nth prim 2) 0)
(= (nth prim 3) 0)
(= (length args) 0))
(list
#f
(list 'c-primitive-null (nth prim 0)))
(and
(>= (nth prim 2) 0)
(>= (length args) (nth prim 2))
(= (nth prim 3) -1))
(list
#f
(list 'c-primitive (nth prim 0)
(map (fn (x) (cadr (generate-code x '() #f rewrites lparams))) args)))
(and
(>= (length args) (nth prim 2))
(<= (length args) (nth prim 3)))
(list
#f
(list 'c-primitive-fixed (nth prim 0)
(fixed-helper args 0 (nth prim 3) rewrites lparams)))
else
(error (format "incorrect arity for primitive ~a" (car block))))))
(define (compile-primitive-procedure block name tail? rewrites lparams)
(let ((proc (nth *procedures* (car block)))
(args (cdr block))
(env (nth lparams "env" "tlenv")))
;(display "keys in lparams: ")
;(write (keys lparams))
;(newline)
(cond
(and
(= (nth proc 1) 0)
(= (nth proc 2) 0)
(= (length args) 0))
(list
#f
(list 'c-procedure (nth proc 0) '(c-nil) env))
(and
(> (nth proc 1) 0)
(>= (length args) (nth proc 1))
(= (nth proc 2) 0))
(list
#f
(list 'c-procedure (nth proc 0)
(map (fn (x) (cadr (generate-code x '() #f rewrites lparams))) args)
env))
(and
(>= (length args) (nth proc 1))
(<= (length args) (nth proc 2)))
(list
#f
(list 'c-procedure (nth proc 0)
(map (fn (x) (cadr (generate-code x '() #f rewrites lparams))) args)
env))
else (error (format "incorrect arity for primitive-procedure ~a" (car block))))))
(define (compile-variable-primitive block name tail? rewrites lparams)
(let ((hd (car block))
(args (cdr block)))
(list
#f
(list 'c-variable-primitive hd
(map (fn (x) (cadr (generate-code x '() #f rewrites lparams))) args)))))
(define (compile-lambda block name tail? rewrites lparams)
(let* ((params (car block))
(name (gensym 'fun_))
(nulparams (dict "params" params "name" name "env" (nth lparams "env" "tlenv"))) ;; should probably merge dicts here...
(body (compile-begin (cdr block) name #t rewrites nulparams)))
(set! *ooblambdas*
(cons
(list 'c-dec name params (cadr body))
*ooblambdas*))
(set-arity! name params)
(if tail?
(list #f (list 'c-function-reference name (length params)))
(list #f name))))
(define (merge-parameters lparams params)
(with ret (dict-copy (keys lparams) lparams (dict))
(cset! ret "parameters"
(append params (nth lparams "parameters" '())))
ret))
(define (compile-procedure block name tail? rewrites lparams)
" compile a top-level procedure, as opposed to
closure conversion of compile-lambda
May need to switch away from using compile-begin,
because procedures have some special cases, wrt
lambda lifting (although, thinking about it, so do
let blocks). Have to masticate on this more, but
this is a decent first start.
- The above actually looks pretty good after testing. One
other thing now though is if function params that are them
selves functions (HOF) should be analyzed here or in the
code generator. If it was handled here in some way, it would
make life a bit easier in the Code generator...
"
;(display (format "in compile-procedure; name == ~a, block == \n" name))
;(write block)
;(newline)
(let* ((params (car block))
(nulparams (merge-parameters lparams params)))
(cset! nulparams "parameters" (append params (nth lparams "parameters" '())))
(cset! nulparams "name" name)
;(display "block.rest == ")
;(write (cdr block))
;(newline)
(let ((body (compile-begin (cdr block) name #t rewrites nulparams)))
;(display "body == ")
;(write body)
;(newline)
(if (car body) ;; body contains a tail-call
(list
#f
(list 'c-dec name params
(list 'c-begin
(list 'c-shadow-params name)
(list 'c-loop (cadr body)))))
(list
#f
(list 'c-dec name params (cadr body)))))))
(define (make-struct-ctor name members)
(let ((ctor-name (coerce (format "make-~a" name) 'atom)))
(set-arity! ctor-name members)
(list
(list 'c-dec ctor-name members (list 'c-make-struct name members)))))
(define (make-struct-setter struct members)
(if (null? members)
'()
(let ((setter-name (coerce (format "~a-set-~a!" struct (car members)) 'atom))
(params '(x y)))
(set-arity! setter-name params)
(cons
(list 'c-dec setter-name params
(list 'c-begin
(list 'c-struct-set! 'x (car members) 'y struct)
(list 'c-return #v)))
(make-struct-setter struct (cdr members))))))
(define (make-struct-getter struct members)
(if (null? members)
'()
(let ((getter-name (coerce (format "~a-~a" struct (car members)) 'atom))
(params '(x)))
(set-arity! getter-name params)
(cons
(list 'c-dec getter-name params
(list 'c-struct-ref 'x (car members) struct))
(make-struct-getter struct (cdr members))))))
(define (make-struct-predicate name)
(let ((pred-name (coerce (format "~a?" name) 'atom))
(params '(x)))
(set-arity! pred-name params)
(list (list 'c-dec pred-name params
(list 'c-struct? 'x name)))))
(define (compile-struct code rewrites lparams)
"compiles a `define-struct` statement into IL.
PARAMETERS:
code: define block
rewrites : rewrite dict
lparams : local compiler parameters.
"
;; should decompose code here, esp. members, since if
;; the struct has inheritence, this won't work
(let ((ctor (make-struct-ctor (car code) (cadr code)))
(sets (make-struct-setter (car code) (cadr code)))
(gets (make-struct-getter (car code) (cadr code))) ;; need to define a ctor too...
(pred (make-struct-predicate (car code))))
(list
#f
(list
'c-begin
(append
(list
(list
'c-define-struct (car code) (cadr code)))
ctor
sets
gets
pred)))))
(define (compile-if block name tail? rewrites lparams)
" compiles an if statement into IL.
PARAMETERS:
block : scheme code
name : current function name, to support TCO
RETURNS:
(RECURSE? AST+)
"
(let* ((<cond> (cadr (generate-code (car block) name #f rewrites lparams)))
(<then> (generate-code (cadr block) name tail? rewrites lparams))
(<else> (generate-code (caddr block) name tail? rewrites lparams))
(<tail-check> (and tail? (or (car <then>) (car <else>)))))
;; need to check tail? here, and, if it is true,
;; add 'c-returns to each of (<then> <else>)
;; this check for #v should be extended to all non-function calls...
;; all literals?
;(display "\n<then> == ")
;(write <then>)
;(newline)
;(display "\n<else> == ")
;(write <else>)
;(newline)
(if (eq? (cadr <else>) #v)
(list
<tail-check>
(list 'c-if <cond> (returnable (cadr <then>) tail? #t)))
(list
<tail-check>
(list
'c-begin
(list 'c-if <cond> (returnable (cadr <then>) tail? #t))
(list 'c-else (returnable (cadr <else>) tail? #t)))))))
(define (cond-unzip block conds thens)
(cond
(null? block) (list (tconc->pair conds) (tconc->pair thens))
(null? (cdr block)) (error "incorrectly formated COND/CASE block")
else
(begin
(tconc! conds (car block))
(tconc! thens (cadr block))
(cond-unzip (cddr block) conds thens))))
(define (compile-cond block name tail? rewrites lparams)
" compiles a cond statement into IL.
PARAMETERS:
block: scheme code
name : current function name in TCO
tail? : boolean for tail calls
rewrites : any let renames.
lparams : dict containing function information (used outside of compile-cond)
RETURNS :
(RECURSE? AST+)
"
;; this is pretty low-level; should clean this up a bit.
;; especially the cond-list/then-list stuff. Actually, the
;; whole thing. Clean it up.
;; possiblity: pass (cddr block) into a "helper" lambda that
;; does the jobs of the below with less mess?
;; save on interation too; a helper could iterate through once,
;; whereas here we're iterating through several times...
(let* ((seps (cond-unzip block (make-tconc '()) (make-tconc '())))
(init-cond (generate-code (caar seps) name #f rewrites lparams))
(init-then (generate-code (caadr seps) name tail? rewrites lparams))
(cond-list (cdar seps))
(then-list (cdadr seps))
(tail-rec? #f))
(if (car init-then)
(set! tail-rec? #t)
#v)
(set! cond-list
(map
(fn (x)
(if (eq? x 'else)
x
(cadr (generate-code x name #f rewrites lparams))))
cond-list))
(set! then-list
(map
(fn (x)
(with res (generate-code x name tail? rewrites lparams)
(if (car res)
(set! tail-rec? #t)
#v)
(returnable (cadr res) tail? #t)))
then-list))
(list
tail-rec?
(list
'c-begin
(list
(cons
(cons 'c-if (list (cadr init-cond) (returnable (cadr init-then) tail? #t)))
(map
(fn (x1)
(if (eq? (car x1) 'else)
(list 'c-else (cadr x1))
(cons 'c-elif x1 )))
(zip cond-list then-list))))))))
(define (compile-case block name tail? rewrites lparams)
" compiles a CASE form into IL.
PARAMETERS:
block: scheme code
name : current function name in TCO
tail? : boolean for tail calls
rewrites : any let renames.
lparams : dict containing function information (used outside of compile-cond)
RETURNS :
(RECURSE? AST+)
"
(let* ((seps (cond-unzip (cdr block) (make-tconc '()) (make-tconc '())))
(cond-list (car seps))
(then-list (cadr seps))
(tail-rec? #f))
(set! then-list
(map
(fn (x)
(with res (generate-code x name tail? rewrites lparams)
(if (car res)
(set! tail-rec? #t)
#v)
(returnable (cadr res) tail?)))
then-list))
(list
tail-rec?
(cons
'c-case
(cons
(cadr (generate-code (car block) name #f rewrites lparams))
(zip cond-list then-list))))))
(define (il-syntax? c)
(cond
(not (pair? c))
#f
(pair? (car c))
(il-syntax? (car c))
(or
(eq? (car c) 'c-if)
(eq? (car c) 'c-elif)
(eq? (car c) 'c-else)
(eq? (car c) 'c-case)
(eq? (car c) 'c-begin)
(eq? (car c) 'c-set!)
(eq? (car c) 'c-return)
(eq? (car c) 'c-while)
(eq? (car c) 'c-for)
(eq? (car c) 'c-shadow-params)
(eq? (car c) 'c-var)
(eq? (car c) 'c-dec)
(eq? (car c) 'c-define-struct)
(eq? (car c) 'c-struct-set!)
(eq? (car c) 'c-tailcall)
(eq? (car c) 'c-struct-ref)
(eq? (car c) 'c-struct?)
(eq? (car c) 'c-docstring)
(eq? (car c) 'c-%prim)
(eq? (car c) 'c-catch)
(eq? (car c) 'c-throw)
(eq? (car c) 'c-loop))
#t
else
#f))
;; perhaps this should return a var assignment here...
;; ActionScript does something like that; it creates a lexically-scoped
;; dummy var for such purposes. Could be a per-scope gensym'd var...
(define (returnable c tail? (last? #f))
(cond
(and tail? (not (il-syntax? c))) (list 'c-return c)
(and last? (not (il-syntax? c))) (list 'c-no-return c)
else c))
(define (compile-begin block name tail? rewrites lparams)
(if tail?
(if (= (length block) 1)
(let ((x (generate-code (car block) name #t rewrites lparams)))
(list (car x)
(list 'c-begin (returnable (cdr x) tail?))))
(let ((b (map
(fn (x)
(if (string? x) ;; just a random string?
(list 'c-docstring x)
(cadr (generate-code x '() #f rewrites lparams))))
(cslice block 0 (- (length block) 1))))
(e (generate-code
(car (cslice block (- (length block) 1) (length block)))
name
tail?
rewrites lparams)))
;(display "\n\nb == ")
;(write b)
;(display "\n\ne == ")
;(write e)
;(display "\n\n")
(list
(car e)
(cons 'c-begin
(append b (list (returnable (cadr e) tail?)))))))
(list
#f
(cons 'c-begin
(map (fn (x) (cadr (generate-code x '() #f rewrites lparams))) block)))))
(define (logic-type t)
(cond
(eq? t 'and) 'c-and
(eq? t 'or) 'c-or
else (error "invalid logic operation lookup attempt")))
(define (compile-logic block name tail? rewrites lparams type)
" compile-logic: handle compilation of and/or/not syntax.
this is a somewhat-complicated generation scheme here, and I'm
not sure I like it. What's happening is that it binds the
individual operations of the form to temporaries, and then
returns a c-begin block with c-var declarations for each,
with a c-and/c-or/c-not IR. so:
(and (> x y) (< x z))
becomes:
(c-begin
(c-var it2 (c-primitive \"flt\" ( x y )))
(c-var it3 (c-primitive \"fgt\" (x z)))
(c-and it2 it3))
which makes if/cond kinda complicated (if less-so than
instances c-elif instances for cond). I'm thinking that something
like just returning (c-and form0 form1 ... formN) and having the
IR output:
if(((it = flt(x,y)) && it->type == BOOL && it->object.c) && ...)
which is much less complicated than the previous scheme, but relies
on destructive update within an if form, which is kinda yucky.
(let ((vals (map
(fn (x)
(list
'c-var
(gen-sym 'it)
(generate-code x '() #f rewrites lparams)))
block))
(ir-type (logic-type type)))
(list
#f
(cons
'c-begin
(list
(cons
ir-type
vals))))))
actually, thinking about it further, I do like the method expounded
previously, so I'm commenting out the above & just running with it."
;; need to do a "tail?" check here, and if so, check if the last
;; member is a tail call. Would be weird in an and/or block, but
;; kinda-sorta makes sense. Also, needs to use "returnable" anyway...
(let ((ir-type (logic-type type)))
(list #f
(cons
ir-type
(map
(fn (x) (show (cadr (generate-code x '() #f rewrites lparams)) "inside compile-logic: "))
block)))))
(define (compile-apply block name tail? rewrites lparams)
(cond
(enyalios@primitive? (car block))
(list
#f
(cons
'c-apply-primitive
(cons
(first (nth *primitives* (car block)))
(map
(fn (x)
(cadr
(generate-code x '() #f rewrites lparams)))
(cdr block)))))
(enyalios@var-prim? (car block))
(list
#f
(cons
'c-apply-variable-primitive
(cons
(nth *varprimitives* (car block))
(map
(fn (x)
(cadr
(generate-code x '() #f rewrites lparams)))
(cdr block)))))
else (error "enyalios does not support this type for use in APPLY")))
(define (generate-let-temps names d)
" generates temporary names for let
variables:
(let ((x 10) (y 30) (z 20))
(+ x y z))
SExp *tmp0 = 10, *tmp1 = 30, *tmp2 = 20;
return fplus(list(3,tmp0,tmp1,tmp2));
"
(if (null? names)
d
(begin
(cset! d (car names) (gensym 'tmp))
(generate-let-temps (cdr names) d))))
(define (dict-copy k dict new-dict)
"shallow copy a dictionary"
(if (null? k)
new-dict
(begin
(cset! new-dict (car k) (nth dict (car k)))
(dict-copy (cdr k) dict new-dict))))
(define (compile-let block name tail? rewrites lparams)
" uses dict-copy to merge rewrites into the new
generated list of let temporaries; new let bindings
shadow their higher-level counterparts
"
(let* ((vals (unzip (car block)))
(vars (car vals))
(data (cadr vals))
(var-temps (generate-let-temps
vars
(dict-copy
(keys rewrites)
rewrites (dict))))
(body (compile-begin (cdr block) name tail? var-temps lparams))
(nulparams (dict-copy (keys lparams) lparams (dict))))
(cset! nulparams "letvals" (cons vars (nth lparams "letvals" '())))
;(display "\n\nlet.body == ")
;(write body)
;(newline)
;(display "\n\nvars == ")
;(write vars)
;(display "\n")
(list
(car body)
(cons
'c-begin
(append
(map
(fn (x2) (list 'c-var
(nth var-temps (car x2))
(cadr (generate-code (cadr x2) name #f var-temps nulparams))))
(car block))
(cdr body))))))
(define (compile-let* block name tail? rewrites lparams)
#f)
(define (generate-code c name tail? rewrites lparams)
" Generates IL code for a given
preDigamma code. The tail? parameter
is used in code that doesn't need to
tail call optimize, like the <cond>
portion of an if block, non-tail members
of a begin, &c.
@params:
c : preDigamma code
name : lambda name for generating tail calls
tail? : boolean to determine whether or not to tail call optimize
@returns:
(PAIR-OF-ATOMS RECURSE?)
"
(cond
(null? c) '()
(or (vector? c)
(dict? c)
(goal? c)
(bool? c)
(char? c)
(string? c)
(number? c)
(void? c)
(key? c)
(eof-object? c))
(if tail?
(list #f (list 'c-return c))
(list #f c))
(symbol? c)
(if (and (dict? rewrites) (dict-has? rewrites c))
(if tail?
(list #f (list 'c-return (nth rewrites c)))
(list #f (nth rewrites c)))
(if tail?
(list #f (list 'c-return c))
(list #f c)))
(pair? (car c)) ;; lambda or the like?
(let ((proc-name (generate-code (car c) name #f rewrites lparams)))
(if (or (not (string? proc-name)) (not (symbol? proc-name)))
(error "Enyalios currently only supports lambda's in CAR position")
(list
#f
(list
'c-call
proc-name
(map
(fn (x) (cadr (generate-code x '() #f rewrites lparams)))
(cdr c))))))
(eq? (car c) '%prim) (list #f (list 'c-%prim (cadr c)))
(eq? (car c) '%include)
(begin
(set! *includes* (append *includes* (list (cdr c))))
(list #f (list 'c-nop)))
(eq? (car c) 'define-syntax)
(begin
(cset! *usyntax* (cadr c) (show (cddr c) "define-syntax capture: "))
(list #f (list 'c-nop)))
(eq? (car c) 'load)
(let* ((fh (open (coerce (cadr c) :string) :read))
(lines (enyalios@load fh))
(load-obj-code (map
(fn (line)
(cadr (generate-code line '() #f {} lparams)))
lines)))
(close fh)
(set! *ooblambdas* (append *ooblambdas* load-obj-code))
(list #f (list 'c-nop)))
(eq? (car c) 'define-external) (compile-external (cdr c) rewrites lparams)
(eq? (car c) 'define-struct) (compile-struct (cdr c) rewrites lparams)
(eq? (car c) 'define-type) (compile-define-type (cdr c) rewrites lparams)
(eq? (car c) 'if) (compile-if (cdr c) name tail? rewrites lparams)
(eq? (car c) 'cond) (compile-cond (cdr c) name tail? rewrites lparams)
(eq? (car c) 'case) (compile-case (cdr c) name tail? rewrites lparams)
(eq? (car c) 'do) (compile-do (cdr c) name tail? rewrites lparams)
(eq? (car c) 'catch)
(let ((body (compile-begin (cddr c) name tail? rewrites lparams)))
(list
(car body)
(list
'c-catch
(cadr c)
(cadr body))))
(eq? (car c) 'throw)
(list #f
(list
'c-throw
(cadr c)
(cadr (generate-code (caddr c) name #f rewrites lparams))))
(eq? (car c) 'quote)
(if (null? (cadr c))
'(#f (c-nil))
(list #f (list 'c-quote (cdr c))))
(or
(eq? (car c) 'define)
(eq? (car c) 'def)) ;; quite a bit of legacy F code uses def
(cond
(symbol? (cadr c))
(if (and
(pair? (caddr c))
(or
(eq? (car (caddr c)) 'lambda)
(eq? (car (caddr c)) 'fn)))
(compile-procedure (cdaddr c) (cadr c) #t rewrites lparams)
(list #f (list 'c-var (cadr c) (cadr (generate-code (caddr c) '() #f rewrites lparams)))))
(pair? (cadr c))
(compile-procedure (cons (cdadr c) (cddr c)) (caadr c) #t rewrites lparams)
else (error "illegal define form; DEFINE (SYMBOL | PAIR) FORM*"))
(or (eq? (car c) 'lambda)
(eq? (car c) 'fn))
(compile-lambda (cdr c) name tail? rewrites lparams)
(eq? (car c) 'let) (compile-let (cdr c) name tail? rewrites lparams)
(eq? (car c) 'let*) (compile-let (cdr c) name tail? rewrites lparams) ;; no difference in PreF
(eq? (car c) 'letrec) #t
; transform this into let, run same code
(eq? (car c) 'with)
(compile-let
(cons
(list (list (cadr c) (caddr c)))
(cdddr c))
name
tail?
rewrites lparams)
(eq? (car c) 'set!)
(list
#f
(list
'c-set!
(if (dict-has? rewrites (cadr c))
(nth rewrites (cadr c))
(cadr c))
(cadr (generate-code (caddr c) name #f rewrites lparams))))
(eq? (car c) 'apply)
(compile-apply (cdr c) name tail? rewrites lparams)
(eq? (car c) 'begin) (compile-begin (cdr c) name tail? rewrites lparams)
(and tail? ;; we don't want to check for tail-call in non-tail position
(eq? (car c) name)) ;; tail-call?
(list
#t