summaryrefslogtreecommitdiff
path: root/src/ast.c
diff options
context:
space:
mode:
authorCarlos Maniero <carlos@maniero.me>2023-05-08 23:53:44 -0300
committerJohnny Richard <johnny@johnnyrichard.com>2023-05-09 22:48:17 +0200
commit8c8fc8cc30b38ef00d606a4991b655df97a52fb6 (patch)
treee5ceb46de265fea9dae3e66c7e4b3a9bdaac80a8 /src/ast.c
parent50ce0fb2a436eb5e765b2764c9468f648f18a4f8 (diff)
parser: create a block node type
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 <carlos@maniero.me>
Diffstat (limited to 'src/ast.c')
-rw-r--r--src/ast.c24
1 files changed, 22 insertions, 2 deletions
diff --git a/src/ast.c b/src/ast.c
index 4fd5a3f..1200e30 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -45,7 +45,10 @@ ast_node_destroy(ast_node_t *node)
{
switch (node->kind) {
case AST_FUNCTION_DECLARATION:
- ast_node_destroy_vector(node->data.function.body);
+ ast_node_destroy(node->data.function.body);
+ break;
+ case AST_BLOCK:
+ ast_node_destroy_vector(node->data.block.body);
break;
case AST_BINARY_OPERATION:
ast_node_destroy(node->data.binary_operation.left);
@@ -82,7 +85,7 @@ ast_node_new_return_stmt(ast_node_t *argument)
}
ast_node_t *
-ast_node_new_function_declaration(string_view_t function_name, type_t return_type, vector_t *body)
+ast_node_new_function_declaration(string_view_t function_name, type_t return_type, ast_node_t *body)
{
ast_node_t *node = ast_node_new();
@@ -101,6 +104,23 @@ ast_node_new_function_declaration(string_view_t function_name, type_t return_typ
}
ast_node_t *
+ast_node_new_block(vector_t *body)
+{
+ ast_node_t *node = ast_node_new();
+
+ *node = (ast_node_t){
+ .kind = AST_BLOCK,
+ .data = {
+ .block = {
+ .body = body,
+ }
+ },
+ };
+
+ return node;
+}
+
+ast_node_t *
ast_node_new_literal_integer(uint32_t number)
{
ast_node_t *node = ast_node_new();