/
reader.c
175 lines (160 loc) · 4.6 KB
/
reader.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
#include "reader.h"
#include "cell.h"
#include "token.h"
#include "symbol.h"
#include "memory.h"
#include "liveness.h"
#include <string.h>
extern frame_t *live_root;
extern cell_t *nil_cell;
cell_t *read_list_intern(STREAM *stream, symbol_table *symbol_table);
cell_t *read_intern(STREAM *stream, symbol_table *symbol_table) {
token_t tok;
cell_t *cell;
int ret;
size_t slen;
u8_cell_t* string;
ret = get_token(stream, &tok);
if (ret != TOKEN_OK) {
return NULL;
}
switch(tok.type) {
case TOKEN_LPAREN:
return read_list_intern(stream, symbol_table);
case TOKEN_RPAREN:
return NULL;
case TOKEN_SYMBOL:
/* We also need to free or steal the payload part. */
cell = intern(tok.atom_name, symbol_table);
if (NULL == cell) {
return NULL;
}
return cell;
case TOKEN_NUMBER:
cell = new(cell_t); // nothing extra live
if (NULL == cell) {
return NULL;
}
cell->slot1.type = NUMBER;
cell->slot2.i_val = tok.i_val;
return cell;
case TOKEN_STRING:
/* As with symbols, we need to free or steal the token payload. */
cell = new(cell_t); // nothing extra live
if (NULL == cell) {
return NULL;
}
slen = strlen((char *)tok.string_val)+1; /* Add '\0' sigh */
string = u8_new(slen);
strncpy((char*)U8DATA(string), (char*)tok.string_val, slen);
cell->slot1.type = STRING;
cell->slot2.string = (cell_t *)string;
free_token_payload(&tok);
return cell;
default:
return NULL;
}
}
cell_t *read_list_intern(STREAM *stream, symbol_table *symbol_table) {
cell_t *last, *current, *first, *temp;
token_t tok;
int ret;
size_t slen;
u8_cell_t* string;
last = current = first = temp = NULL;
do {
ret = get_token(stream, &tok);
if (ret != TOKEN_OK) {
if (first) { // if first, this is the second time in the loop,
// pop the liveframe on error
pop_liveness(&live_root);
}
return NULL;
}
// special case nil
if(tok.type == TOKEN_RPAREN) {
current = nil_cell;
} else {
current = new(cell_t); // liveness tracked through 'first'
current->slot2.cdr = nil_cell; /* pretend this is a proper list so we can
print liveness */
}
/* Will leak all read token payloads' memory in OOM situation. */
if (!current) {
if (first) { // if first, this is the second time in the loop,
// pop the liveframe on error
pop_liveness(&live_root);
}
return NULL;
}
if (last) {
last->slot2.cdr = current;
} else { /* This is the first in the list. */
first = current;
push_liveness(&live_root, new_liveframe(1, first));
}
/* All liveness tracked through first */
switch(tok.type) {
case TOKEN_LPAREN:
current->slot1.car = read_list_intern(stream, symbol_table);
break;
case TOKEN_RPAREN:
// see special case above
pop_liveness(&live_root);
return first;
case TOKEN_SYMBOL:
/* We also need to free or steal the payload part. */
temp = intern(tok.atom_name, symbol_table);
if (!temp) {
if (first) { // if first, this is the second time in the loop,
// pop the liveframe on error
pop_liveness(&live_root);
}
return NULL;
}
current->slot1.car = temp;
break;
case TOKEN_NUMBER:
temp = new(cell_t); // liveness ok
if (!temp) {
if (first) { // if first, this is the second time in the loop,
// pop the liveframe on error
pop_liveness(&live_root);
}
return NULL;
}
current->slot1.car = temp;
temp->slot1.type = NUMBER;
temp->slot2.i_val = tok.i_val;
break;
case TOKEN_STRING:
/* As with symbols, we need to free or steal the token payload. */
temp = new(cell_t); // liveness ok
if (!temp) {
if (first) {
pop_liveness(&live_root);
}
return NULL;
}
slen = strlen((char *)tok.string_val)+1; /* Add '\0' sigh */
string = u8_new(slen);
strncpy((char*)U8DATA(string), (char*)tok.string_val, slen);
current->slot1.car = temp;
temp->slot1.type = STRING;
temp->slot2.string = (cell_t *)string;
free_token_payload(&tok);
break;
default:
if (first) { // if first, this is the second time in the loop,
// pop the liveframe on error
pop_liveness(&live_root);
}
return NULL;
}
last = current;
} while (1);
if (first) {
pop_liveness(&live_root);
}
return first;
}