diff options
author | Carlos Maniero <carlos@maniero.me> | 2023-05-08 23:53:44 -0300 |
---|---|---|
committer | Johnny Richard <johnny@johnnyrichard.com> | 2023-05-09 22:48:17 +0200 |
commit | 8c8fc8cc30b38ef00d606a4991b655df97a52fb6 (patch) | |
tree | e5ceb46de265fea9dae3e66c7e4b3a9bdaac80a8 /src/gas_assembly_generator.c | |
parent | 50ce0fb2a436eb5e765b2764c9468f648f18a4f8 (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/gas_assembly_generator.c')
-rw-r--r-- | src/gas_assembly_generator.c | 18 |
1 files changed, 15 insertions, 3 deletions
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 @@ -42,6 +42,9 @@ 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); static void @@ -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"); } @@ -258,6 +262,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) { gas_assembly_generator_compile(gen, binary_operation->right); |