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