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_pretty_printer.c | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) (limited to 'src/ast_pretty_printer.c') diff --git a/src/ast_pretty_printer.c b/src/ast_pretty_printer.c index 95f5993..718d22a 100644 --- a/src/ast_pretty_printer.c +++ b/src/ast_pretty_printer.c @@ -99,13 +99,33 @@ ast_pretty_printer_print_ast(ast_pretty_printer_t *printer, ast_node_t *ast) ast_pretty_printer_printf(printer, "FunctionDecl name='" SVFMT "'\n", SVARG(&function.identifier.name)); ast_pretty_printer_add_indentation(printer); - for (size_t i = 0; i < function.body->size; ++i) { - if (i + 1 >= function.body->size) { - ast_pretty_printer_set_lst_children(printer); + { + ast_pretty_printer_set_lst_children(printer); + ast_pretty_printer_printf(printer, "body:\n"); + + ast_pretty_printer_add_indentation(printer); + { + ast_pretty_printer_print_ast(printer, ast->data.function.body); + ast_pretty_printer_rm_indentation(printer); + } + + ast_pretty_printer_rm_indentation(printer); + } + break; + } + case AST_BLOCK: { + ast_pretty_printer_set_lst_children(printer); + ast_pretty_printer_printf(printer, "Block\n"); + ast_pretty_printer_add_indentation(printer); + { + for (size_t i = 0; i < ast->data.block.body->size; ++i) { + if (i + 1 >= ast->data.block.body->size) { + ast_pretty_printer_set_lst_children(printer); + } + ast_pretty_printer_print_ast(printer, vector_at(ast->data.block.body, i)); } - ast_pretty_printer_print_ast(printer, vector_at(function.body, i)); + ast_pretty_printer_rm_indentation(printer); } - ast_pretty_printer_rm_indentation(printer); break; } case AST_LITERAL: { -- cgit v1.2.3