Make parser clean NONEs

This commit is contained in:
bʰedoh₂ swé 2024-06-03 22:20:51 +05:00
parent fced7e741f
commit 4c1f95370c

View File

@ -28,6 +28,31 @@ struct SyntaxElement* se_next(struct SyntaxElement* syntaxelement) {
return syntaxelement->next; return syntaxelement->next;
} }
void se_clean(struct SyntaxElement* syntaxtree) {
if (syntaxtree->next != NULL) {
switch (syntaxtree->next->type) {
case NONE:
free(syntaxtree->next);
syntaxtree->next = NULL;
break;
default:
se_clean(syntaxtree->next);
break;
}
}
if (syntaxtree->content != NULL && (syntaxtree->type == TOPTREE || syntaxtree->type == TREE)) {
switch (((struct SyntaxElement*)syntaxtree->content)->type) {
case NONE:
free(syntaxtree->content);
syntaxtree->content = NULL;
break;
default:
se_clean((struct SyntaxElement*)syntaxtree->content);
break;
}
}
}
struct SyntaxElement* parse(FILE* file) { struct SyntaxElement* parse(FILE* file) {
struct SyntaxElement* syntaxtree = se_init(); struct SyntaxElement* syntaxtree = se_init();
struct SyntaxElement* syntaxtreestart = syntaxtree; struct SyntaxElement* syntaxtreestart = syntaxtree;
@ -59,6 +84,7 @@ struct SyntaxElement* parse(FILE* file) {
syntaxtree->content = sstring; syntaxtree->content = sstring;
es_free(string); es_free(string);
string = NULL; string = NULL;
syntaxtree = se_next(syntaxtree);
} else { } else {
if (string == NULL) { if (string == NULL) {
string = malloc(sizeof(struct Extstring)); string = malloc(sizeof(struct Extstring));
@ -77,10 +103,11 @@ struct SyntaxElement* parse(FILE* file) {
syntaxtree->content = sstring; syntaxtree->content = sstring;
es_free(token); es_free(token);
token = NULL; token = NULL;
syntaxtree = se_next(syntaxtree);
} else { } else {
es_addsymbol(token, symbol); es_addsymbol(token, symbol);
continue;
} }
continue;
} }
if (isspace(symbol)) if (isspace(symbol))
continue; continue;
@ -88,7 +115,6 @@ struct SyntaxElement* parse(FILE* file) {
switch (symbol) { switch (symbol) {
case '(': case '(':
syntaxtree = se_bottom(syntaxtree); syntaxtree = se_bottom(syntaxtree);
syntaxtree->type = TREE;
break; break;
case ')': case ')':
if (syntaxtree->top->type == TOPTREE) { if (syntaxtree->top->type == TOPTREE) {
@ -148,5 +174,6 @@ struct SyntaxElement* parse(FILE* file) {
} }
syntaxtree = syntaxtree->next; syntaxtree = syntaxtree->next;
} }
se_clean(syntaxtreestart);
return syntaxtreestart; return syntaxtreestart;
} }