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,38 +5,15 @@
#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) {
pd = calloc(1,sizeof(tProcessingData));
}
if (se_istraversable(tree))
if (((tSyntaxElement*)tree->content)->type == TOKEN && ((tSyntaxElement*)tree->content)->next != NULL) {
tSyntaxElement* token = (tSyntaxElement*)tree->content;
if (strcmp((char*)token->content,"def") == 0) {
char* replacethis = token->next->content;
tSyntaxElement* replacewiththis = token->next->next;
tProcessingData* npd = malloc(sizeof(tSyntaxElement));
npd->replacethis = replacethis;
npd->replacewiththis = replacewiththis;
npd->prev = pd;
pd = npd;
deffound = true;
}
}
if (tree->next != NULL) {
process(tree->next, pd);
}
if (!deffound) {
if (se_istraversable(tree)) {
process((tSyntaxElement*)tree->content, pd);
}
while (1) { while (1) {
if (pd->replacethis == NULL) if (pd->replacethis == NULL)
break; break;
if (tree->type == TOKEN) if (tree->type == TOKEN)
if (strcmp(tree->content, pd->replacethis) == 0) { if (strcmp(tree->content, pd->replacethis) == 0) {
didreplace = true;
free(tree->content); free(tree->content);
tSyntaxElement* clone = se_clone_no_next(pd->replacewiththis, tree); tSyntaxElement* clone = se_clone_no_next(pd->replacewiththis, tree);
tree->content = clone->content; tree->content = clone->content;
@ -47,5 +24,43 @@ void process(tSyntaxElement* tree, tProcessingData* pdata) {
else else
break; break;
} }
return didreplace;
}
bool process_find(tSyntaxElement* tree, tProcessingData** p_pdata) {
tProcessingData* pd = *p_pdata;
bool deffound = false;
if (se_istraversable(tree))
if (((tSyntaxElement*)tree->content)->type == TOKEN && ((tSyntaxElement*)tree->content)->next != NULL) {
tSyntaxElement* token = (tSyntaxElement*)tree->content;
if (strcmp((char*)token->content,"def") == 0) {
char* replacethis = token->next->content;
tSyntaxElement* replacewiththis = token->next->next;
tProcessingData* npd = malloc(sizeof(tSyntaxElement));
npd->replacethis = replacethis;
npd->replacewiththis = replacewiththis;
npd->prev = pd;
*p_pdata = npd;
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) {
process(tree->next, pd);
}
if (!deffound) {
if (se_istraversable(tree)) {
process((tSyntaxElement*)tree->content, pd);
}
replace(tree, pd);
} }
} }