Update ast interface
This commit is contained in:
+19
-5
@@ -22,7 +22,9 @@ Module* parser_parse(TokenStream* ts) {
|
||||
fprintf(stderr, "Out of memory\n");
|
||||
exit(1);
|
||||
}
|
||||
module->name = t;
|
||||
module->name = (const char*)malloc(t.text.length + 1);
|
||||
memcpy((void*)module->name, t.text.data, t.text.length);
|
||||
((char*)module->name)[t.text.length] = '\0';
|
||||
module->imports = NULL;
|
||||
module->import_count = 0;
|
||||
module->aliases = NULL;
|
||||
@@ -60,8 +62,12 @@ Module* parser_parse(TokenStream* ts) {
|
||||
parser_free(module);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char* name = (char*)malloc(t.text.length + 1);
|
||||
memcpy(name, t.text.data, t.text.length);
|
||||
name[t.text.length] = '\0';
|
||||
|
||||
module->imports[module->import_count] = (ImportDeclaration){ .module_name = t, .is_public = is_public };
|
||||
module->imports[module->import_count] = (ImportDeclaration){ .module_name = name, .is_public = is_public };
|
||||
module->import_count++;
|
||||
|
||||
t = tokenstream_next(ts);
|
||||
@@ -72,8 +78,6 @@ Module* parser_parse(TokenStream* ts) {
|
||||
}
|
||||
}
|
||||
|
||||
// Now t holds the first non-import token. If it's not TOKEN_ALIAS, return.
|
||||
// If it is, process it.
|
||||
while (t.token == TOKEN_ALIAS) {
|
||||
AliasDeclaration* new_aliases = realloc(module->aliases, (module->alias_count + 1) * sizeof(AliasDeclaration));
|
||||
if (!new_aliases) {
|
||||
@@ -88,8 +92,11 @@ Module* parser_parse(TokenStream* ts) {
|
||||
parser_free(module);
|
||||
return NULL;
|
||||
}
|
||||
char* name = (char*)malloc(t.text.length + 1);
|
||||
memcpy(name, t.text.data, t.text.length);
|
||||
name[t.text.length] = '\0';
|
||||
AliasDeclaration alias;
|
||||
alias.name = t;
|
||||
alias.name = name;
|
||||
|
||||
t = tokenstream_next(ts);
|
||||
if (t.token != TOKEN_ASSIGN) {
|
||||
@@ -124,10 +131,17 @@ 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((void*)module->imports[i].module_name);
|
||||
}
|
||||
free(module->imports);
|
||||
}
|
||||
if (module->aliases != NULL) {
|
||||
for(size_t i = 0; i < module->alias_count; i++) {
|
||||
free((void*)module->aliases[i].name);
|
||||
}
|
||||
free(module->aliases);
|
||||
}
|
||||
free((void*)module->name);
|
||||
free(module);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user