forked from awwx/ar
-
Notifications
You must be signed in to change notification settings - Fork 7
/
01 nu.rkt
executable file
·1971 lines (1708 loc) · 65.8 KB
/
01 nu.rkt
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
#lang racket
;; Nu Arc Compiler -- Manifest Destiny
;; http://www.youtube.com/watch?v=qXp3qjeM0e4
;; TODO: look for uses of null? and replace them with empty-stream? as needed
;; TODO: replace uses of #%datum with #%quote
(provide (all-defined-out)
(all-from-out racket) ; racket/base
;(all-from-out racket/path)
)
;(require (only-in racket syntax-case syntax))
(require ffi/unsafe)
(require racket/unsafe/ops)
;(require racket/path)
;(require racket/port)
;(require racket/system)
;(require racket/tcp)
;=============================================================================
; Convenient extras; can remove
;=============================================================================
(require racket/pretty)
(define (acompile1 ip op)
(let/ x (read ip)
(if (eof-object? x)
#t
(let/ scm (ac x)
(eval scm)
(pretty-print scm op)
(newline op)
(newline op)
(acompile1 ip op)))))
; compile xx.arc to xx.arc.scm
; useful to examine the Arc compiler output
(define (acompile inname)
(let/ outname (string-append inname ".scm")
(when (file-exists? outname)
(warn "deleting file " outname)
(delete-file outname))
(call-with-input-file inname
(lambda (ip)
(call-with-output-file outname
(lambda (op)
(acompile1 ip op)))))))
(define (prn . args)
(for-each (lambda (x)
(display x)
(display " "))
args)
(newline)
(car args))
;=============================================================================
; General purpose utilities
;=============================================================================
(define-syntax (with/ stx)
;; TODO: map2 is general purpose
(define (map2 f x)
(cond ((null? x)
x)
((null? (cdr x))
(list (f (car x) null)))
(else
(cons (f (car x) (cadr x))
(map2 f (cddr x))))))
(define (arc-let->racket-let stx x)
(datum->syntax stx
(map2 (lambda (x y)
(if (pair? x)
(list x y)
(list (list x) y)))
(syntax->datum x))))
(syntax-case stx ()
[(_ x . body) #`(let-values (#,@(arc-let->racket-let stx #'x)) . body)]))
;(syntax->datum (expand '(with/ () 1 2 3)))
;(syntax->datum (expand '(with/ (x 1 y 2) x y)))
;(syntax->datum (expand '(with/ ((x y) 1 z 2) x y z)))
;(syntax->datum (expand '(let/ x 1 x)))
;(syntax->datum (expand '(let/ (x y) 1 x y)))
;(syntax->datum (expand '(withs (x 1 y 2) x y)))
;(syntax->datum (expand '(withs ((x y) 1 y 2) x y)))
;(syntax->datum (expand '(awith (x 1 y 2) (self x y))))
;(syntax->datum (expand '(awith () (self 1 2))))
;(syntax->datum (expand '(awith (x '(1 2 3 4 5)) (if (null? x) x (cons (car x) (self (cdr x)))))))
;(syntax->datum (expand '(alet x '(1 2 3 4 5) (if (null? x) x (cons (car x) (self (cdr x)))))))
(define-syntax (let/ stx)
(syntax-case stx ()
[(_ (x ...) y . body) #'(let-values (((x ...) y)) . body)]
[(_ x y . body) #'(let/ (x) y . body)]))
#|(define-syntax (let/ stx)
(syntax-case stx ()
[(_ x y . body) #'(with/ (x y) . body)]))|#
#|(define-syntax-rule (let/ x y . body)
(with/ (x y) . body))|#
(define-syntax (withs stx)
(syntax-case stx ()
[(_ ((x ...) y z ...) . body) #'(let-values (((x ...) y)) (withs (z ...) . body))]
[(_ (x y z ...) . body) #'(let-values (((x) y)) (withs (z ...) . body))]
[(_ (x) . body) #'(withs (x null) . body)]
[(_ x . body) #'(begin . body)]))
(define-syntax (awith stx)
;; TODO: pair is general purpose
(define (pair x)
(cond ((null? x)
x)
((null? (cdr x))
(list (list (car x))))
(else
(cons (list (car x) (cadr x))
(pair (cddr x))))))
(syntax-case stx ()
[(_ parms . body) #`(let #,(datum->syntax stx 'self)
(#,@(datum->syntax stx (pair (syntax->datum #'parms))))
. body)]))
(define-syntax (alet stx)
(syntax-case stx ()
[(_ x y . body) #`(let #,(datum->syntax stx 'self) ((x y)) . body)]))
#|(let-syntax (($if (make-rename-transformer #'if)))
(displayln ($if 1 2 3))
(define-syntax if
(lambda (stx)
(displayln (syntax-local-get-shadower #'if))
(displayln ((syntax-local-value #'if) stx))
(syntax-case stx ()
[(_) #'(void)]
[(_ x) #'x]
[(_ x y) #'($if x y (void))]
[(_ x y z) #'($if x y z)]
[(_ a b c d ...) #'($if a b (if c d ...))])))
(void))|#
(define-syntax (if stx)
(syntax-case stx ()
[(_) #'(void)]
[(_ x) #'x]
[(_ x y) #'(cond (x y))]
[(_ x y z) #'(cond (x y) (else z))]
[(_ a b c d ...) #'(if a b (if c d ...))]))
;(syntax->datum (expand '(if)))
;(syntax->datum (expand '(if 1)))
;(syntax->datum (expand '(if 1 2)))
;(syntax->datum (expand '(if 1 2 3)))
;(syntax->datum (expand '(if 1 2 3 4)))
;(syntax->datum (expand '(if 1 2 3 4 5)))
;(syntax->datum (expand '(if 1 2 3 4 5 6)))
(define-syntax-rule (consif p a b)
(if p (cons a b) b))
(define (caris x y)
(and (pair? x)
(eq? (car x) y)))
(define (zip . args)
(apply map list args))
(define (proper-list? x)
(if (pair? x) (proper-list? (cdr x))
(null? x) #t
#f))
;; There's two ways to turn an improper list into a proper one:
;; (1 2 3 . 4) -> (1 2 3)
;; (1 2 3 . 4) -> (1 2 3 4)
;; This uses the first way, because it's intended for function argument lists
(define (dotted->proper x)
(if (pair? x)
(cons (car x) (dotted->proper (cdr x)))
null))
(define (dottedmap f xs #:end (end f))
(alet xs xs
(if (pair? xs)
(cons (f (car xs))
(self (cdr xs)))
(end xs))))
#|(define (imap f xs)
(if (pair? xs)
(cons (f (car xs))
(imap f (cdr xs)))
xs))|#
(define (dottedrec f xs)
(alet xs xs
(if (pair? xs)
(cons (f (self (car xs)))
(self (cdr xs)))
(f xs))))
;; TODO: better name
(define (arg-list* args)
(if (null? (cdr args))
(car args)
(cons (car args)
(arg-list* (cdr args)))))
(define (keyword->symbol x)
(string->symbol (keyword->string x)))
#|(define (keyword-get kw kwa x opt)
(awith (kw kw
kwa kwa)
(if (null? kw)
opt
(eq? (car kw) x)
(car kwa)
(self (cdr kw) (cdr kwa)))))|#
(define (keyword-get kw kwa x opt)
(awith (kw kw
kwa kwa
acc1 null
acc2 null
res opt)
(if (null? kw)
(values (reverse acc1)
(reverse acc2)
res)
(eq? (car kw) x)
(self (cdr kw)
(cdr kwa)
acc1
acc2
(car kwa))
(self (cdr kw)
(cdr kwa)
(cons (car kw) acc1)
(cons (car kwa) acc2)
res))))
(define (process-keywords kw kwa args)
(withs (kws (if (null? kw)
kw
(zip kw kwa))
args (alet x args
(if (null? x)
x
(keyword? (car x))
(begin (set! kws (cons (list (car x) (cadr x)) kws))
(self (cddr x)))
(cons (car x) (self (cdr x))))))
(if (null? kws)
(values kws kws args)
;; WOW Racket seriously expects the keyword list to be *sorted*?
(let/ kws (apply zip (sort kws keyword<? #:key car))
(values (car kws) (cadr kws) args)))))
#|(awith (x args
kws (if (null? kw)
kw
(zip kw kwa))
args null)
(if (null? x)
(if (null? kws)
(values kws kws (reverse args))
;; WOW Racket seriously expects the keyword list to be *sorted*?
(let/ kws (apply zip (sort kws keyword<? #:key car))
(values (car kws) (cadr kws) (reverse args))))
(keyword? (car x))
(self (cddr x)
(cons (list (car x) (cadr x)) kws)
args)
(self (cdr x) kws (cons (car x) args))))|#
(define (keyword-args? args)
(alet x args
(if (null? x) #f
(keyword? (car x)) #t
(self (cdr x)))))
(define (kw-join kw kwa args)
;; TODO: keywords are at the end of the list rather than the front;
;; not a huge deal, but still...
(append* args (zip kw kwa)))
;; based on Arc's reduce. Can't use foldl because it doesn't work well with
;; multiple types (e.g. +-2)
(define (reduce f xs)
(if (null? (cdr xs))
(car xs) ;(f (car xs))
(reduce f (cons (f (car xs) (cadr xs)) (cddr xs)))))
(define (warn . args)
(display "warning: " (current-error-port))
(for ((x args))
(display x (current-error-port)))
(newline (current-error-port)))
(define (fraction? x)
(and (number? x)
(exact? x)
(not (integer? x))))
;=============================================================================
; Arc variables
;=============================================================================
(define arc3-namespace (make-base-empty-namespace))
(define names (make-hash))
;; TODO: see if redef and reset are needed
#|(define-syntax-rule (redef name parms . body)
(reset name (lambda parms . body)))
(define-syntax-rule (reset name val)
((var-raw 'name) val))|#
;; creates a function that is exposed to Arc
;; use #:sig to define a custom Arc sig
(define-syntax sdef
(syntax-rules ()
((_ name parms #:sig parms2 . body)
(sset name parms2 (lambda parms . body)))
((_ name parms . body)
(sset name parms (lambda parms . body)))))
;; like sdef but makes the function mutable from within Arc
;; use #:name to define a different Arc name than the Racket name
(define-syntax mdef
(syntax-rules ()
((_ name parms #:name name2 . body)
(mset name parms #:name name2 (lambda parms . body)))
((_ name parms #:sig parms2 . body)
(mset name parms2 (lambda parms . body)))
((_ name parms . body)
(mset name parms (lambda parms . body)))))
;; wraps the Racket value in a global variable function before making it
;; accessible to Arc while also optionally setting the sig of the name
(define-syntax sset
(syntax-rules ()
((_ a b) (let/ v b
(nameit 'a v)
(set 'a (make-global-var v))))
((_ a parms b) (begin (hash-set! sig 'a 'parms)
(sset a b)))))
;; like sset but makes the Racket value mutable from within Arc
;; use #:name to define a different Arc name than the Racket name
(define-syntax mset
(syntax-rules ()
((_ a #:name name b) (begin (define a b)
(nameit 'name a)
(set 'name (case-lambda
(() a)
((x) (set! a x))))))
((_ a b) (mset a #:name a b))
((_ a parms #:name name b) (begin (hash-set! sig 'name 'parms)
(mset a #:name name b)))
((_ a parms b) (begin (hash-set! sig 'a 'parms)
(mset a b)))))
;; creates a parameter in the compiler's namespace, then makes it implicit in
;; Arc's namespace
(define-syntax-rule (pset a b)
(begin (define a (make-parameter b))
(set 'a a)))
;; this makes the variable accessible to Arc but doesn't wrap it or do
;; anything else
(define (set a b)
;(sref (namespace) b a)
(namespace-set-variable-value! a b #f arc3-namespace)) ;(coerce (namespace) 'namespace)
(define (nameit name val)
(when (or (procedure? val)
(tagged? val))
(hash-set! names val name)))
(define (make-global-var x)
(case-lambda
(() x)
((v) (set! x v))))
;=============================================================================
; Types
;=============================================================================
(struct tagged (type rep)
#:constructor-name make-tagged
;; TODO: make mutable later, maybe
;#:mutable
#|#:property prop:custom-write
(lambda (x port mode)
(display "#(tagged " port)
(display (type x) port)
(display " " port)
(display (rep x) port)
(display ")" port))|#
)
(define (iround x) (inexact->exact (round x)))
(define (wrapnil f) (lambda args (apply f args) nil))
(define (wraptnil f) (lambda (x) (tnil (f x))))
;=============================================================================
; Arc stuff used by the compiler
;=============================================================================
(mset nil null)
(mset sig (make-hash))
(mset t 't)
;; TODO: a better argument name than typ
;; TODO: annotate doesn't need to be mutable, but does need to be exposed to
;; both the compiler and Arc
(mdef annotate (typ rep)
;; TODO: does this need to eqv? rather than eq?
(if (eqv? (type rep) typ)
rep
(make-tagged typ rep)))
;; car and cdr probably will be used later, but not right now
(mdef ac-car (x) #:name car
(if (null? x)
x
#|(stream-empty? x)
x|#
(pair? x)
(car x)
#|(stream? x)
(stream-first x)|#
(raise-type-error 'car "cons" x)))
(mdef ac-cdr (x) #:name cdr
(if (null? x)
x
#|(stream-empty? x)
x|#
(pair? x)
(cdr x)
#|(stream? x)
(stream-rest x)|#
(raise-type-error 'cdr "cons" x)))
(mdef close args
(map close1 args)
(map (lambda (p) (try-custodian p)) args) ;; free any custodian
nil)
(mdef close1 (p)
(if (input-port? p) (close-input-port p)
(output-port? p) (close-output-port p)
(tcp-listener? p) (tcp-close p)
(err "can't close " p)))
;; TODO: list + table of types for coerce
(mdef coerce (x to (base 10))
#:sig (x to (o base 10))
(if (tagged? x) (err "can't coerce annotated object")
;; TODO: does this need to be eqv? rather than eq?
(eqv? to (type x)) x
(symbol? x) (case to
((string) (symbol->string x))
(else (err "can't coerce" x to)))
(pair? x) (case to
((string) (apply string-append
(map (lambda (y) (coerce y 'string))
x)))
(else (err "can't coerce" x to)))
;(eq? x nil)
(null? x) (case to
((string) "")
(else (err "can't coerce" x to)))
(char? x) (case to
((int) (char->integer x))
((string) (string x))
((sym) (string->symbol (string x)))
(else (err "can't coerce" x to)))
(exact-integer? x) (case to
((num) x)
((char) (integer->char x))
((string) (number->string x base))
(else (err "can't coerce" x to)))
(number? x) (case to
((int) (iround x))
((char) (integer->char (iround x)))
((string) (number->string x base))
(else (err "can't coerce" x to)))
(string? x) (case to
((sym) (string->symbol x))
((cons) (string->list x))
((num) (or (string->number x base)
(err "can't coerce" x to)))
((int) (let/ n (string->number x base)
(if n (iround n)
(err "can't coerce" x to))))
(else (err "can't coerce" x to)))
(err "can't coerce" x to)))
(mdef err (x . rest)
(apply error x rest))
(mdef ac-eval (expr) #:name eval
(eval (ac expr) (coerce (namespace) 'namespace)
#|(if (namespace? (namespace))
(namespace)
arc3-namespace)|#
))
;; macroexpand the outer call of a form as much as possible
(mdef macex (e)
(let/ v (macex1 e)
(if (eq? v e)
v
(macex v))))
;; macroexpand the outer call of a form once
(mdef macex1 (e)
(if (pair? e)
(let/ m (macro? (car e))
;; TODO: not sure about this
(if (and m (not (or (eq? m assign)
(eq? m fn)
(eq? m ac-if)
(eq? m ac-quote)
(eq? m ac-quasiquote))))
(mac-call m (cdr e))
e))
e))
;; TODO: not sure what category this should be placed in
;; TODO: should pipe call ((caddddr x) 'wait)?
(mdef pipe (cmd)
;; TODO: destructuring
(withs (x (process/ports #f #f (current-error-port) cmd)
in (car x)
out (cadr x))
(list in out)))
(mdef protect (during after)
(dynamic-wind (lambda () #t) during after))
(mdef rep (x)
(if (tagged? x)
(tagged-rep x)
x))
(mdef scar (p x)
(if (pair? p) (unsafe-set-mcar! p x)
(string? x) (string-set! p 0 x)
(raise-type-error 'scar "cons" p))
x)
;; Later may want to have multiple indices.
(mdef sref (x val key)
(if (namespace? x) (namespace-set-variable-value! key val #f x) ;(global-name key)
;(eq? val nil)
(hash? x) (if (false? val)
(hash-remove! x key)
(hash-set! x key val))
(string? x) (string-set! x key val)
(pair? x) (scar (list-tail x key) val)
(err "can't set reference " x key val))
val)
(mdef type (x)
;; TODO: better ordering for speed
(if (tagged? x) (tagged-type x)
(namespace? x) 'namespace
(pair? x) 'cons
;(stream? x) 'stream ;; TODO: not sure about this
(symbol? x) 'sym
; (type nil) -> sym
(null? x) 'sym
(procedure? x) 'fn
(char? x) 'char
(string? x) 'string
(exact-integer? x) 'int
(number? x) 'num ; unsure about this
(hash? x) 'table
(output-port? x) 'output
(input-port? x) 'input
(tcp-listener? x) 'socket
(exn? x) 'exception
(thread? x) 'thread
;(err "type: unknown type" x)
;; TODO: not sure about this, but seems okay
nil))
;; Racket functions
(sset - args -)
(sset cons (x y) cons) ;; TODO: look for some uses of cons and replace em with ac-cons
(sset instring (str (o name 'string)) open-input-string)
(sset seconds () current-seconds)
;; Racket parameters
(sset stdout ((o out)) current-output-port) ; should be a vars
(sset stdin ((o in)) current-input-port)
(sset stderr ((o err)) current-error-port)
;=============================================================================
; Initialization and loading
;=============================================================================
(define exec-dir (current-directory))
(define (init (dir (current-directory)))
(set! exec-dir dir)
;; TODO: why does Arc 3.1 do this?
(putenv "TZ" ":GMT")
;; TODO: why is this in Arc 3.1?
;(print-hash-table #t)
(current-readtable arc3-readtable)
;(%load-all dir)
)
#|(mdef %load-all (dir)
(aload (build-path dir "02 arc.arc")))
(define (repl)
(aload (build-path exec-dir "03 repl.arc")))|#
(define (aload filename)
;; This is so that it's possible to retrieve the column/line of an input port
(parameterize ((port-count-lines-enabled #t))
(call-with-input-file filename aload1)))
(define (aload1 p)
(let/ x (read p)
(if (eof-object? x)
#t ;; TODO: should probably be (void)
(begin (ac-eval x)
(aload1 p)))))
;=============================================================================
; The compiler
;=============================================================================
(define local-env (make-parameter null)) ;; list of lexically bound variables
(define nocompile (gensym)) ;; if in the car the expression won't be compiled
(define fail (gensym))
;; compile an Arc expression into a Racket expression; both are s-expressions
(define (ac x)
(if (symbol? x)
(if (ssyntax x)
(ac (ssexpand x))
(ac-symbol x))
(pair? x)
(if (caris x nocompile)
(cdr x)
(ac-call (car x) (cdr x)))
(null? x)
;; this causes it to return null
(list '#%datum)
;null
(string? x)
(ac-string x)
x))
(define (ac-all x)
(dottedmap ac x #:end idfn))
(define (idfn x) x)
;=============================================================================
; Namespaces
;=============================================================================
(define (empty-namespace)
(parameterize ((current-namespace (make-base-empty-namespace)))
(namespace-init)
(current-namespace)))
(define (namespace-init)
(namespace-require '(rename '#%kernel #%begin begin))
(namespace-require '(rename '#%kernel #%if if))
(namespace-require '(rename racket/base #%lambda lambda))
(namespace-require '(rename racket/base #%let*-values let*-values))
(namespace-require '(rename '#%kernel #%quote quote))
(namespace-require '(rename '#%kernel #%set set!))
;(namespace-require '(rename '#%kernel #%var case-lambda))
(namespace-require '(only racket/base #%top #%app #%datum))
;(namespace-require '(only racket/base displayln))
)
(parameterize ((current-namespace arc3-namespace))
(namespace-init)
)
(define namespace (make-parameter arc3-namespace))
;(pset namespace arc3-namespace)
;=============================================================================
; Variables
;=============================================================================
(define cached-global-ref (gensym))
(define replace-var (make-parameter null))
(define unique (gensym))
(define (var-raw a (def nil))
(ref (namespace) a def))
(define (var a (def nil))
(let/ v (var-raw a fail)
(if (eq? v fail)
def
(v))))
(mdef %symbol-global (x)
;`(,(global-ref x))
`(,x))
(define (ac-symbol x)
(let/ r (assq x (replace-var))
(if r (cadr r)
(if (lex? x)
x
(%symbol-global x)))))
#|
;; TODO
(define (global-ref name (space (namespace)))
(let/ hash (ref space cached-global-ref
(lambda ()
(sref space (make-hash) cached-global-ref)))
(hash-ref! hash name
(lambda ()
(parameterize ((current-namespace (coerce space 'namespace))
;(compile-allow-set!-undefined #t)
)
(eval `(#%var (() (,name))))
#|(eval `(#%var (() (,name))
((,unique) (#%set ,name ,unique))))|#
)))))|#
(define (lex? v) ;; is v lexically bound?
(memq v (local-env)))
;=============================================================================
; Normal strings and atstrings
;=============================================================================
(define atstrings #f)
(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))))
(unescape-ats s))
;; This is for normal strings
(string-copy s))) ; avoid immutable strings
;; All of this is for atstrings, not needed for normal strings at all
(define (codestring s)
(let/ i (atpos s 0)
(if i (cons (substring s 0 i)
(withs (rest (substring s (+ i 1))
in (open-input-string rest)
expr (read in)
;; TODO: function for this...?
i2 (let/ (x y z) (port-next-location in) z))
(close-input-port in)
(cons expr (codestring (substring rest (- i2 1))))))
(list s))))
; First unescaped @ in s, if any. Escape by doubling.
(define (atpos s i)
;; TODO: shouldn't this use = ?
(if (eqv? i (string-length s))
#f
(eqv? (string-ref s i) #\@)
(if (and (< (+ i 1) (string-length s))
(not (eqv? (string-ref s (+ i 1)) #\@)))
i
(atpos s (+ i 2)))
(atpos s (+ i 1))))
(define (unescape-ats s)
(list->string (alet cs (string->list s)
(if (null? cs)
cs
(and (eqv? (car cs) #\@)
(not (null? (cdr cs)))
(eqv? (cadr cs) #\@))
(self (cdr cs))
(cons (car cs) (self (cdr cs)))))))
;=============================================================================
; Predicates
;=============================================================================
;; convert Racket booleans to Arc booleans
(define (tnil x) (if x t nil))
;; definition of falseness for Arc's if
(define (false? x)
(or (eq? x nil)
(eq? x #f)))
(define (true? x)
(not (false? x)))
(define (isa x y)
(eq? (type x) y))
;=============================================================================
; call / ref
;=============================================================================
(define direct-calls #f)
(define inline-calls #f)
(define (ac-call f args)
(withs (f (if (ssyntax f)
(ssexpand f)
f)
c (and (pair? f)
(macro? (car f)))
m (macro? f))
;(when (and c (not (caris f 'fn))) (prn c f))
; 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)
;; TODO: this is only very slightly better than
;; hardcoding the symbol: figure out a better
;; way
(if (and c (eq? c (var 'compose)))
(ac (de-compose (cdr f) args))
(and c (eq? c (var 'complement)))
(ac (list 'no (cons (cadr f) args)))
(and c (eq? c (var 'andf)))
(ac (de-andf f args))
m
(ac (mac-call m args))
;; inserts the actual value for things in functional position, so
;; (+ 1 2) compiles into (#<fn:+> 1 2)
;;
;; this is much faster than direct-calls but it's even more strict:
;; if you redefine any global, even functions, those changes aren't
;; retroactive: they affect new code, but not old code
(and inline-calls
(symbol? f)
(not (lex? f))
;; TODO: bound
(not (eq? (var f fail) fail)))
(let/ f (var f)
(if (procedure? f)
`( ,f ,@(ac-all args))
`(,ref ,f ,@(ac-all args))))
;; (foo bar) where foo is a global variable bound to a procedure.
;; this breaks if you redefine foo to be a non-fn (like a hash table)
;; but as long as you don't redefine anything, it's faster
(and direct-calls
(symbol? f)
(not (lex? f))
(procedure? (var f)))
`(,(ac f) ,@(ac-all args))
(let/ f (ac f)
;; optimization for (#<fn> ...) and ((fn ...) ...)
(if (or (procedure? f)
(caris f '#%lambda)
;; needed because #%call doesn't accept keyword args
(keyword-args? args))
`( ,f ,@(ac-all args))
`(#%call ,f ,@(ac-all args)))))))
;; the next two are optimizations, except work for macros.
(define (de-compose fns args)
;; TODO: is this needed anywhere in Arc or can I remove it...?
(if ;(null? fns) `((fn vals (car vals)) ,@args)
(null? (cdr fns)) (cons (car fns) args)
(list (car fns) (de-compose (cdr fns) args))))
(define (de-andf f args)
(let/ gs (map (lambda (x) (gensym)) args)
`((fn ,gs
(and ,@(map (lambda (f) `(,f ,@gs))
(cdr f))))
,@args)))
;; returns #f or the macro
(define (macro? f)
(if (and (symbol? f)
(not (lex? f)))
(macro? (var f))
(isa f 'mac)
f
#f))
(define (mac-call m args)
(let/ (kw kwa args) (process-keywords nil nil args)
(parameterize ((local-env (local-env)))
;; TODO: use (keyword-apply #%call ...) ?
(keyword-apply (rep m) kw kwa args))))
#|(define #%keyword-call
(make-keyword-procedure
(lambda (kw kwa x . args)
(if (procedure? x)
(keyword-apply x kw kwa args)
(error "calling non-fn with keyword arguments" x kw kwa args)))))
(set '#%keyword-call #%keyword-call)|#
;; call a function or perform an array ref, hash ref, etc.
(define #%call ;(x . args)
;; uses case-lambda for ridiculous speed: now using call for *all* function
;; calls is just as fast as using the funcall functions, and unlike
;; funcall, this hardcodes up to 6 arguments rather than only 4
;;
;; I could go higher but it'd be kinda pointless and would just make the
;; definition of call even bigger than it already is
(case-lambda
((x) (if (procedure? x)
(x)
(ref x)))
((x a) (if (procedure? x)
(x a)
(ref x a)))
((x a b) (if (procedure? x)
(x a b)
(ref x a b)))
((x a b c) (if (procedure? x)
(x a b c)
(ref x a b c)))
((x a b c d) (if (procedure? x)
(x a b c d)
(ref x a b c d)))
((x a b c d e) (if (procedure? x)
(x a b c d e)
(ref x a b c d e)))
((x a b c d e f) (if (procedure? x)
(x a b c d e f)
(ref x a b c d e f)))
((x . args) ;(prn "warning: called with 7+ arguments:" x args)
(if (procedure? x)
(apply x args)
(apply ref x args)))))
(set '#%call #%call)
;; Non-fn constants in functional position are valuable real estate, so
;; should figure out the best way to exploit it. What could (1 foo) or
;; ('a foo) mean? Maybe it should mean currying.
;;
;; For now the way to make the default val of a hash table be other than
;; nil is to supply the val when doing the lookup. Later may also let
;; defaults be supplied as an arg to table. To implement this, need: an
;; eq table within scheme mapping tables to defaults, and to adapt the
;; code in arc.arc that reads and writes tables to read and write their
;; default vals with them. To make compatible with existing written tables,
;; just use an atom or 3-elt list to keep the default.
;;
;; experiment: means e.g. [1] is a constant fn
;; ((or (number? fn) (symbol? fn)) fn)
;; another possibility: constant in functional pos means it gets
;; passed to the first arg, i.e. ('kids item) means (item 'kids).
(mset ref (x k (o d))
(case-lambda
((x k) (if (namespace? x) (namespace-variable-value k #f (lambda () nil) x) ;(global-name k)
(hash? x) (hash-ref x k nil)
(string? x) (string-ref x k)
(pair? x) (list-ref x k)
(err "function call on inappropriate object" x k)))
((x k d) (if (namespace? x) (namespace-variable-value k #f ;(global-name k)
(if (procedure? d) d (lambda () d))
x)
(hash? x) (hash-ref x k d)
(err "function call on inappropriate object" x k d)))
(args (apply err "function call on inappropriate object" args))))
;=============================================================================
; Binaries
;=============================================================================
;; (pairwise pred '(a b c d)) =>
;; (and (pred a b) (pred b c) (pred c d))
;; pred returns t/nil, as does pairwise