-
Notifications
You must be signed in to change notification settings - Fork 0
/
ac.scm
1585 lines (1281 loc) · 46 KB
/
ac.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
; Arc Compiler.
#lang racket/base
(require json)
(require racket/string)
(require racket/list)
(require racket/vector)
(require racket/sequence)
(require racket/file)
(require racket/port)
(require racket/pretty)
(require racket/runtime-path)
(require racket/system)
(require racket/tcp)
(require racket/unsafe/ops)
(require racket/async-channel)
(require racket/undefined)
(require ffi/unsafe)
(require "./ar.scm")
; configure reader
; (read-square-bracket-with-tag #t)
; (read-curly-brace-with-tag #t)
(print-hash-table #t)
(print-syntax-width 10000)
; sread = scheme read. eventually replace by writing read
(define (shebang? (port (current-input-port)))
(equal? "#!" (peek-string 2 0 port)))
(define (skip-shebang! (port (current-input-port)))
(when (shebang? port)
(read-line port)))
(define (sread (p (current-input-port)) (eof eof))
(parameterize ((read-accept-lang #t)
(read-accept-reader #t))
(port-count-lines! p)
(skip-shebang! p)
(let ((expr (read-syntax (object-name p) p)))
(if (eof-object? expr) eof expr))))
(define (sdata (p (current-input-port)) (eof eof))
(parameterize ((read-accept-lang #f)
(read-accept-reader #f))
(port-count-lines! p)
(skip-shebang! p)
(let ((expr (read p)))
(if (eof-object? expr) eof (ac-quoted expr)))))
(define (syn x (src #f))
(if (syntax? x)
x
(datum->syntax #f x (if (syntax? src) src #f))))
(define (datum x)
(if (syntax? x) (syntax->datum x) x))
(define env* (make-parameter (list) #f 'env*))
; compile an Arc expression into a Scheme expression,
; both represented as s-expressions.
; env is a list of lexically bound variables, which we
; need in order to decide whether set should create a global.
(define (ac s)
(set! s (ac-macex s))
(cond ((syntax? s) (syn (ac (syntax->datum s)) s))
((literal? s) (ac-literal s))
((ssyntax? s) (ac (expand-ssyntax s)))
((symbol? s) (ac-var-ref s))
((ar-car? s '%do) (ac-do (cdr s)))
((ar-car? s 'lexenv) (ac-lexenv))
((ar-car? s 'syntax) (scm-quoted (cadr s)))
((ar-caar? s 'syntax) (map ac s))
((ar-car? s ssyntax?) (ac (cons (expand-ssyntax (car s)) (cdr s))))
((ar-car? s 'quote) (list 'quote (ac-quoted (cadr s))))
((ar-car? s 'quasiquote) (ac-qq (cadr s)))
((ar-car? s 'quasisyntax) (ac-qs (cadr s)))
((ar-car? s 'if) (ac-if (cdr s)))
((ar-car? s 'fn) (ac-fn (cadr s) (cddr s)))
((ar-car? s 'assign) (ac-set (cdr s)))
; the next three clauses could be removed without changing semantics
; ... except that they work for macros (so prob should do this for
; every elt of s, not just the car)
((ar-caar? s 'compose) (ac (decompose (cdar s) (cdr s))))
((ar-caar? s 'complement)
(ac (list 'complement (cons (cadar s) (cdr s)))))
((ar-caar? s 'andf) (ac-andf s))
((pair? s) (ac-call (car s) (cdr s)))
(#t s)))
(define atstrings #t)
(define (ac-string s)
(if atstrings
(if (atpos s 0)
(ac (cons 'string (map (lambda (x)
(if (string? x)
(unescape-ats x)
x))
(codestring s))))
(list 'string-copy (unescape-ats s)))
(list 'string-copy s))) ; avoid immutable strings
(define (ac-flag? x)
(and (symbol? x)
(let ((s (symbol->string x)))
(and (> (string-length s) 1)
(eq? (string-ref s 0) #\:)
(not (eq? (string-ref s 1) #\:))
(string->symbol (substring s 1))))))
(define (ac-unflag x)
(let* ((n (ac-flag? x))
(k (and n (symbol->keyword n)))
(v (and n (if (ac-lex? n) n ar-t))))
(if n `(,k ,v) `(,x))))
(define (ac-unflag-args args)
(apply append (imap ac-unflag args)))
(define (literal? x)
(or (boolean? x)
(char? x)
(string? x)
(number? x)
(bytes? x)
(ar-false? x)
(syntax? x)
(ar-keyword? x)
(ar-tagged? x)
(ac-number-literal x)))
(define (ac-literal x)
(cond ((null? x) (list 'quote x))
((ar-tagged? x) (list 'quote x))
((string? x) (ac-string x))
(#t (ac-quoted x))))
(define (ac-identifier x)
(string->symbol (string-append " " (symbol->string x))))
(define (ac-identifier? x)
(and (symbol? x) (string-prefix? (symbol->string x) " ")))
(define (ssyntax? x)
(and (symbol? x)
(not (eqv? x '_))
(not (ac-identifier? x))
(has-ssyntax-char? (symbol->string x))))
(define (has-ssyntax-char? string (i (- (string-length string) 2)))
(and (>= i 0)
(let ((c (string-ref string i)))
(if (= i 0)
(or (eqv? c #\~) (eqv? c #\!))
(or (eqv? c #\:) (eqv? c #\~)
(eqv? c #\&)
;(eqv? c #\_)
(eqv? c #\.) (eqv? c #\!)
(has-ssyntax-char? string (- i 1)))))))
; Though graphically the right choice, can't use _ for currying
; because then _!foo becomes a function. Maybe use <>. For now
; leave this off and see how often it would have been useful.
; Might want to make ~ have less precedence than &, because
; ~foo&bar prob should mean (andf (complement foo) bar), not
; (complement (andf foo bar)).
(define (expand-ssyntax sym)
((cond ((insym? #\& sym) expand-and)
((or (insym? #\: sym) (insym? #\~ sym)) expand-compose)
((or (insym? #\. sym) (insym? #\! sym)) expand-sexpr)
; ((insym? #\_ sym) expand-curry)
(#t (error "Unknown ssyntax" sym)))
sym))
(define (expand-compose sym)
(let ((elts (map (lambda (tok)
(if (eqv? (car tok) #\~)
(if (null? (cdr tok))
'no
`(complement ,(chars->value (cdr tok))))
(chars->value tok)))
(tokens (lambda (c) (eqv? c #\:))
(symbol->chars sym)
'()
'()
#f))))
(if (null? (cdr elts))
(car elts)
(cons 'compose elts))))
(define (expand-and sym)
(let ((elts (map chars->value
(tokens (lambda (c) (eqv? c #\&))
(symbol->chars sym)
'()
'()
#f))))
(if (null? (cdr elts))
(car elts)
(cons 'andf elts))))
; How to include quoted arguments? Can't treat all as quoted, because
; never want to quote fn given as first. Do we want to allow quote chars
; within symbols? Could be ugly.
; If release, fix the fact that this simply uses v0... as vars. Should
; make these vars gensyms.
(define (expand-curry sym)
(let ((expr (exc (map (lambda (x)
(if (pair? x) (chars->value x) x))
(tokens (lambda (c) (eqv? c #\_))
(symbol->chars sym)
'()
'()
#t))
0)))
(list 'fn
(keep (lambda (s)
(and (symbol? s)
(eqv? (string-ref (symbol->string s) 0)
#\v)))
expr)
expr)))
(define (keep f xs)
(cond ((null? xs) '())
((f (car xs)) (cons (car xs) (keep f (cdr xs))))
(#t (keep f (cdr xs)))))
(define (exc elts n)
(cond ((null? elts)
'())
((eqv? (car elts) #\_)
(cons (string->symbol (string-append "v" (number->string n)))
(exc (cdr elts) (+ n 1))))
(#t
(cons (car elts) (exc (cdr elts) n)))))
(define (expand-sexpr sym)
(build-sexpr (reverse (tokens (lambda (c) (or (eqv? c #\.) (eqv? c #\!)))
(symbol->chars sym)
'()
'()
#t))
sym))
(define (build-sexpr toks orig)
(cond ((null? toks)
'%get)
((null? (cdr toks))
(chars->value (car toks)))
(#t
(list (build-sexpr (cddr toks) orig)
(if (eqv? (cadr toks) #\!)
(list 'quote (chars->value (car toks)))
(if (or (eqv? (car toks) #\.) (eqv? (car toks) #\!))
(ar-err "Bad ssyntax" orig)
(chars->value (car toks))))))))
(define (insym? char sym) (member char (cdr (reverse (symbol->chars sym)))))
(define (symbol->chars x) (string->list (symbol->string x)))
(define (chars->value chars)
(let ((s (list->string chars)))
(if (string-contains? s " ")
(string->symbol s)
(read-from-string s))))
(define (read-from-string str)
(let ((port (open-input-string str)))
(let ((val (read port)))
(close-input-port port)
val)))
(define (tokens test source token acc keepsep?)
(cond ((null? source)
(reverse (if (pair? token)
(cons (reverse token) acc)
acc)))
((test (car source))
(tokens test
(cdr source)
'()
(let ((rec (if (null? token)
acc
(cons (reverse token) acc))))
(if keepsep?
(cons (car source) rec)
rec))
keepsep?))
(#t
(tokens test
(cdr source)
(cons (car source) token)
acc
keepsep?))))
(define (ac-var-ref s)
(cond ((ac-boxed? 'get s) (ac-boxed-get s))
((ac-lex? s) s)
((eq? s 'globe) '(current-namespace))
((eq? s 'scope) (ac-lexenv))
(#t (ar-name s))))
(define (ac-tonumber s (base 10))
(on-err #f (lambda () (ar-coerce s 'num base))))
(define (ac-parse-number s (prefix #f))
(and (string? s)
(cond ((string-prefix? s "+")
(and (not prefix) (ac-parse-number (substring s 1) #t)))
((string-prefix? s "-")
(and (not prefix)
(let ((n (ac-parse-number (substring s 1) #t)))
(and n (- n)))))
(#t (and (not (string-prefix? s "_"))
(let ((s (string-replace s "_" "")))
(cond ((or (string-prefix? s "0x")
(string-prefix? s "0X"))
(ac-tonumber (substring s 2) 16))
((or (string-prefix? s "0b")
(string-prefix? s "0B"))
(ac-tonumber (substring s 2) 2))
((or (string-prefix? s "0o")
(string-prefix? s "0O"))
(ac-tonumber (substring s 2) 8))
(#t (ac-tonumber s)))))))))
(define (ac-number-literal s)
(and (symbol? s) (ac-parse-number (symbol->string s))))
; quote
(define (scm-quoted x)
(cond ((ar-car? x '%brackets)
(cdr x))
((ar-car? x '%braces)
(cdr x))
((pair? x)
(imap scm-quoted x))
(#t x)))
(define (ac-quoted x)
(cond ((pair? x)
(imap ac-quoted x))
((eqv? x 'nil)
ar-nil)
((eqv? x 't)
ar-t)
((eqv? x 'true)
ar-t)
((eqv? x 'false)
#f)
((eqv? x 'nan)
ar-nan)
((eqv? x 'inf)
ar-inf)
((eqv? x '-inf)
ar-ninf)
(#t (or (ar-keyword? x) (ac-number-literal x) x))))
(define (ac-unquoted x)
(cond ((pair? x)
(imap ac-unquoted x))
((null? x)
'nil)
((eqv? x ar-t)
't)
((eqv? x #f)
'false)
((eqv? x ar-nan)
'nan)
((eqv? x ar-inf)
'inf)
((eqv? x ar-ninf)
'-inf)
(#t x)))
; quasiquote
(define (ac-qq args)
(list 'quasiquote (ac-qq1 1 args)))
; process the argument of a quasiquote. keep track of
; depth of nesting. handle unquote only at top level (level = 1).
; complete form, e.g. x or (fn x) or (unquote (fn x))
(define (ac-qq1 level x)
(cond ((= level 0)
(ac x))
((ar-car? x 'unquote)
(list 'unquote (ac-qq1 (- level 1) (cadr x))))
((and (ar-car? x 'unquote-splicing) (= level 1))
(list 'unquote-splicing
(ac-qq1 (- level 1) (cadr x))))
((ar-car? x 'quasiquote)
(list 'quasiquote (ac-qq1 (+ level 1) (cadr x))))
((pair? x)
(imap (lambda (x) (ac-qq1 level x)) x))
(#t (ac-quoted x))))
; quasisyntax
(define (ac-qs args)
(ac-qs1 1 args))
; process the argument of a quasisyntax. keep track of
; depth of nesting. handle unsyntax only at top level (level = 1).
; complete form, e.g. x or (fn x) or (unsyntax (fn x))
(define (ac-qs1 level x)
(cond ((= level 0)
(ac x))
((ar-car? x '%brackets)
(cdr x))
((ar-car? x '%braces)
(cdr x))
((ar-car? x 'unsyntax)
(ac-qs1 (- level 1) (cadr x)))
((and (ar-car? x 'unsyntax-splicing) (= level 1))
(ac-qs1 (- level 1) (cadr x)))
((ar-car? x 'quasisyntax)
(ac-qs1 (+ level 1) (cadr x)))
((pair? x)
(imap (lambda (x) (ac-qs1 level x)) x))
(#t x)))
; like map, but don't demand '()-terminated list
(define (imap f l)
(cond ((pair? l)
(cons (f (car l)) (imap f (cdr l))))
((null? l)
'())
(#t (f l))))
; (if) -> nil
; (if x) -> x
; (if t a ...) -> a
; (if nil a b) -> b
; (if nil a b c) -> (if b c)
(define (ac-if args)
(cond ((null? args) (list 'quote ar-nil))
((null? (cdr args)) (ac (car args)))
(#t `(if (ar-true? ,(ac (car args)))
((lambda ()
,(ac (cadr args))))
((lambda ()
,(ac-if (cddr args))))))))
(define (ac-dbname! name (env (env*)))
(env* (if (symbol? name)
(cons (list name) env)
env))
(env*))
(define (ac-dbname (env (env*)))
(reverse (map car (keep pair? env))))
(define (ac-env! x)
(cond ((symbol? x)
(env* (cons x (env*))))
((ar-caar? x 'o)
(ac-env! (cadr x)))
((pair? x)
(imap ac-env! x)))
x)
(define (ac-fn-args a)
(cond ((null? a) '())
((symbol? a)
(ac-env! a))
((ar-caar? a 'o)
(let* ((it (cdar a))
(var (car it))
(key (ac-flag? var))
(var (or key var))
(val (ar-car (cdr it)))
(expr (parameterize ((env* (env*)))
(ac-dbname! var)
(ac val))))
(ac-env! var)
(if key
`(,(symbol->keyword var)
(,var ,expr) ,@(ac-fn-args (cdr a)))
`((,var ,expr) ,@(ac-fn-args (cdr a))))))
((ar-car? a ar-keyword?)
(let* ((key (ar-keyword? (car a)))
(var (cadr a))
(var (if (symbol? var) `(o ,var) var))) ; ensure all kwargs are optional
(cons key (ac-fn-args `(,var ,@(cddr a))))))
((ar-car? a ac-flag?)
(let* ((var (ac-flag? (car a)))
(key (symbol->keyword var)))
(ac-fn-args `(,key (o ,var) ,@(cdr a)))))
(#t
(cons (ac-env! (car a)) (ac-fn-args (cdr a))))))
; translate fn directly into a lambda if it has ordinary
; parameters, otherwise use a rest parameter and parse it.
(define (ac-fn args body)
(parameterize ((env* (env*)))
(if (ac-complex-args? args)
(ac-complex-fn args body)
(ac-simple-fn args body))))
(define (ac-simple-fn args body)
(let* ((a (ac-fn-args args))
(f (ac-nameit `(lambda ,a ,@(ac-body* body)))))
(if (ac-kwargs? a)
`(ar-kwproc ,f)
f)))
(define (ac-kwargs? args)
(or (ar-car? args '#:kws)
(and (pair? args)
(ac-kwargs? (ar-cdr args)))))
; does an fn arg list use optional parameters or destructuring?
; a rest parameter is not complex
(define (ac-complex-args? args)
(cond ((null? args) #f)
((symbol? args) #f)
((or (ar-car? args symbol?)
(ar-car? args ar-keyword?)
(and (ar-caar? args 'o)
(ar-car? (cdar args) symbol?)))
(ac-complex-args? (cdr args)))
(#t #t)))
; translate a fn with optional or destructuring args
; (fn (x (o y x) (o z 21) (x1 x2) . rest) ...)
; arguments in top-level list are mandatory (unless optional),
; but it's OK for parts of a list you're destructuring to
; be missing.
(define (ac-complex-fn args body)
(let* ((ra (ar-gensym))
(z (ac-complex-args args ra #t)))
(ac-nameit
`(lambda ,ra
(let* ,z
,@(ac-body* body))))))
; returns a list of two-element lists, first is variable name,
; second is (compiled) expression. to be used in a let.
; caller should extract variables and add to env.
; ra is the rest argument to the fn.
; is-params indicates that args are function arguments
; (not destructuring), so they must be passed or be optional.
(define (ac-complex-args args ra is-params (is-kw #f))
(cond ((null? args) '())
((symbol? args)
(list (list (ac-env! args) ra)))
((ar-car? args ac-flag?)
(let ((var (ac-flag? (car args))))
(ac-complex-args `(,(symbol->keyword var) (o ,var) ,@(cdr args)) ra is-params)))
((ar-car? args ar-keyword?)
(ac-complex-args (cdr args) ra #f (keyword->symbol (ar-keyword? (car args)))))
((and (ar-caar? args 'o)
(ac-flag? (cadar args)))
(let ((var (ac-flag? (cadar args)))
(val (cddar args)))
(ac-complex-args
`(,(symbol->keyword var) (o ,var ,@val) ,@(cdr args)) ra is-params #f)))
((pair? args)
(let* ((x (if (ar-caar? args 'o)
(let ((var (cadar args))
(val (if (pair? (cddar args))
(caddar args)
ar-nil)))
(ac-complex-opt var val ra is-kw))
(ac-complex-args
(car args)
(cond (is-kw `(ar-funcall2 ,ra ',is-kw ar-nil))
(is-params `(car ,ra))
(#t `(ar-car ,ra)))
#f))))
(append x (ac-complex-args (cdr args)
(if is-kw `(ar-without ,ra ',is-kw) `(ar-cdr ,ra))
is-params))))
(#t (ar-err "Can't understand fn arg list" args))))
; (car ra) is the argument
; so it's not present if ra is nil or '()
(define (ac-complex-opt var expr ra is-kw)
(let* ((val (ac expr)))
(list (list (ac-env! var)
(if is-kw
`(if (ar-unset? (ar-funcall2 ,ra ',is-kw ar-unset))
,val
(ar-funcall1 ,ra ',is-kw))
`(if (pair? ,ra) (car ,ra) ,val))))))
; (a b . c) -> (a b c)
; a -> (a)
; ((o a)) -> (a)
; (a: b) -> (b)
; (a: (o b)) -> (b)
(define (ac-arglist a)
(cond ((null? a) '())
((symbol? a) (list a))
((ar-caar? a 'o)
(cons (cadar a) (ac-arglist (cdr a))))
((ar-car? a ar-keyword?) (ac-arglist (cdr a)))
(#t (cons (car a) (ac-arglist (cdr a))))))
(define (ac-body body)
(map ac body))
; like ac-body, but spits out a nil expression if empty
(define (ac-body* body)
(if (null? body)
(list (list 'quote ar-nil))
(ac-body body)))
(define (ac-do body)
(let ((expr (ac-body* body)))
(cond ((= (length expr) 0)
'(begin))
((= (length expr) 1)
(car expr))
(#t `(begin ,@expr)))))
; (set v1 expr1 v2 expr2 ...)
(define (ac-set x)
`(begin ,@(ac-setn x)))
(define (ac-setn x)
(if (null? x)
'()
(cons (ac-set1 (ac-macex (car x)) (cadr x))
(ac-setn (cddr x)))))
; trick to tell Scheme the name of something, so Scheme
; debugging and profiling make more sense.
(define (ac-nameit v (name (ac-lexname)))
(if (symbol? name)
(let ((n (ac-identifier name)))
(list 'let `((,n ,v)) n))
v))
; = replaced by set, which is only for vars
; = now defined in arc (is it?)
; name is to cause fns to have their arc names for debugging
(define (ac-set1 a b1)
(if (symbol? a)
(let ((n (ac-identifier a))
(b (parameterize ((env* (env*)))
(ac-dbname! a)
(ac b1))))
(list 'let `((,n ,b))
(cond ((ac-boxed? 'set a) (ac-boxed-set a b))
((ac-lex? a) `(set! ,a ,n))
((not (eqv? a (ac-quoted a)))
(ar-err "Can't rebind constant" a))
(#t `(ar-namespace-set! ',(ar-name a) ,n)))
n))
(ar-err "First arg to set must be a symbol" a)))
; given a list of Arc expressions, return a list of Scheme expressions.
; for compiling passed arguments.
(define (ac-args names exprs)
(if (null? exprs)
'()
(cons (parameterize ((env* (env*)))
(ac-dbname! (ar-car? names))
(ac (car exprs)))
(ac-args (if (pair? names) (cdr names) '())
(cdr exprs)))))
(define (ac-lexname (names (ac-dbname)))
(define (lex x)
(if (ar-car? x 'quote)
(lex (ar-cadr x))
((ar-to 'sym) x)))
(ar-concat (map lex (keep ar-true? names)) "--"))
(define (ac-lexvars (env (env*)))
(remove-duplicates (keep symbol? env)))
(define (ac-lexenv (env (env*)))
`(list ,@(map ac-lexframe (ac-lexvars env))))
(define (ac-lexframe var)
(let ((val (ar-gensym 'val)))
`(list ',var
(lambda () ,var)
(lambda (,val) (set! ,var ,val)))))
(define boxed* (make-parameter '() #f 'boxed*))
(define (ac-boxed? op name)
(let ((result
(when (not (ar-false? name))
(when (not (ar-false? (boxed*)))
(let ((slot (assoc name (boxed*))))
(case op
((get) (when (and slot (>= (length slot) 2)) (cadr slot)))
((set) (when (and slot (>= (length slot) 3)) (caddr slot)))
(else (ar-err "ac-boxed?: bad op" name op))))))))
(if (void? result) #f result)))
(define (ac-boxed-set name val)
(let ((setter (ac-boxed? 'set name)))
(if (procedure? setter)
`(,setter ,val)
(ar-err "invalid setter" name val setter))))
(define (ac-boxed-get name)
(let ((getter (ac-boxed? 'get name)))
(if (procedure? getter)
`(,getter)
getter)))
; generate special fast code for ordinary two-operand
; calls to the following functions. this is to avoid
; calling e.g. ar-is with its &rest and apply.
(define ac-binaries
'((is ar-is)
(< ar-<2)
(> ar->2)
(+ ar-+2)))
; (foo bar) where foo is a global variable bound to a procedure.
(define (ac-global-call fn args)
(cond ((and (assoc fn ac-binaries) (= (length args) 2))
`(,(cadr (assoc fn ac-binaries)) ,@(ac-args '() args)))
(#t
`(,(ar-name fn) ,@(ac-args '() args)))))
; compile a function call
; special cases for speed, to avoid compiled output like
; (ar-apply _pr (list 1 2))
; which results in 1/2 the CPU time going to GC. Instead:
; (ar-funcall2 _pr 1 2)
; and for (foo bar), if foo is a reference to a global variable,
; and it's bound to a function, generate (foo bar) instead of
; (ar-funcall1 foo bar)
(define direct-calls #f)
(define (ac-compile-call fn args)
(let* ((argc (length args)))
(cond ((ar-car? fn 'fn)
`(,(ac fn) ,@(ac-args (cadr fn) args)))
((and direct-calls (symbol? fn) (not (ac-lex? fn))
(procedure? (ar-bound? fn)))
(ac-global-call fn args))
((memf ar-keyword? args)
`((ar-symbol-function ,(ac fn)) ,@(map ac args)))
((= argc 0) `(ar-funcall0 ,(ac fn) ,@(map ac args)))
((= argc 1) `(ar-funcall1 ,(ac fn) ,@(map ac args)))
((= argc 2) `(ar-funcall2 ,(ac fn) ,@(map ac args)))
((= argc 3) `(ar-funcall3 ,(ac fn) ,@(map ac args)))
((= argc 4) `(ar-funcall4 ,(ac fn) ,@(map ac args)))
(#t `(ar-apply ,(ac fn) (list ,@(map ac args)))))))
(define (ac-call fn args)
(let* ((args (ac-unflag-args args))
(expr (ac-compile-call fn args))
(kws (map ar-keyword? (keep ar-keyword? args))))
(if (null? kws)
expr
`(parameterize ((ar-kworder ',kws)) ,expr))))
; returns #f or the macro function
(define (ac-macro? fn (kind 'mac))
(define v (and (symbol? fn) (ar-bound? fn)))
(and (eq? (ar-type v) kind)
(ar-rep v)))
; macroexpand the outer call of a form as much as possible
(define (ac-macex e (once #f))
(if (pair? e)
(let ((m (ac-macro? (car e))))
(if m
(let ((expansion (ar-kwapply m #f (ac-unflag-args (cdr e)))))
(if (ar-car? expansion '%expansion)
(cadr expansion)
(if once expansion (ac-macex expansion))))
e))
e))
; is v lexically bound?
(define (ac-lex? v (env (env*)))
(memq v env))
; The next two are optimizations, except work for macros.
(define (decompose fns args)
(cond ((null? fns) `((fn vals (car vals)) ,@args))
((null? (cdr fns)) (cons (car fns) args))
(#t (list (car fns) (decompose (cdr fns) args)))))
(define (ac-andf s)
(ac (let ((gs (map (lambda (x) (or (ar-keyword? x) (ar-gensym 'andf))) (cdr s))))
`((fn ,gs
(and ,@(map (lambda (f) `(,f ,@gs))
(cdar s))))
,@(cdr s)))))
(define-namespace-anchor arc-anchor)
; (define (arc-namespace) (namespace-anchor->namespace arc-anchor))
(define arc-namespace (make-parameter (current-namespace) #f 'arc-namespace))
; run-time primitive procedures
;(define (xdef a b)
; (namespace-set-variable-value! (ar-name a) b)
; b)
(define-syntax xdef
(syntax-rules ()
((xxdef a b)
(let* ((nm (ar-name 'a))
(a b)
(val (namespace-variable-value nm #t (lambda () (void)))))
(when (and (not (eqv? 'a 'b))
(not (ar-nil? val)))
(display "*** redefining " (current-error-port))
(display 'a (current-error-port))
(display " (was " (current-error-port))
(write a (current-error-port))
(display ")\n" (current-error-port)))
(ar-namespace-set! nm a)))
((xxdef name parms body ...)
(begin
(hash-set! fn-signatures 'name 'parms)
(xdef name (lambda parms body ...))))))
(define fn-signatures (make-hash))
; This is a replacement for xdef that stores opeator signatures.
; Haven't started using it yet.
(define (odef a parms b)
(ar-namespace-set! (ar-name a) b)
(hash-set! fn-signatures a (list parms))
b)
(xdef null ar-nil?)
(xdef unstash (x)
(let* ((it (ar-unstash x))
(args (car it))
(kws (cadr it)))
(list args (hash->plist kws))))
(xdef lex ac-lex?)
(xdef lexname ac-lexname)
(xdef eof eof)
(xdef sig fn-signatures)
(xdef quoted ac-quoted)
(xdef unquoted ac-unquoted)
; circular references will go into an infinite loop
(xdef symbol-value (name (fail ar-unset))
(ar-symbol-value name fail))
(xdef symbol-function (name)
(ar-symbol-function name))
(xdef apply
(make-keyword-procedure
(lambda (keys vals fn . args)
(keyword-apply (ar-symbol-function fn) keys vals (ar-apply-args args)))
(lambda (fn . args)
(apply ar-kwapply fn #f args))))
(xdef kwapply ar-kwapply)
(xdef join ((x ar-nil) (y ar-nil))
(ar-join x y))
(xdef car (x) (ar-car x))
(xdef cdr (x) (ar-cdr x))
(xdef pairwise ar-pairwise)
(xdef id args (ar-pairwise ar-id args))
(xdef is args (ar-pairwise ar-is args))
(xdef raise raise)
(xdef err ar-err)
(xdef true #t)
(xdef false #f)
(xdef t ar-t)
(xdef nil ar-nil)
(xdef unset ar-unset)
(xdef nan ar-nan)
(xdef inf ar-inf)
(xdef -inf ar-ninf)
(define (all test seq)
(or (null? seq)
(and (test (car seq)) (all test (cdr seq)))))
(define (ar-concat xs (sep ""))
(and (pair? xs)
(apply ar-+ (add-between xs sep))))
(xdef join! (l x . args)
(apply ar-join! l x args))
(xdef + ar-+)
(xdef - -)
(xdef * ar-*)
(xdef / /)
; https://stackoverflow.com/questions/65411276/how-to-compute-a-floating-point-modulo-in-racket-flmod
(define (flmod x m)
(- x (* (floor (/ x m)) m)))
(xdef mod flmod)
(xdef expt expt)
(xdef sqrt sqrt)
(xdef > args (ar-pairwise ar->2 args))
(xdef < args (ar-pairwise ar-<2 args))
(xdef len (x) (ar-len x))
(xdef annotate (type rep)
(cond ((eqv? (ar-type rep) type) rep)
(#t (ar-tagged type rep))))
(xdef type ar-type)
(xdef typeof ar-typeof)
(xdef typename ar-typename)
(xdef rep ar-rep)
(define (ar-gensym . names)
(ac-identifier (gensym (or (ac-lexname names) 'x))))
(xdef uniq ar-gensym)
(xdef ccc call-with-current-continuation)
(xdef modtime file-or-directory-modify-seconds)
(xdef infile (f #:bytes (binary? #f))
(open-input-file f #:mode (if (ar-true? binary?) 'binary 'text)))
(xdef outfile (f #:bytes (binary? #f) #:append (append? #f))
(open-output-file f
#:mode (if (ar-true? binary?) 'binary 'text)
#:exists (if (ar-true? append?) 'append 'truncate)))
(xdef instring (x)
(if (bytes? x)
(open-input-bytes x)
(open-input-string x)))
(xdef outstring open-output-string)
; use as general fn for looking inside things