-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA-read.lisp
316 lines (261 loc) · 12 KB
/
A-read.lisp
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
(in-package :lang-a)
;;;; This language implements Common Lisp's READ up to the point of
;;;; calling the conversion function from the string represented as a
;;;; token to the lisp object.
(defun .error (err &rest args)
;; emulate in lisp;
(apply #'error err args))
;;;
;;; Check constituent traits
;;;
(defun %.has-some-constituent-trait-p (rt ch &rest traits)
;; See if the character has at least one of the traits specified
(let ((rt-stable (%readtable-syn-table rt)))
(multiple-value-bind (value presentp)
(gethash ch rt-stable)
(when presentp
(intersection traits (%syntax-type-constituent-traits value))))))
(defun .invalid-character-p (rt ch)
(%.has-some-constituent-trait-p rt ch :invalid))
;;;
;;; Check syntax-types
;;;
(defun %.is-syntax-type (rt ch syntype)
(let ((rt-stable (%readtable-syn-table rt)))
(multiple-value-bind (value presentp)
(gethash ch rt-stable)
(when presentp
(eq syntype (%syntax-type-syntax-type value))))))
(defun .whitespace[2]-p (rt ch)
(%.is-syntax-type rt ch :whitespace[2]))
(defun .terminating-macro-character-p (rt ch)
(%.is-syntax-type rt ch :terminating-macro-char))
(defun .nonterminating-macro-character-p (rt ch)
(%.is-syntax-type rt ch :nonterminating-macro-char))
(defun .single-escape-character-p (rt ch)
(%.is-syntax-type rt ch :single-escape))
(defun .multiple-escape-character-p (rt ch)
(%.is-syntax-type rt ch :multiple-escape))
(defun .constituent-character-p (rt ch)
(%.is-syntax-type rt ch :constituent))
(defun .has-case-p (rt ch)
(let ((rt-stable (%readtable-syn-table rt)))
(multiple-value-bind (value presentp)
(gethash ch rt-stable)
(when presentp
(%syntax-type-case-spec value)))))
(defun .possibly-change-case (rt ch)
(declare (ignore rt))
;; Depending upon the read table settings, we may change the case of this
;; character and return it.
;; TODO: Implement me.
ch)
(defun .reader-macro-character-p (rt ch)
(or (.terminating-macro-character-p rt ch)
(.nonterminating-macro-character-p rt ch)))
(defun .invoke-reader-macro (rt is ch)
(declare (ignore rt is ch))
nil)
;; This is the function which takes the READ in token and converts it into
;; a true object.
(defun .objectify (token)
"Convert the string token into a real lisp object and return the values
of VALID and OBJECT."
(values t (list :object token)))
;; This differs from READ because I MUST pass in a readtable.
(defun .read (rt &optional input-stream eof-error-p eof-value recursive-p)
(declare (ignorable recursive-p))
(let ((x nil)
(token-accum ()))
(tagbody
1 ;; If at end of file, end-of-file processing is performed as
;; specified in read. Otherwise, one character, x, is read
;; from the input stream, and dispatched according to the
;; syntax type of x to one of steps 2 to 7.
(setf x (.read-char input-stream nil 'the-end))
(format t "Step 1: ~S~%" x)
(when (eq x 'the-end)
(if eof-error-p
(.error 'end-of-file)
(return-from .read eof-value)))
2 ;; If x is an invalid character, an error of type
;; reader-error is signaled.
(format t "Step 2: ~S~%" x)
(when (and (.constituent-character-p rt x)
(.invalid-character-p rt x))
(.error 'reader-error))
3 ;; If x is a whitespace[2] character, then it is discarded
;; and step 1 is re-entered.
(format t "Step 3: ~S~%" x)
(when (.whitespace[2]-p rt x)
(go 1))
4 ;; If x is a terminating or non-terminating macro character
;; then its associated reader macro function is called with two
;; arguments, the input stream and x.
;; The reader macro function may read characters from the input
;; stream; if it does, it will see those characters following
;; the macro character. The Lisp reader may be invoked
;; recursively from the reader macro function.
;; The reader macro function must not have any side effects
;; other than on the input stream; because of backtracking and
;; restarting of the read operation, front ends to the Lisp
;; reader (e.g., "editors" and "rubout handlers") may cause
;; the reader macro function to be called repeatedly during the
;; reading of a single expression in which x only appears once.
;; The reader macro function may return zero values or one
;; value. If one value is returned, then that value is returned
;; as the result of the read operation; the algorithm is
;; done. If zero values are returned, then step 1 is
;; re-entered.
(format t "Step 4: ~S~%" x)
(when (.reader-macro-character-p rt x)
(let ((ret (multiple-value-list
(.invoke-reader-macro rt input-stream x))))
(if (null ret)
(go 1)
(return-from .read (car ret)))))
5 ;; If x is a single escape character then the next character,
;; y, is read, or an error of type end-of-file is signaled if
;; at the end of file. y is treated as if it is a constituent
;; whose only constituent trait is alphabetic[2]. y is used to
;; begin a token, and step 8 is entered.
(format t "Step 5: ~S~%" x)
(when (.single-escape-character-p rt x)
(let ((y (.read-char input-stream nil 'the-end)))
(when (eq y 'the-end)
(.error 'end-of-file))
(setf token-accum nil)
(push y token-accum)
(go 8)))
6 ;; If x is a multiple escape character then a token (initially
;; containing no characters) is begun and step 9 is entered.
(format t "Step 6: ~S~%" x)
(when (.multiple-escape-character-p rt x)
(setf token-accum nil)
(go 9))
7 ;; If x is a constituent character, then it begins a
;; token. After the token is read in, it will be interpreted
;; either as a Lisp object or as being of invalid syntax. If
;; the token represents an object, that object is returned as
;; the result of the read operation. If the token is of
;; invalid syntax, an error is signaled. If x is a character
;; with case, it might be replaced with the corresponding
;; character of the opposite case, depending on the readtable
;; case of the current readtable, as outlined in Section
;; 23.1.2 (Effect of Readtable Case on the Lisp Reader). X is
;; used to begin a token, and step 8 is entered.
(format t "Step 7: ~S~%" x)
(when (.constituent-character-p rt x)
(setf token-accum nil)
(when (.has-case-p rt x)
(setf x (.possibly-change-case rt x))) ;; possible change case
(push x token-accum)
(go 8))
8 ;; At this point a token is being accumulated, and an even
;; number of multiple escape characters have been
;; encountered. If at end of file, step 10 is
;; entered. Otherwise, a character, y, is read, and one of the
;; following actions is performed according to its syntax type:
(let ((y (.read-char input-stream nil 'the-end)))
(format t "Step 8(y): ~S~%" y)
(when (eq y 'the-end)
(go 10))
;; If y is a constituent or non-terminating macro character:
(when (or (.constituent-character-p rt y)
(.nonterminating-macro-character-p rt y))
;; If y is a character with case, it might be replaced
;; with the corresponding character of the opposite case,
;; depending on the readtable case of the current
;; readtable, as outlined in Section 23.1.2 (Effect of
;; Readtable Case on the Lisp Reader).
(when (.has-case-p rt y)
(setf y (.possibly-change-case rt y))) ; possibly change case.
;; Y is appended to the token being built.
(push y token-accum)
(go 8))
;; If y is a single escape character, then the next
;; character, z, is read, or an error of type end-of-file is
;; signaled if at end of file. Z is treated as if it is a
;; constituent whose only constituent trait is
;; alphabetic[2]. Z is appended to the token being built,
;; and step 8 is repeated.
(when (.single-escape-character-p rt y)
(let ((z (.read-char input-stream nil 'the-end)))
(when (eq z 'the-end)
(.error 'end-of-file))
(push z token-accum)
(go 8)))
;; If y is a multiple escape character, then step 9 is entered.
(when (.multiple-escape-character-p rt y)
(go 9))
;; If y is an invalid character, an error of type
;; reader-error is signaled.
(when (and (.constituent-character-p rt y)
(.invalid-character-p rt y))
(.error 'reader-error))
;; If y is a terminating macro character, then it terminates
;; the token. First the character y is unread (see
;; unread-char), and then step 10 is entered.
(when (.terminating-macro-character-p rt y)
(.unread-char y input-stream)
(go 10))
;; If y is a whitespace[2] character, then it terminates the
;; token. First the character y is unread if appropriate
;; (see read-preserving-whitespace), and then step 10 is
;; entered.
(when (.whitespace[2]-p rt y)
;; TODO: not handling read-preserving-whitespace
(.unread-char y input-stream)
(go 10)))
9 ;; At this point a token is being accumulated, and an odd
;; number of multiple escape characters have been
;; encountered. If at end of file, an error of type
;; end-of-file is signaled. Otherwise, a character, y, is
;; read, and one of the following actions is performed
;; according to its syntax type:
(let ((y (.read-char input-stream nil 'the-end)))
(format t "Step 9(y): ~S~%" y)
(when (eq y 'the-end)
(.error 'end-of-file))
;; If y is a constituent, macro, or whitespace[2] character,
;; y is treated as a constituent whose only constituent
;; trait is alphabetic[2]. Y is appended to the token being
;; built, and step 9 is repeated.
(when (or (.constituent-character-p rt y)
(.reader-macro-character-p rt y)
(.whitespace[2]-p rt y))
(push y token-accum)
(go 9))
;; If y is a single escape character, then the next
;; character, z, is read, or an error of type end-of-file is
;; signaled if at end of file. Z is treated as a constituent
;; whose only constituent trait is alphabetic[2]. Z is
;; appended to the token being built, and step 9 is
;; repeated.
(when (.single-escape-character-p rt y)
(let ((z (.read-char input-stream nil 'the-end)))
(when (eq z 'the-end)
(.error 'end-of-file))
(push z token-accum)
(go 9)))
;; If y is a multiple escape character, then step 8 is entered.
(when (.multiple-escape-character-p rt y)
(go 8))
;; If y is an invalid character, an error of type
;; reader-error is signaled.
(when (.invalid-character-p rt y)
(.error 'reader-error)))
10 ;; An entire token has been accumulated. The object
;; represented by the token is returned as the result of the
;; read operation, or an error of type reader-error is
;; signaled if the token is not of valid syntax.
(format t "Step 10: ~S~%" token-accum)
(let ((token (concatenate 'string (nreverse token-accum))))
(multiple-value-bind (valid object) (.objectify token)
(if valid
(return-from .read object)
(.error 'reader-error))))
;; Should never reach here.
)))
(defun six-dead-mice ()
nil)