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/gas_assembly_generator.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src/gas_assembly_generator.c') diff --git a/src/gas_assembly_generator.c b/src/gas_assembly_generator.c index d9feedb..0cce205 100644 --- a/src/gas_assembly_generator.c +++ b/src/gas_assembly_generator.c @@ -41,6 +41,9 @@ static void gas_assembly_generator_compile_variable_assignment(gas_assembly_generator_t *gen, ast_variable_assignment_t *variable_assignment); +static void +gas_assembly_generator_compile_block(gas_assembly_generator_t *gen, ast_block_t *block); + static void gas_assembly_generator_compile_variable(gas_assembly_generator_t *gen, ast_variable_t *variable); @@ -132,6 +135,9 @@ gas_assembly_generator_compile(gas_assembly_generator_t *gen, ast_node_t *ast) case AST_VARIABLE: gas_assembly_generator_compile_variable(gen, &ast->data.variable); break; + case AST_BLOCK: + gas_assembly_generator_compile_block(gen, &ast->data.block); + break; case AST_UNKOWN_NODE: assert(false && "unreachable"); } @@ -153,9 +159,7 @@ gas_assembly_generator_compile_function(gas_assembly_generator_t *gen, ast_funct fprintf(gen->stream, " push %%rbp\n"); fprintf(gen->stream, " mov %%rsp, %%rbp\n"); - for (size_t i = 0; i < func->body->size; i++) { - gas_assembly_generator_compile(gen, vector_at(func->body, i)); - } + gas_assembly_generator_compile(gen, func->body); fprintf(gen->stream, " pop %%rbp\n"); } @@ -257,6 +261,14 @@ gas_assembly_generator_compile_literal(gas_assembly_generator_t *gen, ast_litera } } +static void +gas_assembly_generator_compile_block(gas_assembly_generator_t *gen, ast_block_t *block) +{ + for (size_t i = 0; i < block->body->size; i++) { + gas_assembly_generator_compile(gen, vector_at(block->body, i)); + } +} + static void gas_assembly_generator_binary_operation(gas_assembly_generator_t *gen, ast_binary_operation_t *binary_operation) { -- cgit v1.2.3