summaryrefslogtreecommitdiff
path: root/src/parser/parser.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/parser/parser.c')
-rw-r--r--src/parser/parser.c36
1 files changed, 21 insertions, 15 deletions
diff --git a/src/parser/parser.c b/src/parser/parser.c
index d8b1982..4f8543c 100644
--- a/src/parser/parser.c
+++ b/src/parser/parser.c
@@ -35,6 +35,8 @@
#include "parser/ast.h"
#include "parser/types.h"
+#define SYMS_HASHMAP_ROWS 16
+
static void
tok_error(struct token *tok, const char *fmt, ...)
{
@@ -49,6 +51,8 @@ tok_error(struct token *tok, const char *fmt, ...)
static bool
parse_func_decl(struct parser *ctx, struct ast_node *func)
{
+ func->kind = NOK_FUNCTION;
+
/*
* TODO: Parse parameters.
*/
@@ -69,12 +73,7 @@ parse_func_decl(struct parser *ctx, struct ast_node *func)
return false;
}
- /*
- * TODO: Keep track of nodes.
- */
- log_debug("Parsed function \"%.*s\" (return type %s)\n", func->name_len, func->name, func->type->name);
- free(func);
-
+ hashmap_add(ctx->syms, &func->hashmap_entry);
parser_advance(ctx);
return true;
}
@@ -130,27 +129,34 @@ parse_decl(struct parser *ctx)
}
bool
-parser_parse(struct lexer *lexer)
+parser_parse(struct parser *ctx)
{
- struct parser ctx;
bool success;
log_debug("parsing...\n");
- ctx.lexer = lexer;
- if (!parser_advance(&ctx)) {
+ if (!parser_advance(ctx)) {
log_error("failed to get first token\n");
return false;
}
- while (ctx.tok.kind != TOK_EOF) {
- if (ctx.tok.kind == TOK_UNKNOWN) {
- tok_error(&ctx.tok, "unrecognized token\n");
+ ctx->syms.rows = malloc(SYMS_HASHMAP_ROWS * sizeof(struct list));
+ if (ctx->syms.rows == NULL) {
+ log_error("failed to allocate memory for symbol hashmap\n");
+ return false;
+ }
+
+ ctx->syms.row_count = SYMS_HASHMAP_ROWS;
+ hashmap_init(ctx->syms);
+
+ while (ctx->tok.kind != TOK_EOF) {
+ if (ctx->tok.kind == TOK_UNKNOWN) {
+ tok_error(&ctx->tok, "unrecognized token\n");
return false;
}
- if ((ctx.tok.flags & TF_BUILTIN_TYPE) || ctx.tok.kind == TOK_IDENTIFIER) {
- success = parse_decl(&ctx);
+ if ((ctx->tok.flags & TF_BUILTIN_TYPE) || ctx->tok.kind == TOK_IDENTIFIER) {
+ success = parse_decl(ctx);
}
if (!success) {