aboutsummaryrefslogtreecommitdiffstats
path: root/src/bootstrap/lexer.c
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-19 17:21:28 +0200
committerBad Diode <bd@badd10de.dev>2021-10-19 17:21:28 +0200
commitf7b5da260fc7b6b73b5ed6c87d3593de372db6ad (patch)
treec13531ae52cbe0fb6fb5ed5f45d6f718dfcc9619 /src/bootstrap/lexer.c
parentc0202d26b94434253fb99450734152b7cb1ae388 (diff)
downloadbdl-f7b5da260fc7b6b73b5ed6c87d3593de372db6ad.tar.gz
bdl-f7b5da260fc7b6b73b5ed6c87d3593de372db6ad.zip
Add generic dynamic array and change tokens to use it
Diffstat (limited to 'src/bootstrap/lexer.c')
-rw-r--r--src/bootstrap/lexer.c32
1 files changed, 10 insertions, 22 deletions
diff --git a/src/bootstrap/lexer.c b/src/bootstrap/lexer.c
index 05324eb..38ca37c 100644
--- a/src/bootstrap/lexer.c
+++ b/src/bootstrap/lexer.c
@@ -44,19 +44,6 @@ print_token(Token tok) {
44 printf("\n"); 44 printf("\n");
45} 45}
46 46
47void
48push_token(Tokens *tokens, Token tok) {
49 if (tokens->buf == NULL) {
50 tokens->size = 0;
51 tokens->cap = TOK_BUF_CAP;
52 tokens->buf = malloc(tokens->cap * sizeof(Token));
53 } else if (tokens->size == tokens->cap) {
54 tokens->cap *= 2;
55 tokens->buf = realloc(tokens->buf, tokens->cap * sizeof(Token));
56 }
57 tokens->buf[tokens->size++] = tok;
58}
59
60char 47char
61scan_next(Scanner *scanner) { 48scan_next(Scanner *scanner) {
62 char c = sv_next(&scanner->current); 49 char c = sv_next(&scanner->current);
@@ -147,9 +134,10 @@ find_primitive_type(const StringView value) {
147 return TOKEN_SYMBOL; 134 return TOKEN_SYMBOL;
148} 135}
149 136
150Tokens 137Token *
151tokenize(const StringView *sv) { 138tokenize(const StringView *sv) {
152 Tokens tokens = (Tokens){0}; 139 Token *tokens = NULL;
140 array_init(tokens, 1);
153 Scanner scanner = (Scanner){ 141 Scanner scanner = (Scanner){
154 .current = *sv, 142 .current = *sv,
155 .line_number = 1, 143 .line_number = 1,
@@ -197,7 +185,7 @@ tokenize(const StringView *sv) {
197 .line = line, 185 .line = line,
198 .column = col, 186 .column = col,
199 }; 187 };
200 push_token(&tokens, token); 188 array_push(tokens, token);
201 } break; 189 } break;
202 case '\'': { 190 case '\'': {
203 Token token = (Token){ 191 Token token = (Token){
@@ -205,7 +193,7 @@ tokenize(const StringView *sv) {
205 .line = line, 193 .line = line,
206 .column = col, 194 .column = col,
207 }; 195 };
208 push_token(&tokens, token); 196 array_push(tokens, token);
209 } break; 197 } break;
210 case '(': { 198 case '(': {
211 if (scan_peek(&scanner) == ')') { 199 if (scan_peek(&scanner) == ')') {
@@ -215,14 +203,14 @@ tokenize(const StringView *sv) {
215 .line = line, 203 .line = line,
216 .column = col, 204 .column = col,
217 }; 205 };
218 push_token(&tokens, token); 206 array_push(tokens, token);
219 } else { 207 } else {
220 Token token = (Token){ 208 Token token = (Token){
221 .type = TOKEN_LPAREN, 209 .type = TOKEN_LPAREN,
222 .line = line, 210 .line = line,
223 .column = col, 211 .column = col,
224 }; 212 };
225 push_token(&tokens, token); 213 array_push(tokens, token);
226 } 214 }
227 } break; 215 } break;
228 case ')': { 216 case ')': {
@@ -231,7 +219,7 @@ tokenize(const StringView *sv) {
231 .line = line, 219 .line = line,
232 .column = col, 220 .column = col,
233 }; 221 };
234 push_token(&tokens, token); 222 array_push(tokens, token);
235 } break; 223 } break;
236 default: { 224 default: {
237 size_t n = 1; 225 size_t n = 1;
@@ -252,7 +240,7 @@ tokenize(const StringView *sv) {
252 .column = col, 240 .column = col,
253 }; 241 };
254 token.type = find_primitive_type(token.value); 242 token.type = find_primitive_type(token.value);
255 push_token(&tokens, token); 243 array_push(tokens, token);
256 } break; 244 } break;
257 } 245 }
258 } 246 }
@@ -263,7 +251,7 @@ tokenize(const StringView *sv) {
263 .line = scanner.line_number, 251 .line = scanner.line_number,
264 .column = 1, 252 .column = 1,
265 }; 253 };
266 push_token(&tokens, token); 254 array_push(tokens, token);
267 255
268 return tokens; 256 return tokens;
269} 257}