-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread.cpp
More file actions
224 lines (190 loc) · 3.69 KB
/
read.cpp
File metadata and controls
224 lines (190 loc) · 3.69 KB
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
//
// read() function and supporting functions
//
#include "consVM.h"
static const char* PROMPT = ">> ";
static const char* CONTINUATION_PROMPT = ">>>> ";
enum Token {
LPAREN,
RPAREN,
PERIOD,
QUOTE,
SYMBOL,
START_OF_FILE,
END_OF_FILE,
UNDEF_TOKEN
};
// The current token
static const int TOKEN_SIZE = 256;
static char token_text[TOKEN_SIZE] = { '\0' };
static Token token = START_OF_FILE;
// The current input text
static const int BUFFER_SIZE = 1024;
static char buffer[BUFFER_SIZE] = { '\0' };
static int lineCount = 0;
// This points to the character immediately following the current token
static char* bufp;
// Local functions
static void init_reader();
static bool parse_sexpr();
static bool read_list();
static bool read_quote();
static Token next_token();
static Token scan_symbol();
static char* find_token_start();
static bool fill_buffer();
static void init_reader()
{
buffer[0] = '\0';
bufp = &buffer[0];
lineCount = 0;
}
// Here, 'token' is at the token before the sexpr.
// Leave 'token' at the last token of the sexpr.
bool read(bool top_level)
{
if (top_level) {
init_reader();
}
next_token();
if (token == END_OF_FILE) {
return false;
} else {
return parse_sexpr();
}
}
// Here, 'token' is at the first token of the sexpr.
// Leave 'token' at the last token of the sexpr.
static bool parse_sexpr()
{
switch (token)
{
case LPAREN:
return read_list();
case RPAREN:
throw LispError("Extraneous right paren");
case PERIOD:
throw LispError("Extraneous period");
case QUOTE:
return read_quote();
case SYMBOL:
push(atom(token_text));
return true;
case END_OF_FILE:
throw LispError("read: Premature end of file");
default:
throw LispError("read: unhandled token type");
}
// Unreachable!
return false;
}
static bool read_list()
{
next_token();
if (token == RPAREN) {
push(nil);
return true;
}
int nElem = 0;
parse_sexpr();
nElem++;
for (;;) {
next_token();
if (token == RPAREN) {
push(nil);
break;
}
if (token == PERIOD) {
next_token();
parse_sexpr();
if (next_token() != RPAREN) {
LispError("Missing right paren after dot notation");
}
break;
}
parse_sexpr();
nElem++;
}
while (nElem > 0) {
cons();
nElem--;
}
return true;
}
static bool read_quote()
{
next_token();
push(a_quote);
parse_sexpr();
push(nil);
cons();
cons();
return true;
}
static Token next_token()
{
if (find_token_start() == NULL) {
return (token = END_OF_FILE);
}
char ch = *bufp;
switch (ch)
{
case '(':
bufp++;
return (token = LPAREN);
case ')':
bufp++;
return (token = RPAREN);
case '.':
bufp++;
return (token = PERIOD);
case '\'':
bufp++;
return (token = QUOTE);
}
if (std::isalnum(ch)) {
return scan_symbol();
}
throw LispError("read: Non-alpha-numeric character");
}
static Token scan_symbol()
{
char* p = token_text;
while(std::isalnum(*bufp)) {
*p++ = *bufp++;
}
*p++ = '\0';
return (token = SYMBOL);
}
static char* find_token_start()
{
while (1) {
while (*bufp && *bufp != ';') { // ';' starts a comment
if (!std::isspace(*bufp)) {
return bufp;
}
bufp++;
}
if (!fill_buffer()) {
return NULL;
}
}
}
static bool fill_buffer()
{
buffer[0] = '\0';
bufp = buffer;
while (!std::cin.eof()) {
std::cout << (lineCount == 0 ? PROMPT : CONTINUATION_PROMPT);
std::cin.getline(buffer, BUFFER_SIZE);
if (std::cin.fail()) {
break;
}
++lineCount;
// Empty line?
if (buffer[0] != '\n') {
return true;
}
}
return false;
}