Try (unsuccessfully) to make code a bit more readable

This commit is contained in:
bʰedoh₂ swé 2024-06-03 23:23:57 +05:00
parent 512a86ed48
commit b4243755ef

View File

@ -6,7 +6,7 @@
#include "parser.h"
#include "extstring.h"
struct SyntaxElement* se_init(void) {
struct SyntaxElement* se_create(void) {
struct SyntaxElement* syntaxelement = malloc(sizeof(struct SyntaxElement));
syntaxelement->type = NONE;
syntaxelement->content = NULL;
@ -17,13 +17,13 @@ struct SyntaxElement* se_init(void) {
}
struct SyntaxElement* se_bottom(struct SyntaxElement* syntaxelement) {
syntaxelement->content = se_init();
syntaxelement->content = se_create();
((struct SyntaxElement*)syntaxelement->content)->top = syntaxelement;
return ((struct SyntaxElement*)syntaxelement->content);
}
struct SyntaxElement* se_next(struct SyntaxElement* syntaxelement) {
syntaxelement->next = se_init();
syntaxelement->next = se_create();
syntaxelement->next->top = syntaxelement->top;
return syntaxelement->next;
}
@ -49,6 +49,7 @@ void se_free(struct SyntaxElement* syntaxtree) {
free(syntaxtree);
}
// Remove NONEs from the syntax tree.
void se_clean(struct SyntaxElement* syntaxtree) {
if (syntaxtree->next != NULL) {
switch (syntaxtree->next->type) {
@ -75,7 +76,7 @@ void se_clean(struct SyntaxElement* syntaxtree) {
}
struct SyntaxElement* parse(FILE* file) {
struct SyntaxElement* syntaxtree = se_init();
struct SyntaxElement* syntaxtree = se_create();
struct SyntaxElement* syntaxtreestart = syntaxtree;
syntaxtree->type = TOPTREE;
char symbol;
@ -83,8 +84,8 @@ struct SyntaxElement* parse(FILE* file) {
bool incomment = false;
bool instring = false;
bool intoken = false;
struct Extstring* token = NULL;
struct Extstring* string = NULL;
struct Extstring* exttoken = NULL;
struct Extstring* extstring = NULL;
while (1) {
symbol = fgetc(file);
if (feof(file))
@ -96,25 +97,25 @@ struct SyntaxElement* parse(FILE* file) {
}
if (instring) {
if (symbol == '"') {
if (string == NULL) {
string = malloc(sizeof(struct Extstring));
string->next = NULL;
if (extstring == NULL) {
extstring = malloc(sizeof(struct Extstring));
extstring->next = NULL;
}
instring = false;
syntaxtree->type = STRING;
char* sstring = es_tostring(string);
syntaxtree->content = sstring;
es_free(string);
string = NULL;
char* string = es_tostring(extstring);
syntaxtree->content = string;
es_free(extstring);
extstring = NULL;
syntaxtree = se_next(syntaxtree);
} else {
if (string == NULL) {
string = malloc(sizeof(struct Extstring));
string->symbol = symbol;
string->next = NULL;
if (extstring == NULL) {
extstring = malloc(sizeof(struct Extstring));
extstring->symbol = symbol;
extstring->next = NULL;
continue;
}
es_addsymbol(string, symbol);
es_addsymbol(extstring, symbol);
}
continue;
}
@ -122,13 +123,13 @@ struct SyntaxElement* parse(FILE* file) {
if (isspace(symbol) || symbol == '(' || symbol == ')') {
intoken = false;
syntaxtree->type = TOKEN;
char* sstring = es_tostring(token);
syntaxtree->content = sstring;
es_free(token);
token = NULL;
char* string = es_tostring(exttoken);
syntaxtree->content = string;
es_free(exttoken);
exttoken = NULL;
syntaxtree = se_next(syntaxtree);
} else {
es_addsymbol(token, symbol);
es_addsymbol(exttoken, symbol);
continue;
}
}
@ -144,7 +145,7 @@ struct SyntaxElement* parse(FILE* file) {
intree = false;
syntaxtree = syntaxtree->top;
syntaxtree = se_next(syntaxtree);
syntaxtree->type = TOPTREE;
syntaxtree->type = NONE;
} else {
syntaxtree = syntaxtree->top;
syntaxtree = se_next(syntaxtree);
@ -158,9 +159,9 @@ struct SyntaxElement* parse(FILE* file) {
instring = true;
continue;
default:
token = malloc(sizeof(struct Extstring));
token->symbol = symbol;
token->next = NULL;
exttoken = malloc(sizeof(struct Extstring));
exttoken->symbol = symbol;
exttoken->next = NULL;
intoken = true;
break;
}
@ -175,7 +176,7 @@ struct SyntaxElement* parse(FILE* file) {
continue;
case '(':
intree = true;
syntaxtree->type = TOPTREE;
syntaxtree = se_bottom(syntaxtree);
syntaxtree->type = TREE;
continue;
@ -189,15 +190,6 @@ struct SyntaxElement* parse(FILE* file) {
fprintf(stderr, "Unexpected EOF.\n");
return NULL;
}
syntaxtree = syntaxtreestart;
while (1) {
if (syntaxtree->next->next == NULL) {
free(syntaxtree->next);
syntaxtree->next = NULL;
break;
}
syntaxtree = syntaxtree->next;
}
se_clean(syntaxtreestart);
return syntaxtreestart;
}