From 8c8fc8cc30b38ef00d606a4991b655df97a52fb6 Mon Sep 17 00:00:00 2001 From: Carlos Maniero Date: Mon, 8 May 2023 23:53:44 -0300 Subject: parser: create a block node type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since the next step to pipa programming language is about having control flow statements we could benefit ourselves by having a block node to control scope. Now, functions has a block node, instead of an vector as body. As you can see through the ast-dump: FunctionDecl name='main' └─ body: └─ Block └─ ReturnStmt └─ Literal type=i32 value='69' This same node kind can be used for parsing if, for and while blocks. I could use ast_block_t as body for functions but instead, I opted to use an ast_node_t. This brings the flexibility to, in the future, having another function body kinds, such as arrow functions if we want to: fn add(a: i32, b: i32): i32 => a + b; Signed-off-by: Carlos Maniero --- test/parser_test.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'test/parser_test.c') diff --git a/test/parser_test.c b/test/parser_test.c index 830fed7..2d8f0bb 100644 --- a/test/parser_test.c +++ b/test/parser_test.c @@ -76,7 +76,7 @@ test_parse_function(const MunitParameter params[], void *user_data_or_fixture) assert_string_equal("main", actual); assert_int(AST_FUNCTION_DECLARATION, ==, ast_function->kind); - ast_node_t *ast_return = vector_at(ast_function->data.function.body, 0); + ast_node_t *ast_return = vector_at(ast_function->data.function.body->data.block.body, 0); assert_int(AST_RETURN_STMT, ==, ast_return->kind); @@ -110,12 +110,12 @@ test_parse_variable_definition(const MunitParameter params[], void *user_data_or assert_string_equal("main", actual); assert_int(AST_FUNCTION_DECLARATION, ==, ast_function->kind); - ast_node_t *ast_variable = vector_at(ast_function->data.function.body, 0); + ast_node_t *ast_variable = vector_at(ast_function->data.function.body->data.block.body, 0); assert_int(AST_VARIABLE_DECLARATION, ==, ast_variable->kind); assert_string_view_equal("variable", ast_variable->data.variable_declaration.identifier.name); - ast_node_t *ast_return = vector_at(ast_function->data.function.body, 1); + ast_node_t *ast_return = vector_at(ast_function->data.function.body->data.block.body, 1); assert_int(AST_RETURN_STMT, ==, ast_return->kind); @@ -146,7 +146,7 @@ test_parse_boolean(const MunitParameter params[], void *user_data_or_fixture) assert_string_view_equal("my_bool_fn", ast_function->data.function.identifier.name); assert_int(AST_FUNCTION_DECLARATION, ==, ast_function->kind); - ast_node_t *ast_variable = vector_at(ast_function->data.function.body, 0); + ast_node_t *ast_variable = vector_at(ast_function->data.function.body->data.block.body, 0); assert_int(AST_VARIABLE_DECLARATION, ==, ast_variable->kind); @@ -156,7 +156,7 @@ test_parse_boolean(const MunitParameter params[], void *user_data_or_fixture) assert_int(AST_LITERAL_BOOL, ==, ast_variable->data.variable_declaration.value->data.literal.kind); assert_true(ast_variable->data.variable_declaration.value->data.literal.value.boolean); - ast_node_t *ast_return = vector_at(ast_function->data.function.body, 1); + ast_node_t *ast_return = vector_at(ast_function->data.function.body->data.block.body, 1); assert_int(AST_RETURN_STMT, ==, ast_return->kind); -- cgit v1.2.3