Refactor parser to C11 and update build configuration

This commit is contained in:
2026-04-29 10:38:34 +02:00
parent 15714393c3
commit 34b7939f76
2 changed files with 7 additions and 17 deletions
+6 -16
View File
@@ -5,13 +5,7 @@
#include <stdio.h>
Module* parser_parse(TokenStream* ts) {
/* Declarations first for C89 */
Token t;
Module* module;
ImportDeclaration* new_imports;
int is_public;
t = tokenstream_next(ts);
Token t = tokenstream_next(ts);
if (t.token != TOKEN_MODULE) {
log_on_line(&t.location, t.location.column_end, "expected 'module' keyword");
return NULL;
@@ -23,7 +17,7 @@ Module* parser_parse(TokenStream* ts) {
return NULL;
}
module = (Module*)malloc(sizeof(Module));
Module* module = (Module*)malloc(sizeof(Module));
if (module == NULL) {
fprintf(stderr, "Out of memory\n");
exit(1);
@@ -41,16 +35,13 @@ Module* parser_parse(TokenStream* ts) {
return NULL;
}
module->imports = NULL;
module->import_count = 0;
while (1) {
t = tokenstream_next(ts);
if (t.token != TOKEN_IMPORT) {
break;
}
new_imports = realloc(module->imports, (module->import_count + 1) * sizeof(ImportDeclaration));
ImportDeclaration* new_imports = realloc(module->imports, (module->import_count + 1) * sizeof(ImportDeclaration));
if (!new_imports) {
fprintf(stderr, "Out of memory\n");
exit(1);
@@ -58,9 +49,9 @@ Module* parser_parse(TokenStream* ts) {
module->imports = new_imports;
t = tokenstream_next(ts);
is_public = 0;
bool is_public = false;
if (t.token == TOKEN_IDENTIFIER && strncmp(t.text.data, "public", t.text.length) == 0) {
is_public = 1;
is_public = true;
t = tokenstream_next(ts);
}
@@ -70,8 +61,7 @@ Module* parser_parse(TokenStream* ts) {
return NULL;
}
module->imports[module->import_count].module_name = t;
module->imports[module->import_count].is_public = (bool)is_public;
module->imports[module->import_count] = (ImportDeclaration){ .module_name = t, .is_public = is_public };
module->import_count++;
t = tokenstream_next(ts);