Refactor parser to use Token in AST and update tests
This commit is contained in:
+8
-24
@@ -28,18 +28,11 @@ Module* parser_parse(TokenStream* ts) {
|
||||
fprintf(stderr, "Out of memory\n");
|
||||
exit(1);
|
||||
}
|
||||
module->name = NULL;
|
||||
module->name = t;
|
||||
module->imports = NULL;
|
||||
module->import_count = 0;
|
||||
|
||||
module->name = (char*)malloc(t.text.length + 1);
|
||||
if (module->name == NULL) {
|
||||
fprintf(stderr, "Out of memory\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
memcpy(module->name, t.text.data, t.text.length);
|
||||
module->name[t.text.length] = '\0';
|
||||
module->aliases = NULL;
|
||||
module->alias_count = 0;
|
||||
|
||||
t = tokenstream_next(ts);
|
||||
if (t.token != TOKEN_SEMICOLON) {
|
||||
@@ -77,14 +70,8 @@ Module* parser_parse(TokenStream* ts) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
module->imports[module->import_count].module_name = (char*)malloc(t.text.length + 1);
|
||||
if (!module->imports[module->import_count].module_name) {
|
||||
fprintf(stderr, "Out of memory\n");
|
||||
exit(1);
|
||||
}
|
||||
memcpy(module->imports[module->import_count].module_name, t.text.data, t.text.length);
|
||||
module->imports[module->import_count].module_name[t.text.length] = '\0';
|
||||
module->imports[module->import_count].is_public = is_public;
|
||||
module->imports[module->import_count].module_name = t;
|
||||
module->imports[module->import_count].is_public = (bool)is_public;
|
||||
module->import_count++;
|
||||
|
||||
t = tokenstream_next(ts);
|
||||
@@ -101,13 +88,10 @@ Module* parser_parse(TokenStream* ts) {
|
||||
void parser_free(Module* module) {
|
||||
if (module == NULL) return;
|
||||
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);
|
||||
if (module->aliases != NULL) {
|
||||
free(module->aliases);
|
||||
}
|
||||
free(module);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user