-
Notifications
You must be signed in to change notification settings - Fork 13
/
ac.t
212 lines (170 loc) · 6.55 KB
/
ac.t
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
(use arc test runtime)
(def matches (pattern form)
;; todo the cadr is cheating; what I really want is a recursive
;; match
(and (alist cadr.form)
(iso (firstn len.pattern cadr.form) pattern)))
(def ac-upto (pattern)
(prn)
(write pattern) (prn)
(let arc (runtime '(ar) (list (racket-path->string (racket-current-directory))))
(catch
(each form (readfile "ac.arc")
(arc!eval form)
(when (matches pattern form) (throw nil)))
(err "pattern not found in source" pattern))
arc))
(mac testfor (pattern . body)
`(let a (ac-upto ',pattern)
,@body))
(testfor (ar-extend ac (s env) (ac-literal? s))
(testis (a!eval 123) 123)
(testis (a!eval #\a) #\a)
(testis (a!eval "abc") "abc")
(testis (a!eval car) car))
(testfor (ar-def ac-lex? (v env))
(testis (a!ac-lex? 'y '(x y z)) t)
(testis (a!ac-lex? 'w '(x y z)) nil))
(testfor (ar-extend ac (s env) (ar-tnil (racket-symbol? s)))
(testis (a!eval 'foo)
(makeerr "reference to undefined identifier: foo"))
(testis (a!eval 'car) a!car)
(testis (a!eval 't) t))
(testfor (ar-extend ac (s env)
(ar-tnil
(racket-and
(racket-mpair? s)
(racket-not (ar-true (is (car s) (racket-quote ail-code))))
(racket-not (ar-true (is (car s) (racket-quote use)))))))
(testis (a!eval '(+)) 0)
(testis (a!eval '(+ 1 2)) 3)
(testis (a!eval '(+ 1 2 3)) 6)
(testis (a!eval '(+ 1 2 3 4)) 10)
(testis (a!eval '(+ 1 2 3 4 5)) 15))
(testfor (ar-extend ac (s env) (caris s (racket-quote quote)))
(testis (a!eval ''abc) 'abc)
(testis (a!eval ''()) 'nil)
(testis (a!eval ''(a)) '(a))
(testis (a!eval ''(nil)) '(nil))
(testis (a!eval ''(a . b)) '(a . b))
(testis (a!eval '(apply list 1 2 '(3 4))) '(1 2 3 4))
(testis (a!eval '(apply +)) 0)
(testis (a!eval '(apply + nil)) 0)
(testis (a!eval '(apply + '(1))) 1)
(testis (a!eval '(apply + '(1 2 3 4))) 10)
(testis (a!eval '(apply + 1 2 nil)) 3)
(testis (a!eval '(apply + 1 2 '(3 4))) 10))
(testfor (ar-def ac-body (body env))
(testis (a!ac-body '(1 2 3) nil) '(1 2 3)))
(testfor (ar-def ac-dotted-list? (x))
(testis (a!ac-dotted-list? '()) nil))
(testfor (ar-extend ac (s env) (caris s (racket-quote ar-fn)))
(testis (a!eval '((ar-fn ()))) nil)
(testis (a!eval '((ar-fn () 3))) 3)
(testis (a!eval '((ar-fn (a) a) 3)) 3)
(testis (a!eval '((ar-fn (a b) b) 1 2)) 2)
(testis (a!eval '((ar-fn (a b) (+ a b 3)) 1 2)) 6))
(testfor (ar-extend ac (s env) (caris s (racket-quote quasiquote)))
(testis (a!eval '`nil) nil)
(testis (a!eval '`3) 3)
(testis (a!eval '`a) 'a)
(testis (a!eval '`()) nil)
(testis (a!eval '`(1)) '(1))
(testis (a!eval '`(1 . 2)) '(1 . 2))
(testis (a!eval '`(1 2)) '(1 2))
(testis (a!eval '`((1 2))) '((1 2)))
(testis (a!eval '`,(+ 1 2)) 3)
(testis (a!eval '`(,(+ 1 2))) (list 3))
(testis (a!eval '`(1 2 ,(+ 1 2) 4)) '(1 2 3 4))
(testis (a!eval '(eval ``3)) 3)
(testis (a!eval '(eval ``,,3)) 3)
(testis (a!eval '(eval ``,,(+ 1 2))) 3)
(testis (a!eval '`(1 ,@(list 2 3) 4)) '(1 2 3 4))
(testis (a!eval '(eval ``,(+ 1 ,@(list 2 3) 4))) 10)
;; Note the following gives the wrong answer in Arc3.1 because of
;; Racket's nested list splicing bug.
(testis (a!eval '(eval (eval ``(+ 1 ,,@(list 2 3) 4)))) 10))
(testfor (ar-extend ac (s env) (caris s (racket-quote if)))
(testis (a!eval '(if)) nil)
(testis (a!eval '(if nil)) nil)
(testis (a!eval '(if 9)) 9)
(testis (a!eval '(if nil 1 2)) 2)
(testis (a!eval '(if 9 1 2)) 1)
(testis (a!eval '(if 9 1 2 3)) 1)
(testis (a!eval '(if nil 1 2 3)) 3))
(testfor (ar-extend ac (s env) (caris s (racket-quote assign)))
(testis (a!eval '(assign x 123)) 123)
(testis (a!eval '((ar-fn ()
(assign x 123)
x)))
123)
(testis (a!eval '((ar-fn (x)
(assign x 123))
456))
123)
(testis (a!eval '((ar-fn (x)
(assign x 123)
x)
456))
123)
(testis (a!eval '((ar-fn (a b)
(assign a 11)
(assign b 22)
(list a b))
1 2))
'(11 22)))
(testfor (ar-def ac-macro?)
(testis (a!eval '(ac-macro? 5)) nil)
(testis (a!eval '(ac-macro? 'foo)) nil)
(testis (a!eval '(ac-macro? (annotate 'mac 123))) 123)
(testis (a!eval '((ar-fn ()
(assign foo 5)
(ac-macro? 'foo))))
nil)
(testis (a!eval '((ar-fn ()
(assign foo (annotate 'mac 123))
(ac-macro? 'foo))))
123))
(testfor (ar-extend ac-call (fn args env)
(racket-if (ar-true (ac-lex? fn env)) nil (ac-macro? fn)))
(a!eval '(assign foo (annotate 'mac (ar-fn (x) x))))
(testis (a!eval '(foo 123)) 123))
(testfor (ar-def ac-fn-rest (args body env))
(testis (a!eval '((ar-fn args (car args)) 1 2)) 1)
(testis (a!eval '(cdr ((ar-fn args args) 1))) nil)
(testis (a!eval '((ar-fn (a b . rest) (car rest)) 1 2 3 4)) 3))
(testfor (racket-define bound)
(testis (a!eval '(bound 'QmrQOCYWOy)) nil)
(a!eval '(assign foo nil))
(testis (a!eval '(bound 'foo)) t)
(a!eval '(assign foo 123))
(testis (a!eval '(bound 'foo)) t))
(testfor (racket-define (ar-disp x (port (racket-current-output-port))))
(let port (outstring)
(a!eval `(ar-disp "a" ',port))
(testis (inside port) "a"))
(testis (tostring (a!ar-disp "abc")) "abc"))
(testfor (racket-define (ar-write x (port (racket-current-output-port))))
(testis (tostring (a!eval '(ar-write "a"))) "\"a\""))
(testfor (racket-define (table (init nil)))
(testis (a!eval '(table)) (table))
(testis (a!eval `(table ,(ar-fn (k) (= k!a 3)))) (obj a 3)))
(testfor (ar-def sref (com val ind))
(a!eval '(assign a '(x y z)))
(testis (a!eval '(sref a 'M 1)) 'M)
(testis a!a '(x M z))
(a!eval '(assign a (table)))
(a!eval '(sref a 55 'x))
(testis a!a (obj x 55))
(testis (a!eval '(table (ar-fn (h)
(sref h 55 'x)
(sref h 66 'y))))
(obj x 55 y 66))
(a!eval '(assign a "abcd"))
(a!eval '(sref a #\M 2))
(testis a!a "abMd"))
(testfor (ar-def details (c))
(testis (a!eval '(on-err details (ar-fn () (/ 1 0))))
"/: division by zero"))
(testfor (ar-def parameter (init))
(testis (type (a!eval '(parameter 3))) 'parameter))