-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathreader.ol
284 lines (245 loc) · 8.68 KB
/
reader.ol
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
(require (fs "fs")
(ast "./ast"))
(define chars-whitespace " \n\t\r")
(define chars-special "(){}[],@'`:")
(define chars-delim (str chars-whitespace chars-special ";"))
(define (in str char)
(number? (vector-find str char)))
(define (vec-getter i)
(lambda (vec)
(vector-ref vec i)))
(define (read src)
(define index 0)
(define len (vector-length src))
(define lineno 0)
(define colno 0)
(define (current)
(if (finished)
""
(vector-ref src index)))
(define (previous)
(vector-ref src (- index 1)))
(define (forward)
(set! index (+ index 1))
(if (== (previous) "\n")
(begin
(set! lineno (+ lineno 1))
(set! colno 0))
(set! colno (+ colno 1))))
(define (back)
(set! index (- index 1))
(if (== (current) "\n")
(set! lineno (- lineno 1))))
(define (finished)
(>= index len))
(define (skip-whitespace)
(let loop ()
(if (in chars-whitespace (current))
(begin
(forward)
(loop)))))
(define (parse-string lineno colno)
(let loop ((s ""))
(forward)
(cond
((== (current) "\\")
(forward)
(loop
(str s (let ((c (current)))
(cond
((== c "n") "\n")
((== c "t") "\t")
((== c "r") "\r")
(else c))))))
((== (current) "\"") (make-token 'STRING s lineno colno))
(else (loop (str s (current)))))))
(define (parse-token s lineno colno)
(cond
((s.match (RegExp "^[-+]?[0-9]+$")) (make-token 'INTEGER s lineno colno))
((s.match (RegExp "^[-+]?[0-9]+\\.[0-9]*$")) (make-token 'FLOAT s lineno colno))
((s.match (RegExp "^[-+]?0x"))
(let ((m (s.match (RegExp "0x([0-9a-fA-F]+)$")))
(prefix (if (== (vector-ref s 0) "-") "-" "")))
(if m
(make-token 'HEX (str prefix (vector-ref m 1)) lineno colno)
(throw (str "invalid hex value: " s)))))
((or (== s "#f") (== s "#t")) (make-token 'BOOLEAN s lineno colno))
(else (make-token 'SYMBOL s lineno colno))))
(define (parse-comment lineno colno)
(let loop ((s ""))
(forward)
(if (or (finished)
(== (current) "\n"))
(make-token 'COMMENT s lineno colno)
(loop (str s (current))))))
;; tokens
;; create a unique reference to disambiguate internal objects in the
;; reader
(define unique-obj (list #t))
(define (make-token type data lineno colno)
[unique-obj type data lineno colno])
(define token-type (vec-getter 1))
(define token-data (vec-getter 2))
(define token-lineno (vec-getter 3))
(define token-colno (vec-getter 4))
(define (token? tok)
(and (vector? tok)
(== (vector-ref tok 0) unique-obj)))
(define (get-token)
(skip-whitespace)
(let ((c (current))
(lineno lineno)
(colno colno))
(cond
((in chars-special c)
(forward)
(make-token 'SPECIAL c lineno colno))
((== c "\"")
(let ((s (parse-string lineno colno)))
(forward)
s))
((== c ";")
(parse-comment lineno colno))
((== c "") #f)
((finished) #f)
(else
(let loop ((s ""))
(if (or (in chars-delim (current))
(finished))
(parse-token s lineno colno)
(begin
(forward)
(loop (str s (previous))))))))))
;; parser
(define (token->exp token)
(let ((type (token-type token))
(data (token-data token)))
(cond
((== type 'STRING) data)
((== type 'SYMBOL) (string->symbol data))
((== type 'BOOLEAN) (if (== data "#f") #f #t))
((== type 'INTEGER) (parseInt data))
((== type 'FLOAT) (parseFloat data))
((== type 'HEX) (parseInt data 16))
(else
(throw (str "cannot convert token to exp: " token))))))
(define (special? t chars)
(and (token? t)
(== (token-type t) 'SPECIAL)
(in chars (token-data t))))
(define (compound-start? t)
(or (special? t "(")
(special? t "[")
(special? t "{")))
(define (compound-end? t)
(or (special? t ")")
(special? t "]")
(special? t "}")))
(define (end? t)
(and (token? t)
(== (token-type t) 'END)))
(define (read-exp)
(let ((token (get-token)))
(cond
((not token)
(make-token 'END #f #f #f))
((compound-end? token)
;; we simply return the token so the list/vector/dict loop
;; knows when to end
token)
((compound-start? token)
(let loop ((lst '())
(exp (read-exp)))
(if (or (end? exp)
(compound-end? exp))
(begin
;; the loop will only break when it hits the end of
;; file or an end delimiter, so we check the current
;; character and move forward
(define in-list? (special? token "("))
(define in-vector? (special? token "["))
(define in-dict? (special? token "{"))
(cond
((and in-list? (special? exp ")")) (ast.make-node 'LIST (reverse lst)
(token-lineno token)
(token-colno token)))
((and in-vector? (special? exp "]")) (ast.make-node 'VECTOR (reverse lst)
(token-lineno token)
(token-colno token)))
((and in-dict? (special? exp "}")) (ast.make-node 'DICT (reverse lst)
(token-lineno token)
(token-colno token)))
(else
(throw (str "unterminated "
(cond
(list? "list")
(vector? "vector")
(dict? "dict")))))))
(begin
(loop (cons exp lst) (read-exp))))))
((special? token "'")
(ast.make-node
'LIST
(list (ast.make-node 'ATOM 'quote
(token-lineno token)
(token-colno token))
(read-exp))
(token-lineno token)
(token-colno token)))
((special? token ":")
(let ((e (read-exp)))
(if (or (not (ast.atom? e))
(not (symbol? (ast.node-data e))))
(throw (str "invalid key expression: " (ast.node-data e))))
(ast.make-node 'ATOM (symbol->key (ast.node-data e))
(token-lineno token)
(token-colno token))))
((special? token "`")
(ast.make-node
'LIST
(list (ast.make-node 'ATOM 'quasiquote
(token-lineno token)
(token-colno token))
(read-exp))
(token-lineno token)
(token-colno token)))
((special? token ",")
(let ((next (current)))
(if (== next "@")
(begin
(forward)
(ast.make-node
'LIST
(list (ast.make-node 'ATOM 'unquote-splicing
(token-lineno token)
(token-colno token))
(read-exp))
(token-lineno token)
(token-colno token)))
(begin
(ast.make-node
'LIST
(list (ast.make-node 'ATOM 'unquote
(token-lineno token)
(token-colno token))
(read-exp))
(token-lineno token)
(token-colno token))))))
(else
(if (== (token-type token) 'COMMENT)
(read-exp)
(ast.make-node 'ATOM
(token->exp token)
(token-lineno token)
(token-colno token)))))))
(let loop ((e* '())
(e (read-exp)))
(if (end? e)
(if (== (length e*) 1)
(car e*)
(ast.make-node 'LIST
(cons (ast.make-node 'ATOM 'begin 0 1)
(reverse e*))
0 0))
(loop (cons e e*) (read-exp)))))
(set! module.exports {:read read})