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 --- src/ast.h | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/ast.h') diff --git a/src/ast.h b/src/ast.h index f95aee2..1637173 100644 --- a/src/ast.h +++ b/src/ast.h @@ -29,6 +29,11 @@ typedef enum typedef struct ast_node_t ast_node_t; +typedef struct ast_block_t +{ + vector_t *body; +} ast_block_t; + typedef struct ast_return_stmt_t { ast_node_t *argument; @@ -48,7 +53,7 @@ typedef struct ast_function_declaration_t { ast_identifier_t identifier; type_t return_type; - vector_t *body; + ast_node_t *body; } ast_function_declaration_t; typedef enum ast_binary_operation_kind_t @@ -109,6 +114,7 @@ typedef struct ast_variable_assignment_t typedef enum { AST_BINARY_OPERATION, + AST_BLOCK, AST_FUNCTION_DECLARATION, AST_LITERAL, AST_RETURN_STMT, @@ -123,6 +129,7 @@ typedef union ast_binary_operation_t binary_operation; ast_function_declaration_t function; ast_literal_t literal; + ast_block_t block; ast_return_stmt_t return_stmt; ast_variable_declaration_t variable_declaration; ast_variable_assignment_t variable_assignment; @@ -155,7 +162,7 @@ ast_node_new_binary_operation(ast_binary_operation_kind_t kind, type_t result_type); 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 * ast_node_new_return_stmt(ast_node_t *argument); @@ -166,6 +173,9 @@ ast_node_new_variable_declaration(string_view_t variable_name, type_t type, ast_ ast_node_t * ast_node_new_literal_integer(uint32_t number); +ast_node_t * +ast_node_new_block(vector_t *body); + ast_node_t * ast_node_new_literal_bool(bool boolean); -- cgit v1.2.3