aboutsummaryrefslogtreecommitdiff
path: root/tests/codegen_test.c
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2026-08-30 17:57:24 +0200
committerhachem <im@hachem.wtf>2026-08-30 17:57:24 +0200
commitea2ebf063cc6a87c73068a3e8b06fb744655159f (patch)
treefa928314c3618262c48501265cb56bc7b00c4b66 /tests/codegen_test.c
parent542e15041ea1d93e74a06c588f16ce7956d6a9bb (diff)
add: start code gen
Diffstat (limited to 'tests/codegen_test.c')
-rw-r--r--tests/codegen_test.c37
1 files changed, 37 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);
+}