A bit of refactoring

This commit is contained in:
bʰedoh₂ swé 2024-06-09 23:50:10 +05:00
parent 743bda0437
commit eedecc7b51

View File

@ -5,12 +5,31 @@
#include "syntax.h" #include "syntax.h"
#include "process.h" #include "process.h"
void process(tSyntaxElement* tree, tProcessingData* pdata) { bool replace(tSyntaxElement* tree, tProcessingData* pdata) {
tProcessingData* pd = pdata; tProcessingData* pd = pdata;
bool deffound = false; bool didreplace = false;
if (pd == NULL) { while (1) {
pd = calloc(1,sizeof(tProcessingData)); if (pd->replacethis == NULL)
break;
if (tree->type == TOKEN)
if (strcmp(tree->content, pd->replacethis) == 0) {
didreplace = true;
free(tree->content);
tSyntaxElement* clone = se_clone_no_next(pd->replacewiththis, tree);
tree->content = clone->content;
tree->type = clone->type;
}
if (pd->prev != NULL)
pd = pd->prev;
else
break;
} }
return didreplace;
}
bool process_find(tSyntaxElement* tree, tProcessingData** p_pdata) {
tProcessingData* pd = *p_pdata;
bool deffound = false;
if (se_istraversable(tree)) if (se_istraversable(tree))
if (((tSyntaxElement*)tree->content)->type == TOKEN && ((tSyntaxElement*)tree->content)->next != NULL) { if (((tSyntaxElement*)tree->content)->type == TOKEN && ((tSyntaxElement*)tree->content)->next != NULL) {
tSyntaxElement* token = (tSyntaxElement*)tree->content; tSyntaxElement* token = (tSyntaxElement*)tree->content;
@ -21,10 +40,20 @@ void process(tSyntaxElement* tree, tProcessingData* pdata) {
npd->replacethis = replacethis; npd->replacethis = replacethis;
npd->replacewiththis = replacewiththis; npd->replacewiththis = replacewiththis;
npd->prev = pd; npd->prev = pd;
pd = npd; *p_pdata = npd;
deffound = true; deffound = true;
} }
} }
return deffound;
}
void process(tSyntaxElement* tree, tProcessingData* pdata) {
tProcessingData* pd = pdata;
bool deffound = false;
if (pd == NULL) {
pd = calloc(1,sizeof(tProcessingData));
}
deffound = process_find(tree, &pd);
if (tree->next != NULL) { if (tree->next != NULL) {
process(tree->next, pd); process(tree->next, pd);
} }
@ -32,20 +61,6 @@ void process(tSyntaxElement* tree, tProcessingData* pdata) {
if (se_istraversable(tree)) { if (se_istraversable(tree)) {
process((tSyntaxElement*)tree->content, pd); process((tSyntaxElement*)tree->content, pd);
} }
while (1) { replace(tree, pd);
if (pd->replacethis == NULL)
break;
if (tree->type == TOKEN)
if (strcmp(tree->content, pd->replacethis) == 0) {
free(tree->content);
tSyntaxElement* clone = se_clone_no_next(pd->replacewiththis, tree);
tree->content = clone->content;
tree->type = clone->type;
}
if (pd->prev != NULL)
pd = pd->prev;
else
break;
}
} }
} }