From 750c26fedb02db025bcb86176a7d94cb386b6b18 Mon Sep 17 00:00:00 2001 From: Quinn Stephens Date: Mon, 9 Jun 2025 21:09:21 -0400 Subject: parser: Work on parsing function declarations Signed-off-by: Quinn Stephens --- src/parser/parser.c | 74 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 10 deletions(-) (limited to 'src/parser/parser.c') diff --git a/src/parser/parser.c b/src/parser/parser.c index 039bfa6..6f795cc 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -32,6 +32,7 @@ #include "lexer.h" #include "log.h" #include "parser.h" +#include "parser/ast.h" #include "parser/types.h" static void @@ -46,33 +47,86 @@ tok_error(struct token *tok, const char *fmt, ...) } static bool -parse_declaration(struct lexer *lexer, struct token *tok) +parse_func_decl(struct lexer *lexer, struct token *tok, struct ast_node *func) { - struct type *typ; + /* + * TODO: Parse parameters. + */ + lexer_next(lexer, tok); + if (tok->kind != TOK_RPAREN) { + tok_error(tok, "expected \")\" after \"(\"\n"); + free(func); + return false; + } /* - * TODO: Support custom types. + * TODO: Parse body. + */ + lexer_next(lexer, tok); + if (tok->kind != TOK_SEMICOLON) { + tok_error(tok, "expected \";\" after \")\"\n"); + free(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); + + lexer_next(lexer, tok); + return true; +} + +static bool +parse_decl(struct lexer *lexer, struct token *tok) +{ + struct type *type; + struct ast_node *node; + + /* + * TODO: Parse custom types. */ if (!(tok->flags & TF_BUILTIN_TYPE)) { - tok_error(tok, "expected built-in type\n"); + tok_error(tok, "expected type\n"); return false; } - typ = types_find_builtin(tok->kind); - if (typ == NULL) { + type = types_find_builtin(tok->kind); + if (type == NULL) { return false; } lexer_next(lexer, tok); if (tok->kind != TOK_IDENTIFIER) { - tok_error(tok, "expected identifier\n"); + tok_error(tok, "expected identifier after type\n"); + return false; + } + + node = malloc(sizeof(struct ast_node)); + if (node == NULL) { + log_error("failed to allocate memory for AST node\n"); return false; } - log_debug("name: \"%.*s\", type: { name: \"%s\", size: %lu }\n", tok->len, tok->pos, typ->name, typ->size); + node->kind = NOK_UNKNOWN; + node->name = tok->pos; + node->name_len = tok->len; + node->type = type; + node->ptr_levels = 0; lexer_next(lexer, tok); - return true; + if (tok->kind == TOK_LPAREN) { + return parse_func_decl(lexer, tok, node); + } + + /* + * TODO: Parse variable declarations. + */ + tok_error(tok, "expected \"(\" after identifier\n"); + free(node); + return false; } bool @@ -95,7 +149,7 @@ parser_parse(struct lexer *lexer) } if ((tok.flags & TF_BUILTIN_TYPE) || tok.kind == TOK_IDENTIFIER) { - success = parse_declaration(lexer, &tok); + success = parse_decl(lexer, &tok); } if (!success) { -- cgit v1.2.3