Implement parser module and update AST

This commit is contained in:
2026-04-24 14:50:54 +02:00
parent 2d1ccd52e6
commit a173e37adc
6 changed files with 78 additions and 48 deletions
+45
View File
@@ -0,0 +1,45 @@
#include "parser.h"
#include <stdlib.h>
#include <string.h>
Module* parser_parse(TokenStream* ts) {
Token t = tokenstream_next(ts);
if (t != TOKEN_MODULE) {
return NULL;
}
t = tokenstream_next(ts);
if (t != TOKEN_IDENTIFIER) {
return NULL;
}
TokenInfo info;
tokenstream_info(ts, &info);
Module* module = (Module*)malloc(sizeof(Module));
if (module == NULL) return NULL;
module->name = (char*)malloc(info.text_length + 1);
if (module->name == NULL) {
free(module);
return NULL;
}
memcpy(module->name, info.text, info.text_length);
module->name[info.text_length] = '\0';
t = tokenstream_next(ts);
if (t != TOKEN_SEMICOLON) {
free(module->name);
free(module);
return NULL;
}
return module;
}
void parser_free(Module* module) {
if (module == NULL) return;
free(module->name);
free(module);
}