Function parsing

This commit is contained in:
bʰedoh₂ swé 2024-06-10 19:14:53 +05:00
parent a7aefd33ad
commit f0e6936008
2 changed files with 38 additions and 1 deletions

View File

@ -74,6 +74,41 @@ bool process_find(tSyntaxElement* tree, tProcessingData** p_pdata) {
deffound = true; 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;
} }
} }

View File

@ -19,7 +19,9 @@ typedef struct {
typedef struct { typedef struct {
char** args; char* name;
int argc;
char** argv;
tSyntaxElement* body; tSyntaxElement* body;
} tProcessingFunction; } tProcessingFunction;