forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjulia-syntax.scm
3229 lines (2986 loc) · 96.3 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
(define (quoted? e) (memq (car e) '(quote top line break inert)))
(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))
;; allow (:: T) => (:: #gensym T) in formal argument lists
(define (fill-missing-argname a)
(if (and (pair? a) (eq? (car a) '|::|) (null? (cddr a)))
`(|::| ,(gensy) ,(cadr a))
a))
(define (fix-arglist l)
(if (any vararg? (butlast l))
(error "invalid ... on non-final argument"))
(map (lambda (a)
(if (and (pair? a) (eq? (car a) 'kw))
`(kw ,(fill-missing-argname (cadr a)) ,(caddr a))
(fill-missing-argname a)))
l))
(define (arg-name v)
(cond ((and (symbol? v) (not (eq? v 'true)) (not (eq? v 'false)))
v)
((not (pair? v))
(error (string "malformed function arguments \"" v "\"")))
(else
(case (car v)
((... kw) (decl-var (cadr v)))
((|::|) (decl-var v))
(else (error (string "malformed function argument \"" v "\"")))))))
; convert a lambda list into a list of just symbols
(define (llist-vars lst)
(map arg-name (filter (lambda (a)
(not (and (pair? a)
(eq? (car a) 'parameters))))
lst)))
(define (llist-keywords lst)
(apply append
(map (lambda (a)
(if (and (pair? a) (eq? (car a) 'parameters))
(map arg-name (cdr a))
'()))
lst)))
(define (arg-type v)
(cond ((symbol? v) 'Any)
((not (pair? v))
(error (string "malformed function arguments \"" v "\"")))
(else
(case (car v)
((...) `(... ,(decl-type (cadr v))))
((|::|) (decl-type v))
(else (error
(string "malformed function arguments \"" v "\"")))))))
; get just argument types
(define (llist-types lst)
(map arg-type lst))
(define (decl? e)
(and (pair? e) (eq? (car e) '|::|)))
; get the variable name part of a declaration, x::int => x
(define (decl-var v)
(if (decl? v) (cadr v) v))
(define (decl-type v)
(if (decl? v) (caddr v) 'Any))
(define (sym-dot? e)
(and (length= e 3) (eq? (car e) '|.|)
(symbol? (cadr e))))
(define (effect-free? e)
(or (not (pair? e)) (sym-dot? e) (quoted? e) (equal? e '(null))))
(define (undot-name e)
(if (symbol? e)
e
(cadr (caddr e))))
; make an expression safe for multiple evaluation
; for example a[f(x)] => (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 '()))
(cond
((and (decl? e) (symbol? (cadr e)))
(cons (cadr e) (list e)))
((not (pair? e))
(cons e '()))
(else
(cons (map (lambda (x)
(cond
((and (decl? x) (symbol? (cadr x)))
(set! a (cons x a))
(cadr x))
((not (effect-free? x))
(let ((g (gensy)))
(if (or (eq? (car x) '...) (eq? (car x) '&))
(if (and (pair? (cadr x))
(not (quoted? (cadr x))))
(begin (set! a (cons `(= ,g ,(cadr x)) a))
`(,(car x) ,g))
x)
(begin (set! a (cons `(= ,g ,x) a))
g))))
(else
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)))))
(define (expand-update-operator op lhs rhs)
(if (and (pair? lhs) (eq? (car lhs) 'ref))
;; expand indexing inside op= first, to remove "end" and ":"
(let* ((ex (partially-expand-ref lhs))
(stmts (butlast (cdr ex)))
(refex (last (cdr ex)))
(nuref `(ref ,(caddr refex) ,@(cdddr refex))))
`(block ,@stmts
,(expand-update-operator- op nuref rhs)))
(expand-update-operator- op lhs rhs)))
(define (dotop? o) (and (symbol? o) (eqv? (string.char (string o) 0) #\.)))
(define (partially-expand-ref e)
(let ((a (cadr e))
(idxs (cddr e)))
(let* ((reuse (and (pair? a)
(contains (lambda (x)
(or (eq? x 'end)
(eq? x ':)
(and (pair? x)
(eq? (car x) ':))))
idxs)))
(arr (if reuse (gensy) a))
(stmts (if reuse `((= ,arr ,a)) '())))
(receive
(new-idxs stuff) (process-indexes arr idxs)
`(block
,@(append stmts stuff)
(call getindex ,arr ,@new-idxs))))))
;; accumulate a series of comparisons, with the given "and" constructor,
;; exit criteria, and "take" function that consumes part of a list,
;; returning (expression . rest)
(define (comp-accum e make-and done? take)
(let loop ((e e)
(expr '()))
(if (done? e) (cons expr e)
(let ((ex_rest (take e)))
(loop (cdr ex_rest)
(if (null? expr)
(car ex_rest)
(make-and expr (car ex_rest))))))))
(define (add-init arg arg2 expr)
(if (eq? arg arg2) expr
`(block (= ,arg2 ,arg) ,expr)))
;; generate a comparison from e.g. (a < b ...)
;; returning (expr . rest)
(define (compare-one e)
(let* ((arg (caddr e))
(arg2 (if (and (pair? arg)
(pair? (cdddr e)))
(gensy) arg)))
(if (and (not (dotop? (cadr e)))
(length> e 5)
(pair? (cadddr (cdr e)))
(dotop? (cadddr (cddr e))))
;; look ahead: if the 2nd argument of the next comparison is also
;; an argument to an eager (dot) op, make sure we don't skip the
;; initialization of its variable by short-circuiting
(let ((s (gensy)))
(cons `(block
,@(if (eq? arg arg2) '() `((= ,arg2 ,arg)))
(= ,s ,(cadddr (cdr e)))
(call ,(cadr e) ,(car e) ,arg2))
(list* arg2 (cadddr e) s (cddddr (cdr e)))))
(cons
(add-init arg arg2
`(call ,(cadr e) ,(car e) ,arg2))
(cons arg2 (cdddr e))))))
;; convert a series of scalar comparisons into && expressions
(define (expand-scalar-compare e)
(comp-accum e
(lambda (a b) `(&& ,a ,b))
(lambda (x) (or (not (length> x 2)) (dotop? (cadr x))))
compare-one))
;; convert a series of scalar and vector comparisons into & calls,
;; combining as many scalar comparisons as possible into short-circuit
;; && sequences.
(define (expand-vector-compare e)
(comp-accum e
(lambda (a b) `(call & ,a ,b))
(lambda (x) (not (length> x 2)))
(lambda (e)
(if (dotop? (cadr e))
(compare-one e)
(expand-scalar-compare e)))))
(define (expand-compare-chain e)
(car (expand-vector-compare e)))
;; last = is this last index?
(define (end-val a n tuples last)
(if (null? tuples)
(if last
(if (= n 1)
`(call (top endof) ,a)
`(call (top trailingsize) ,a ,n))
#;`(call (top div)
(call (top length) ,a)
(call (top *)
,@(map (lambda (d) `(call (top size) ,a ,(1+ d)))
(iota (- n 1)))))
`(call (top size) ,a ,n))
(let ((dimno `(call (top +) ,(- n (length tuples))
,.(map (lambda (t) `(call (top length) ,t))
tuples))))
(if last
`(call (top trailingsize) ,a ,dimno)
`(call (top size) ,a ,dimno)))))
; replace end inside ex with (call (top size) a n)
; affects only the closest ref expression, so doesn't go inside nested refs
(define (replace-end ex a n tuples last)
(cond ((eq? ex 'end) (end-val a n tuples last))
((or (atom? ex) (quoted? ex)) ex)
((eq? (car ex) 'ref)
;; inside ref only replace within the first argument
(list* 'ref (replace-end (cadr ex) a n tuples last)
(cddr ex)))
(else
(cons (car ex)
(map (lambda (x) (replace-end x a n tuples last))
(cdr ex))))))
; translate index x from colons to ranges
(define (expand-index-colon x)
(cond ((eq? x ':) `(call colon 1 end))
((and (pair? x)
(eq? (car x) ':))
(cond ((length= x 3)
(if (eq? (caddr x) ':)
;; (: a :) a:
`(call colon ,(cadr x) end)
;; (: a b)
`(call colon ,(cadr x) ,(caddr x))))
((length= x 4)
(if (eq? (cadddr x) ':)
;; (: a b :) a:b:
`(call colon ,(cadr x) ,(caddr x) end)
;; (: a b c)
`(call colon ,@(cdr x))))
(else x)))
(else x)))
;; : inside indexing means 1:end
;; expand end to size(a,n),
;; or div(length(a), prod(size(a)[1:(n-1)])) for the last index
;; a = array being indexed, i = list of indexes
;; returns (values index-list stmts) where stmts are statements that need
;; to execute first.
(define (process-indexes a i)
(let loop ((lst i)
(n 1)
(stmts '())
(tuples '())
(ret '()))
(if (null? lst)
(values (reverse ret) (reverse stmts))
(let ((idx (car lst))
(last (null? (cdr lst))))
(if (and (pair? idx) (eq? (car idx) '...))
(if (symbol? (cadr idx))
(loop (cdr lst) (+ n 1)
stmts
(cons (cadr idx) tuples)
(cons `(... ,(replace-end (cadr idx) a n tuples last))
ret))
(let ((g (gensy)))
(loop (cdr lst) (+ n 1)
(cons `(= ,g ,(replace-end (cadr idx) a n tuples last))
stmts)
(cons g tuples)
(cons `(... ,g) ret))))
(loop (cdr lst) (+ n 1)
stmts tuples
(cons (replace-end (expand-index-colon idx) a n tuples last)
ret)))))))
(define (make-decl n t) `(|::| ,n ,(if (and (pair? t) (eq? (car t) '...))
`(curly Vararg ,(cadr t))
t)))
(define (function-expr argl body)
(let ((t (llist-types argl))
(n (llist-vars argl)))
(if (has-dups n)
(error "function argument names not unique"))
(let ((argl (map make-decl n t)))
`(lambda ,argl
(scope-block ,body)))))
;; GF method does not need to keep decl expressions on lambda args
;; except for rest arg
(define (method-lambda-expr argl body)
(let ((argl (map (lambda (x)
(if (and (pair? x) (eq? (car x) '...))
(make-decl (arg-name x) (arg-type x))
(arg-name x)))
argl)))
`(lambda ,argl
(scope-block ,body))))
(define (symbols->typevars sl upperbounds bnd)
(let ((bnd (if bnd '(true) '())))
(if (null? upperbounds)
(map (lambda (x) `(call (top TypeVar) ',x ,@bnd)) sl)
(map (lambda (x ub) `(call (top TypeVar) ',x ,ub ,@bnd)) sl upperbounds))))
(define (sparam-name sp)
(cond ((symbol? sp)
sp)
((and (length= sp 3)
(eq? (car sp) '|<:|)
(symbol? (cadr sp)))
(cadr sp))
(else (error "malformed type parameter list"))))
(define (sparam-name-bounds sparams names bounds)
(cond ((null? sparams)
(values (reverse names) (reverse bounds)))
((symbol? (car sparams))
(sparam-name-bounds (cdr sparams) (cons (car sparams) names)
(cons '(top Any) bounds)))
((and (length= (car sparams) 3)
(eq? (caar sparams) '|<:|)
(symbol? (cadar sparams)))
(sparam-name-bounds (cdr sparams) (cons (cadr (car sparams)) names)
(cons (caddr (car sparams)) bounds)))
(else
(error "malformed type parameter list"))))
(define (method-expr-name m)
(let ((lhs (cadr m)))
(cond ((symbol? lhs) lhs)
((eq? (car lhs) 'kw) (cadr lhs))
(else lhs))))
(define (sym-ref? e)
(or (symbol? e)
(and (length= e 3) (eq? (car e) '|.|)
(or (atom? (cadr e)) (sym-ref? (cadr e)))
(pair? (caddr e)) (eq? (car (caddr e)) 'quote)
(symbol? (cadr (caddr e))))))
(define (method-def-expr- name sparams argl body)
(receive
(names bounds) (sparam-name-bounds sparams '() '())
(begin
(let ((anames (llist-vars argl)))
(if (has-dups anames)
(error "function argument names not unique"))
(if (has-dups names)
(error "function static parameter names not unique"))
(if (any (lambda (x) (memq x names)) anames)
(error "function argument and static parameter names must be distinct")))
(if (not (or (sym-ref? name)
(and (pair? name) (eq? (car name) 'kw)
(sym-ref? (cadr name)))))
(error (string "invalid method name \"" name "\"")))
(let* ((types (llist-types argl))
(body (method-lambda-expr argl body)))
(if (null? sparams)
`(method ,name (tuple (tuple ,@types) (tuple)) ,body)
`(method ,name
(call (lambda ,names
(tuple (tuple ,@types) (tuple ,@names)))
,@(symbols->typevars names bounds #t))
,body))))))
(define (vararg? x) (and (pair? x) (eq? (car x) '...)))
(define (trans? x) (and (pair? x) (eq? (car x) '|.'|)))
(define (ctrans? x) (and (pair? x) (eq? (car x) '|'|)))
(define (const-default? x)
(or (number? x) (string? x) (char? x) (and (pair? x) (eq? (car x) 'quote))))
(define (keywords-method-def-expr name sparams argl body)
(let* ((kargl (cdar argl)) ;; keyword expressions (= k v)
(pargl (cdr argl)) ;; positional args
;; 1-element list of vararg argument, or empty if none
(vararg (let ((l (if (null? pargl) '() (last pargl))))
(if (vararg? l)
(list l) '())))
;; positional args without vararg
(pargl (if (null? vararg) pargl (butlast pargl)))
;; keywords glob
(restkw (let ((l (last kargl)))
(if (vararg? l)
(list (cadr l)) '())))
(kargl (if (null? restkw) kargl (butlast kargl)))
;; the keyword::Type expressions
(vars (map cadr kargl))
;; keyword default values
(vals (map caddr kargl))
;; just the keyword names
(keynames (map decl-var vars))
;; do some default values depend on other keyword arguments?
(ordered-defaults (any (lambda (v) (contains
(lambda (x) (eq? x v))
vals))
keynames))
;; 1-element list of function's line number node, or empty if none
(lno (if (and (pair? (cdr body))
(pair? (cadr body)) (eq? (caadr body) 'line))
(list (cadr body))
'()))
;; body statements, minus line number node
(stmts (if (null? lno) (cdr body) (cddr body)))
(positional-sparams
(filter (lambda (s)
(let ((name (if (symbol? s) s (cadr s))))
(or (expr-contains-eq name (cons 'list pargl))
(and (pair? vararg) (expr-contains-eq name (car vararg)))
(not (expr-contains-eq name (cons 'list kargl))))))
sparams))
(keyword-sparams
(filter (lambda (s)
(let ((name (if (symbol? s) s (cadr s))))
(not (expr-contains-eq name (cons 'list positional-sparams)))))
sparams))
(keyword-sparam-names
(map (lambda (s) (if (symbol? s) s (cadr s))) keyword-sparams)))
(let ((kw (gensy)) (i (gensy)) (ii (gensy)) (elt (gensy)) (rkw (gensy))
(mangled (symbol (string "__"
(undot-name name)
"#"
(string.sub (string (gensym)) 1)
"__")))
(flags (map (lambda (x) (gensy)) vals)))
`(block
;; call with keyword args pre-sorted - original method code goes here
,(method-def-expr-
mangled sparams
`(,@vars ,@restkw ,@pargl ,@vararg)
`(block
,@(if (null? lno) '()
(list (append (car lno) (list (undot-name name)))))
,@stmts))
;; call with no keyword args
,(method-def-expr-
name positional-sparams (append pargl vararg)
`(block
,(if (null? lno)
`(line 0 || ||)
(append (car lno) '(||)))
,@(if (not ordered-defaults)
'()
(map make-assignment keynames vals))
;; call mangled(vals..., [rest_kw ,]pargs..., [vararg]...)
(return (call ,mangled
,@(if ordered-defaults keynames vals)
,@(if (null? restkw) '() '((cell1d)))
,@(map arg-name pargl)
,@(if (null? vararg) '()
(list `(... ,(arg-name (car vararg)))))))))
;; call with unsorted keyword args. this sorts and re-dispatches.
,(method-def-expr-
(list 'kw name) (filter
;; remove sparams that don't occur, to avoid printing
;; the warning twice
(lambda (s)
(let ((name (if (symbol? s) s (cadr s))))
(expr-contains-eq name (cons 'list argl))))
positional-sparams)
`((:: ,kw (top Array)) ,@pargl ,@vararg)
`(block
(line 0 || ||)
;; initialize keyword args to their defaults, or set a flag telling
;; whether this keyword needs to be set.
,@(map (lambda (name dflt flag)
(if (const-default? dflt)
`(= ,name ,dflt)
`(= ,flag true)))
keynames vals flags)
,@(if (null? restkw) '()
`((= ,rkw (cell1d))))
;; for i = 1:(length(kw)>>1)
(for (= ,i (: 1 (call (top >>) (call (top length) ,kw) 1)))
(block
;; ii = i*2 - 1
(= ,ii (call (top -) (call (top *) ,i 2) 1))
(= ,elt (call (top arrayref) ,kw ,ii))
,(foldl (lambda (kvf else)
(let* ((k (car kvf))
(rval0 `(call (top arrayref) ,kw
(call (top +) ,ii 1)))
;; note: if the "declared" type of a KW arg
;; includes something from keyword-sparam-names,
;; then don't assert it here, since those static
;; parameters don't have values yet.
;; instead, the type will be picked up when the
;; underlying method is called.
(rval (if (and (decl? k)
(not (any (lambda (s)
(expr-contains-eq s (caddr k)))
keyword-sparam-names)))
`(call (top typeassert)
,rval0
,(caddr k))
rval0)))
;; if kw[ii] == 'k; k = kw[ii+1]::Type; end
`(if (comparison ,elt === (quote ,(decl-var k)))
(block
(= ,(decl-var k) ,rval)
,@(if (not (const-default? (cadr kvf)))
`((= ,(caddr kvf) false))
'()))
,else)))
(if (null? restkw)
;; if no rest kw, give error for unrecognized
`(call (top error) "unrecognized keyword argument \"" ,elt "\"")
;; otherwise add to rest keywords
`(ccall 'jl_cell_1d_push Void (tuple Any Any)
,rkw (tuple ,elt
(call (top arrayref) ,kw
(call (top +) ,ii 1)))))
(map list vars vals flags))))
;; set keywords that weren't present to their default values
,@(apply append
(map (lambda (name dflt flag)
(if (const-default? dflt)
'()
`((if ,flag (= ,name ,dflt)))))
keynames vals flags))
;; finally, call the core function
(return (call ,mangled
,@keynames
,@(if (null? restkw) '() (list rkw))
,@(map arg-name pargl)
,@(if (null? vararg) '()
(list `(... ,(arg-name (car vararg)))))))))
;; return primary function
,name))))
(define (optional-positional-defs name sparams req opt dfl body overall-argl . kw)
(let ((lno (if (and (pair? (cdr body))
(pair? (cadr body)) (eq? (caadr body) 'line))
(list (cadr body))
'())))
`(block
,@(map (lambda (n)
(let* ((passed (append req (list-head opt n)))
;; only keep static parameters used by these arguments
(sp (filter (lambda (sp)
(contains (lambda (e) (eq? e (sparam-name sp)))
passed))
sparams))
(vals (list-tail dfl n))
(absent (list-tail opt n)) ;; absent arguments
(body
(if (any (lambda (defaultv)
;; does any default val expression...
(contains (lambda (e)
;; contain "e" such that...
(any (lambda (a)
;; "e" is in an absent arg
(contains (lambda (u)
(eq? u e))
a))
absent))
defaultv))
vals)
;; then add only one next argument
`(block
,@lno
(call ,name ,@kw ,@(map arg-name passed) ,(car vals)))
;; otherwise add all
`(block
,@lno
(call ,name ,@kw ,@(map arg-name passed) ,@vals)))))
(method-def-expr name sp (append kw passed) body)))
(iota (length opt)))
,(method-def-expr name sparams overall-argl body))))
(define (method-def-expr name sparams argl body)
(if (any kwarg? argl)
;; has optional positional args
(begin
(let check ((l argl)
(seen? #f))
(if (pair? l)
(if (kwarg? (car l))
(check (cdr l) #t)
(if (and seen? (not (vararg? (car l))))
(error "optional positional arguments must occur at end")
(check (cdr l) #f)))))
(receive
(kws argl) (separate kwarg? argl)
(let ((opt (map cadr kws))
(dfl (map caddr kws)))
(if (has-parameters? argl)
;; both!
;; separate into keyword version with all positional args,
;; and a series of optional-positional-defs that delegate keywords
(let ((kw (car argl))
(argl (cdr argl)))
(check-kw-args (cdr kw))
(receive
(vararg req) (separate vararg? argl)
(optional-positional-defs name sparams req opt dfl body
(cons kw (append req opt vararg))
`(parameters (... ,(gensy))))))
;; optional positional only
(receive
(vararg req) (separate vararg? argl)
(optional-positional-defs name sparams req opt dfl body
(append req opt vararg)))))))
(if (has-parameters? argl)
;; keywords only
(begin (check-kw-args (cdar argl))
(keywords-method-def-expr name sparams argl body))
;; neither
(method-def-expr- name sparams argl body))))
(define (struct-def-expr name params super fields mut)
(receive
(params bounds) (sparam-name-bounds params '() '())
(struct-def-expr- name params bounds super (flatten-blocks fields) mut)))
;; replace field names with gensyms if they conflict with field-types
(define (safe-field-names field-names field-types)
(if (any (lambda (v) (contains (lambda (e) (eq? e v)) field-types))
field-names)
(map (lambda (x) (gensy)) field-names)
field-names))
(define (default-inner-ctor name field-names field-types)
(let ((field-names (safe-field-names field-names field-types)))
`(function (call ,name ,@field-names)
(block
(call new ,@field-names)))))
(define (default-outer-ctor name field-names field-types params bounds)
(let ((field-names (safe-field-names field-names field-types)))
`(function (call (curly ,name
,@(map (lambda (p b) `(<: ,p ,b))
params bounds))
,@(map make-decl field-names field-types))
(block
(call (curly ,name ,@params) ,@field-names)))))
(define (new-call Texpr args field-names field-types mutabl)
(if (any vararg? args)
(error "... is not supported inside \"new\""))
(if (any kwarg? args)
(error "\"new\" does not accept keyword arguments"))
(cond ((length> args (length field-names))
`(call (top error) "new: too many arguments"))
(else
`(new ,Texpr
,@(map (lambda (fty val)
`(call (top convert) ,fty ,val))
(list-head field-types (length args)) args)))))
;; insert a statement after line number node
(define (prepend-stmt stmt body)
(if (and (pair? body) (eq? (car body) 'block))
(cond ((atom? (cdr body))
`(block ,stmt (null)))
((and (pair? (cadr body)) (eq? (caadr body) 'line))
`(block ,(cadr body) ,stmt ,@(cddr body)))
(else
`(block ,stmt ,@(cdr body))))
body))
(define (rewrite-ctor ctor Tname params field-names field-types mutabl iname)
(define (ctor-body body)
(prepend-stmt
`(global ,Tname)
(pattern-replace (pattern-set
(pattern-lambda
(call (-/ new) . args)
(new-call (if (null? params)
;; be careful to avoid possible conflicts
;; with local & arg names
`(|.| ,(current-julia-module) ',Tname)
`(curly (|.| ,(current-julia-module) ',Tname)
,@params))
args
field-names
field-types
mutabl)))
body)))
(let ((ctor2
(pattern-replace
(pattern-set
(pattern-lambda (function (call (curly name . p) . sig) body)
`(function (call (curly ,(if (eq? name Tname) iname name) ,@p) ,@sig)
,(ctor-body body)))
(pattern-lambda (function (call name . sig) body)
`(function (call ,(if (eq? name Tname) iname name) ,@sig)
,(ctor-body body)))
(pattern-lambda (= (call (curly name . p) . sig) body)
`(= (call (curly ,(if (eq? name Tname) iname name) ,@p) ,@sig)
,(ctor-body body)))
(pattern-lambda (= (call name . sig) body)
`(= (call ,(if (eq? name Tname) iname name) ,@sig)
,(ctor-body body))))
ctor)))
ctor2))
;; remove line numbers and nested blocks
(define (flatten-blocks e)
(if (atom? e)
e
(apply append!
(map (lambda (x)
(cond ((atom? x) (list x))
((eq? (car x) 'line) '())
((eq? (car x) 'block) (cdr (flatten-blocks x)))
(else (list x))))
e))))
(define (struct-def-expr- name params bounds super fields mut)
(receive
(fields defs) (separate (lambda (x) (or (symbol? x) (decl? x)))
fields)
(let* ((defs (filter (lambda (x) (not (effect-free? x))) defs))
(field-names (map decl-var fields))
(field-types (map decl-type fields))
(defs2 (if (null? defs)
(list (default-inner-ctor name field-names field-types))
defs)))
(if (null? params)
`(block
(global ,name)
(const ,name)
(composite_type ,name (tuple ,@params)
(tuple ,@(map (lambda (x) `',x) field-names))
(null) ,super (tuple ,@field-types)
,mut)
(call
(lambda ()
(scope-block
(block
(global ,name)
,@(map (lambda (c)
(rewrite-ctor c name '() field-names field-types mut name))
defs2)))))
(null))
;; parametric case
`(block
(scope-block
(block
(global ,name)
(const ,name)
,@(map (lambda (v) `(local ,v)) params)
,@(map make-assignment params (symbols->typevars params bounds #t))
(composite_type ,name (tuple ,@params)
(tuple ,@(map (lambda (x) `',x) field-names))
,(let ((instantiation-name (gensy)))
`(lambda (,instantiation-name)
(scope-block
;; don't capture params; in here they are static
;; parameters
(block
(global ,@params)
,@(map
(lambda (c)
(rewrite-ctor c name params field-names
field-types mut instantiation-name))
defs2)
,name))))
,super (tuple ,@field-types)
,mut)))
(scope-block
(block
(global ,name)
(global ,@params)
,@(if (and (null? defs)
;; don't generate an outer constructor if the type has
;; parameters not mentioned in the field types. such a
;; constructor would not be callable anyway.
(every (lambda (sp)
(expr-contains-eq sp (cons 'list field-types)))
params))
`(,(default-outer-ctor name field-names field-types
params bounds))
'())))
(null))))))
(define (abstract-type-def-expr name params super)
(receive
(params bounds)
(sparam-name-bounds params '() '())
`(block
(const ,name)
,@(map (lambda (v) `(local ,v)) params)
,@(map make-assignment params (symbols->typevars params bounds #f))
(abstract_type ,name (tuple ,@params) ,super))))
(define (bits-def-expr n name params super)
(receive
(params bounds)
(sparam-name-bounds params '() '())
`(block
(const ,name)
,@(map (lambda (v) `(local ,v)) params)
,@(map make-assignment params (symbols->typevars params bounds #f))
(bits_type ,name (tuple ,@params) ,n ,super))))
; take apart a type signature, e.g. T{X} <: S{Y}
(define (analyze-type-sig ex)
(or ((pattern-lambda (-- name (-s))
(values name '() 'Any)) ex)
((pattern-lambda (curly (-- name (-s)) . params)
(values name params 'Any)) ex)
((pattern-lambda (|<:| (-- name (-s)) super)
(values name '() super)) ex)
((pattern-lambda (|<:| (curly (-- name (-s)) . params) super)
(values name params super)) ex)
(error "invalid type signature")))
;; insert calls to convert() in ccall, and pull out expressions that might
;; need to be rooted before conversion.
(define (lower-ccall name RT atypes args)
(define (ccall-conversion T x)
(cond ((eq? T 'Any) x)
((and (pair? x) (eq? (car x) '&))
`(& (call (top ptr_arg_convert) ,T ,(cadr x))))
(else
`(call (top cconvert) ,T ,x))))
(define (argument-root a)
;; something to keep rooted for this argument
(cond ((and (pair? a) (eq? (car a) '&))
(argument-root (cadr a)))
((and (pair? a) (sym-dot? a))
(cadr a))
((symbol? a) a)
(else 0)))
(let loop ((F atypes) ;; formals
(A args) ;; actuals
(stmts '()) ;; initializers
(C '())) ;; converted
(if (or (null? F) (null? A))
`(block
,.(reverse! stmts)
(call (top ccall) ,name ,RT (tuple ,@atypes) ,.(reverse! C)
,@A))
(let* ((a (car A))
(isseq (and (pair? (car F)) (eq? (caar F) '...)))
(ty (if isseq (cadar F) (car F)))
(rt (if (eq? ty 'Any)
0
(argument-root a)))
(ca (cond ((eq? ty 'Any)
a)
((and (pair? a) (eq? (car a) '&))
(if (and (pair? (cadr a)) (not (sym-dot? (cadr a))))
(let ((g (gensy)))
(begin
(set! stmts (cons `(= ,g ,(cadr a)) stmts))
`(& ,g)))
a))
((and (pair? a) (not (sym-dot? a)) (not (quoted? a)))
(let ((g (gensy)))
(begin
(set! stmts (cons `(= ,g ,a) stmts))
g)))
(else
a))))
(loop (if isseq F (cdr F)) (cdr A) stmts
(list* rt (ccall-conversion ty ca) C))))))
(define (block-returns? e)
(if (assignment? e)
(block-returns? (caddr e))
(and (pair? e) (eq? (car e) 'block)
(any return? (cdr e)))))
(define (replace-return e bb ret retval)
(cond ((or (atom? e) (quoted? e)) e)
((or (eq? (car e) 'lambda)
(eq? (car e) 'function)
(eq? (car e) '->)) e)
((eq? (car e) 'return)
`(block ,@(if ret `((= ,ret true)) '())
(= ,retval ,(cadr e))
(break ,bb)))
(else (map (lambda (x) (replace-return x bb ret retval)) e))))
(define (expand-binding-forms e)
(cond
((atom? e) e)
((quoted? e) e)
(else
(case (car e)
((function)
(let ((name (cadr e)))
(if (pair? name)
(if (eq? (car name) 'call)
(expand-binding-forms
(if (and (pair? (cadr name))
(eq? (car (cadr name)) 'curly))
(method-def-expr (cadr (cadr name))
(cddr (cadr name))
(fix-arglist (cddr name))
(caddr e))
(method-def-expr (cadr name)
'()
(fix-arglist (cddr name))
(caddr e))))
(if (eq? (car name) 'tuple)
(expand-binding-forms
`(-> ,name ,(caddr e)))
e))
e)))
((->)
(let ((a (cadr e))
(b (caddr e)))
(let ((a (if (and (pair? a)
(eq? (car a) 'tuple))
(cdr a)
(list a))))
(expand-binding-forms
(function-expr (fix-arglist a)
`(block
,.(map (lambda (d)
`(= ,(cadr d)
(typeassert ,@(cdr d))))
(filter decl? a))
,b))))))
((let)
(let ((ex (cadr e))
(binds (cddr e)))
(expand-binding-forms
(if
(null? binds)
`(scope-block (block ,ex))
(let loop ((binds (reverse binds))
(blk ex))
(if (null? binds)
blk
(cond
((or (symbol? (car binds)) (decl? (car binds)))
;; just symbol -> add local
(loop (cdr binds)
`(scope-block
(block
(local ,(car binds))
(newvar ,(decl-var (car binds)))
,blk))))
((and (length= (car binds) 3)
(eq? (caar binds) '=))
;; some kind of assignment
(cond
((or (symbol? (cadar binds))
(decl? (cadar binds)))
(let ((vname (decl-var (cadar binds))))
(loop (cdr binds)
(if (contains (lambda (x) (eq? x vname))
(caddar binds))
(let ((tmp (gensy)))
`(scope-block
(block
(local (= ,tmp ,(caddar binds)))
(scope-block
(block
(local ,(cadar binds))
(newvar ,vname)
(= ,vname ,tmp)
,blk)))))
`(scope-block
(block
(local ,(cadar binds))
(newvar ,vname)
(= ,vname ,(caddar binds))
,blk))))))
((and (pair? (cadar binds))
(eq? (caadar binds) 'call))