-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse.c
220 lines (175 loc) · 5.03 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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#include "parse.h"
struct chr_lst {
char *chrs;
int capacity;
int len;
};
struct chr_lst new_chr_lst() {
struct chr_lst a_list = {
malloc(4 * sizeof(char)),
4,
0
};
return a_list;
}
void append_chr_lst(struct chr_lst *lst, char chr) {
if (lst->capacity >= lst->len) {
lst->capacity *= 2;
lst->chrs = realloc(lst->chrs, lst->capacity * sizeof(char));
}
lst->chrs[lst->len++] = chr;
}
/* Lexing = String -> Tokens */
struct token lex(struct p_state *state);
struct p_state new_p_state(FILE *file) {
struct p_state state;
state.file = file;
// Lex the first token into the buffer
state.tok = lex(&state);
return state;
}
struct token lex(struct p_state *state) {
struct token tok;
for (;;) {
int chr = getc(state->file);
switch (chr) {
case EOF:
tok.type = EOF_TOK;
return tok;
case '(':
tok.type = LPAREN_TOK;
return tok;
case ')':
tok.type = RPAREN_TOK;
return tok;
}
// Parse a integer
if ('0' <= chr && chr <= '9') {
// Create the token
tok.type = INT_TOK;
tok.integer = chr - '0';
// Read in the integer
chr = getc(state->file);
while ('0' <= chr && chr <= '9') {
tok.integer *= 10;
tok.integer += chr - '0';
chr = getc(state->file);
}
ungetc(chr, state->file);
return tok;
}
// Parse a symbol
if (chr != ' ' && chr != '\n') {
// Read in the characters into the list
struct chr_lst symb = new_chr_lst();
while (chr != ' ' && chr != '\n' && chr != '(' && chr != ')') {
append_chr_lst(&symb, chr);
chr = getc(state->file);
}
// Put the character back
ungetc(chr, state->file);
append_chr_lst(&symb, '\0');
// Create the token
tok.type = SYMBOL_TOK;
tok.symbol = symb.chrs;
return tok;
}
// If we read anything else, skip it
}
}
struct token peek_tok(struct p_state *state) {
return state->tok;
}
struct token eat_tok(struct p_state *state) {
struct token tok = state->tok;
state->tok = lex(state);
return tok;
}
/* Parsing = Tokens -> AST (Abstract Syntax Tree) */
struct sexp parse_sexp(struct p_state *state) {
struct sexp new_sexp;
struct token tok = eat_tok(state);
switch (tok.type) {
case LPAREN_TOK:
new_sexp.type = LIST;
// Read in sexps to create a list
int len = 0;
int capacity = 4;
struct sexp *list = malloc(sizeof(struct sexp) * capacity);
while (peek_tok(state).type != RPAREN_TOK) { // Stop when we reach an RPAREN
if (len == capacity) {
capacity *= 2;
list = realloc(list, sizeof(struct sexp) * capacity);
}
list[len++] = parse_sexp(state);
}
eat_tok(state);
if (len == capacity) {
capacity *= 2;
list = realloc(list, sizeof(struct sexp) * capacity);
}
list[len].type = END_OF_LIST;
new_sexp.list = list;
return new_sexp;
case INT_TOK:
new_sexp.type = INTEGER;
new_sexp.integer = tok.integer;
return new_sexp;
case SYMBOL_TOK:
new_sexp.type = SYMBOL;
new_sexp.symbol = tok.symbol;
return new_sexp;
default:
printf("Invalid start of sexp token: %d\n", tok.type);
assert(0 && "Invalid start of sexp token!");
}
}
/*
A program is a list of s-expressions. s-expressions are (lists of elements) or symbols or numbers
parse(tokens) parses an entire program by parsing sexp until it reaches a NULL
*/
struct sexp *parse(struct p_state *state) {
int len = 0;
int capacity = 4;
struct sexp *list = malloc(sizeof(struct sexp) * capacity);
while (peek_tok(state).type != EOF_TOK) {
if (len == capacity) {
capacity *= 2;
list = realloc(list, sizeof(struct sexp) * capacity);
}
list[len++] = parse_sexp(state);
}
if (len == capacity) {
capacity *= 2;
list = realloc(list, sizeof(struct sexp) * capacity);
}
list[len].type = END_OF_LIST;
return list;
}
void print_ast_node(struct sexp ast_node) {
switch (ast_node.type) {
case INTEGER:
printf("INTEGER(%lld)", ast_node.integer);
break;
case SYMBOL:
printf("SYMBOL(%s)", ast_node.symbol);
break;
case LIST:
printf("LIST(\n");
struct sexp *list = ast_node.list;
while (list->type != END_OF_LIST) {
print_ast_node(*list);
printf(",");
list++;
}
printf("\n)");
break;
case END_OF_LIST:
printf("**EOL**\n");
break;
}
}