-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.c
295 lines (263 loc) · 7.97 KB
/
io.c
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
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "interpreter.h"
enum TOK_T {LIST_START, LIST_END, STRING, SYMBOL, NUMBER, QUOTE, BACKQUOTE, UNQUOTE, SPLICE, DOT, FILE_END};
static int pair_count;
pointer _read(port *in);
pointer read(port *in)
{
pair_count = 0;
return _read(in);
}
static enum TOK_T get_next_token(port *in, pointer *token);
static pointer read_list(port *in);
static pointer cons_with_symbol(char *string, pointer object);
pointer _read(port *in)
{
pointer token;
switch (get_next_token(in, &token)) {
case FILE_END:
return E_O_F;
case STRING:
case SYMBOL:
case NUMBER:
return token;
case LIST_START:
return read_list(in);
case LIST_END:
pair_count--;
return NULL;
case QUOTE:
return cons_with_symbol("quote", cons(_read(in), NULL));
case BACKQUOTE:
return cons_with_symbol("backquote", cons(_read(in), NULL));
case UNQUOTE:
return cons_with_symbol("unquote", cons(_read(in), NULL));
case SPLICE:
return cons_with_symbol("splice", cons(_read(in), NULL));
case DOT:
return cons_with_symbol("dot", cons(_read(in), NULL));
default:
printf("Unknown token %s\n", str(token));
exit(1);
break;
}
}
/* this is really ugly! */
static pointer read_list(port *in)
{
int saved_pair_count = pair_count++;
pointer pre, cur, subsexpr, sexpr = NULL;
if ((subsexpr = _read(in)) == NULL && pair_count == saved_pair_count)
return NULL;
pre = sexpr = cons(subsexpr, NULL);
for(;;) {
subsexpr = _read(in);
if (saved_pair_count == pair_count)
break;
cur = cons(subsexpr, NULL);
cdr(pre) = cur;
pre = cur;
}
return sexpr;
}
static pointer cons_with_symbol(char *string, pointer object)
{
return cons(mk_symbol(string), object);
}
static int get_next_char(port *in);
static void push_back_char(port *in, int ch);
static void skip_white_spc(port *in);
static char *read_string(port *in);
static char *get_char_until_delim(port *in);
static int test_number(const char *string);
static enum TOK_T get_next_token(port *in, pointer *token)
{
char ch, *t;
skip_white_spc(in);
switch (ch = get_next_char(in)) {
case EOF:
return FILE_END;
case '(':
return LIST_START;
case ')':
return LIST_END;
case '"':
*token = mk_string(read_string(in));
return STRING;
case '\'':
return QUOTE;
case '`':
return BACKQUOTE;
case ',':
if ((ch = get_next_char(in)) == '@')
return SPLICE;
else {
push_back_char(in, ch);
return UNQUOTE;
}
case '.':
return DOT;
default:
push_back_char(in, ch);
t = get_char_until_delim(in);
if (test_number(t)) {
*token = mk_number(atoi(t));
return NUMBER;
} else {
*token = mk_symbol(t);
return SYMBOL;
}
}
}
static int get_next_char(port *in)
{
return fgetc(in);
}
static void push_back_char(port *in, int ch)
{
ungetc(ch, in);
}
static int is_white_space(char ch)
{
return ch == '\n' || ch == '\t' || ch == ' ';
}
static void skip_white_spc(port *in)
{
int ch;
while ((ch = get_next_char(in)) != EOF && is_white_space(ch))
;
if (ch != EOF)
push_back_char(in, ch);
}
static char *read_string(port *in)
{
int ch, i = 0;
char str[MAX_STR_LEN], *dst;
if ((ch = get_next_char(in)) != '"')
do {
str[i++] = ch;
} while ((ch = get_next_char(in)) != '"' && str[i - 1] != '\\');
str[i] = '\0';
memcpy((dst = alloc_strseg(i + 1)), str, i + 1);
return dst;
}
static char *get_char_until_delim(port *in)
{
int i = 0, ch;
char str[MAX_SYM_NUM_LEN], *dst;
str[0] = '\0';
while ((ch = get_next_char(in)) != EOF && !is_white_space(ch) && ch != '(' && ch != ')')
str[i++] = ch;
str[i] = '\0';
memcpy((dst = alloc_strseg(i + 1)), str, i + 1);
if (ch != EOF)
push_back_char(in, ch);
return dst;
}
static int test_number(const char *string)
{
int i = 0, len = strlen(string);
if (string[0] == '+' || string[0] == '-') {
if (len == 1)
return 0;
else
i++;
}
while (string[i] != '\0')
if (!isdigit(string[i++]))
return 0;
return 1;
}
static void write_string(port *out, pointer object);
static void write_number(port *out, pointer object);
static void write_symbol(port *out, pointer object);
static void write_pair(port *out, pointer object);
static void write_proc(port *out, pointer object);
static void write_macro(port *out, pointer object);
static void wirte_unknown(port *out, pointer object);
void write(port *out, pointer object)
{
if (object == NULL) {
printf("()");
return;
}
switch (type(object)) {
case T_STRING:
write_string(out, object);
break;
case T_NUMBER:
write_number(out, object);
break;
case T_SYMBOL:
write_symbol(out, object);
break;
case T_PAIR:
write_pair(out, object);
break;
case T_BUILT_IN_REGULAR_PROC:
case T_BUILT_IN_SPECIAL_PROC:
case T_EXTEND_PROC:
write_proc(out, object);
break;
case T_MACRO:
write_macro(out, object);
break;
default:
wirte_unknown(out, object);
break;
}
}
static void write_string(port *out, pointer object)
{
fprintf(out, "\"%s\"", str(object));
}
static void write_number(port *out, pointer object)
{
fprintf(out, "%d", num(object));
}
static void write_symbol(port *out, pointer object)
{
fprintf(out, "%s", sym(object));
}
static void write_pair(port *out, pointer object)
{
fprintf(out, "(");
if (cdr(object) == NULL) {
write(out, car(object));
fprintf(out, ")");
return;
}
if (!ispair(cdr(object))) {
write(out, car(object));
fprintf(out, " . ");
write(out, cdr(object));
fprintf(out, " ");
} else
while (object != NULL) {
if (!ispair(object)) {
fprintf(out, ". ");
write(out, object);
fprintf(out, " ");
break;
}
write(out, car(object));
fprintf(out, " ");
object = cdr(object);
}
fprintf(out, "\b)");
}
static void write_proc(port *out, pointer object)
{
fprintf(out, "procedure %p", object);
}
static void write_macro(port *out, pointer object)
{
fprintf(out, "macro %p", object);
}
static void wirte_unknown(port *out, pointer object)
{
fprintf(out, "unknown object %p", object);
}