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