-
Notifications
You must be signed in to change notification settings - Fork 13
/
ar.t
264 lines (219 loc) · 8.03 KB
/
ar.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
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
(use arc test runtime)
(def matches (pattern form)
(iso (firstn len.pattern form) pattern))
;; Returns an Arc list of Racket forms. Assumes ar.arc contains
;; a single (ail-code ...) expression.
(def read-ar ()
(w/infile in "ar.arc"
(ar-r/list-toarc (racket-cdr (racket-read in)))))
(def ac-upto (pattern)
(prn)
(write pattern) (prn)
(let arc (runtime '() (list (racket-path->string (racket-current-directory))))
(catch
(each form (read-ar)
(arc!ar-racket-eval arc form)
(when (matches pattern (ar-toarc form)) (throw nil)))
(err "pattern not found in source" pattern))
arc))
(mac rq (lit)
`(ail-code ,(+ "(racket-quote " lit ")")))
(mac testfor (pattern . body)
`(let a (ac-upto ',pattern)
,@body))
(testfor (racket-define -)
(testis (a!- 5) -5)
(testis (a!- 20 4 3 2) 11))
(testfor (racket-define /)
(testis (a!/ 24 3 2) 4))
(testfor (racket-define *)
(testis (a!*) 1)
(testis (a!* 5) 5)
(testis (a!* 3 4 7) 84))
(testfor (racket-define cons)
(testis (a!cons 1 2) '(1 . 2)))
(testfor (racket-define inside)
(let s (outstring)
(disp "foo" s)
(testis (a!inside s) "foo")))
(testfor (racket-define instring)
(let s (a!instring "foo")
(testis (n-of 3 (readc s)) '(#\f #\o #\o))))
(testfor (racket-define nil)
(testis a!nil nil))
(testfor (racket-define outstring)
(let s (a!outstring)
(disp "foo" s)
(testis (inside s) "foo")))
(testfor (racket-define t)
(testis a!t t))
(testfor (racket-define uniq)
(testis (type (a!uniq)) 'sym))
(testfor (racket-define (ar-toarc x))
(testis (a!ar-toarc (rq "(1 2 (3 . 4) 5 () 6)"))
'(1 2 (3 . 4) 5 nil 6)))
(testfor (ar-def ar-r/list-toarc (x))
(testis (a!ar-r/list-toarc (rq "()")) 'nil)
(testis (a!ar-r/list-toarc (rq "(1 2 3)")) '(1 2 3))
(testis (a!ar-r/list-toarc (rq "(1 2 . 3)")) '(1 2 . 3)))
(testfor (ar-def list args)
(testis (a!list 1 2 3) '(1 2 3)))
(mac test-racket-equal (expression expected)
`(testis (racket-equal? ,expression ,expected)
(rq "#t")))
(testfor (ar-def ar-list-fromarc (x))
(test-racket-equal (a!ar-list-fromarc '())
(rq "()"))
(test-racket-equal (a!ar-list-fromarc '(1 2))
(rq "(1 2)"))
(test-racket-equal (a!ar-list-fromarc '(1 . 2))
(rq "(1 . 2)")))
(testfor (ar-def car (x))
(testis (a!car 'nil) 'nil)
(testis (a!car '(1 2 3)) 1))
(testfor (ar-def cdr (x))
(testis (a!cdr 'nil) 'nil)
(testis (a!cdr '(1 2 3)) '(2 3)))
(testfor (ar-def cadr (x))
(testis (a!cadr '(1 2 3)) 2))
(testfor (ar-def cddr (x))
(testis (a!cddr '(1 2 3 4)) '(3 4)))
(testfor (ar-def ar-apply (fn . racket-arg-list))
(testis (a!ar-apply + 1 2 3) 6)
(testis (a!ar-apply '(1 2 3) 1) 2)
(testis (a!ar-apply "abcde" 2) #\c)
(testis (a!ar-apply (obj a 1 b 2) 'b) 2)
(testis (a!ar-apply (obj a 1 b 2) 'x) nil)
(testis (a!ar-apply (obj a 1 b 2) 'x 3) 3))
(testfor (ar-def ar-funcall0 (fn))
(testis (a!ar-funcall0 +) 0))
(testfor (ar-def ar-funcall1 (fn arg1))
(testis (a!ar-funcall1 + 3) 3)
(testis (a!ar-funcall1 "abcd" 2) #\c))
(testfor (ar-def ar-funcall2 (fn arg1 arg2))
(testis (a!ar-funcall2 + 3 4) 7)
(testis (a!ar-funcall2 (obj a 1 b 2) 'x 3) 3))
(testfor (ar-def ar-funcall3 (fn arg1 arg2 arg3))
(testis (a!ar-funcall3 + 3 4 5) 12))
(testfor (ar-def ar-funcall4 (fn arg1 arg2 arg3 arg4))
(testis (a!ar-funcall4 + 3 4 5 6) 18))
(testfor (ar-def ar-combine-args (as))
(test-racket-equal (a!ar-combine-args (racket-list))
(rq "()"))
(test-racket-equal (a!ar-combine-args (racket-list '()))
(rq "()"))
(test-racket-equal (a!ar-combine-args (racket-list '(a)))
(rq "(a)"))
(test-racket-equal (a!ar-combine-args (racket-list '(a b c)))
(rq "(a b c)"))
(test-racket-equal (a!ar-combine-args (racket-list 'a '()))
(rq "(a)"))
(test-racket-equal (a!ar-combine-args (racket-list 'a '(b c d)))
(rq "(a b c d)"))
(test-racket-equal (a!ar-combine-args (racket-list 'a 'b '(c d)))
(rq "(a b c d)")))
(testfor (ar-def apply (fn . args))
(testis (a!apply +) 0)
(testis (a!apply join nil '((a b) (c d)))
'(a b c d))
(testis (a!apply + 1 2 '(3 4)) 10))
(testfor (ar-def annotate (totype rep))
(let x (a!annotate 'mytype 'foo)
(testis (a!type x) 'mytype)
(testis (a!rep x) 'foo)))
(testfor (ar-def map1 (f xs))
(testis (a!map1 odd '(1 2 3 4)) '(t nil t nil))
(testis (a!map1 (table) '(nil)) '(nil)))
(testfor (ar-def coerce (x totype . args))
(testis (a!coerce #\A 'int) 65)
(testis (a!coerce #\A 'string) "A")
(testis (a!coerce #\A 'sym) 'A)
(testis (a!coerce 123 'num) 123)
(testis (a!coerce 65 'char) #\A)
(testis (a!coerce 123 'string) "123")
(testis (a!coerce 128 'string 16) "80")
(testis (a!coerce 13.4 'int) 13)
(testis (a!coerce 65.0 'char) #\A)
(testis (a!coerce 14.5 'string) "14.5")
(testis (a!coerce "foo" 'sym) 'foo)
(testis (a!coerce "foo" 'cons) '(#\f #\o #\o))
(testis (a!coerce "123.5" 'num) 123.5)
(testis (a!coerce "123" 'int) 123)
(testis (a!coerce '("a" b #\c) 'string) "abc")
(testis (a!coerce 'nil 'string) "")
(testis (a!coerce 'nil 'cons) 'nil))
(testfor (ar-def is args)
(testis (a!is) 't)
(testis (a!is 4) 't)
(testis (a!is 3 4) 'nil)
(testis (a!is 4 4) 't)
(testis (a!is 4 4 5) 'nil)
(testis (a!is 4 4 4) 't))
(testfor (ar-def caris (x val))
(testis (a!caris 4 'x) 'nil)
(testis (a!caris '(y z) 'x) 'nil)
(testis (a!caris '(x y) 'x) 't))
(testfor (ar-def < args)
(testis (a!<) t)
(testis (a!< 1) t)
(testis (a!< 1 2) t)
(testis (a!< 2 1) nil)
(testis (a!< 1 2 3) t)
(testis (a!< 1 3 2) nil)
(testis (a!< 3 1 2) nil)
(testis (a!< 1 2 3 4) t)
(testis (a!< 1 3 2 4) nil)
(testis (a!< "abc" "def") t)
(testis (a!< "def" "abc") nil)
(testis (a!< 'abc 'def) t)
(testis (a!< 'def 'abc) nil)
(testis (a!< #\a #\b) t)
(testis (a!< #\b #\a) nil))
(testfor (ar-def > args)
(testis (a!>) t)
(testis (a!> 1) t)
(testis (a!> 2 1) t)
(testis (a!> 1 2) nil)
(testis (a!> 3 2 1) t)
(testis (a!> 3 1 2) nil)
(testis (a!> 2 3 1) nil)
(testis (a!> 4 3 2 1) t)
(testis (a!> 4 3 1 2) nil)
(testis (a!> "def" "abc") t)
(testis (a!> "abc" "def") nil)
(testis (a!> 'def 'abc) t)
(testis (a!> 'abc 'def) nil)
(testis (a!> #\b #\a) t)
(testis (a!> #\a #\b) nil))
(testfor (ar-def len (x))
(testis (a!len "abc") 3)
(testis (a!len (obj 'a 1 'b 2)) 2)
(testis (a!len '()) 0)
(testis (a!len '(1)) 1)
(testis (a!len '(1 2)) 2)
(testis (a!len '(1 2 3)) 3))
(testfor (ar-def join args)
(testis (a!join) '())
(testis (a!join '()) '())
(testis (a!join 1) 1)
(testis (a!join '(1 2)) '(1 2))
(testis (a!join '() '()) '())
(testis (a!join '(1 2) '()) '(1 2))
(testis (a!join '(1 2) 3) '(1 2 . 3))
(testis (a!join '() '(1 2)) '(1 2))
(testis (a!join '(1 2) '(3 4)) '(1 2 3 4))
(testis (a!join '(1 2) 3) '(1 2 . 3))
(testis (a!join '(1) '(2) '(3)) '(1 2 3)))
(testfor (ar-def + args)
(testis (a!+) 0)
(testis (a!+ #\a "b" 'c 3) "abc3")
(testis (a!+ "a" 'b #\c) "abc")
(testis (a!+ 'nil '(1 2 3)) '(1 2 3))
(testis (a!+ '(1 2) '(3)) '(1 2 3))
(testis (a!+ 1 2 3) 6))
(testfor (ar-def ac (s env))
(testis (a!ac + nil)
(makeerr "Bad object in expression #<procedure:+>")))
(testfor (racket-define (eval form (runtime nil)))
(testis (a!eval '(ail-code (racket-+ 3 4))) 7)
(testis (a!eval '(ail-code (racket-set! foo 123) foo)) 123))