/
read.arc
278 lines (244 loc) · 7.96 KB
/
read.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
; Copyright (c) 2008 Dissegna Stefano
; Released under the terms of the GNU LGPL
(set separators* '(#\Space #\Newline #\Tab))
(set special-chars* '(#\; #\( #\) #\[ #\] #\' #\, #\`))
(set eof* "end-of-file")
(def skip-line (stream)
(let c (readc stream)
(if (not (is c #\Newline))
(skip-line stream))))
(def read-table (list))
(def read/tbl (stream read-table)
(withs (c (readc stream)
f (assoc-f read-table c stream))
(if
(nullp c) eof*
f (do
(ungetc stream c)
(f stream read-table))
(mem c separators*) (read/tbl stream read-table)
(is c #\;) (do
(skip-line stream)
(read/tbl stream read-table))
(err (str-append "Unknown char: " (char->str c))))))
; handles ssyntax -- currently hardwired for f:g -> (compose f g)
(def expand-ssyntax (str)
(let aux nil
(set aux (fn (pos)
(if (< pos (str-len str))
(if (is (str-ref str pos) #\:)
pos
(aux (+ pos 1))))))
(let sep (aux 0)
(if sep
(list 'compose
(intern (substr str 0 sep))
(expand-ssyntax (substr str (+ sep 1) (str-len str))))
(intern str)))))
(def read-symbol (s tbl)
(with (l nil
c nil)
(fwhile (fn () (set c (readc s)) (and (not (mem c separators*))
(not (mem c special-chars*))))
(fn ()
(set l (cons c l))))
(ungetc s c)
(with (str (mkstr (len l))
l (rev l)
i 0)
(feach (fn (c) (do (str-set str i c) (set i (+ i 1)))) l)
(if (iso str "t") t
(iso str "nil") nil
(intern str)))))
;(expand-ssyntax str)))))
; read-symbol must be the last, because it catches (almost) everything
(set read-table
(cons
(cons
(fn (x y)
(and (not (mem x special-chars*)) (not (mem x separators*))))
read-symbol)
read-table))
(def adigit (c)
(and (>= (char->fx c) (char->fx #\0)) (<= (char->fx c) (char->fx #\9))))
(def read-num (s tbl)
(withs (c (readc s)
sign (if (is c #\-) -1 1)
n (if (is c #\-) 0 (- (char->fx c) (char->fx #\0))))
(set c (readc s))
(fwhile (fn () (adigit c))
(fn ()
(set n (+ (* n 10) (- (char->fx c) (char->fx #\0))))
(set c (readc s))))
(if (is c #\.)
(let p 10.0
(set c (readc s))
(fwhile (fn () (adigit c))
(fn ()
(set n (+ n (/ (- (char->fx c) (char->fx #\0)) p)))
(set p (* p 10))
(set c (readc s))))))
(ungetc s c)
(* sign n)))
(def read-num-p (c stream)
(if (and c (adigit c))
t
(if (is c #\-)
(let next (readc stream)
(ungetc stream next)
(if (adigit next)
t)))))
(set read-table (cons (cons read-num-p read-num) read-table))
(def read-string (s tbl)
(with (escape-next nil
res nil
c #\Null)
(readc s) ; skip first #\"
(fwhile (fn ()
(set c (readc s))
(not (and (is c #\") (not escape-next))))
(fn ()
(if
escape-next (do
(set escape-next nil)
(set res (cons (escape-char c) res)))
(is c #\\) (set escape-next t)
(not (is c #\")) (set res (cons c res)))))
(with (str (mkstr (len res))
res (rev res)
i 0)
(feach (fn (c) (do (str-set str i c) (set i (+ i 1)))) res)
str)))
(let escape-table '((#\" . #\")
(#\\ . #\\)
(#\n . #\Newline)
(#\t . #\Tab))
(def escape-char (c)
(let res (cdr (assoc c escape-table))
(if res res (err (make-string "Don't know how to escape char " c))))))
(set read-table (cons (cons #\" read-string) read-table))
(def read-quote (s tbl)
(readc s) ; skip '
(list 'quote (read/tbl s tbl)))
(def read-quasiquote (s tbl)
(readc s) ; skip `
(list 'quasiquote (read/tbl s tbl)))
(def read-unquote (s tbl)
(readc s) ; skip ,
(let next (readc s)
(if (is next #\@) ; splice
(list 'splice (read/tbl s tbl))
(do
(ungetc s next)
(list 'unquote (read/tbl s tbl))))))
(set read-table (append (list (cons #\' read-quote)
(cons #\` read-quasiquote)
(cons #\, read-unquote))
read-table))
(def read-char-p (c stream)
(and
(is c #\#)
(let next (readc stream)
(ungetc stream next)
(is next #\\))))
(let name->char '((Newline . #\Newline)
(Tab . #\Tab)
(Null . #\Null)
(Space . #\Space)
(\ . #\\))
(def read-char (s tbl)
(readc s) ; skip #
(readc s) ; skip \
(let c (readc s)
(if (or (mem c separators*) (mem c special-chars*))
c
(do
(ungetc s c)
(let word (read-symbol s tbl)
(if (is (str-len (to-string word)) 1)
(str-ref (to-string word) 0)
(let res (assoc word name->char)
(if (not res)
(err (make-string "Unknown char name: " word))
(cdr res))))))))))
; (let c (readc s)
; (if (is c #\\)
; (let next (readc s)
; (if (or (mem next separators*) (mem next special-chars*))
; (do (ungetc s next) #\\)
; (escape-char next)))
; c)))
(set read-table (cons (cons read-char-p read-char) read-table))
(def skip-separators (s)
(let c (readc s)
(if (mem c separators*)
(skip-separators s)
(ungetc s c))))
(def read-list-with-ter (s tbl ter)
(skip-separators s)
(let next (readc s)
(if (is next ter)
nil
(do
(ungetc s next)
(with (the-car (read/tbl s tbl)
the-cdr nil
c nil)
(skip-separators s)
(set c (readc s))
(if (is c #\.)
(do
(set the-cdr (read/tbl s tbl))
(skip-separators s)
(if (not (is (readc s) ter))
(err "More than one object follows ."))
(cons the-car the-cdr))
(is c ter) (cons the-car nil)
(do (ungetc s c)
(cons the-car (read-list-with-ter s tbl ter)))))))))
(def read-list (s tbl)
(readc s) ; skip (
(read-list-with-ter s tbl #\)))
(set read-table (cons (cons #\( read-list) read-table))
(def read-vector-p (c stream)
(and
(is c #\#)
(let next (readc stream)
(ungetc stream next)
(is next #\())))
(def read-vector (s tbl)
(readc s) ; skip #
(apply vector (read-list s tbl)))
(set read-table (cons (cons read-vector-p read-vector) read-table))
(def read-square-bracket (s tbl)
(readc s) ; skip [
(let body (read-list-with-ter s tbl #\])
(list 'fn (list '_) body)))
(set read-table (cons (cons #\[ read-square-bracket) read-table))
(def readall (in-stream)
(read-list-with-ter in-stream read-table nil))
(def do-splice (l before)
(if (and l (atom l)) l
(not l) before
(consp (car l))
(if (is (caar l) 'tosplice)
(let res (list 'append before (do-splice (cadr (car l)) nil))
(list 'append res (cons 'list (do-splice (cdr l) nil))))
(do-splice (cdr l) (append before (list (do-splice (car l) nil)))))
(do-splice (cdr l) (append before (list (car l))))))
(def do-quasiquote (x level)
(if (is level 0) x
(atom x) (list 'quote x)
(and (is level 1) (is (car x) 'unquote))
(do-quasiquote (cadr x) (- level 1))
(is (car x) 'unquote)
(list 'unquote (do-quasiquote (cadr x) (- level 1)))
(and (is level 1) (is (car x) 'splice))
(list 'tosplice (do-quasiquote (cadr x) (- level 1)))
(is (car x) 'splice)
(list 'splice (do-quasiquote (cadr x) (- level 1)))
(is (car x) 'quasiquote)
(list 'quasiquote (do-quasiquote (cadr x) (+ level 1)))
(cons 'list (map [do-quasiquote _ level] x))))
(mac quasiquote (l)
(do-splice (do-quasiquote l 1) nil))