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