diff options
author | Quinn Stephens <quinn@osmora.org> | 2025-06-09 21:09:21 -0400 |
---|---|---|
committer | Quinn Stephens <quinn@osmora.org> | 2025-06-09 21:09:21 -0400 |
commit | 750c26fedb02db025bcb86176a7d94cb386b6b18 (patch) | |
tree | 2fc12b9d15dfb8a6cb08f326b431b098b811df80 /src | |
parent | ca038a85d6efaf3cfc7be7b44296adafa8281ac9 (diff) |
parser: Work on parsing function declarations
Signed-off-by: Quinn Stephens <quinn@osmora.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/main.c | 2 | ||||
-rw-r--r-- | src/parser/parser.c | 74 |
2 files changed, 65 insertions, 11 deletions
@@ -32,7 +32,7 @@ #include "log.h" #include "parser.h" -static const char *src = "void test\nint main"; +static const char *src = "void test();\nint main();"; int main(int argc, char **argv) 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) { |