From 2cb992bfd4fea07fcdb3df1b9ffe1e11d34c70f3 Mon Sep 17 00:00:00 2001 From: Quinn Stephens Date: Sat, 2 Nov 2024 09:55:58 -0400 Subject: [parser] Improve error handling parse_enum() and parse_struct() now free all allocated memory in case of an error. They also no longer allow the type to be registered if an error occurs. parse_type() no longer allows/requires semicolons after enum/struct definitions. Signed-off-by: Quinn Stephens --- compiler/parser/parser.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'compiler/parser/parser.c') diff --git a/compiler/parser/parser.c b/compiler/parser/parser.c index 261385b..a630e37 100644 --- a/compiler/parser/parser.c +++ b/compiler/parser/parser.c @@ -15,7 +15,7 @@ #include "parser.h" static void -add_builtin(struct hashmap *map, char *name, size_t size, int n_ptrs) +add_builtin(struct hashmap *map, char *name, size_t size) { struct type *type; size_t name_len; @@ -27,7 +27,7 @@ add_builtin(struct hashmap *map, char *name, size_t size, int n_ptrs) type->name = name; type->name_len = name_len; type->size = size; - type->n_ptrs = n_ptrs; + type->n_ptrs = 0; hashmap_add(map, &type->hashmap_entry); } @@ -67,8 +67,7 @@ parser_parse(struct parser *ctx) break; default: tok_error(&ctx->tok, "unexpected \"%.*s\"\n", (int)ctx->tok.len, ctx->tok.pos); - next_token(ctx); - break; + return; } } } @@ -79,10 +78,10 @@ parser_init(struct parser *ctx, char *source) debug("Initializing parser...\n"); lexer_init(&ctx->lexer, source); - add_builtin(ctx->types, "any", 0, 0); - add_builtin(ctx->types, "uint", sizeof(void*), 0); - add_builtin(ctx->types, "uint64", 8, 0); - add_builtin(ctx->types, "uint32", 4, 0); - add_builtin(ctx->types, "uint16", 2, 0); - add_builtin(ctx->types, "uint8", 1, 0); + add_builtin(ctx->types, "any", 0); + add_builtin(ctx->types, "uint", sizeof(void*)); + add_builtin(ctx->types, "uint64", 8); + add_builtin(ctx->types, "uint32", 4); + add_builtin(ctx->types, "uint16", 2); + add_builtin(ctx->types, "uint8", 1); } -- cgit v1.2.3