-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.h
298 lines (237 loc) · 7.24 KB
/
parser.h
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
296
297
298
#pragma(once)
typedef unsigned int uint32;
struct String {
uint32 count = 0;
char *data = 0;
};
struct Token {
int col, row;
int from_end;
int type;
String text;
float f;
int s;
};
struct Tokenizer {
int col, row;
char at[2];
String input;
};
void refill(Tokenizer *tokenizer) {
if (tokenizer->input.count == 0) {
tokenizer->at[0] = 0;
tokenizer->at[1] = 0;
} else if (tokenizer->input.count == 1) {
tokenizer->at[0] = tokenizer->input.data[0];
tokenizer->at[1] = 0;
} else {
tokenizer->at[0] = tokenizer->input.data[0];
tokenizer->at[1] = tokenizer->input.data[1];
}
}
void advance_chars(Tokenizer *tokenizer, uint32 count) {
tokenizer->col += count;
if (tokenizer->input.count >= count) {
tokenizer->input.data += count;
tokenizer->input.count -= count;
} else {
tokenizer->input.data += tokenizer->input.count;
tokenizer->input.count = 0;
}
refill(tokenizer);
}
Tokenizer get_tokenizer(String file) {
Tokenizer result;
result.col = 1;
result.row = 1;
result.input = file;
refill(&result);
return result;
}
bool is_end_of_line(char c) {
if (c == '\n' || c == '\r') return true;
return false;
}
bool is_space(char c) {
if (c == ' ' || c == '\t' || c == '\v' || c == '\f') return true;
return false;
}
bool is_numeric(char c) {
if (c >= '0' && c <= '9') return true;
return false;
}
bool is_alpha(char c) {
if (c >= 'a' && c <='z') return true;
if (c >= 'A' && c <='Z') return true;
if (c >= '_') return true;
return false;
}
bool double_return_sequence(char c0, char c1) {
if (c0 == '\r' && c1 == '\n') return true;
if (c0 == '\n' && c1 == '\r') return true;
return false;
}
enum Token_Types {
Token_Unknown,
Token_Identifier,
Token_Number,
Token_Comment,
Token_Open_Parenthesis,
Token_Close_Parenthesis,
Token_Semicolon,
Token_Plus,
Token_Minus,
Token_Asterisk,
Token_Percent,
Token_Slash,
Token_Comma,
Token_Pound,
Token_Equals,
Token_Spaces,
Token_End_Of_Line,
Token_End_Of_File,
Token_Types_Count,
};
Token get_raw_token(Tokenizer *tokenizer) {
Token token;
token.col = tokenizer->col;
token.row = tokenizer->row;
token.from_end = tokenizer->input.count;
token.text = tokenizer->input;
char c = tokenizer->at[0];
advance_chars(tokenizer, 1);
switch (c) {
case '\0': token.type = Token_End_Of_File; break;
case '(': token.type = Token_Open_Parenthesis; break;
case ')': token.type = Token_Close_Parenthesis; break;
case ';': token.type = Token_Semicolon; break;
case ',': token.type = Token_Comma; break;
case '*': token.type = Token_Asterisk; break;
case '+': token.type = Token_Plus; break;
case '-': token.type = Token_Minus; break;
case '%': token.type = Token_Percent; break;
case '=': token.type = Token_Equals; break;
case '#': token.type = Token_Pound; break;
case '/': {
if (tokenizer->at[0] == '/') {
token.type = Token_Comment;
advance_chars(tokenizer, 1);
while (tokenizer->at[0] && !is_end_of_line(tokenizer->at[0])) {
advance_chars(tokenizer, 1);
}
} else {
token.type = Token_Slash;
}
} break;
default: {
if (is_alpha(c)) {
token.type = Token_Identifier;
while (tokenizer->at[0] && (is_alpha(tokenizer->at[0]) || is_numeric(tokenizer->at[0]))) {
advance_chars(tokenizer, 1);
}
} else if (is_numeric(c)) {
token.type = Token_Number;
float parsed_f = (float) (c - '0');
while (tokenizer->at[0] && is_numeric(tokenizer->at[0])) {
float digit = (float) (tokenizer->at[0] - '0');
parsed_f = parsed_f * 10.0f + digit;
advance_chars(tokenizer, 1);
}
if (tokenizer->at[0] == '.') {
advance_chars(tokenizer, 1);
float coefficient = 0.1f;
while (tokenizer->at[0] && is_numeric(tokenizer->at[0])) {
float digit = (float) (tokenizer->at[0] - '0');
parsed_f = parsed_f + digit * coefficient;
coefficient *= 0.1f;
advance_chars(tokenizer, 1);
}
}
while (tokenizer->at[0] && is_alpha(tokenizer->at[0])) {
advance_chars(tokenizer, 1);
}
token.f = parsed_f;
token.s = (int) parsed_f;
} else if (is_end_of_line(c)) {
token.type = Token_End_Of_Line;
if (double_return_sequence(c, tokenizer->at[0])) {
advance_chars(tokenizer, 1);
}
tokenizer->row++;
tokenizer->col = 1;
} else if (is_space(c)) {
token.type = Token_Spaces;
while (tokenizer->at[0] && is_space(tokenizer->at[0])) {
advance_chars(tokenizer, 1);
}
} else {
token.type = Token_Unknown;
}
}
}
token.text.count = tokenizer->input.data - token.text.data;
return token;
}
Token get_token(Tokenizer *tokenizer) {
Token token;
while (true) {
token = get_raw_token(tokenizer);
switch (token.type) {
case Token_Spaces:
case Token_Comment: continue;
}
break;
}
return token;
}
Token peek_token(Tokenizer *tokenizer) {
Tokenizer tokenizer_copy = *tokenizer;
Token token = get_token(&tokenizer_copy);
return token;
}
Token peek_raw_token(Tokenizer *tokenizer) {
Tokenizer tokenizer_copy = *tokenizer;
Token token = get_raw_token(&tokenizer_copy);
return token;
}
int string_length(char *s) {
int len = 0;
while (s[len] != '\0') len++;
return len;
}
void eat_spaces_new_lines_and_comments(Tokenizer *tokenizer) {
while (true) {
Token token = peek_raw_token(tokenizer);
switch (token.type) {
case Token_Spaces:
case Token_Comment:
case Token_End_Of_Line: {
get_raw_token(tokenizer);
continue;
}
}
break;
}
}
bool peek_token_type(Tokenizer *tokenizer, Token *token, int token_type, bool eat_token_if_match = true) {
*token = peek_token(tokenizer);
if (token->type == token_type) {
if (eat_token_if_match) get_token(tokenizer);
return true;
}
return false;
}
bool maybe_parse_number(Tokenizer *tokenizer, Token *token) {
Tokenizer t = *tokenizer;
*token = get_token(&t);
int sign = 1;
if (token->type == Token_Minus) {
sign = -1;
*token = get_token(&t);
}
if (token->type != Token_Number) return false;
token->f = sign * token->f;
token->s = sign * token->s;
*tokenizer = t;
return true;
}