summaryrefslogtreecommitdiff
path: root/compiler/parser/parser.c
diff options
context:
space:
mode:
authorQuinn Stephens <quinn@osmora.org>2024-11-02 09:55:58 -0400
committerQuinn Stephens <quinn@osmora.org>2024-11-02 09:55:58 -0400
commit2cb992bfd4fea07fcdb3df1b9ffe1e11d34c70f3 (patch)
treeb6a9c13b291c09151ee87e2b2dbac979c6254de3 /compiler/parser/parser.c
parentfc26be4293b03cac62d6db7bb597d0c8cf1d5b3a (diff)
[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 <quinn@osmora.org>
Diffstat (limited to 'compiler/parser/parser.c')
-rw-r--r--compiler/parser/parser.c19
1 files changed, 9 insertions, 10 deletions
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);
}