forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
julia-syntax.scm
1259 lines (1136 loc) · 38.2 KB
/
julia-syntax.scm
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
; TODO:
; * expand lvalues, e.g. (= (call ref A i) x) => (= A (call assign A i x))
; * expand operators like +=
; * expand for into while
; * expand -> and function into lambda/addmethod
; * replace (. a b) with (call get a (quote b))
; * tuple destructuring
; - validate argument lists, replace a=b in arg lists with keyword exprs
; - type parameter renaming stuff
; - use (top x) more consistently
; * make goto-form safe for inlining (delay label to index mapping)
(define *julia-interpreter* #f)
(define (quoted? e) (memq (car e) '(quote top unbound line break)))
(define (lam:args x) (cadr x))
(define (lam:vars x) (llist-vars (lam:args x)))
(define (lam:vinfo x) (caddr x))
(define (lam:body x) (cadddr x))
; convert x => (x), (tuple x y) => (x y)
; used to normalize function signatures like "x->y" and "function +(a,b)"
(define (fsig-to-lambda-list arglist)
(if (pair? arglist)
(if (eq? (car arglist) 'tuple)
(cdr arglist)
arglist)
(if (symbol? arglist)
(list arglist)
arglist)))
(define (arg-name v)
(if (symbol? v)
v
(case (car v)
((...) (decl-var (cadr v)))
((= keyword) (decl-var (caddr v)))
((|::|) (decl-var v))
(else (error "malformed function argument" v)))))
; convert a lambda list into a list of just symbols
(define (llist-vars lst)
(map arg-name lst))
; get just argument types
(define (llist-types lst)
(map (lambda (v)
(cond ((symbol? v) 'Any)
((not (pair? v))
(error "malformed function arguments"))
(else
(case (car v)
((...) `(... ,(decl-type (cadr v))))
((= keyword) (decl-type (caddr v)))
((|::|) (decl-type v))
(else (error "malformed function arguments" lst))))))
lst))
; get the variable name part of a declaration, x::int => x
(define (decl-var v)
(if (and (pair? v) (eq? (car v) '|::|))
(cadr v)
v))
(define (decl-type v)
(if (and (pair? v) (eq? (car v) '|::|))
(caddr v)
'Any))
; make an expression safe for multiple evaluation
; for example a[f(x)] => do(temp=f(x), a[temp])
; retuns a pair (expr . assignments)
; where 'assignments' is a list of needed assignment statements
(define (remove-argument-side-effects e)
(let ((a '()))
(if (not (pair? e))
(cons e '())
(cons (map (lambda (x)
(if (pair? x)
(let ((g (gensym)))
(set! a (cons `(= ,g ,x) a))
g)
x))
e)
(reverse a)))))
(define (expand-update-operator op lhs rhs)
(let ((e (remove-argument-side-effects lhs)))
`(block ,@(cdr e) (= ,(car e) (call ,op ,(car e) ,rhs)))))
; (a > b > c) => (&& (call > a b) (call > b c))
(define (expand-compare-chain e)
(if (length> e 3)
(let ((arg2 (caddr e)))
(if (pair? arg2)
(let ((g (gensym)))
`(&& (call ,(cadr e) ,(car e) (= ,g ,arg2))
,(expand-compare-chain (cons g (cdddr e)))))
`(&& (call ,(cadr e) ,(car e) ,arg2)
,(expand-compare-chain (cddr e)))))
`(call ,(cadr e) ,(car e) ,(caddr e))))
; : inside indexing means :1:
; a:b and a:b:c are ranges instead of calls to colon
(define (process-indexes i)
(map (lambda (x)
(cond ((eq? x ':) '(: (: 1 :)))
((and (pair? x)
(eq? (car x) ':)
(length= x 3)
(not (eq? (caddr x) ':)))
`(call (top Range) ,(cadr x) 1 ,(caddr x)))
((and (pair? x)
(eq? (car x) ':)
(length= x 4)
(not (eq? (cadddr x) ':)))
`(call (top Range) ,@(cdr x)))
(else x)))
i))
(define (function-expr argl body)
(let ((t (llist-types argl))
(n (llist-vars argl)))
(let ((argl (map (lambda (n t) `(|::| ,n ,t))
n t)))
`(lambda ,argl
(scope-block ,body)))))
(define (symbols->typevars sl)
(map (lambda (x) `(call (top typevar) ',x)) sl))
(define (gf-def-expr- name argl argtypes body)
(gf-def-expr-- name argl argtypes body 'new_generic_function 'add_method))
(define (gf-def-expr-- name argl argtypes body new add)
`(block
,(if (symbol? name)
`(if (unbound ,name)
(= ,name (call (top ,new) (quote ,name))))
`(null))
(call (top ,add)
,name
,argtypes
,(function-expr argl body))))
(define (generic-function-def-expr name sparams argl body)
(let* ((argl (fsig-to-lambda-list argl))
(types (llist-types argl)))
(gf-def-expr-
name argl
(if (null? sparams)
`(tuple ,@types)
`(call (lambda ,sparams
(tuple ,@types))
,@(symbols->typevars sparams)))
body)))
(define (struct-def-expr name params super fields)
; extract the name from a function def expr
(define (f-exp-name e)
(let ((head (cadadr e)))
(if (symbol? head) head
(cadr head))))
(receive
(funcs fields) (separate
(lambda (x)
(and (pair? x)
(or (eq? (car x) 'function)
(and (length= x 3)
(eq? (car x) '=)
(pair? (cadr x))
(eq? (caadr x) 'call))
(not (or (eq? (car x) '|::|)
(error "invalid struct syntax:" x))))))
fields)
(let ((field-names (map decl-var fields))
(field-types (map decl-type fields))
(func-names (delete-duplicates (map f-exp-name funcs)))
(func-args (map (lambda (fexp)
(fsig-to-lambda-list (cddadr fexp))) funcs))
(func-types (map (lambda (x) (gensym)) funcs))
(func-sparms (map (lambda (fexp)
(let ((head (cadadr fexp)))
(if (symbol? head) '()
(cddr head))))
funcs))
(tvars (gensym))
(proto (gensym)))
`(call
(lambda ()
(scope-block
(block
(local (tuple ,@func-names ,@func-types ,tvars ,proto))
(call
(lambda (,@params)
; the static parameters are bound to new TypeVars in here,
; so everything that sees the TypeVars is evaluated here.
(block
(= ,tvars (tuple ,@params))
(= ,proto
(call (top new_struct_type)
(quote ,name)
,super
,tvars
(tuple ,@(map (lambda (x) `',x) field-names))))
; wrap type prototype in a type constructor
(= ,name (call (top new_type_constructor) ,tvars ,proto))
; now add the type fields, which might reference the type
; itself. tie the recursive knot.
(call (top new_struct_fields)
,name (tuple ,@field-types))
,@(map (lambda (type argl sp)
`(= ,type
,(if (null? sp)
`(tuple ,@(llist-types argl))
`(call (lambda ,sp
(tuple ,@(llist-types argl)))
,@(symbols->typevars sp)))))
func-types func-args func-sparms)))
,@(symbols->typevars params))
; build method definitions
,@(map (lambda (fdef fargs ftype)
(gf-def-expr- (f-exp-name fdef)
fargs
ftype
(caddr fdef)))
funcs func-args func-types)
; assign methods to type fields
,@(map (lambda (fname)
`(= (|.| ,proto ,fname) ,fname))
func-names))))))))
(define (type-def-expr name params super)
`(block
(call
(lambda ,params
(block
(= ,name
(call (top new_tag_type)
(quote ,name)
,super
(tuple ,@params)))
,(if (null? params)
`(null)
`(= ,name (call (top new_type_constructor)
(tuple ,@params) ,name)))))
,@(symbols->typevars params))))
(define *anonymous-generic-function-name* (gensym))
(define dotdotdotpattern (pattern-lambda (... a) `(curly ... ,a)))
; patterns that introduce lambdas
(define binding-form-patterns
(pattern-set
; function with static parameters
(pattern-lambda (function (call (curly name . sparams) . argl) body)
(generic-function-def-expr name sparams argl body))
; function definition
(pattern-lambda (function (call name . argl) body)
(generic-function-def-expr name '() argl body))
(pattern-lambda (function (-- arg (-s)) body)
`(-> ,arg ,body))
(pattern-lambda (function (tuple . args) body)
`(-> (tuple ,@args) ,body))
; expression form function definition
(pattern-lambda (= (call (curly name . sparams) . argl) body)
`(function (call (curly ,name . ,sparams) . ,argl) ,body))
(pattern-lambda (= (call name . argl) body)
`(function (call ,name ,@argl) ,body))
(pattern-lambda (-> a b)
(let ((a (if (and (pair? a)
(eq? (car a) 'tuple))
(cdr a)
(list a))))
; TODO: anonymous generic function
(function-expr a b)))
; type definition
(pattern-lambda (struct (-- name (-s)) (block . fields))
(struct-def-expr name '() 'Any fields))
(pattern-lambda (struct (curly (-- name (-s)) . params) (block . fields))
(struct-def-expr name params 'Any fields))
(pattern-lambda (struct (comparison (-- name (-s)) (-/ |<:|) super)
(block . fields))
(struct-def-expr name '() super fields))
(pattern-lambda (struct (comparison (curly (-- name (-s)) . params)
(-/ |<:|) super)
(block . fields))
(struct-def-expr name params super fields))
; macro for timing evaluation
(pattern-lambda (call (-/ time) expr)
`(call time_thunk (-> (tuple) ,expr)))
)) ; binding-form-patterns
(define patterns
(pattern-set
#;(pattern-lambda (--> a b)
`(call curly Function ,a ,b))
(pattern-lambda (|.| a b)
`(call (top getfield) ,a (quote ,b)))
(pattern-lambda (= (|.| a b) rhs)
`(call (top setfield) ,a (quote ,b) ,rhs))
(pattern-lambda (type (-- name (-s)))
(type-def-expr name '() 'Any))
(pattern-lambda (type (curly (-- name (-s)) . params))
(type-def-expr name params 'Any))
(pattern-lambda (type (comparison (-- name (-s)) (-/ |<:|) super))
(type-def-expr name '() super))
(pattern-lambda (type (comparison (curly (-- name (-s)) . params)
(-/ |<:|) super))
(type-def-expr name params super))
; typealias is an assignment; should be const when that exists
(pattern-lambda (typealias (-- name (-s)) type-ex)
`(= ,name ,type-ex))
(pattern-lambda (typealias (curly (-- name (-s)) . params) type-ex)
`(call (lambda ,params
(= ,name (call (top new_type_constructor)
(tuple ,@params) ,type-ex)))
,@(symbols->typevars params)))
(pattern-lambda (comparison . chain) (expand-compare-chain chain))
; multiple value assignment
(pattern-lambda (= (tuple . lhss) x)
(let ((t (gensym)))
`(block (= ,t ,x)
,@(let loop ((lhs lhss)
(i 1))
(if (null? lhs) '((null))
(cons `(= ,(car lhs)
(call tupleref ,t ,i))
(loop (cdr lhs)
(+ i 1))))))))
(pattern-lambda (= (ref a . idxs) rhs)
`(call set ,a ,rhs ,@(process-indexes idxs)))
(pattern-lambda (ref a . idxs)
`(call ref ,a ,@(process-indexes idxs)))
(pattern-lambda (cell . elts)
`(call (top cell_literal) ,@elts))
(pattern-lambda (curly type . elts)
`(call (top instantiate_type) ,type ,@elts))
; call with splat
(pattern-lambda (call f ... (... _) ...)
(let ((argl (cddr __)))
; wrap sequences of non-... arguments in tuple()
(define (tuple-wrap a run)
(if (null? a)
(if (null? run) '()
(list `(call tuple ,@(reverse run))))
(let ((x (car a)))
(if (and (length= x 2)
(eq? (car x) '...))
(if (null? run)
(list* (cadr x)
(tuple-wrap (cdr a) '()))
(list* `(call tuple ,@(reverse run))
(cadr x)
(tuple-wrap (cdr a) '())))
(tuple-wrap (cdr a) (cons x run))))))
`(call apply ,f ,@(tuple-wrap argl '()))))
; tuple syntax (a, b...)
; note, inside tuple ... means sequence type
(pattern-lambda (tuple . args)
(pattern-expand (list dotdotdotpattern)
`(call tuple ,@args)))
dotdotdotpattern
; local x,y,z => local x;local y;local z
(pattern-lambda (local (tuple . vars))
`(block
,@(map (lambda (x) `(local ,x)) vars)))
; local x::int=2 => local x::int; x=2
(pattern-lambda (local (= var rhs))
`(block (local ,var)
(= ,(decl-var var) ,rhs)))
; x::T = rhs => x::T; x = rhs
(pattern-lambda (= (|::| x T) rhs)
(let ((e (remove-argument-side-effects x)))
`(block ,@(cdr e)
(|::| ,(car e) ,T)
(= ,(car e) ,rhs))))
; adding break/continue support to while loop
(pattern-lambda (while cnd body)
`(scope-block
(break-block loop-exit
(_while ,cnd
(break-block loop-cont
,body)))))
(pattern-lambda (break) '(break loop-exit))
(pattern-lambda (continue) '(break loop-cont))
;; for loops
; for loop over ranges
(pattern-lambda
(for (= var (: a b (-? c))) body)
(begin
(if (not (symbol? var))
(error "invalid for loop syntax: expected symbol"))
(if c
(let ((cnt (gensym))
(lim (gensym)))
`(scope-block
(block
(= ,cnt 0)
(= ,lim (call div (call - ,c ,a) ,b))
(break-block loop-exit
(_while (call <= ,cnt ,lim)
(block
(= ,var (call + ,a (call * ,cnt ,b)))
(break-block loop-cont
,body)
(= ,cnt (call + 1 ,cnt))))))))
(let ((lim (gensym)))
`(scope-block
(block
(= ,var ,a)
(= ,lim ,b)
(break-block loop-exit
(_while (call <= ,var ,lim)
(block
(break-block loop-cont
,body)
(= ,var (call + 1 ,var)))))))))))
; for loop over arbitrary vectors
(pattern-lambda
(for (= i X) body)
(let ((coll (gensym))
(state (gensym)))
`(scope-block
(block (= ,coll ,X)
(= ,state (call (top start) ,coll))
(while (call (top !) (call (top done) ,coll ,state))
(block
(= (tuple ,i ,state) (call (top next) ,coll ,state))
,body))))))
; update operators
(pattern-lambda (+= a b) (expand-update-operator '+ a b))
(pattern-lambda (-= a b) (expand-update-operator '- a b))
(pattern-lambda (*= a b) (expand-update-operator '* a b))
(pattern-lambda (.*= a b) (expand-update-operator '.* a b))
(pattern-lambda (/= a b) (expand-update-operator '/ a b))
(pattern-lambda (./= a b) (expand-update-operator './ a b))
(pattern-lambda (//= a b) (expand-update-operator '// a b))
(pattern-lambda (.//= a b) (expand-update-operator '.// a b))
(pattern-lambda (|\\=| a b) (expand-update-operator '|\\| a b))
(pattern-lambda (|.\\=| a b) (expand-update-operator '|.\\| a b))
(pattern-lambda (^= a b) (expand-update-operator '^ a b))
(pattern-lambda (.^= a b) (expand-update-operator '.^ a b))
(pattern-lambda (%= a b) (expand-update-operator '% a b))
(pattern-lambda (|\|=| a b) (expand-update-operator '|\|| a b))
(pattern-lambda (&= a b) (expand-update-operator '& a b))
(pattern-lambda ($= a b) (expand-update-operator '$ a b))
(pattern-lambda (<<= a b) (expand-update-operator '<< a b))
(pattern-lambda (>>= a b) (expand-update-operator '>> a b))
;; colon
(pattern-lambda (: a (-/ :))
`(call (top RangeFrom) ,a 1))
(pattern-lambda (: a b (-/ :))
`(call (top RangeFrom) ,a ,b))
(pattern-lambda (: (: b (-/ :)))
`(call (top RangeBy) ,b))
(pattern-lambda (: (: b c))
`(call (top RangeTo) ,b ,c))
(pattern-lambda (: c)
`(call (top RangeTo) 1 ,c))
(pattern-lambda
(: a b (-? c))
(if c
`(call colon ,a ,c ,b)
`(call colon ,a ,b 1)))
;; hcat, vcat
(pattern-lambda (hcat . a)
`(call hcat ,@a))
(pattern-lambda (vcat . a)
`(call vcat ,@a))
)) ; patterns
; patterns that verify all syntactic sugar was well-formed
; if any sugary forms remain after the above patterns, it means the
; patterns didn't match, which implies a syntax error.
(define check-desugared
(pattern-set
(pattern-lambda (function . any)
(error "invalid function definition"))
(pattern-lambda (for . any)
(error "invalid for loop syntax"))
(pattern-lambda (type . any)
(error "invalid type definition"))
(pattern-lambda (typealias . any)
(error "invalid typealias statement"))
(pattern-lambda (macro . any)
(error "macros must be defined at the top level"))
))
;; Comprehensions
(define identify-comprehensions
(pattern-set
(pattern-lambda
(hcat (if expr expr-then (call (-/ |\||) expr-else i)) . rest)
`(comprehension-int (if ,expr ,expr-then ,expr-else) ,i ,@rest))
(pattern-lambda
(hcat (= (if expr expr-then (call (-/ |\||) expr-else i)) range) . rest)
`(comprehension-int (if ,expr ,expr-then ,expr-else) (= ,i ,range) ,@rest))
(pattern-lambda
(hcat (call (-/ |\||) expr (= i range)) . rest)
`(comprehension-int ,expr (= ,i ,range) ,@rest))
(pattern-lambda
(hcat (= (call (-/ |\||) expr i) range) . rest)
`(comprehension-int ,expr (= ,i ,range) ,@rest))
(pattern-lambda
(hcat (call (-/ |\||) expr i) . rest)
`(comprehension-int ,expr ,i ,@rest))
(pattern-lambda
(comprehension-int expr . ranges)
`(comprehension
,expr
,@(map (lambda (r) (pattern-expand
(list
(pattern-lambda (: a b) `(call (top Range) ,a 1 ,b))
(pattern-lambda (: a b c) `(call (top Range) ,a ,b ,c)) )
r))
ranges) ))
(pattern-lambda
(comprehension-int expr . ranges)
`(comprehension ,expr ,@ranges))
)) ;; identify-comprehensions
(define lower-comprehensions
(pattern-set
(pattern-lambda
(comprehension expr . ranges)
(let ( (result (gensym)) (ri (gensym)) (oneresult (gensym)) )
;; evaluate one expression to figure out type and size
;; compute just one value by inserting a break inside loops
(define (evaluate-one ranges)
(if (null? ranges)
`(= ,oneresult ,expr)
(if (eq? (car ranges) `:)
(evaluate-one (cdr ranges))
`(for ,(car ranges)
(block ,(evaluate-one (cdr ranges))
(break)) ))))
;; compute the dimensions of the result
(define (compute-dims ranges oneresult-dim)
(if (null? ranges)
(list)
(if (eq? (car ranges) `:)
(cons `(call size ,oneresult ,oneresult-dim) (compute-dims (cdr ranges) (+ oneresult-dim 1)))
(cons `(call length ,(car ranges)) (compute-dims (cdr ranges) oneresult-dim)) )))
;; construct loops to cycle over all dimensions of an n-d comprehension
(define (construct-loops ranges iters oneresult-dim)
(if (null? ranges)
(if (null? iters)
`(block (call set ,result ,expr ,ri)
(+= ,ri 1))
`(block (call set ,result (ref ,expr ,@(reverse iters)) ,ri)
(+= ,ri 1)) )
(if (eq? (car ranges) `:)
(let ((i (gensym)))
`(for (= ,i (: 1 (call size ,oneresult ,oneresult-dim)))
,(construct-loops (cdr ranges) (cons i iters) (+ oneresult-dim 1)) ))
`(for ,(car ranges)
,(construct-loops (cdr ranges) iters oneresult-dim) ))))
;; Evaluate the comprehension
`(scope-block
(block
(= ,oneresult (tuple))
,(evaluate-one ranges)
(= ,result (call jl_comprehension_zeros ,oneresult ,@(compute-dims ranges 1) ))
(= ,ri 1)
,(construct-loops (reverse ranges) (list) 1)
,result ))))
)) ;; lower-comprehensions
; (op (op a b) c) => (op a b c) etc.
(define (flatten-op op e)
(if (not (pair? e)) e
(if (eq? (car e) op)
(apply append (map (lambda (x)
(let ((x (flatten-op op x)))
(if (and (pair? x)
(eq? (car x) op))
(cdr x)
(list x))))
e))
(map (lambda (x) (flatten-op op x)) e))))
(define (expand-and-or e)
(if (or (not (pair? e)) (quoted? e)) e
(case (car e)
((&&) (let ((e (flatten-op '&& e)))
(let loop ((tail (cdr e)))
(if (null? tail)
'true
(if (null? (cdr tail))
(expand-and-or (car tail))
`(if ,(expand-and-or (car tail))
,(loop (cdr tail))
false))))))
((|\|\||) (let ((e (flatten-op '|\|\|| e)))
(let loop ((tail (cdr e)))
(if (null? tail)
'false
(if (null? (cdr tail))
(expand-and-or (car tail))
(if (symbol? (car tail))
`(if ,(car tail) ,(car tail)
,(loop (cdr tail)))
(let ((g (gensym)))
`(block (= ,g ,(expand-and-or (car tail)))
(if ,g ,g
,(loop (cdr tail)))))))))))
(else (map expand-and-or e)))))
; conversion to "linear flow form"
;
; This pass removes control flow constructs from value position.
; A "control flow construct" is anything that would require a branch.
; (block ... (value-expr ... control-expr ...) ...) =>
; (block ... (= var control-expr) (value-expr ... var ...) ...)
; except the assignment is incorporated into control-expr, so that
; control exprs only occur in statement position.
;
; The conversion works by passing around the intended destination of
; the value being computed: #f for statement position, #t for value position,
; or a symbol if the value needs to be assigned to a particular variable.
; This is the "dest" argument to to-lff.
;
; This also keeps track of tail position, and converts the code so that
; everything in tail position is returned explicitly.
;
; The result is that every expression whose value is needed is either
; a function argument, an assignment RHS, or returned explicitly.
; In this form, expressions can be analyzed freely without fear of
; intervening branches. Similarly, control flow can be analyzed without
; worrying about implicit value locations (the "evaluation stack").
(define (to-LFF e)
(define (to-blk r)
(if (length= r 1)
(car r)
(cons 'block (reverse r))))
(define (blk-tail r)
(reverse r))
; to-lff returns (new-ex . stmts) where stmts is a list of statements that
; must run before new-ex is valid.
;
; If the input expression needed to be removed from its original context,
; like the 'if' in "1+if(a,b,c)", then new-ex is a symbol holding the
; result of the expression.
;
; If dest is a symbol or #f, new-ex can be a statement.
;
; We essentially maintain a stack of control-flow constructs that need to be
; run in statement position as we walk around an expression. If we hit
; statement context, we can dump the control-flow stuff there.
; This expression walk is entirely within the "else" clause of the giant
; case expression. Everything else deals with special forms.
(define (to-lff e dest tail)
(if (or (not (pair? e)) (quoted? e) (equal? e '(null)))
(cond ((symbol? dest) (cons `(= ,dest ,e) '()))
(dest (cons (if tail `(return ,e) e)
'()))
(else (cons e '())))
(case (car e)
((=) (let ((r (to-lff (caddr e) (cadr e) #f)))
(cond ((symbol? dest)
(cons `(block ,(car r)
(= ,dest ,(cadr e)))
(cdr r)))
(dest
(cons (if tail `(return ,(cadr e)) (cadr e)) r))
(else r))))
((if)
(cond ((or tail (eq? dest #f) (symbol? dest))
(let ((r (to-lff (cadr e) #t #f)))
(cons `(if
,(car r)
,(to-blk (to-lff (caddr e) dest tail))
,(if (length= e 4)
(to-blk (to-lff (cadddr e) dest tail))
(to-blk (to-lff '(null) dest tail))))
(cdr r))))
(else (let ((g (gensym)))
(cons g
(to-lff e g #f))))))
((block)
(let* ((g (gensym))
(stmts
(let loop ((tl (cdr e)))
(if (null? tl) '()
(if (null? (cdr tl))
(cond ((or tail (eq? dest #f) (symbol? dest))
(blk-tail (to-lff (car tl) dest tail)))
(else
(blk-tail (to-lff (car tl) g tail))))
(cons (to-blk (to-lff (car tl) #f #f))
(loop (cdr tl))))))))
(if (and (eq? dest #t) (not tail))
(cons g (reverse stmts))
(if (and tail (null? stmts))
(cons '(return (null))
'())
(cons (cons 'block stmts)
'())))))
((return)
(if (and dest (not tail))
(error "misplaced return statement")
(to-lff (cadr e) #t #t)))
((_while) (cond ((eq? dest #t)
(cons (if tail '(return (null)) '(null))
(to-lff e #f #f)))
(else
(let* ((r (to-lff (cadr e) #t #f))
(w (cons `(_while ,(car r)
,(to-blk
(to-lff (caddr e) #f #f)))
(cdr r))))
(if (symbol? dest)
(cons `(= ,dest (null)) w)
w)))))
((break-block)
(let ((r (to-lff (caddr e) dest tail)))
(if dest
(cons (car r)
(list `(break-block ,(cadr e) ,(to-blk (cdr r)))))
(cons `(break-block ,(cadr e) ,(car r))
(cdr r)))))
((scope-block)
(if (and dest (not tail))
(let* ((g (gensym))
(r (to-lff (cadr e) g tail)))
(cons (car (to-lff g dest tail))
; tricky: need to introduce a new local outside the
; scope-block so the scope-block's value can propagate
; out. otherwise the value could be inaccessible due
; to being wrapped inside a scope.
`((scope-block ,(to-blk r))
(local! ,g))))
(let ((r (to-lff (cadr e) dest tail)))
(cons `(scope-block ,(to-blk r))
'()))))
((break) (if dest
(error "misplaced break or continue")
(cons e '())))
((lambda)
(let ((l `(lambda ,(cadr e)
,(to-blk (to-lff (caddr e) #t #t)))))
(if (symbol? dest)
(cons `(= ,dest ,l) '())
(cons (if tail `(return ,l) l) '()))))
((local)
(if (symbol? dest)
(error "misplaced local declaration"))
(cons (to-blk (to-lff '(null) dest tail))
(list e)))
(else
(let ((r (map (lambda (arg) (to-lff arg #t #f))
e)))
(cond ((symbol? dest)
(cons `(= ,dest ,(map car r))
(apply append (map cdr r))))
(else
(let ((ex (map car r)))
(cons (if tail `(return ,ex) ex)
(apply append (map cdr r)))))))))))
(to-blk (to-lff e #t #t)))
#|
future issue:
right now scope blocks need to be inside functions:
> (julia-expand '(block (call + 1 (scope-block (block (= a b) c)))))
(block (scope-block (local a) (local #:g13) (block (= a b) (= #:g13 c)))
(return (call + 1 #:g13)))
> (julia-expand '(scope-block (call + 1 (scope-block (block (= a b) c)))))
(scope-block
(local #:g15)
(block (scope-block (local a) (block (= a b) (= #:g15 c)))
(return (call + 1 #:g15))))
The first one gave something broken, but the second case works.
So far only the second case can actually occur.
|#
; local variable identification
; convert (scope-block x) to `(scope-block ,@locals ,x)
; where locals is a list of (local x) expressions, derived from two sources:
; 1. (local x) expressions inside this scope-block and lambda
; 2. variables assigned inside this scope-block that don't exist in outer
; scopes
(define (identify-locals e)
(define (find-assigned-vars e env)
(if (or (not (pair? e)) (quoted? e))
'()
(case (car e)
((lambda scope-block) '())
((local local!) (list (decl-var (cadr e))))
((=)
(let ((others (find-assigned-vars (caddr e) env))
(v (decl-var (cadr e))))
(if (memq v env)
others
(cons v others))))
(else
(apply append (map (lambda (x) (find-assigned-vars x env))
e))))))
(define (add-local-decls e env)
(if (or (not (pair? e)) (quoted? e)) e
(cond ((eq? (car e) 'lambda)
(let* ((env (append (lam:vars e) env))
(body (add-local-decls (caddr e) env)))
(list 'lambda (cadr e) body)))
((eq? (car e) 'scope-block)
(let* ((vars (delete-duplicates
(find-assigned-vars (cadr e) env)))
(body (add-local-decls (cadr e) (append vars env))))
`(scope-block ,@(map (lambda (v) `(local ,v))
vars)
,body)))
(else
; form (local! x) adds a local to a normal (non-scope) block
(let ((newenv (append (declared-local!-vars e) env)))
(map (lambda (x)
(add-local-decls x newenv))
e))))))
(add-local-decls e '()))
(define (declared-local-vars e)
(map (lambda (x) (decl-var (cadr x)))
(filter (lambda (x)
(and (pair? x)
(or (eq? (car x) 'local)
(eq? (car x) 'local!))))
(cdr e))))
(define (declared-local!-vars e)
(map cadr
(filter (lambda (x)
(and (pair? x)
(eq? (car x) 'local!)))
(cdr e))))
; e - expression
; renames - assoc list of (oldname . newname)
; this works on any tree format after identify-locals
(define (rename-vars e renames)
(define (without alst remove)
(cond ((null? alst) '())
((null? remove) alst)
((memq (caar alst) remove) (without (cdr alst) remove))
(else (cons (car last)
(without (cdr alst) remove)))))
(cond ((null? renames) e)
((symbol? e) (lookup e renames e))
((not (pair? e)) e)
((quoted? e) e)
(else
(let (; remove vars bound by current expr from rename list
(new-renames (without renames
(case (car e)
((lambda) (lam:vars e))
((scope-block) (declared-local-vars e))
(else '())))))
(cons (car e)
(map (lambda (x)
(rename-vars x new-renames))
(cdr e)))))))
; remove (scope-block) and (local), convert lambdas to the form
; (lambda (argname...) (locals var...) body)
(define (flatten-scopes e)
(define scope-block-vars '())
(define (remove-scope-blocks e)
(cond ((or (atom? e) (quoted? e)) e)
((eq? (car e) 'lambda) (flatten-scopes e))
((eq? (car e) 'scope-block)
(let ((vars (declared-local-vars e))
(body (car (last-pair e))))
(let* ((newnames (map (lambda (x) (gensym)) vars))
(bod (rename-vars (remove-scope-blocks body)
(map cons vars newnames))))
(set! scope-block-vars (nconc newnames scope-block-vars))
bod)))
(else (map remove-scope-blocks e))))
(cond ((not (pair? e)) e)
((quoted? e) e)
((eq? (car e) 'lambda)
(let* ((argnames (lam:vars e))
(body0 (caddr e))
(body (if (eq? (car body0) 'scope-block)
(car (last-pair body0))
body0))
(l0
(if (eq? (car body0) 'scope-block)
(filter ; remove locals conflicting with arg names
(lambda (v) (not (memq v argnames)))
(declared-local-vars body0))
(declared-local!-vars body0)))
(r-s-b (remove-scope-blocks body)))
`(lambda ,(cadr e)
(locals ,@l0 ,@scope-block-vars)
,r-s-b)))
(else (map (lambda (x) (if (not (pair? x)) x
(flatten-scopes x)))
e))))
(define (make-var-info name) (list name 'Any #f))
(define (var-info/t name type) (list name type #f))
(define vinfo:name car)
(define vinfo:type cadr)
(define vinfo:capt caddr)
(define (vinfo:set-type! v t) (set-car! (cdr v) t))
(define (vinfo:set-capt! v c) (set-car! (cddr v) c))
(define var-info-for assq)
(define (lambda-all-vars e)
(append (lam:vars e)
(cdr (caddr e))))
(define (fix-seq-type t)
; wrap (call (top instantiate_type) ... . args) in (tuple ...)
(if (and (length> t 2)
(eq? (car t) 'call)
(equal? (cadr t) '(top instantiate_type))
(eq? (caddr t) '...))
`(call (top tuple) ,t)
t))
(define (free-vars e)
(cond ((symbol? e) (list e))
((or (atom? e) (quoted? e)) '())
((eq? (car e) 'lambda)
(diff (free-vars (lam:body e))