-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathreader.scm
191 lines (172 loc) · 5.99 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
;; from https://www.cs.rpi.edu/academics/courses/fall00/ai/scheme/reference/schintro-v14/schintro_115.html#SEC137
(define peeks '())
(define (assq k m)
(if (null? m)
#f
(if (eq? k (car (car m)))
(car m)
(assq k (cdr m)))))
(define (del-assq k m)
(if (null? m)
m
(if (eq? k (car (car m)))
(cdr m)
(cons (car m) (del-assq k (cdr m))))))
(define (read1-char port)
(let ((pc (assq port peeks)))
(if pc
(begin
(set! peeks (del-assq port peeks))
(cdr pc))
(read-char port))))
(define (peek-char port)
(let ((c (read1-char port)))
(set! peeks (cons (cons port c) peeks))
c))
(define (char-numeric? c)
(and (<= (char->fixnum #\0) (char->fixnum c))
(<= (char->fixnum c) (char->fixnum #\9))))
(define (char-alphabetic? c)
(or
(and (<= (char->fixnum #\A) (char->fixnum c))
(<= (char->fixnum c) (char->fixnum #\z)))
(eq? #\+ c)
(eq? #\- c)
(eq? #\* c)
(eq? #\? c)
(eq? #\! c)
(eq? #\> c)
(eq? #\< c)
(eq? #\= c)
(eq? #\. c)))
(define (list->string xs)
(apply string xs))
(define (list->vector xs)
(apply vector xs))
(define (list->number xs (neg #f))
(define (iter xs r)
(if (null? xs)
r
(iter (cdr xs)
(+ ((if neg - +)
0
(- (char->fixnum (car xs)) (char->fixnum #\0)))
(* 10 r)))))
(iter xs 0))
(define (read-token port)
(let ((first-char (read1-char port)))
(cond ((eof-object? first-char)
first-char)
((char-whitespace? first-char)
(read-token port))
((or (eq? first-char #\( ) (eq? first-char #\[ ))
left-paren-token)
((or (eq? first-char #\)) (eq? first-char #\] ))
right-paren-token)
((and (eq? first-char #\.) (char-whitespace? (peek-char port)))
dot-token)
((eq? first-char #\-)
(let ((next-char (peek-char port)))
(if (char-numeric? next-char)
(read-number #t (read1-char port) port)
(read-identifier first-char port))))
((char-alphabetic? first-char)
(read-identifier first-char port))
((char-numeric? first-char)
(read-number #f first-char port))
((eq? #\" first-char)
(read-string first-char port))
((eq? #\' first-char)
(list 'quote (read port)))
((eq? first-char #\#)
(read-character first-char port))
((eq? first-char #\;)
(read-comment first-char port)
(read-token port))
(else
(error 'read-token (format "illegal lexical syntax: ~s" first-char))))))
(define (char-whitespace? char)
(or (eq? char #\space)
(eq? char #\newline)
(eq? char #\tab)
(eq? char #\return)))
(define left-paren-token (list '*left-paren-token*))
(define right-paren-token (list '*right-paren-token*))
(define dot-token (list '*dot-token*))
(define (token-leftpar? thing)
(eq? thing left-paren-token))
(define (token-rightpar? thing)
(eq? thing right-paren-token))
(define (token-dot? thing)
(eq? thing dot-token))
(define (read-string chr port)
(define (helper list-so-far)
(let ((next-char (read1-char port)))
(if (eq? #\\ next-char)
(helper (cons (read1-char port) list-so-far))
(if (eq? #\" next-char)
(list->string (reverse list-so-far))
(helper (cons next-char list-so-far))))))
(helper '()))
(define (read-character chr port)
(let ((next-char (read1-char port)))
(if (eq? next-char #\\)
(let ((first-char (read1-char port)))
(let ((s (read-identifier first-char port)))
(cond ((eq? s 'space) #\space)
((eq? s 'newline) #\newline)
((eq? s 'tab) #\tab)
((eq? s 'return) #\return)
(else first-char))))
(if (eq? next-char #\t)
#t
(if (eq? next-char #\f)
#f
(if (eq? next-char #\()
(list->vector (read-list '() port))
(error 'read-character "expected bool, char or vector")))))))
(define (read-identifier chr port)
(define (read-identifier-helper list-so-far)
(let ((next-char (peek-char port)))
(if (or (char-alphabetic? next-char)
(char-numeric? next-char))
(read-identifier-helper (cons (read1-char port) list-so-far))
(reverse list-so-far))))
(string->symbol (list->string (read-identifier-helper (list chr)))))
(define (read-number neg chr port)
(define (read-number-helper list-so-far)
(let ((next-char (peek-char port)))
(if (char-numeric? next-char)
(read-number-helper (cons (read1-char port) list-so-far))
(reverse list-so-far))))
(list->number (read-number-helper (list chr)) neg))
(define (read-comment chr port)
(define (slurp-line port)
(let ((next-char (read1-char port)))
(if (eq? next-char #\newline)
#t
(slurp-line port))))
(let ((next-char (read1-char port)))
(if (eq? next-char chr)
(slurp-line port)
(error 'read-comment "expected a comment"))))
(define (read port)
(let ((next-token (read-token port)))
(cond ((token-leftpar? next-token)
(read-list '() port))
(else
next-token))))
(define (read-list list-so-far port)
(let ((token (read-token port)))
(cond ((token-rightpar? token)
(reverse list-so-far))
((token-leftpar? token)
(read-list (cons (read-list '() port) list-so-far) port))
((token-dot? token)
(let* ((rest (read-token port))
(r (read-token port)))
(if (token-rightpar? r)
(append (reverse list-so-far) rest)
(error (format "unexpected dotted list ~s ~s" rest r)))))
(else
(read-list (cons token list-so-far) port)))))