From ad54ee1182b1549880eddc8b1969d3992d9f7f1d Mon Sep 17 00:00:00 2001 From: Carlos Maniero Date: Tue, 9 May 2023 16:18:18 -0300 Subject: 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 --- test/parser_test.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'test/parser_test.c') 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'"); -- cgit v1.2.3