summaryrefslogtreecommitdiff
path: root/test/parser_test.c
diff options
context:
space:
mode:
authorCarlos Maniero <carlos@maniero.me>2023-05-02 23:45:49 -0300
committerJohnny Richard <johnny@johnnyrichard.com>2023-05-03 22:38:25 +0200
commit77dbf3a5011566b4a6274b3cdfa075dd723642d3 (patch)
tree8bd9022499ab1d403ca3c59bbd05b09bd44ccbbd /test/parser_test.c
parent57cee82f9136f0a0246d3b1d3191226bd4234d43 (diff)
Parser: Make the parser function return the ast_node
In many situations, the parser is responsible for reserving memory for nodes, particularly during function body parsing. This commit introduces a new standard where parser functions not only allocate memory for ast_nodes, but also return them. In case of a parser error, a NULL pointer is returned. This standard will be extended to other parsers in future commits, ensuring consistency throughout the codebase. Signed-off-by: Carlos Maniero <carlos@maniero.me>
Diffstat (limited to 'test/parser_test.c')
-rw-r--r--test/parser_test.c18
1 files changed, 6 insertions, 12 deletions
diff --git a/test/parser_test.c b/test/parser_test.c
index 8f75e07..f923c46 100644
--- a/test/parser_test.c
+++ b/test/parser_test.c
@@ -48,14 +48,12 @@ assert_parser_error(char *src, char *error_msg)
make_lexer_from_static_src(&lexer, src);
parser_init(&parser, &lexer, scope);
- ast_node_t *ast_function = ast_node_new();
+ ast_node_t *ast_function = parser_parse_function_declaration(&parser);
- bool parsed = parser_parse_function_declaration(&parser, ast_function);
- assert_false(parsed);
+ assert_false(ast_function != NULL);
assert_int(1, ==, parser.errors_len);
assert_string_equal(error_msg, parser.errors[0].message);
- ast_node_destroy(ast_function);
scope_destroy(scope);
}
@@ -68,10 +66,8 @@ test_parse_function(const MunitParameter params[], void *user_data_or_fixture)
make_lexer_from_static_src(&lexer, "main(): i32 { \nreturn 42;\n }");
parser_init(&parser, &lexer, scope);
- ast_node_t *ast_function = ast_node_new();
-
- bool parsed = parser_parse_function_declaration(&parser, ast_function);
- assert_true(parsed);
+ ast_node_t *ast_function = parser_parse_function_declaration(&parser);
+ assert_true(ast_function != NULL);
char actual[5];
@@ -103,11 +99,9 @@ test_parse_variable_definition(const MunitParameter params[], void *user_data_or
make_lexer_from_static_src(&lexer, "main(): i32 { \nvariable : i32 = 42; \nreturn variable;\n }");
parser_init(&parser, &lexer, scope);
- ast_node_t *ast_function = ast_node_new();
+ ast_node_t *ast_function = parser_parse_function_declaration(&parser);
- bool parsed = parser_parse_function_declaration(&parser, ast_function);
-
- assert_true(parsed);
+ assert_true(ast_function != NULL);
char actual[5];