From 9283b46a08a1c58c321b6c7ebad1353725c3bc47 Mon Sep 17 00:00:00 2001 From: hachem Date: Fri, 4 Sep 2026 20:00:52 +0200 Subject: 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. --- tests/codegen_test.c | 21 +++++++++++++++++++++ tests/parser_test.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) (limited to 'tests') 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); -- cgit v1.3