Refactor a bit

This commit is contained in:
bʰedoh₂ swé 2024-06-03 21:27:53 +05:00
parent 221d15fbf0
commit fced7e741f

View File

@ -6,13 +6,32 @@
#include "parser.h" #include "parser.h"
#include "extstring.h" #include "extstring.h"
struct SyntaxElement* se_init(void) {
struct SyntaxElement* syntaxelement = malloc(sizeof(struct SyntaxElement));
syntaxelement->type = NONE;
syntaxelement->content = NULL;
syntaxelement->next = NULL;
syntaxelement->top = NULL;
return syntaxelement;
}
struct SyntaxElement* se_bottom(struct SyntaxElement* syntaxelement) {
syntaxelement->content = se_init();
((struct SyntaxElement*)syntaxelement->content)->top = syntaxelement;
return ((struct SyntaxElement*)syntaxelement->content);
}
struct SyntaxElement* se_next(struct SyntaxElement* syntaxelement) {
syntaxelement->next = se_init();
syntaxelement->next->top = syntaxelement->top;
return syntaxelement->next;
}
struct SyntaxElement* parse(FILE* file) { struct SyntaxElement* parse(FILE* file) {
struct SyntaxElement* syntaxtree = malloc(sizeof(struct SyntaxElement)); struct SyntaxElement* syntaxtree = se_init();
struct SyntaxElement* syntaxtreestart = syntaxtree; struct SyntaxElement* syntaxtreestart = syntaxtree;
syntaxtree->type = TOPTREE; syntaxtree->type = TOPTREE;
syntaxtree->next = NULL;
syntaxtree->content = NULL;
syntaxtree->top = NULL;
char symbol; char symbol;
bool intree = false; bool intree = false;
bool incomment = false; bool incomment = false;
@ -40,7 +59,6 @@ struct SyntaxElement* parse(FILE* file) {
syntaxtree->content = sstring; syntaxtree->content = sstring;
es_free(string); es_free(string);
string = NULL; string = NULL;
continue;
} else { } else {
if (string == NULL) { if (string == NULL) {
string = malloc(sizeof(struct Extstring)); string = malloc(sizeof(struct Extstring));
@ -48,52 +66,40 @@ struct SyntaxElement* parse(FILE* file) {
continue; continue;
} }
es_addsymbol(string, symbol); es_addsymbol(string, symbol);
continue;
} }
continue;
} }
if (intoken) { if (intoken) {
if (isspace(symbol) || symbol == '(' || symbol == ')') { if (isspace(symbol) || symbol == '(' || symbol == ')') {
intoken = false; intoken = false;
syntaxtree->type = TOKEN; syntaxtree->type = TOKEN;
syntaxtree->content = es_tostring(token)->string; char* sstring = es_tostring(token)->string;
syntaxtree->content = sstring;
es_free(token); es_free(token);
token = NULL; token = NULL;
continue;
} else { } else {
es_addsymbol(token, symbol); es_addsymbol(token, symbol);
continue;
} }
continue;
} }
if (isspace(symbol)) if (isspace(symbol))
continue; continue;
if (intree) { if (intree) {
switch (symbol) { switch (symbol) {
case '(': case '(':
syntaxtree->content = malloc(sizeof(struct SyntaxElement)); syntaxtree = se_bottom(syntaxtree);
((struct SyntaxElement*)syntaxtree->content)->top = syntaxtree;
syntaxtree = syntaxtree->content;
syntaxtree->content = NULL;
syntaxtree->next = NULL;
syntaxtree->type = TREE; syntaxtree->type = TREE;
break; break;
case ')': case ')':
if (syntaxtree->top->type == TOPTREE) { if (syntaxtree->top->type == TOPTREE) {
intree = false; intree = false;
syntaxtree = syntaxtree->top; syntaxtree = syntaxtree->top;
syntaxtree->next = malloc(sizeof(struct SyntaxElement)); syntaxtree = se_next(syntaxtree);
syntaxtree = syntaxtree->next;
syntaxtree->type = TOPTREE; syntaxtree->type = TOPTREE;
syntaxtree->content = NULL;
syntaxtree->next = NULL;
syntaxtree->top = NULL;
} else { } else {
syntaxtree = syntaxtree->top; syntaxtree = syntaxtree->top;
syntaxtree->next = malloc(sizeof(struct SyntaxElement)); syntaxtree = se_next(syntaxtree);
syntaxtree->next->top = syntaxtree->top;
syntaxtree = syntaxtree->next;
syntaxtree->type = TREE; syntaxtree->type = TREE;
syntaxtree->content = NULL;
syntaxtree->next = NULL;
} }
break; break;
case ';': case ';':
@ -119,11 +125,8 @@ struct SyntaxElement* parse(FILE* file) {
continue; continue;
case '(': case '(':
intree = true; intree = true;
syntaxtree->content = malloc(sizeof(struct SyntaxElement));
((struct SyntaxElement*)syntaxtree->content)->top = syntaxtree; syntaxtree = se_bottom(syntaxtree);
syntaxtree = syntaxtree->content;
syntaxtree->content = NULL;
syntaxtree->next = NULL;
syntaxtree->type = TREE; syntaxtree->type = TREE;
continue; continue;
default: default: