Refactor parser to use Token in AST and update tests

This commit is contained in:
2026-04-29 10:35:12 +02:00
parent 146aa4d9d1
commit 15714393c3
3 changed files with 96 additions and 38 deletions
+31 -7
View File
@@ -1,13 +1,19 @@
#include "test.h"
#include "parser.h"
#include <string.h>
#include <stdlib.h>
static void test_parser_module_name(void) {
TokenStream* ts = tokenstream_open("test.c", "module my_module;");
Module* m = parser_parse(ts);
char* buf;
assert_not_null(m, "expected module to be parsed");
assert_str("my_module", m->name, "expected name 'my_module'");
buf = malloc(m->name.text.length + 1);
memcpy(buf, m->name.text.data, m->name.text.length);
buf[m->name.text.length] = '\0';
assert_str("my_module", buf, "expected name 'my_module'");
free(buf);
parser_free(m);
tokenstream_close(ts);
@@ -56,13 +62,22 @@ static void test_parser_bad_import_name(void) {
static void test_parser_imports(void) {
TokenStream* ts = tokenstream_open("test.c", "module my_module; import other_module;");
Module* m = parser_parse(ts);
char* buf;
assert_not_null(m, "expected module to be parsed");
assert_str("my_module", m->name, "expected name 'my_module'");
buf = malloc(m->name.text.length + 1);
memcpy(buf, m->name.text.data, m->name.text.length);
buf[m->name.text.length] = '\0';
assert_str("my_module", buf, "expected name 'my_module'");
free(buf);
assert_not_null(m->imports, "expected imports to be parsed");
assert_int(1, m->import_count, "expected one import");
assert_str("other_module", m->imports[0].module_name, "expected import name 'other_module'");
assert_int(1, (int)m->import_count, "expected one import");
buf = malloc(m->imports[0].module_name.text.length + 1);
memcpy(buf, m->imports[0].module_name.text.data, m->imports[0].module_name.text.length);
buf[m->imports[0].module_name.text.length] = '\0';
assert_str("other_module", buf, "expected import name 'other_module'");
free(buf);
assert_false(m->imports[0].is_public, "expected import to not be public");
parser_free(m);
@@ -72,13 +87,22 @@ static void test_parser_imports(void) {
static void test_parser_public_imports(void) {
TokenStream* ts = tokenstream_open("test.c", "module my_module; import public other_module;");
Module* m = parser_parse(ts);
char* buf;
assert_not_null(m, "expected module to be parsed");
assert_str("my_module", m->name, "expected name 'my_module'");
buf = malloc(m->name.text.length + 1);
memcpy(buf, m->name.text.data, m->name.text.length);
buf[m->name.text.length] = '\0';
assert_str("my_module", buf, "expected name 'my_module'");
free(buf);
assert_not_null(m->imports, "expected imports to be parsed");
assert_int(1, m->import_count, "expected one import");
assert_str("other_module", m->imports[0].module_name, "expected import name 'other_module'");
assert_int(1, (int)m->import_count, "expected one import");
buf = malloc(m->imports[0].module_name.text.length + 1);
memcpy(buf, m->imports[0].module_name.text.data, m->imports[0].module_name.text.length);
buf[m->imports[0].module_name.text.length] = '\0';
assert_str("other_module", buf, "expected import name 'other_module'");
free(buf);
assert_true(m->imports[0].is_public, "expected import to be public");
parser_free(m);