-
Notifications
You must be signed in to change notification settings - Fork 13
/
ar.arc
450 lines (384 loc) · 14.5 KB
/
ar.arc
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
(ail-code
(racket-define - racket--)
(racket-define / racket-/)
(racket-define * racket-*)
(racket-define cons racket-mcons)
(racket-define inside racket-get-output-string)
(racket-define instring racket-open-input-string)
(racket-define nil (racket-quote nil))
(racket-define outstring racket-open-output-string)
(racket-define t (racket-quote t))
(racket-define uniq racket-gensym)
(racket-define (ar-toarc x)
(racket-cond
((racket-pair? x)
(racket-mcons (ar-toarc (racket-car x))
(ar-toarc (racket-cdr x))))
((racket-null? x)
nil)
((racket-string? x)
(racket-string-copy x))
(racket-else x)))
(racket-define sig (racket-make-hash))
(racket-hash-set! sig (racket-quote -) (ar-toarc (racket-quote args)))
(racket-hash-set! sig (racket-quote /) (ar-toarc (racket-quote args)))
(racket-hash-set! sig (racket-quote *) (ar-toarc (racket-quote args)))
(racket-hash-set! sig (racket-quote cons) (ar-toarc (racket-quote (a b))))
(racket-hash-set! sig (racket-quote inside) (ar-toarc (racket-quote (s))))
(racket-hash-set! sig (racket-quote instring) (ar-toarc (racket-quote (str))))
(racket-hash-set! sig (racket-quote outstring) (ar-toarc (racket-quote ())))
(racket-hash-set! sig (racket-quote uniq) (ar-toarc (racket-quote ())))
(racket-hash-set! sig (racket-quote use-apply) (ar-toarc (racket-quote (item))))
(racket-hash-set! sig (racket-quote ar-toarc) (ar-toarc (racket-quote (x))))
(racket-define-syntax-rule (ar-def name signature . body)
(racket-begin
(racket-hash-set!
sig
(racket-quote name)
(ar-toarc (racket-quote signature)))
(racket-define (name . signature)
. body)))
(ar-def ar-r/list-toarc (x)
(racket-cond
((racket-pair? x)
(racket-mcons (racket-car x) (ar-r/list-toarc (racket-cdr x))))
((racket-null? x)
nil)
(racket-else x)))
(ar-def list args
(ar-r/list-toarc args))
(ar-def ar-list-fromarc (x)
(racket-cond
((racket-mpair? x)
(racket-cons (racket-mcar x) (ar-list-fromarc (racket-mcdr x))))
((racket-eq? x nil)
(racket-quote ()))
(racket-else x)))
(racket-define-struct ar-tunnel-struct (v))
(racket-define ar-tunnel make-ar-tunnel-struct)
(ar-def ar-deep-fromarc (x)
(racket-cond
((ar-tunnel-struct? x)
(ar-tunnel-struct-v x))
;; nil in the car position isn't a list terminator, and so can
;; be left alone.
((racket-mpair? x)
(racket-cons (racket-let ((a (racket-mcar x)))
(racket-if (racket-eq? a nil)
nil
(ar-deep-fromarc a)))
(racket-let ((b (racket-mcdr x)))
(racket-if (racket-eq? b nil)
(racket-quote ())
(ar-deep-fromarc b)))))
(racket-else
(racket-when (racket-pair? x)
(err "Racket pair passed to ar-deep-fromarc" x))
x)))
(racket-define err racket-error)
(racket-hash-set! sig (racket-quote err) (racket-quote args))
(ar-def car (x)
(racket-if (racket-eq? x nil)
nil
(racket-mcar x)))
(ar-def cdr (x)
(racket-if (racket-eq? x nil)
nil
(racket-mcdr x)))
(ar-def cadr (x)
(car (cdr x)))
(ar-def cddr (x)
(cdr (cdr x)))
(ar-def ar-apply-non-fn (x args)
(racket-cond
((racket-mpair? x)
(racket-mlist-ref x (car args)))
((racket-string? x)
(racket-string-ref x (car args)))
((racket-hash? x)
(racket-hash-ref x
(car args)
(racket-let ((default
(racket-if (racket-mpair? (cdr args))
(car (cdr args))
nil)))
(racket-lambda () default))))
(racket-else
(err "Function call on inappropriate object" x args))))
(ar-def ar-apply (fn . racket-arg-list)
(racket-if (racket-procedure? fn)
(racket-apply fn racket-arg-list)
(ar-apply-non-fn fn (ar-r/list-toarc racket-arg-list))))
(ar-def ar-funcall0 (fn)
(racket-if (racket-procedure? fn)
(fn)
(ar-apply fn)))
(ar-def ar-funcall1 (fn arg1)
(racket-if (racket-procedure? fn)
(fn arg1)
(ar-apply fn arg1)))
(ar-def ar-funcall2 (fn arg1 arg2)
(racket-if (racket-procedure? fn)
(fn arg1 arg2)
(ar-apply fn arg1 arg2)))
(ar-def ar-funcall3 (fn arg1 arg2 arg3)
(racket-if (racket-procedure? fn)
(fn arg1 arg2 arg3)
(ar-apply fn arg1 arg2 arg3)))
(ar-def ar-funcall4 (fn arg1 arg2 arg3 arg4)
(racket-if (racket-procedure? fn)
(fn arg1 arg2 arg3 arg4)
(ar-apply fn arg1 arg2 arg3 arg4)))
(ar-def ar-combine-args (as)
(racket-let next ((as as) (accum (racket-list)))
(racket-cond
((racket-null? as)
accum)
((racket-null? (racket-cdr as))
(racket-append accum (ar-list-fromarc (racket-car as))))
(racket-else
(next (racket-cdr as)
(racket-append accum (racket-list (racket-car as))))))))
(ar-def apply (fn . args)
(racket-apply ar-apply fn (ar-combine-args args)))
(ar-def ar-exint (x)
(racket-and (racket-integer? x) (racket-exact? x)))
(ar-def ar-tagged (x)
(racket-and (racket-vector? x)
(racket-eq? (racket-vector-ref x 0) (racket-quote tagged))))
(ar-def rep (x)
(racket-if (ar-tagged x)
(racket-vector-ref x 2)
x))
(ar-def type (x)
(racket-cond
((ar-tagged x) (racket-vector-ref x 1))
((racket-mpair? x) (racket-quote cons))
((racket-symbol? x) (racket-quote sym))
((racket-parameter? x) (racket-quote parameter))
((racket-procedure? x) (racket-quote fn))
((racket-char? x) (racket-quote char))
((racket-string? x) (racket-quote string))
((ar-exint x) (racket-quote int))
((racket-number? x) (racket-quote num))
((racket-hash? x) (racket-quote table))
((racket-output-port? x) (racket-quote output))
((racket-input-port? x) (racket-quote input))
((racket-exn? x) (racket-quote exception))
((racket-thread? x) (racket-quote thread))
((racket-thread-cell? x) (racket-quote thread-cell))
((racket-semaphore? x) (racket-quote semaphore))
(racket-else (racket-quote unknown))))
(ar-def annotate (totype rep)
(racket-cond
((racket-eqv? (type rep) totype) rep)
(racket-else (racket-vector (racket-quote tagged) totype rep))))
(ar-def ar-tnil (x)
(racket-if x t nil))
(ar-def ar-no (x)
(racket-eq? x nil))
(ar-def ar-true (x)
(racket-not (ar-no x)))
(ar-def map1 (f xs)
(racket-if (ar-no xs)
nil
(cons (ar-funcall1 f (car xs)) (map1 f (cdr xs)))))
(ar-def ar-iround (x)
(racket-inexact->exact (racket-round x)))
(ar-def coerce (x totype . args)
(racket-cond
((ar-tagged x)
(err "Can't coerce annotated object"))
((racket-eqv? totype (type x))
x)
((racket-char? x)
(racket-case totype
((int) (racket-char->integer x))
((string) (racket-string x))
((sym) (racket-string->symbol (racket-string x)))
(racket-else (err "Can't coerce" x type))))
((ar-exint x)
(racket-case totype
((num) x)
((char) (racket-integer->char x))
((string) (racket-apply racket-number->string x args))
(racket-else (err "Can't coerce" x type))))
((racket-number? x)
(racket-case totype
((int) (ar-iround x))
((char) (racket-integer->char (ar-iround x)))
((string) (racket-apply racket-number->string x args))
(racket-else (err "Can't coerce" x type))))
((racket-string? x)
(racket-case totype
((sym) (racket-string->symbol x))
((cons) (ar-r/list-toarc (racket-string->list x)))
((num) (racket-or (racket-apply racket-string->number x args)
(err "Can't coerce" x totype)))
((int) (racket-let ((n (racket-apply racket-string->number x args)))
(racket-if n
(ar-iround n)
(err "Can't coerce" x totype))))
(racket-else (err "Can't coerce" x totype))))
((racket-mpair? x)
(racket-case totype
((string) (racket-apply racket-string-append
(ar-list-fromarc
(map1 (racket-lambda (y)
(coerce y (racket-quote string)))
x))))
(racket-else (err "Can't coerce" x totype))))
((racket-eq? x nil)
(racket-case totype
((string) "")
((cons) nil)
(racket-else (err "Can't coerce" x type))))
((racket-symbol? x)
(racket-case totype
((string) (racket-symbol->string x))
(racket-else (err "Can't coerce" x type))))
(racket-else x)))
(ar-def ar-pairwise (pred lst)
(racket-cond
((racket-null? lst) t)
((racket-null? (racket-cdr lst)) t)
((racket-not (racket-eqv? (pred (racket-car lst) (racket-cadr lst))
nil))
(ar-pairwise pred (racket-cdr lst)))
(racket-else nil)))
(ar-def is2 (a b)
(ar-tnil
(racket-or (racket-eqv? a b)
(racket-and (racket-string? a)
(racket-string? b)
(racket-string=? a b)))))
(ar-def is args
(ar-pairwise is2 (ar-list-fromarc args)))
(ar-def caris (x val)
(ar-tnil
(racket-and (racket-mpair? x)
(ar-true (is (car x) val)))))
(ar-def <2 (x y)
(ar-tnil
(racket-cond
((racket-and (racket-number? x) (racket-number? y)) (racket-< x y))
((racket-and (racket-string? x) (racket-string? y)) (racket-string<? x y))
((racket-and (racket-symbol? x) (racket-symbol? y))
(racket-string<? (racket-symbol->string x)
(racket-symbol->string y)))
((racket-and (racket-char? x) (racket-char? y)) (racket-char<? x y))
(racket-else (err "Can't <" x y)))))
(ar-def < args
(ar-pairwise <2 (ar-list-fromarc args)))
(ar-def >2 (x y)
(ar-tnil
(racket-cond
((racket-and (racket-number? x) (racket-number? y)) (racket-> x y))
((racket-and (racket-string? x) (racket-string? y)) (racket-string>? x y))
((racket-and (racket-symbol? x) (racket-symbol? y))
(racket-string>? (racket-symbol->string x) (racket-symbol->string y)))
((racket-and (racket-char? x) (racket-char? y)) (racket-char>? x y))
(racket-else (err "Can't >" x y)))))
(ar-def > args
(ar-pairwise >2 (ar-list-fromarc args)))
(ar-def list-len (x)
(racket-cond
((ar-no x) 0)
((racket-mpair? x) (racket-+ 1 (list-len (racket-mcdr x))))
(racket-else (err "len expects a proper list"))))
(ar-def len (x)
(racket-cond
((racket-string? x) (racket-string-length x))
((racket-hash? x) (racket-hash-count x))
(racket-else (list-len x))))
(ar-def join args
(ar-r/list-toarc
(racket-apply racket-append
(racket-map ar-list-fromarc (ar-list-fromarc args)))))
(ar-def ar-alist (x)
(racket-or (ar-no x) (racket-mpair? x)))
(ar-def + args
(racket-cond
((racket-null? args)
0)
((racket-or (racket-char? (racket-car args))
(racket-string? (racket-car args)))
(racket-apply racket-string-append
(racket-map (racket-lambda (a)
(coerce a (racket-quote string)))
args)))
((ar-alist (racket-car args))
(racket-apply join args))
(racket-else
(racket-apply racket-+ args))))
(racket-hash-set! sig
(racket-quote peekc)
(ar-toarc (racket-quote ((o port stdin)))))
(racket-define (peekc (port (racket-current-input-port)))
(racket-let ((c (racket-peek-char port)))
(racket-if (racket-eof-object? c) nil c)))
(racket-hash-set! sig
(racket-quote readc)
(ar-toarc (racket-quote ((o port stdin) (o eof nil)))))
(racket-define (readc (port (racket-current-input-port))
(eof nil))
(racket-let ((c (racket-read-char port)))
(racket-if (racket-eof-object? c) eof c)))
(racket-hash-set! sig
(racket-quote writec)
(ar-toarc (racket-quote (c (o port stdout)))))
(racket-define (writec c (port (racket-current-output-port)))
(racket-write-char c port))
(ar-def ar-rread-from-string (str)
(racket-parameterize ((racket-current-readtable #f))
(racket-read (racket-open-input-string str))))
(ar-def ac-ail-code (forms)
(ar-tunnel
(racket-cons
(racket-quote racket-begin)
(racket-map (racket-lambda (form)
(racket-if (racket-string? form)
(ar-rread-from-string form)
(ar-deep-fromarc form)))
(ar-list-fromarc forms)))))
(ar-def ac-use (items)
(cons (racket-quote racket-begin)
(map1 (racket-lambda (item)
(cons (racket-quote use-apply)
(list (list (racket-quote racket-quote)
item))))
items)))
(ar-def ac (s env)
(racket-cond
((ar-true (caris s (racket-quote ail-code)))
(ac-ail-code (cdr s)))
((ar-true (caris s (racket-quote use)))
(ac-use (cdr s)))
(racket-else
(err "Bad object in expression" s))))
(racket-define (eval form (runtime nil))
(ar-racket-eval
(racket-if (ar-true runtime) runtime runtime*)
(ar-deep-fromarc (ac form nil))))
(racket-define arc-readtable* #f)
(racket-hash-set! sig
(racket-quote ar-read)
(ar-toarc (racket-quote (input (o eof nil)))))
(racket-define (ar-read input (eof nil))
(racket-let ((v (racket-parameterize
((racket-current-readtable arc-readtable*))
(racket-read input))))
(racket-if (racket-eof-object? v)
eof
(ar-toarc v))))
(ar-def loadin (in)
(racket-let ((x (ar-read in)))
(racket-if (ar-no x)
nil
(racket-begin
(eval x)
(loadin in)))))
(ar-def load (filename)
(racket-call-with-input-file filename
(racket-lambda (p) (loadin p))))
)