From e927a4b22115e0461db6feeee891256b10650c0e Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Fri, 14 Apr 2023 23:10:25 +0200 Subject: parser: Create parser for function with return statements This is a very limited parser implementation which parses a single function with return type i32 and body containing a return number statement. The parser doesn't show the 'filepath:row:col' when it fails, a future improvement would be display it to easy find where the compilation problem is located. The ast_nodes are taking the token.value ownership (which is a really bad design since not all token.value ownership has been taken causing memory leaking) but we never free them. For a future fix we could use a string_view instead since we never change the original source code. The string_view will also improve the performance a lot avoiding unnecessary heap memory allocation. Signed-off-by: Johnny Richard --- src/pipac.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/pipac.c') diff --git a/src/pipac.c b/src/pipac.c index f1e5865..0df4a08 100644 --- a/src/pipac.c +++ b/src/pipac.c @@ -17,6 +17,7 @@ #include #include #include "lexer.h" +#include "parser.h" void print_usage() @@ -46,8 +47,12 @@ main(int argc, char **argv) lexer_init(&lexer, filepath); printf("[INFO]: %ld bytes loaded [filename='%s']\n", lexer.srclen, lexer.filepath); + + parser_t parser; + parser_init(&parser, &lexer); + ast_function_t func = parser_parse_function(&parser); - print_tokens(&lexer); + printf("function name='%s' and return type '%d' with value %d\n", func.name, func.return_type, func.body.number); return EXIT_SUCCESS; } -- cgit v1.2.3