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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
#include "lexer.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <utils/memory/memory.h>
#include "utils/types.h"
const char *TOKEN_STRINGS[] = {
"TOKEN_NONE",
"TOKEN_IDENTIFIER",
"TOKEN_NUMBER",
"TOKEN_STRING",
"TOKEN_OPERATOR",
"TOKEN_OPERATOR_PARENTHESES_OPEN",
"TOKEN_OPERATOR_PARENTHESES_CLOSE",
"TOKEN_OPERATOR_CURLY_BRACKET_OPEN",
"TOKEN_OPERATOR_CURLY_BRACKET_CLOSE",
"TOKEN_OPERATOR_ASSIGN",
"TOKEN_OPERATOR_EQUAL",
"TOKEN_OPERATOR_COLON",
"TOKEN_OPERATOR_COMMA",
"TOKEN_OPERATOR_EOL",
"TOKEN_OPERATOR_FUNCTION",
"TOKEN_SYMBOL",
"TOKEN_KEYWORD_STRUCT",
"TOKEN_KEYWORD_EXTERNAL",
"TOKEN_KEYWORD_IMPORT",
"TOKEN_PARSED",
};
static const char *KEYWORDS_STRINGS[] = {
"struct",
"external",
"import",
};
static const Token KEYWORDS_TOKENS[] = {
TOKEN_KEYWORD_STRUCT,
TOKEN_KEYWORD_EXTERNAL,
TOKEN_KEYWORD_IMPORT,
};
static const size_t KEYWORDS_SIZE = sizeof(KEYWORDS_STRINGS) / sizeof(char *);
static const char *OPERATORS_STRINGS[] = {
"(", ")", "{", "}", "=", "==", ":", ",", ";", "->",
};
static const Token OPERATORS_TOKENS[] = {
TOKEN_OPERATOR_PARENTHESES_OPEN,
TOKEN_OPERATOR_PARENTHESES_CLOSE,
TOKEN_OPERATOR_CURLY_BRACKET_OPEN,
TOKEN_OPERATOR_CURLY_BRACKET_CLOSE,
TOKEN_OPERATOR_ASSIGN,
TOKEN_OPERATOR_EQUAL,
TOKEN_OPERATOR_COLON,
TOKEN_OPERATOR_COMMA,
TOKEN_OPERATOR_EOL,
TOKEN_OPERATOR_FUNCTION,
};
static const size_t OPERATORS_SIZE = sizeof(OPERATORS_STRINGS) / sizeof(char *);
void printNodes(Nodes nodes) {
for (size_t i = 0; i < nodes.size; ++i) {
const Node node = nodes.nodes[i];
printf("{'%.*s',%s}\n", (int)(node.strEnd - node.strBegin), node.strBegin,
TOKEN_STRINGS[node.token]);
}
}
void deleteNodes(Nodes nodes) { free(nodes.nodes); }
Nodes lexer(SourceCode *sourceCode, size_t sourceIndex) {
size_t nodes_size = 10;
Node *nodes = a404m_malloc(nodes_size * sizeof(Node));
size_t nodes_inserted = 0;
const char *const code = sourceCode->codes[sourceIndex]->code;
Node node = {
.strBegin = code,
.strEnd = code,
.token = TOKEN_NONE,
};
for (int i = 0;; ++i) {
const char c = code[i];
if (c == '\0') {
push_if_not_empty(&nodes, &nodes_size, &nodes_inserted, &node, code, i,
TOKEN_NONE);
break;
} else if (c == '/') {
const char follow = code[i + 1];
if (follow == '/') {
push_if_not_empty(&nodes, &nodes_size, &nodes_inserted, &node, code, i,
TOKEN_NONE);
for (i += 2; code[i] != '\0' && code[i] != '\n'; ++i);
push_if_not_empty(&nodes, &nodes_size, &nodes_inserted, &node, code, i,
TOKEN_NONE);
if (code[i] == '\0') {
goto RETURN_SUCCESS;
}
continue;
} else if (follow == '*') {
push_if_not_empty(&nodes, &nodes_size, &nodes_inserted, &node, code, i,
TOKEN_NONE);
int in = 1;
for (i += 2;; ++i) {
switch (code[i]) {
case '\0':
printError("Expected multi line comment to end", sourceCode,
node.strBegin, code + i);
goto RETURN_ERROR;
case '*':
++i;
if (code[i] == '/') {
--in;
if (in == 0) {
goto END_OF_BLOCK_COMMENT_LOOP;
}
} else if (code[i] == '\0') {
printError("Expected multi line comment to end", sourceCode,
node.strBegin, code + i);
goto RETURN_ERROR;
}
break;
case '/':
++i;
if (code[i] == '*') {
++in;
} else if (code[i] == '\0') {
printError("Expected multi line comment to end", sourceCode,
node.strBegin, code + i);
goto RETURN_ERROR;
}
break;
}
}
END_OF_BLOCK_COMMENT_LOOP:
push_if_not_empty(&nodes, &nodes_size, &nodes_inserted, &node, code, i,
TOKEN_NONE);
if (code[i] == '\0') {
goto RETURN_SUCCESS;
}
continue;
}
}
if (isSpace(c)) {
push_if_not_empty(&nodes, &nodes_size, &nodes_inserted, &node, code, i,
TOKEN_NONE);
} else if (isIdentifier(c)) {
if (node.token != TOKEN_IDENTIFIER && node.token != TOKEN_SYMBOL) {
push_if_not_empty(&nodes, &nodes_size, &nodes_inserted, &node, code, i,
TOKEN_IDENTIFIER);
}
} else if (isIdentifierSymbol(c)) {
push_if_not_empty(&nodes, &nodes_size, &nodes_inserted, &node, code, i,
TOKEN_IDENTIFIER);
for (++i;; ++i) {
const char current = code[i];
if (current == c) {
break;
} else if (current == '\0') {
printError("Expected %c to end", sourceCode, node.strBegin, code + i, c);
goto RETURN_ERROR;
}
}
++node.strBegin;
push_clear_without_check(&nodes, &nodes_size, &nodes_inserted, &node,
code, i, TOKEN_NONE);
} else if (isNumber(c)) {
if (node.token != TOKEN_NUMBER && node.token != TOKEN_IDENTIFIER &&
node.token != TOKEN_SYMBOL) {
push_if_not_empty(&nodes, &nodes_size, &nodes_inserted, &node, code, i,
TOKEN_NUMBER);
}
} else if (isString(c)) {
push_if_not_empty(&nodes, &nodes_size, &nodes_inserted, &node, code, i,
TOKEN_STRING);
for (++i;; ++i) {
const char current = code[i];
if (current == c) {
break;
} else if (current == '\\') {
++i;
} else if (current == '\0') {
printError("Expected %c to end", sourceCode, node.strBegin, code + i, c);
goto RETURN_ERROR;
}
}
++i;
push_if_not_empty(&nodes, &nodes_size, &nodes_inserted, &node, code, i,
TOKEN_NONE);
--i;
} else if (isOperator(c)) {
if (node.token == TOKEN_OPERATOR) {
const Token token = getOperator(node.strBegin, code + i + 1);
if (token != TOKEN_NONE) {
continue;
} else {
node.token = getOperator(node.strBegin, code + i);
if (node.token == TOKEN_NONE) {
printError("Unknown operator '%.*s'", sourceCode, node.strBegin,
node.strEnd, (int)(code + i - node.strBegin),
node.strBegin);
goto RETURN_ERROR;
}
push_clear_without_check(&nodes, &nodes_size, &nodes_inserted, &node,
code, i, TOKEN_OPERATOR);
}
} else {
push_if_not_empty(&nodes, &nodes_size, &nodes_inserted, &node, code, i,
TOKEN_OPERATOR);
}
} else if (isSymbol(c)) {
if (node.token != TOKEN_SYMBOL) {
push_if_not_empty(&nodes, &nodes_size, &nodes_inserted, &node, code, i,
TOKEN_SYMBOL);
}
} else {
printError("Unexpected char '%c'", sourceCode, code + i, code + i + 1, c);
goto RETURN_ERROR;
}
}
RETURN_SUCCESS:
Nodes result = {
.nodes = a404m_realloc(nodes, nodes_inserted * sizeof(Node)),
.size = nodes_inserted,
};
return result;
RETURN_ERROR:
free(nodes);
Nodes error = {
.nodes = NULL,
.size = ERROR_SIZE,
};
return error;
}
void push_if_not_empty(Node **restrict nodes, size_t *restrict nodes_size,
size_t *restrict nodes_inserted, Node *restrict node,
char const *restrict str, int i, Token token) {
if (node->token != TOKEN_NONE) {
if (*nodes_size == *nodes_inserted) {
*nodes_size += *nodes_size / 2 + 1;
*nodes = a404m_realloc(*nodes, *nodes_size * sizeof(Node));
}
node->strEnd = str + i;
if (node->token == TOKEN_IDENTIFIER) {
const Token foundToken = getKeyword(node->strBegin, node->strEnd);
if (foundToken != TOKEN_NONE) {
node->token = foundToken;
}
} else if (node->token == TOKEN_OPERATOR) {
const Token foundToken = getOperator(node->strBegin, node->strEnd);
if (foundToken != TOKEN_NONE) {
node->token = foundToken;
}
}
(*nodes)[*nodes_inserted] = *node;
++*nodes_inserted;
}
node->strBegin = str + i;
node->token = token;
}
void push_clear_without_check(Node **restrict nodes,
size_t *restrict nodes_size,
size_t *restrict nodes_inserted, Node *node,
char const *restrict str, int i, Token token) {
if (*nodes_size == *nodes_inserted) {
*nodes_size += *nodes_size / 2 + 1;
*nodes = a404m_realloc(*nodes, *nodes_size * sizeof(Node));
}
node->strEnd = str + i;
(*nodes)[*nodes_inserted] = *node;
++*nodes_inserted;
node->strBegin = str + i;
node->token = token;
}
bool isSpace(char c) { return isspace(c); }
bool isIdentifier(char c) {
return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || c == '_';
}
bool isIdentifierSymbol(char c) { return c == '`'; }
bool isNumber(char c) { return '0' <= c && c <= '9'; }
bool isString(char c) { return c == '"' || c == '\''; }
bool isOperator(char c) {
switch (c) {
case '(':
case ')':
case '[':
case ']':
case '{':
case '}':
case '*':
case '/':
case '%':
case '+':
case '-':
case ':':
case '!':
case '=':
case '<':
case '>':
case '&':
case '|':
case '.':
case ',':
case ';':
return true;
}
return false;
}
bool isSymbol(char c) { return c == '#'; }
Token getTokenInStrings(char const *strBegin, char const *strEnd,
const char *strings[], const Token tokens[],
size_t size) {
const size_t strSize = strEnd - strBegin;
for (size_t i = 0; i < size; ++i) {
const char *search = strings[i];
// faster than strlen+strncmp
for (size_t j = 0;; ++j) {
const char searchChar = search[j];
if (j == strSize) {
if (searchChar == '\0') {
return tokens[i];
} else {
break;
}
} else if (searchChar == '\0') {
break;
} else if (searchChar != strBegin[j]) {
break;
}
}
}
return TOKEN_NONE;
}
Token getKeyword(char const *strBegin, char const *strEnd) {
return getTokenInStrings(strBegin, strEnd, KEYWORDS_STRINGS, KEYWORDS_TOKENS,
KEYWORDS_SIZE);
}
Token getOperator(char const *strBegin, char const *strEnd) {
return getTokenInStrings(strBegin, strEnd, OPERATORS_STRINGS,
OPERATORS_TOKENS, OPERATORS_SIZE);
}
|