aboutsummaryrefslogtreecommitdiff
path: root/tests/sema_test.c
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2026-08-31 13:48:26 +0200
committerhachem <im@hachem.wtf>2026-08-31 13:48:26 +0200
commitcaf904c9a8a30a44fb3fca6df93e97c5cabdb0c0 (patch)
tree690c3eb05188fd067c9e1726d5332fe48f1c37f5 /tests/sema_test.c
parent81e960be2953a9953fd89c42cc6f71ce6ff54c85 (diff)
feat: validate undefined names/labels/calls in semantics
Diffstat (limited to 'tests/sema_test.c')
-rw-r--r--tests/sema_test.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/tests/sema_test.c b/tests/sema_test.c
index 5dbdfa7..095321d 100644
--- a/tests/sema_test.c
+++ b/tests/sema_test.c
@@ -51,6 +51,50 @@ static void test_duplicate_across_kinds(struct TestContext* context)
check(context, !analyze_source("data foo = \"a\"\nproc foo\n{\nsyscall\n}\nproc main\n{\nsyscall\n}\n"));
}
+static void test_undefined_name(struct TestContext* context)
+{
+ check(context, !analyze_source("proc main\n{\nrax = MISSING\n}\n"));
+}
+
+static void test_undefined_label(struct TestContext* context)
+{
+ check(context, !analyze_source("proc main\n{\ngoto nowhere\n}\n"));
+}
+
+static void test_undefined_call(struct TestContext* context)
+{
+ check(context, !analyze_source("proc main\n{\nnope(rax)\n}\n"));
+}
+
+static void test_call_arg_count(struct TestContext* context)
+{
+ check(context, !analyze_source("proc f(a: rdi)\n{\nsyscall\n}\nproc main\n{\nf(rax, rbx)\n}\n"));
+}
+
+static void test_assign_to_const(struct TestContext* context)
+{
+ check(context, !analyze_source("const K = 5\nproc main\n{\nK = 1\n}\n"));
+}
+
+static void test_references_resolve(struct TestContext* context)
+{
+ // registers, params, consts, data (+ .len), stack buffers, labels, calls
+ check(context, analyze_source(
+ "const N = 1\n"
+ "data msg = \"hi\"\n"
+ "proc helper(value: rdi)\n{\nrax = value\n}\n"
+ "proc main\n{\n"
+ "stack buf[8]\n"
+ "rax = N\n"
+ "rsi = msg\n"
+ "rdx = msg.len\n"
+ "rdi = buf + 1\n"
+ "helper(rax)\n"
+ "loop:\n"
+ "goto loop\n"
+ "}\n"));
+}
+
void run_sema_tests(struct TestContext* context)
{
test_valid_program(context);
@@ -59,4 +103,10 @@ void run_sema_tests(struct TestContext* context)
test_undefined_entry(context);
test_duplicate_const(context);
test_duplicate_across_kinds(context);
+ test_undefined_name(context);
+ test_undefined_label(context);
+ test_undefined_call(context);
+ test_call_arg_count(context);
+ test_assign_to_const(context);
+ test_references_resolve(context);
}