diff options
author | Carlos Maniero <carlos@maniero.me> | 2023-05-09 16:18:18 -0300 |
---|---|---|
committer | Johnny Richard <johnny@johnnyrichard.com> | 2023-05-09 22:54:46 +0200 |
commit | ad54ee1182b1549880eddc8b1969d3992d9f7f1d (patch) | |
tree | 34d6a33a6a5ccba3ff6198d0e49f1e1d4701fc27 | |
parent | 8c8fc8cc30b38ef00d606a4991b655df97a52fb6 (diff) |
parser: parses an if statement no code generation
This commit parses a if statement following the grammar bellow:
if boolean_expression {
n_epressions;
}
No else neither code generation was implemented.
Signed-off-by: Carlos Maniero <carlos@maniero.me>
-rw-r--r-- | examples/if.pipa | 9 | ||||
-rw-r--r-- | src/ast.c | 23 | ||||
-rw-r--r-- | src/ast.h | 11 | ||||
-rw-r--r-- | src/ast_pretty_printer.c | 25 | ||||
-rw-r--r-- | src/gas_assembly_generator.c | 1 | ||||
-rw-r--r-- | src/lexer.c | 6 | ||||
-rw-r--r-- | src/lexer.h | 1 | ||||
-rw-r--r-- | src/parser.c | 54 | ||||
-rw-r--r-- | test/parser_test.c | 34 |
9 files changed, 164 insertions, 0 deletions
diff --git a/examples/if.pipa b/examples/if.pipa new file mode 100644 index 0000000..2c5578d --- /dev/null +++ b/examples/if.pipa @@ -0,0 +1,9 @@ +fn main(): i32 { + let n: i32 = 42; + + if n > 42 { + return 42; + } + + return n; +} @@ -47,6 +47,10 @@ ast_node_destroy(ast_node_t *node) case AST_FUNCTION_DECLARATION: ast_node_destroy(node->data.function.body); break; + case AST_IF_STMT: + ast_node_destroy(node->data.if_stmt.condition); + ast_node_destroy(node->data.if_stmt.body); + break; case AST_BLOCK: ast_node_destroy_vector(node->data.block.body); break; @@ -140,6 +144,25 @@ ast_node_new_literal_integer(uint32_t number) } ast_node_t * +ast_node_new_if_stmt(ast_node_t *condition, ast_node_t *body) +{ + ast_node_t *node = ast_node_new(); + + *node = (ast_node_t){ + .kind = AST_IF_STMT, + .result_type = TYPE_VOID, + .data = { + .if_stmt = { + .condition = condition, + .body = body + }, + }, + }; + + return node; +} + +ast_node_t * ast_node_new_literal_bool(bool boolean) { ast_node_t *node = ast_node_new(); @@ -111,6 +111,12 @@ typedef struct ast_variable_assignment_t ast_node_t *expression; } ast_variable_assignment_t; +typedef struct ast_if_stmt_t +{ + ast_node_t *condition; + ast_node_t *body; +} ast_if_stmt_t; + typedef enum { AST_BINARY_OPERATION, @@ -118,6 +124,7 @@ typedef enum AST_FUNCTION_DECLARATION, AST_LITERAL, AST_RETURN_STMT, + AST_IF_STMT, AST_UNKOWN_NODE, AST_VARIABLE_DECLARATION, AST_VARIABLE_ASSIGNMENT, @@ -130,6 +137,7 @@ typedef union ast_function_declaration_t function; ast_literal_t literal; ast_block_t block; + ast_if_stmt_t if_stmt; ast_return_stmt_t return_stmt; ast_variable_declaration_t variable_declaration; ast_variable_assignment_t variable_assignment; @@ -185,4 +193,7 @@ ast_node_new_variable(ast_identifier_t *identifier, type_t result_type); ast_node_t * ast_node_new_variable_assignment(ast_identifier_t *identifier, ast_node_t *expression); +ast_node_t * +ast_node_new_if_stmt(ast_node_t *condition, ast_node_t *body); + #endif /* AST_H */ diff --git a/src/ast_pretty_printer.c b/src/ast_pretty_printer.c index 718d22a..e2d007a 100644 --- a/src/ast_pretty_printer.c +++ b/src/ast_pretty_printer.c @@ -63,6 +63,31 @@ ast_pretty_printer_print_ast(ast_pretty_printer_t *printer, ast_node_t *ast) assert(ast); switch (ast->kind) { + case AST_IF_STMT: { + ast_if_stmt_t if_stmt = ast->data.if_stmt; + ast_pretty_printer_printf(printer, "IfStmt\n"); + ast_pretty_printer_add_indentation(printer); + { + ast_pretty_printer_printf(printer, "condition:\n"); + ast_pretty_printer_add_indentation(printer); + { + ast_pretty_printer_set_lst_children(printer); + ast_pretty_printer_print_ast(printer, if_stmt.condition); + + ast_pretty_printer_rm_indentation(printer); + } + ast_pretty_printer_printf(printer, "body:\n"); + ast_pretty_printer_add_indentation(printer); + { + ast_pretty_printer_set_lst_children(printer); + ast_pretty_printer_print_ast(printer, if_stmt.body); + + ast_pretty_printer_rm_indentation(printer); + } + ast_pretty_printer_rm_indentation(printer); + } + break; + } case AST_BINARY_OPERATION: { ast_binary_operation_t binop = ast->data.binary_operation; ast_pretty_printer_printf(printer, "BinaryOperation operation='%c'\n", operation_kinds[binop.kind]); diff --git a/src/gas_assembly_generator.c b/src/gas_assembly_generator.c index 0cce205..4b898ed 100644 --- a/src/gas_assembly_generator.c +++ b/src/gas_assembly_generator.c @@ -138,6 +138,7 @@ gas_assembly_generator_compile(gas_assembly_generator_t *gen, ast_node_t *ast) case AST_BLOCK: gas_assembly_generator_compile_block(gen, &ast->data.block); break; + case AST_IF_STMT: case AST_UNKOWN_NODE: assert(false && "unreachable"); } diff --git a/src/lexer.c b/src/lexer.c index f84163d..a6c8ae2 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -111,6 +111,10 @@ lexer_token_process_keyword(token_t *token) token->kind = TOKEN_KEYWORD_FN; return; } + if (string_view_eq(string_view_from_str("if"), token->value)) { + token->kind = TOKEN_KEYWORD_IF; + return; + } if (string_view_eq(string_view_from_str("true"), token->value)) { token->kind = TOKEN_TRUE; return; @@ -462,6 +466,8 @@ token_kind_to_str(token_kind_t kind) return "fn"; case TOKEN_KEYWORD_LET: return "let"; + case TOKEN_KEYWORD_IF: + return "if"; case TOKEN_TRUE: return "true"; case TOKEN_FALSE: diff --git a/src/lexer.h b/src/lexer.h index 899c0aa..0e36ada 100644 --- a/src/lexer.h +++ b/src/lexer.h @@ -59,6 +59,7 @@ typedef enum TOKEN_KEYWORD_RETURN, TOKEN_KEYWORD_FN, TOKEN_KEYWORD_LET, + TOKEN_KEYWORD_IF, TOKEN_TRUE, TOKEN_FALSE, diff --git a/src/parser.c b/src/parser.c index 848bb4a..9167f1e 100644 --- a/src/parser.c +++ b/src/parser.c @@ -30,6 +30,9 @@ static bool parser_expression_matches_the_expected_type(parser_t *parser, ast_node_t *expression, type_t type, token_t token); +static ast_node_t * +parser_parse_block_declarations(parser_t *parser, type_t result_type); + void parser_init(parser_t *parser, lexer_t *lexer, scope_t *scope) { @@ -463,6 +466,35 @@ parser_parse_variable_declaration(parser_t *parser) return node; } +static ast_node_t * +parser_parse_if_stmt(parser_t *parser, type_t result_type) +{ + token_t if_token; + if (!expected_token(&if_token, parser, TOKEN_KEYWORD_IF)) { + return NULL; + } + + ast_node_t *condition = parser_parse_expression(parser); + + if (condition == NULL) { + return NULL; + } + + if (!parser_expression_matches_the_expected_type(parser, condition, TYPE_BOOL, if_token)) { + ast_node_destroy(condition); + return NULL; + } + + ast_node_t *body = parser_parse_block_declarations(parser, result_type); + + if (body == NULL) { + ast_node_destroy(condition); + return NULL; + } + + return ast_node_new_if_stmt(condition, body); +} + static bool is_next_statement_a_variable_declaration(parser_t *parser) { @@ -488,6 +520,15 @@ is_next_statement_a_variable_assignement(parser_t *parser) } static bool +is_next_statement_a_if_stmt(parser_t *parser) +{ + token_t token; + lexer_peek_next_token(parser->lexer, &token); + + return token.kind == TOKEN_KEYWORD_IF; +} + +static bool is_next_statement_return(parser_t *parser) { token_t token; @@ -563,6 +604,19 @@ parser_parse_block_declarations(parser_t *parser, type_t result_type) continue; } + if (is_next_statement_a_if_stmt(parser)) { + ast_node_t *if_stmt = parser_parse_if_stmt(parser, result_type); + + if (if_stmt == NULL) { + scope_leave(parser->scope); + ast_node_destroy_vector(body); + return NULL; + } + + vector_push_back(body, if_stmt); + continue; + } + if (is_next_statement_a_variable_declaration(parser)) { ast_node_t *variable_node = parser_parse_variable_declaration(parser); diff --git a/test/parser_test.c b/test/parser_test.c index 2d8f0bb..43eb27b 100644 --- a/test/parser_test.c +++ b/test/parser_test.c @@ -356,6 +356,35 @@ test_parse_all_boolean_expression(const MunitParameter params[], void *user_data return MUNIT_OK; } +static MunitResult +test_parse_if(const MunitParameter params[], void *user_data_or_fixture) +{ + parser_t parser; + lexer_t lexer; + scope_t *scope = scope_new(); + + make_lexer_from_static_src(&lexer, "fn main(): i32 { if 42 > 2 { return 32; } \nreturn 42;\n }"); + parser_init(&parser, &lexer, scope); + ast_node_t *ast_function = parser_parse_function_declaration(&parser); + + assert_not_null(ast_function); + + ast_node_t *ast_if = vector_at(ast_function->data.function.body->data.block.body, 0); + + assert_int(AST_IF_STMT, ==, ast_if->kind); + + ast_node_t *condition_node = ast_if->data.if_stmt.condition; + ast_node_t *body = ast_if->data.if_stmt.body; + + assert_int(AST_BINARY_OPERATION, ==, condition_node->kind); + assert_int(AST_BLOCK, ==, body->kind); + + ast_node_destroy(ast_function); + scope_destroy(scope); + + return MUNIT_OK; +} + void assert_string_view_equal(char *expected, string_view_t actual) { @@ -395,6 +424,11 @@ test_parse_basic_syntax_errors(const MunitParameter params[], void *user_data_or assert_parser_error("fn main(): i32 { return true; }", "incompatible types: expected 'i32', actual 'bool'."); assert_parser_error("fn main(): i32 { let a: bool = true; return a; }", "incompatible types: expected 'i32', actual 'bool'."); + assert_parser_error("fn main(): i32 { if { return 32; } \nreturn 42;\n }", "unexpected '{ ({)' token"); + assert_parser_error("fn main(): i32 { if 33 { return 32; } \nreturn 42;\n }", + "incompatible types: expected 'bool', actual 'i32'."); + assert_parser_error("fn main(): i32 { if true { return true; } \nreturn 42;\n }", + "incompatible types: expected 'i32', actual 'bool'."); // FIXME: once function calls are implemented, this error should inform that // neither a variable or function call was found. assert_parser_error("fn main(): i32 { oxi 42; }", "unexpected token 'TOKEN_NAME' value='oxi'"); |