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 | |
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>
-rw-r--r-- | src/ast.c | 24 | ||||
-rw-r--r-- | src/ast.h | 14 | ||||
-rw-r--r-- | src/ast_pretty_printer.c | 30 | ||||
-rw-r--r-- | src/gas_assembly_generator.c | 18 | ||||
-rw-r--r-- | src/parser.c | 15 | ||||
-rw-r--r-- | test/parser_test.c | 10 |
6 files changed, 87 insertions, 24 deletions
@@ -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(); @@ -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); @@ -167,6 +174,9 @@ 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); ast_node_t * 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: { 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); diff --git a/src/parser.c b/src/parser.c index 2817fa0..848bb4a 100644 --- a/src/parser.c +++ b/src/parser.c @@ -516,9 +516,11 @@ parser_error_report_unexpected_token(parser_t *parser) } static bool -parser_ensure_function_return_statement(parser_t *parser, vector_t *body, token_t *function_token) +parser_ensure_function_return_statement(parser_t *parser, ast_node_t *block, token_t *function_token) { - ast_node_t *latest_node = vector_at(body, body->size - 1); + assert(block->kind == AST_BLOCK); + + ast_node_t *latest_node = vector_at(block->data.block.body, block->data.block.body->size - 1); if (latest_node->kind != AST_RETURN_STMT) { parser_error_t error; @@ -527,14 +529,13 @@ parser_ensure_function_return_statement(parser_t *parser, vector_t *body, token_ sprintf(error.message, "expected 'return' keyword."); parser->errors[parser->errors_len++] = error; - vector_destroy(body); return false; } return true; } -static vector_t * +static ast_node_t * parser_parse_block_declarations(parser_t *parser, type_t result_type) { if (!drop_expected_token(parser, TOKEN_OCURLY)) { @@ -602,7 +603,7 @@ parser_parse_block_declarations(parser_t *parser, type_t result_type) return NULL; } - return body; + return ast_node_new_block(body); } static bool @@ -638,14 +639,14 @@ parser_parse_function_declaration(parser_t *parser) return NULL; } - vector_t *body = parser_parse_block_declarations(parser, return_type); + ast_node_t *body = parser_parse_block_declarations(parser, return_type); if (body == NULL) { return NULL; } if (!parser_ensure_function_return_statement(parser, body, &func_name_token)) { - vector_destroy(body); + ast_node_destroy(body); return NULL; } diff --git a/test/parser_test.c b/test/parser_test.c index 830fed7..2d8f0bb 100644 --- a/test/parser_test.c +++ b/test/parser_test.c @@ -76,7 +76,7 @@ test_parse_function(const MunitParameter params[], void *user_data_or_fixture) assert_string_equal("main", actual); assert_int(AST_FUNCTION_DECLARATION, ==, ast_function->kind); - ast_node_t *ast_return = vector_at(ast_function->data.function.body, 0); + ast_node_t *ast_return = vector_at(ast_function->data.function.body->data.block.body, 0); assert_int(AST_RETURN_STMT, ==, ast_return->kind); @@ -110,12 +110,12 @@ test_parse_variable_definition(const MunitParameter params[], void *user_data_or assert_string_equal("main", actual); assert_int(AST_FUNCTION_DECLARATION, ==, ast_function->kind); - ast_node_t *ast_variable = vector_at(ast_function->data.function.body, 0); + ast_node_t *ast_variable = vector_at(ast_function->data.function.body->data.block.body, 0); assert_int(AST_VARIABLE_DECLARATION, ==, ast_variable->kind); assert_string_view_equal("variable", ast_variable->data.variable_declaration.identifier.name); - ast_node_t *ast_return = vector_at(ast_function->data.function.body, 1); + ast_node_t *ast_return = vector_at(ast_function->data.function.body->data.block.body, 1); assert_int(AST_RETURN_STMT, ==, ast_return->kind); @@ -146,7 +146,7 @@ test_parse_boolean(const MunitParameter params[], void *user_data_or_fixture) assert_string_view_equal("my_bool_fn", ast_function->data.function.identifier.name); assert_int(AST_FUNCTION_DECLARATION, ==, ast_function->kind); - ast_node_t *ast_variable = vector_at(ast_function->data.function.body, 0); + ast_node_t *ast_variable = vector_at(ast_function->data.function.body->data.block.body, 0); assert_int(AST_VARIABLE_DECLARATION, ==, ast_variable->kind); @@ -156,7 +156,7 @@ test_parse_boolean(const MunitParameter params[], void *user_data_or_fixture) assert_int(AST_LITERAL_BOOL, ==, ast_variable->data.variable_declaration.value->data.literal.kind); assert_true(ast_variable->data.variable_declaration.value->data.literal.value.boolean); - ast_node_t *ast_return = vector_at(ast_function->data.function.body, 1); + ast_node_t *ast_return = vector_at(ast_function->data.function.body->data.block.body, 1); assert_int(AST_RETURN_STMT, ==, ast_return->kind); |