Convert codebase to C89 compatibility and update test scripts

This commit is contained in:
2026-04-29 10:20:30 +02:00
parent 189c21667b
commit 146aa4d9d1
14 changed files with 287 additions and 192 deletions
+39 -31
View File
@@ -5,26 +5,32 @@
#include <stdio.h>
Module* parser_parse(TokenStream* ts) {
Token t = tokenstream_next(ts);
/* Declarations first for C89 */
Token t;
Module* module;
ImportDeclaration* new_imports;
int is_public;
t = tokenstream_next(ts);
if (t.token != TOKEN_MODULE) {
log_on_line(&t.location, t.location.column_end, "expected 'module' keyword");
return NULL;
}
t = tokenstream_next(ts);
if (t.token != TOKEN_IDENTIFIER) {
log_on_line(&t.location, t.location.column_end, "expected module name");
return NULL;
}
t = tokenstream_next(ts);
if (t.token != TOKEN_IDENTIFIER) {
log_on_line(&t.location, t.location.column_end, "expected module name");
return NULL;
}
Module* module = (Module*)malloc(sizeof(Module));
if (module == NULL) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
module->name = NULL;
module->imports = NULL;
module->import_count = 0;
module = (Module*)malloc(sizeof(Module));
if (module == NULL) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
module->name = NULL;
module->imports = NULL;
module->import_count = 0;
module->name = (char*)malloc(t.text.length + 1);
if (module->name == NULL) {
@@ -51,19 +57,19 @@ Module* parser_parse(TokenStream* ts) {
break;
}
ImportDeclaration* new_imports = realloc(module->imports, (module->import_count + 1) * sizeof(ImportDeclaration));
if (!new_imports) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
module->imports = new_imports;
new_imports = realloc(module->imports, (module->import_count + 1) * sizeof(ImportDeclaration));
if (!new_imports) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
module->imports = new_imports;
t = tokenstream_next(ts);
bool is_public = false;
if (t.token == TOKEN_IDENTIFIER && strncmp(t.text.data, "public", t.text.length) == 0) {
is_public = true;
t = tokenstream_next(ts);
}
is_public = 0;
if (t.token == TOKEN_IDENTIFIER && strncmp(t.text.data, "public", t.text.length) == 0) {
is_public = 1;
t = tokenstream_next(ts);
}
if (t.token != TOKEN_IDENTIFIER) {
log_on_line(&t.location, t.location.column_end, "expected module name to import");
@@ -94,12 +100,14 @@ Module* parser_parse(TokenStream* ts) {
void parser_free(Module* module) {
if (module == NULL) return;
if (module->imports != NULL) {
for (size_t i = 0; i < module->import_count; i++) {
free(module->imports[i].module_name);
}
free(module->imports);
}
if (module->imports != NULL) {
/* C89: declare index variable before the loop */
size_t i;
for (i = 0; i < module->import_count; i++) {
free(module->imports[i].module_name);
}
free(module->imports);
}
free(module->name);
free(module);
}