aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2026-09-04 20:00:52 +0200
committerhachem <im@hachem.wtf>2026-09-04 20:00:52 +0200
commit9283b46a08a1c58c321b6c7ebad1353725c3bc47 (patch)
tree5fba917f42bdbaf86f27abc01577b3c1b232e409 /tests
parent56ba8b2a373dd1386950592bda9c68ff9f25c203 (diff)
feat: add if/else blocks
if now guards a { } block or bare statement with an optional else (and now else-if chaining). The branch body becomes a statement list; a bare guarded statement is a list of length one.
Diffstat (limited to 'tests')
-rw-r--r--tests/codegen_test.c21
-rw-r--r--tests/parser_test.c42
2 files changed, 63 insertions, 0 deletions
diff --git a/tests/codegen_test.c b/tests/codegen_test.c
index 8fe1465..db23791 100644
--- a/tests/codegen_test.c
+++ b/tests/codegen_test.c
@@ -80,6 +80,26 @@ static void test_generate_if(struct TestContext* context)
free_program(&program);
}
+static void test_generate_if_else(struct TestContext* context)
+{
+ struct Lexer lexer = create_lexer(
+ "proc main\n{\nif rax > 3\n{\nrdi = 1\n}\nelse\n{\nrdi = 0\n}\n}\n");
+ struct Program program;
+ check(context, parse_program(&lexer, &program));
+
+ char buffer[1024];
+ generate_to_buffer(&program, buffer, sizeof(buffer));
+
+ check(context, strstr(buffer, "jle .if_else_0") != NULL);
+ check(context, strstr(buffer, "mov rdi, 1") != NULL);
+ check(context, strstr(buffer, "jmp .if_end_0") != NULL);
+ check(context, strstr(buffer, ".if_else_0:") != NULL);
+ check(context, strstr(buffer, "mov rdi, 0") != NULL);
+ check(context, strstr(buffer, ".if_end_0:") != NULL);
+
+ free_program(&program);
+}
+
static void test_generate_call(struct TestContext* context)
{
struct Lexer lexer = create_lexer(
@@ -428,6 +448,7 @@ void run_codegen_tests(struct TestContext* context)
test_generate_consts_and_data(context);
test_generate_text(context);
test_generate_if(context);
+ test_generate_if_else(context);
test_generate_call(context);
test_generate_param_substitution(context);
test_generate_divide(context);
diff --git a/tests/parser_test.c b/tests/parser_test.c
index ca1d345..32ea422 100644
--- a/tests/parser_test.c
+++ b/tests/parser_test.c
@@ -160,8 +160,48 @@ static void test_parse_if(struct TestContext* context)
check(context, primary_is(branch.branch.left, "rax"));
check(context, text_is(branch.branch.comparison, "!="));
check(context, primary_is(branch.branch.right, "0"));
+ check(context, branch.branch.body_count == 1);
check(context, branch.branch.body->kind == STATEMENT_GOTO);
check(context, text_is(branch.branch.body->jump.label, "loop"));
+ check(context, branch.branch.else_count == 0);
+
+ free_program(&program);
+}
+
+static void test_parse_if_block(struct TestContext* context)
+{
+ struct Lexer lexer = create_lexer(
+ "proc main\n{\nif rax > 3\n{\nrdi = 1\nrbx = 2\n}\nelse\n{\nrdi = 0\n}\n}\n");
+ struct Program program;
+
+ check(context, parse_program(&lexer, &program));
+ check(context, program.procs[0].body_count == 1);
+
+ struct Statement branch = program.procs[0].body[0];
+ check(context, branch.kind == STATEMENT_IF);
+ check(context, branch.branch.body_count == 2);
+ check(context, branch.branch.body[0].kind == STATEMENT_ASSIGN);
+ check(context, text_is(branch.branch.body[1].assign.target, "rbx"));
+ check(context, branch.branch.else_count == 1);
+ check(context, text_is(branch.branch.else_body[0].assign.target, "rdi"));
+
+ free_program(&program);
+}
+
+static void test_parse_else_if(struct TestContext* context)
+{
+ struct Lexer lexer = create_lexer(
+ "proc main\n{\nif rax == 0\nrdi = 1\nelse if rax == 1\nrdi = 2\n}\n");
+ struct Program program;
+
+ check(context, parse_program(&lexer, &program));
+
+ struct Statement branch = program.procs[0].body[0];
+ check(context, branch.kind == STATEMENT_IF);
+ check(context, branch.branch.body_count == 1);
+ check(context, branch.branch.else_count == 1);
+ check(context, branch.branch.else_body[0].kind == STATEMENT_IF);
+ check(context, primary_is(branch.branch.else_body[0].branch.right, "1"));
free_program(&program);
}
@@ -315,6 +355,8 @@ void run_parser_tests(struct TestContext* context)
test_parse_simple_statements(context);
test_parse_expressions(context);
test_parse_if(context);
+ test_parse_if_block(context);
+ test_parse_else_if(context);
test_parse_call(context);
test_parse_stack(context);
test_parse_sized_deref(context);