diff options
author | Ian Moffett <ian@osmora.org> | 2024-11-01 23:46:08 -0400 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2024-11-01 23:46:08 -0400 |
commit | a515dfb3b8f8e999362db7a6b52b3104c03b750a (patch) | |
tree | d0180f0cbc39d9c3e367af30791ad774e4d419ff /compiler/parser/parser.c |
Import quark sources
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'compiler/parser/parser.c')
-rw-r--r-- | compiler/parser/parser.c | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/compiler/parser/parser.c b/compiler/parser/parser.c new file mode 100644 index 0000000..aeec48b --- /dev/null +++ b/compiler/parser/parser.c @@ -0,0 +1,84 @@ +/* + * Quark parser. + * Turns tokens into an AST (Abstract Syntax Tree). + * Copyright (c) 2023-2024, Quinn Stephens and the OSMORA team. + * Provided under the BSD 3-Clause license. + */ + +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "debug.h" +#include "hashmap.h" +#include "parser/type.h" +#include "parser.h" + +static void +add_builtin(struct hashmap *map, char *name, size_t size, int n_ptrs) +{ + struct type *type; + size_t name_len; + + name_len = strlen(name); + + type = malloc(sizeof(struct type)); + type->hashmap_entry.hash = hash_data(name, name_len); + type->name = name; + type->name_len = name_len; + type->size = size; + type->n_ptrs = n_ptrs; + + hashmap_add(map, &type->hashmap_entry); +} + +void +tok_error(struct token *tok, const char *fmt, ...) +{ + va_list ap; + + fprintf(stderr, "\033[1m%s:%d:%d: \033[31merror:\033[0m ", tok->fname, tok->line, tok->col); + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); +} + +void +tok_warn(struct token *tok, const char *fmt, ...) +{ + va_list ap; + + printf("\033[1m%s:%d:%d: \033[33mwarning:\033[0m ", tok->fname, tok->line, tok->col); + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); +} + +void +parser_parse(struct parser *ctx) +{ + debug("Parsing...\n"); + + next_token(ctx); + while (ctx->tok.kind != TK_EOF) { + switch (ctx->tok.kind) { + case TK_TYPE: + parse_type(ctx); + break; + default: + tok_error(&ctx->tok, "unexpected \"%.*s\"\n", (int)ctx->tok.len, ctx->tok.pos); + next_token(ctx); + break; + } + } +} + +void +parser_init(struct parser *ctx, char *source) +{ + debug("Initializing parser...\n"); + lexer_init(&ctx->lexer, source); + + add_builtin(ctx->types, "uint32", 4, 0); + add_builtin(ctx->types, "any", 0, 0); +} |