167 lines
3.3 KiB
C
167 lines
3.3 KiB
C
#include <stdlib.h>
|
|
#include <stdbool.h>
|
|
#include <string.h>
|
|
|
|
#include "syntax.h"
|
|
#include "process.h"
|
|
|
|
bool replace(tSyntaxElement* tree, tProcessingData* pdata) {
|
|
|
|
tProcessingData* pd = pdata;
|
|
|
|
bool didreplace = false;
|
|
|
|
while (1) {
|
|
|
|
if (pd->data.replace.replacethis == NULL)
|
|
break;
|
|
|
|
if (tree->type == TOKEN)
|
|
|
|
if (strcmp(tree->content.string, pd->data.replace.replacethis) == 0) {
|
|
|
|
didreplace = true;
|
|
|
|
free(tree->content.none);
|
|
|
|
tSyntaxElement* clone = se_clone_no_next(pd->data.replace.replacewiththis, tree);
|
|
tree->content = clone->content;
|
|
tree->type = clone->type;
|
|
|
|
free(clone);
|
|
|
|
}
|
|
|
|
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 (tree->content.syntax->type == TOKEN) {
|
|
|
|
tSyntaxElement* token = tree->content.syntax;
|
|
|
|
if (strcmp(token->content.string,"def") == 0 &&
|
|
token->next != NULL &&
|
|
token->next->next != NULL) {
|
|
|
|
char* replacethis = token->next->content.string;
|
|
|
|
tSyntaxElement* replacewiththis = token->next->next;
|
|
|
|
tProcessingData* npd = malloc(sizeof(tSyntaxElement));
|
|
|
|
npd->type = REPLACE;
|
|
npd->data.replace.replacethis = replacethis;
|
|
npd->data.replace.replacewiththis = replacewiththis;
|
|
npd->prev = pd;
|
|
|
|
*p_pdata = npd;
|
|
|
|
deffound = true;
|
|
|
|
} else if (strcmp(token->content.string,"fn") == 0 &&
|
|
token->next != NULL && token->next->type == TOKEN &&
|
|
token->next->next != NULL && se_istraversable(token->next->next) &&
|
|
token->next->next->next != NULL) {
|
|
|
|
tProcessingData* npd = malloc(sizeof(tSyntaxElement));
|
|
|
|
npd->type = FUNCTION;
|
|
|
|
npd->data.function.name = malloc(strlen(token->next->content.string));
|
|
strcpy(npd->data.function.name, token->next->content.string);
|
|
|
|
npd->data.function.body = se_clone(token->next->next->next, NULL);
|
|
|
|
// TODO: Check if all function's arguments are TOKENs
|
|
int size = 0;
|
|
for (tSyntaxElement* i = token->next->next->content.syntax; i != NULL; i = i->next)
|
|
size++;
|
|
|
|
// TODO: Fix memory leak
|
|
char** argv = calloc(size, sizeof(char*)); // This time there's actually should be an asterisk inside sizeof...
|
|
tSyntaxElement* ptr = token->next->next->content.syntax;
|
|
for (int i = 0; i < size; i++) {
|
|
argv[i] = malloc(strlen(ptr->content.string));
|
|
strcpy(argv[i], ptr->content.string);
|
|
ptr = ptr->next;
|
|
}
|
|
|
|
npd->data.function.argc = size;
|
|
npd->data.function.argv = argv;
|
|
|
|
*p_pdata = npd;
|
|
|
|
deffound = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return deffound;
|
|
|
|
}
|
|
|
|
|
|
void process(tSyntaxElement* tree, tProcessingData* pdata) {
|
|
|
|
tProcessingData* pd = pdata;
|
|
|
|
bool deffound = false;
|
|
|
|
tProcessingData* firstpd = NULL;
|
|
|
|
if (pd == NULL) {
|
|
|
|
pd = calloc(1,sizeof(tProcessingData));
|
|
firstpd = pd;
|
|
|
|
}
|
|
|
|
deffound = process_find(tree, &pd);
|
|
|
|
if (tree->next != NULL) {
|
|
process(tree->next, pd);
|
|
}
|
|
|
|
if (!deffound) {
|
|
|
|
if (se_istraversable(tree))
|
|
process(tree->content.syntax, pd);
|
|
|
|
switch (pd->type) {
|
|
|
|
case REPLACE:
|
|
replace(tree, pd);
|
|
break;
|
|
|
|
case FUNCTION: // TODO
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pd != pdata)
|
|
free(pd);
|
|
|
|
if (firstpd != pd && firstpd != NULL)
|
|
free(firstpd);
|
|
|
|
}
|