diff options
| author | hachem <im@hachem.wtf> | 2026-08-30 17:57:24 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2026-08-30 17:57:24 +0200 |
| commit | ea2ebf063cc6a87c73068a3e8b06fb744655159f (patch) | |
| tree | fa928314c3618262c48501265cb56bc7b00c4b66 /tests | |
| parent | 542e15041ea1d93e74a06c588f16ce7956d6a9bb (diff) | |
add: start code gen
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/codegen_test.c | 37 | ||||
| -rw-r--r-- | tests/main.c | 1 | ||||
| -rw-r--r-- | tests/tests.h | 1 |
3 files changed, 39 insertions, 0 deletions
diff --git a/tests/codegen_test.c b/tests/codegen_test.c new file mode 100644 index 0000000..64421ea --- /dev/null +++ b/tests/codegen_test.c @@ -0,0 +1,37 @@ +#include <stdio.h> +#include <string.h> + +#include "parser/parser.h" +#include "codegen/nasm.h" +#include "tests.h" + +static void test_generate_consts_and_data(struct TestContext* context) +{ + struct Lexer lexer = create_lexer("const N = 5\ndata msg = \"hi\"\n"); + struct Program program; + check(context, parse_program(&lexer, &program)); + + FILE* out = tmpfile(); + generate_nasm(&program, out); + fflush(out); + rewind(out); + + char buffer[1024]; + size_t read = fread(buffer, 1, sizeof(buffer) - 1, out); + buffer[read] = '\0'; + fclose(out); + + check(context, strstr(buffer, "N equ 5") != NULL); + check(context, strstr(buffer, "section .data") != NULL); + check(context, strstr(buffer, "msg: db `hi`") != NULL); + check(context, strstr(buffer, ".len equ $ - msg") != NULL); + check(context, strstr(buffer, "section .text") != NULL); + check(context, strstr(buffer, "global _start") != NULL); + + free_program(&program); +} + +void run_codegen_tests(struct TestContext* context) +{ + test_generate_consts_and_data(context); +} diff --git a/tests/main.c b/tests/main.c index b69bb3e..f0caf52 100644 --- a/tests/main.c +++ b/tests/main.c @@ -8,6 +8,7 @@ int main(void) run_lexer_tests(&context); run_parser_tests(&context); + run_codegen_tests(&context); printf("\n%d checks, %d failure(s)\n", context.checks, context.failures); return context.failures == 0 ? 0 : 1; diff --git a/tests/tests.h b/tests/tests.h index 9136910..32ee4a6 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -4,3 +4,4 @@ void run_lexer_tests(struct TestContext* context); void run_parser_tests(struct TestContext* context); +void run_codegen_tests(struct TestContext* context); |
