From f7b5da260fc7b6b73b5ed6c87d3593de372db6ad Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Tue, 19 Oct 2021 17:21:28 +0200 Subject: Add generic dynamic array and change tokens to use it --- src/bootstrap/lexer.c | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-) (limited to 'src/bootstrap/lexer.c') 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) { printf("\n"); } -void -push_token(Tokens *tokens, Token tok) { - if (tokens->buf == NULL) { - tokens->size = 0; - tokens->cap = TOK_BUF_CAP; - tokens->buf = malloc(tokens->cap * sizeof(Token)); - } else if (tokens->size == tokens->cap) { - tokens->cap *= 2; - tokens->buf = realloc(tokens->buf, tokens->cap * sizeof(Token)); - } - tokens->buf[tokens->size++] = tok; -} - char scan_next(Scanner *scanner) { char c = sv_next(&scanner->current); @@ -147,9 +134,10 @@ find_primitive_type(const StringView value) { return TOKEN_SYMBOL; } -Tokens +Token * tokenize(const StringView *sv) { - Tokens tokens = (Tokens){0}; + Token *tokens = NULL; + array_init(tokens, 1); Scanner scanner = (Scanner){ .current = *sv, .line_number = 1, @@ -197,7 +185,7 @@ tokenize(const StringView *sv) { .line = line, .column = col, }; - push_token(&tokens, token); + array_push(tokens, token); } break; case '\'': { Token token = (Token){ @@ -205,7 +193,7 @@ tokenize(const StringView *sv) { .line = line, .column = col, }; - push_token(&tokens, token); + array_push(tokens, token); } break; case '(': { if (scan_peek(&scanner) == ')') { @@ -215,14 +203,14 @@ tokenize(const StringView *sv) { .line = line, .column = col, }; - push_token(&tokens, token); + array_push(tokens, token); } else { Token token = (Token){ .type = TOKEN_LPAREN, .line = line, .column = col, }; - push_token(&tokens, token); + array_push(tokens, token); } } break; case ')': { @@ -231,7 +219,7 @@ tokenize(const StringView *sv) { .line = line, .column = col, }; - push_token(&tokens, token); + array_push(tokens, token); } break; default: { size_t n = 1; @@ -252,7 +240,7 @@ tokenize(const StringView *sv) { .column = col, }; token.type = find_primitive_type(token.value); - push_token(&tokens, token); + array_push(tokens, token); } break; } } @@ -263,7 +251,7 @@ tokenize(const StringView *sv) { .line = scanner.line_number, .column = 1, }; - push_token(&tokens, token); + array_push(tokens, token); return tokens; } -- cgit v1.2.1