Ensure alias and import can be mixed

This commit is contained in:
2026-04-29 11:46:02 +02:00
parent f90cad2b96
commit 84747028f5
4 changed files with 117 additions and 96 deletions
+5 -8
View File
@@ -39,10 +39,7 @@ Module* parser_parse(TokenStream* ts) {
while (1) {
t = tokenstream_next(ts);
if (t.token != TOKEN_IMPORT) {
break;
}
if (t.token == TOKEN_IMPORT) {
ImportDeclaration* new_imports = realloc(module->imports, (module->import_count + 1) * sizeof(ImportDeclaration));
if (!new_imports) {
fprintf(stderr, "Out of memory\n");
@@ -76,9 +73,7 @@ Module* parser_parse(TokenStream* ts) {
parser_free(module);
return NULL;
}
}
while (t.token == TOKEN_ALIAS) {
} else if (t.token == TOKEN_ALIAS) {
AliasDeclaration* new_aliases = realloc(module->aliases, (module->alias_count + 1) * sizeof(AliasDeclaration));
if (!new_aliases) {
fprintf(stderr, "Out of memory\n");
@@ -139,7 +134,9 @@ Module* parser_parse(TokenStream* ts) {
parser_free(module);
return NULL;
}
t = tokenstream_next(ts);
} else {
break;
}
}
return module;
+1
View File
@@ -199,6 +199,7 @@ static TestCase s_tests[] = {
{"log_error", test_log_error},
{"log_on_line", test_log_on_line},
{"log_on_line_variadic", test_log_on_line_variadic},
{"parser_alias_and_import_mix", test_parser_alias_and_import_mix},
};
+14
View File
@@ -110,3 +110,17 @@ static void test_parser_alias_array(void) {
assert_true(valueType->builtin.isSigned, "expected signed");
}
static void test_parser_alias_and_import_mix(void) {
Module* m = test_get_ast();
assert_not_null(m, "expected module to be parsed");
assert_int(2, (int)m->import_count, "expected 2 imports");
assert_int(2, (int)m->alias_count, "expected 2 aliases");
assert_str("foo", m->imports[0].module_name, "expected import 1 name 'foo'");
assert_str("bar", m->imports[1].module_name, "expected import 2 name 'bar'");
assert_str("myalias", m->aliases[0].name, "expected alias 1 name 'myalias'");
assert_str("otheralias", m->aliases[1].name, "expected alias 2 name 'otheralias'");
parser_free(m);
}
+9
View File
@@ -0,0 +1,9 @@
module mymodule;
import foo;
alias myalias = int32[];
import bar;
alias otheralias = int32;