From 41bfa91100b3ddf36496b5dff6e10bfd5394017d Mon Sep 17 00:00:00 2001 From: Preston Pan Date: Tue, 3 Jan 2023 13:52:38 -0800 Subject: implemented parse_all; I think i fixed some bugs but i forgor --- src/include/.ast.h.swp | Bin 12288 -> 12288 bytes src/parser.c | 20 +++++++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/include/.ast.h.swp b/src/include/.ast.h.swp index c3e8ba3..355bb9f 100644 Binary files a/src/include/.ast.h.swp and b/src/include/.ast.h.swp differ diff --git a/src/parser.c b/src/parser.c index 1fff876..92e9ccf 100644 --- a/src/parser.c +++ b/src/parser.c @@ -25,11 +25,11 @@ parser_t *init_parser(lexer_t *lexer) { while (true) { t = lexer_collect_next(lexer); - if (t == NULL) - break; size++; p->tokens = realloc(p->tokens, size * sizeof(token_t *)); p->tokens[size - 1] = t; + if (t == NULL) + break; } p->size = size; @@ -205,5 +205,19 @@ ast_t *parse_expr(parser_t *parser) { return NULL; } -ast_t *parse_all(parser_t *parser) {} +ast_t *parse_all(parser_t *parser) { + token_t *t = parser->tokens[parser->i]; + ast_t **asts = malloc(sizeof(ast_t *)); + ast_t *cur; + int i = 0; + while (t != NULL) { + cur = parse_expr(parser); + i++; + asts = realloc(asts, i * sizeof(ast_t *)); + asts[i - 1] = cur; + t = parser->tokens[parser->i]; + } + return init_ast_root(asts, i); +} + void parser_error(parser_t *parser) { exit(1); } -- cgit