This repository has been archived by the owner on Sep 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreader.scm
211 lines (194 loc) · 6.16 KB
/
reader.scm
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
(def cur-char #f)
(defn read-char ()
(if (char? cur-char)
(let ((old cur-char))
(set! cur-char #f)
old)
(getchar)))
(defn peek-char ()
(if (char? cur-char)
cur-char
(let ((new (getchar)))
(set! cur-char new)
new)))
(defn skip-chars (n)
(if (eq? n 0)
'done
(begin
(read-char)
(skip-chars (fxsub1 n)))))
(defn skip-line ()
(let ((next-char (peek-char)))
(if (eq? next-char #\newline)
; TODO: Refactor once one-armed ifs are supported
'done
(begin
(read-char)
(skip-line)))))
(defn read-token ()
(let ((first-char (read-char)))
(cond
; ((null? first-char)
; 'eof)
((char-whitespace? first-char)
(read-token))
((eq? first-char #\;)
(skip-line)
(read-token))
((eq? first-char #\()
'left-paren)
((eq? first-char #\))
'right-paren)
((eq? first-char #\')
'quote-token)
((eq? first-char #\`)
'quasiquote-token)
((eq? first-char #\,)
'unquote-token)
((eq? first-char #\")
(read-string ""))
; TODO: support unquote splicing
((eq? first-char #\#)
(let ((next-char (read-char)))
(cond
((eq? next-char #\\) (read-character))
((eq? next-char #\t) #t)
((eq? next-char #\f) #f)
((eq? next-char #\b)
(read-number (read-char) 2))
((eq? next-char #\o)
(read-number (read-char) 8))
; TODO: Handle hex numbers,
; bin & oct work right now bc/
; char - 48 = value,
; for "A".."F" this is not the case
(else (error "Illegal symbol after #: " next-char)))))
((and (eq? first-char #\-)
(char-numeric? (peek-char)))
(fxneg (read-number (read-char) 10)))
((char-numeric? first-char)
(read-number first-char 10))
((or (char-alphabetic? first-char)
(char-special? first-char))
(list 'escaped (read-identifier first-char)))
(else
(error "Illegal lexical syntax: " first-char)))))
(defn escaped? (expr)
(tagged-list? expr 'escaped))
(def string-escape-codes
(list (cons #\n #\newline)
(cons #\t #\tab)
(cons #\" #\")
(cons #\\ #\\)))
(defn read-string (acc)
(let ((first-char (read-char)))
(cond
((eq? first-char #\\)
(read-string
(string-append acc
(~> (read-char)
(assoc string-escape-codes)
rst
char->string))))
((eq? first-char #\")
acc)
(else (~>> first-char
char->string
(string-append acc)
read-string)))))
(defn read-character ()
(let ((first-char (read-char))
(next-char (peek-char)))
(cond
((and (eq? first-char #\n)
(eq? next-char #\e))
(skip-chars 6)
#\newline)
((and (eq? first-char #\t)
(eq? next-char #\a))
(skip-chars 2)
#\tab)
((and (eq? first-char #\s)
(eq? next-char #\p))
(skip-chars 4)
#\space)
((and (eq? first-char #\r)
(eq? next-char #\e))
(skip-chars 5)
#\return)
(else first-char))))
(defn read-identifier (first-char)
(~> first-char
(cons '())
read-identifier_
list->string
string->symbol))
(defn read-identifier_ (acc)
(let ((next-char (peek-char)))
(if (or (char-alphabetic? next-char)
(char-numeric? next-char)
(char-special? next-char))
(read-identifier_ (cons (read-char) acc))
(reverse acc))))
(defn read-number (first-char base)
(~> first-char
char->fixnum
(fx- 48)
(read-number_ base)))
(defn read-number_ (acc base)
(let ((next-char (peek-char)))
(if (char-numeric? next-char)
(read-number_
(fx+ (fx* acc base)
(~> (read-char) char->fixnum (fx- 48)))
base)
acc)))
(defn read-all ()
(read-all_ '()))
(defn read-all_ (acc)
(let ((next-char (peek-char)))
(cond
((null? next-char)
(reverse acc))
; This is needed if a newline is the last char of a file
((char-whitespace? next-char)
(read-char)
(read-all_ acc))
(else
(read-all_ (cons (read_) acc))))))
(defn read_ ()
(let ((next-token (read-token)))
(cond
((eq? next-token 'left-paren)
(read-list '()))
((eq? next-token 'quote-token)
(list 'quote (read_)))
((eq? next-token 'quasiquote-token)
(list 'quasiquote (read_)))
((eq? next-token 'unquote-token)
(list 'unquote (read_)))
((escaped? next-token)
(frst next-token))
(else next-token))))
(defn read-list (acc)
(let ((next-token (read-token)))
(cond
((eq? next-token 'right-paren)
(reverse acc))
((eq? next-token 'left-paren)
(read-list (cons (read-list '())
acc)))
((eq? next-token 'quote-token)
(read-list (cons (list 'quote (read_))
acc)))
((eq? next-token 'quasiquote-token)
(read-list (cons (list 'quasiquote (read_))
acc)))
((eq? next-token 'unquote-token)
(read-list (cons (list 'unquote (read_))
acc)))
((escaped? next-token)
(read-list (cons (frst next-token)
acc)))
(else
(read-list (cons next-token acc))))))