-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread.scm
197 lines (172 loc) · 5.18 KB
/
read.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
; read.scm
;
; Schemec lexical analyzer.
; Build a list representation of the character
; sequence from an input port.
; Read an identifier.
(define (read-id input)
(string->symbol (_read-id input "")))
(define (_read-id input prefix)
(let ((c (peek-char input)))
(if (not (or (eof-object? c)
(char-whitespace? c)
(eqv? c #\()
(eqv? c #\))))
(_read-id
input
(string-append-char prefix (read-char input)))
prefix)))
; Read a character or a boolean (starting with #).
(define (read-character-or-boolean input)
(let ((c (read-char input)))
(cond ((eqv? c #\t) #t)
((eqv? c #\f) #f)
((eqv? c #\\) (read-character input))
(else #\#))))
(define (read-character input)
(let ((c (read-char input)))
(cond ((eqv? c #\n) (let ((c (read-special-character input "ewline" 0)))
(if (eqv? c #f)
#\n
c)))
((eqv? c #\s) (let ((c (read-special-character input "pace" 0)))
(if (eqv? c #f)
#\s
c)))
((eqv? c #\t) (let ((c (read-special-character input "ab" 0)))
(if (eqv? c #f)
#\t
c)))
(else c))))
(define (read-special-character input str i)
(if (>= i (string-length str))
#f
(let ((c (peek-char input)))
(if (eqv? c (string-ref str i))
(begin
(read-char input)
(if (= i (- (string-length str) 1))
(cond ((= (string-length str) 6) #\newline)
((= (string-length str) 4) #\space)
((= (string-length str) 2) #\tab))
(read-special-character input str (+ i 1))))
#f))))
; Read an integer.
(define (read-integer input)
(string->number (_read-integer input "")))
(define (_read-integer input prefix)
(let ((c (peek-char input)))
(if (and (not (eof-object? c))
(char-numeric? c))
(_read-integer
input
(string-append-char
prefix
(read-char input)))
prefix)))
; Read a string.
(define (read-string input)
(begin (read-char input)
(_read-string input "")))
(define (_read-string input prefix)
(let ((c (peek-char input)))
(cond
((eof-object? c)
prefix)
((eqv? (char->integer c) 34)
(begin (read-char input) prefix))
((eqv? c #\\)
(begin (read-char input)
(let ((cn (read-char input)))
(cond
((eof-object? cn)
(_read-string
input
(string-append-char prefix #\\)))
(else
(_read-string
input
(string-append
prefix
(list->string (list #\\ cn)))))))))
(else
(_read-string
input
(string-append-char
prefix
(read-char input)))))))
; Set the input-port to the next non-whitespace character.
(define (skip-ws input)
(let ((c (peek-char input)))
(if (not (eof-object? c))
(if (char-whitespace? c)
(begin (read-char input)
(skip-ws input))))))
(define (skip-to-nextline input)
(let ((c (read-char input)))
(if (not (eof-object? c))
(if (not (eqv? #\newline c))
(skip-to-nextline input)))))
; Append a single character to a string.
(define (string-append-char s c)
(string-append s (list->string (list c))))
(define (read input)
(_read input (list)))
(define (_read input prefix)
(begin
(skip-ws input)
(let ((c (peek-char input)))
(cond
((eof-object? c)
prefix)
; Start reading a sub-expression.
((eqv? c #\()
(begin (read-char input)
(_read
input
(append
prefix
(list (_read input (list)))))))
; End of the current expression.
((eqv? c #\))
(begin (read-char input)
prefix))
; Read an inputteger on a numeric character.
((char-numeric? c)
(_read
input
(append
prefix
(list (read-integer input)))))
; Read a string on a " character.
((eqv? (char->integer c) 34)
(_read
input
(append
prefix
(list (read-string input)))))
; Read a character or a boolean on a # character.
((eqv? c #\#)
(begin (read-char input)
(_read
input
(append
prefix
(list (read-character-or-boolean input))))))
((eqv? c #\.)
(begin (read-char input)
(_read
input
(append
prefix
(list c)))))
((eqv? c #\;)
(begin
(skip-to-nextline input)
(_read input prefix)))
; Read an identifier on any other character.
(else (_read
input
(append
prefix
(list (read-id input)))))))))