-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.c
272 lines (210 loc) · 5.98 KB
/
parse.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
#include "parse.h"
Obj parse(char* expr) {
Token_list* tokens = tokenize(expr);
Obj tokens_read = read_tokens(tokens);
// free_tokens(&tokens);
return tokens_read;
}
// clang-format flushes goto labels to the left
// clang-format off
// TODO: special case for nonlist expr (???)
Token_list* tokenize(char* expr) {
if (DEBUG) printf("%s\n", "tokenizing...");
State state = READY;
int length = strlen(expr);
if (length == 0) return NULL;
int i = 0;
char c;
Token_list* tokens = malloc(sizeof(Token_list)); // initialize to NULL?
Token_list* tail = tokens;
tail->next = NULL;
// temporary variables
int start;
int end;
int sub_length;
char* text;
// debugging
// Token_list* temp = tokens;
// int parens = 0;
START:
if (i >= length) {
if (state == SYMBOL) goto END_TEXT;
goto DONE;
}
c = expr[i];
// TODO: special case for nonlist expr (???)
if (OPENPAREN(c)) goto OPEN;
if (CLOSEPAREN(c)) goto CLOSE;
if (WHITESPACE(c)) goto SEPARATOR;
// default case is any other char so no other if clause is
// needed? if (isalnum(c))?
goto TEXT;
OPEN: //printf("%d @ %s\n", state, "OPEN");
if (state == SYMBOL) goto END_TEXT;
tail->token.start = i;
tail->token.end = i + 1;
tail->token.id = OP;
tail->token.text = "OPEN";
tail->next = malloc(sizeof(Token_list));
tail = tail->next;
tail->next = NULL;
i++;
goto START;
CLOSE: //printf("%d @ %s\n", state, "CLOSE");
if (state == SYMBOL) goto END_TEXT;
tail->token.start = i;
tail->token.end = i + 1;
tail->token.id = CP;
tail->token.text = "CLOSE";
// what should this value be???
if (i < length - 2) {
tail->next = malloc(sizeof(Token_list));
tail = tail->next;
tail->next = NULL;
}
i++;
goto START;
SEPARATOR: //printf("%d @ %s\n", state, "SEPARATOR");
if (state == SYMBOL) goto END_TEXT;
i++;
goto START;
TEXT://printf("%d @ %s\n", state, "TEXT");
if (state == READY) {
state = SYMBOL;
tail->token.id = SYM;
tail->token.start = i;
}
i++;
goto START;
END_TEXT: //printf("%d @ %s\n", state, "END_TEXT");
tail->token.end = i;
start = tail->token.start;
end = tail->token.end;
sub_length = end - start;
text = malloc(sub_length * sizeof(char));
strncpy(text, expr + start, sub_length);
text[sub_length] = '\0';
tail->token.text = text;
if (i < length - 1) {
tail->next = malloc(sizeof(Token_list));
tail = tail->next;
tail->next = NULL;
}
state = READY;
goto START;
DONE:
if (DEBUG) print_tokens(tokens);
return tokens;
}
// clang-format on
Obj read_tokens(Token_list* tokens) {
if (DEBUG) {
printf("%s\n", "parsing...");
print_tokens(tokens);
}
if (tokens == NULL) {
printf("%s\n", "no tokens -- read_from_tokens");
exit(1);
}
Obj obj;
Token token = tokens->token;
// code is a name or number
if (token.id == SYM) {
// if (DEBUG) printf("token: %s\n", token.text);
char* text = token.text;
obj = isdigit(text[0]) ? NUMOBJ(atol(text)) : NAMEOBJ(text);
return obj;
}
// code is a list
List* result = NULL;
Token_list* remainder = slice_ends(&tokens);
Token_list* head = NULL;
Token_list* tail = NULL;
int op = 0;
int cp = 0;
while (remainder) {
Token first = remainder->token;
// first item is an atom
if (first.id == SYM) {
// wrap first (Token) in a Token_list to pass to read_tokens
Token_list dummy;
dummy.next = NULL;
dummy.token = first;
push(read_tokens(&dummy), &result);
// do we need this?
// if (remainder->next == NULL)
// return result;
// else
remainder = remainder->next;
}
// first item is a list
else {
op = 1;
cp = 0;
head = remainder;
tail = remainder;
while (op > cp) { // this will terminate if code is wellformed
tail = tail->next;
if (tail->token.id == OP) op++;
if (tail->token.id == CP) cp++;
}
remainder = tail->next;
tail->next = NULL;
push(read_tokens(head), &result);
}
}
// record memory usage
append_to_lists(result);
// if (!lists_head)
// lists_head = lists_tail;
obj = LISTOBJ(result);
return obj;
}
/* list manipulation */
// token list
void dock(Token_list** list) {
if (*list == NULL) return;
if ((*list)->next == NULL) {
free(*list);
*list = NULL;
return;
}
dock(&((*list)->next));
}
Token_list* slice_ends(Token_list** list) {
Token_list* sliced = (*list)->next;
free(*list);
*list = NULL;
dock(&sliced);
return sliced;
}
// obj list
void push(Obj obj, List** list) {
if (*list == NULL) {
*list = malloc(sizeof(List));
(*list)->car = obj;
(*list)->cdr = NULL;
return;
}
push(obj, &((*list)->cdr));
}
/* for debugging */
void print_tokens(Token_list* tokens) {
printf("printing tokens...\n");
while (tokens) {
printf("text: %s\n", tokens->token.text);
printf("next: %p\n", tokens->next);
tokens = tokens->next;
}
printf("tokens printed!\n");
}
/* memory management */
void free_tokens(Token_list** list) {
if (DEBUG) printf("freeing tokens...\n");
if (*list == NULL) return;
Token_list* temp = *list;
*list = (*list)->next;
free(temp);
temp = NULL;
free_tokens(list);
}