summaryrefslogtreecommitdiff
path: root/test/parser_test.c
diff options
context:
space:
mode:
authorCarlos Maniero <carlos@maniero.me>2023-05-09 16:18:18 -0300
committerJohnny Richard <johnny@johnnyrichard.com>2023-05-09 22:54:46 +0200
commitad54ee1182b1549880eddc8b1969d3992d9f7f1d (patch)
tree34d6a33a6a5ccba3ff6198d0e49f1e1d4701fc27 /test/parser_test.c
parent8c8fc8cc30b38ef00d606a4991b655df97a52fb6 (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>
Diffstat (limited to 'test/parser_test.c')
-rw-r--r--test/parser_test.c34
1 files changed, 34 insertions, 0 deletions
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'");