Funny C unions
This commit is contained in:
parent
eedecc7b51
commit
95862b56d2
@ -45,7 +45,7 @@ tSyntaxElement* parse(FILE* file) {
|
|||||||
instring = false;
|
instring = false;
|
||||||
syntaxtree->type = STRING;
|
syntaxtree->type = STRING;
|
||||||
char* string = es_tostring(extstring);
|
char* string = es_tostring(extstring);
|
||||||
syntaxtree->content = string;
|
syntaxtree->content.string = string;
|
||||||
es_free(extstring);
|
es_free(extstring);
|
||||||
extstring = NULL;
|
extstring = NULL;
|
||||||
} else {
|
} else {
|
||||||
@ -64,7 +64,7 @@ tSyntaxElement* parse(FILE* file) {
|
|||||||
intoken = false;
|
intoken = false;
|
||||||
syntaxtree->type = TOKEN;
|
syntaxtree->type = TOKEN;
|
||||||
char* string = es_tostring(exttoken);
|
char* string = es_tostring(exttoken);
|
||||||
syntaxtree->content = string;
|
syntaxtree->content.string = string;
|
||||||
es_free(exttoken);
|
es_free(exttoken);
|
||||||
exttoken = NULL;
|
exttoken = NULL;
|
||||||
} else {
|
} else {
|
||||||
|
@ -12,13 +12,13 @@ void printtree(tSyntaxElement* syntaxtree, int depth) {
|
|||||||
switch (syntaxtree->type) {
|
switch (syntaxtree->type) {
|
||||||
case TREE:
|
case TREE:
|
||||||
printf("TREE\n");
|
printf("TREE\n");
|
||||||
printtree((tSyntaxElement*)syntaxtree->content, depth + 1);
|
printtree(syntaxtree->content.syntax, depth + 1);
|
||||||
break;
|
break;
|
||||||
case TOKEN:
|
case TOKEN:
|
||||||
printf("TOKEN: %s\n", (char*)syntaxtree->content);
|
printf("TOKEN: %s\n", syntaxtree->content.string);
|
||||||
break;
|
break;
|
||||||
case STRING:
|
case STRING:
|
||||||
printf("STRING: %s\n", (char*)syntaxtree->content);
|
printf("STRING: %s\n", syntaxtree->content.string);
|
||||||
break;
|
break;
|
||||||
case NONE:
|
case NONE:
|
||||||
printf("NONE\n");
|
printf("NONE\n");
|
||||||
|
@ -9,13 +9,13 @@ bool replace(tSyntaxElement* tree, tProcessingData* pdata) {
|
|||||||
tProcessingData* pd = pdata;
|
tProcessingData* pd = pdata;
|
||||||
bool didreplace = false;
|
bool didreplace = false;
|
||||||
while (1) {
|
while (1) {
|
||||||
if (pd->replacethis == NULL)
|
if (pd->data.replace.replacethis == NULL)
|
||||||
break;
|
break;
|
||||||
if (tree->type == TOKEN)
|
if (tree->type == TOKEN)
|
||||||
if (strcmp(tree->content, pd->replacethis) == 0) {
|
if (strcmp(tree->content.string, pd->data.replace.replacethis) == 0) {
|
||||||
didreplace = true;
|
didreplace = true;
|
||||||
free(tree->content);
|
free(tree->content.string);
|
||||||
tSyntaxElement* clone = se_clone_no_next(pd->replacewiththis, tree);
|
tSyntaxElement* clone = se_clone_no_next(pd->data.replace.replacewiththis, tree);
|
||||||
tree->content = clone->content;
|
tree->content = clone->content;
|
||||||
tree->type = clone->type;
|
tree->type = clone->type;
|
||||||
}
|
}
|
||||||
@ -31,14 +31,15 @@ bool process_find(tSyntaxElement* tree, tProcessingData** p_pdata) {
|
|||||||
tProcessingData* pd = *p_pdata;
|
tProcessingData* pd = *p_pdata;
|
||||||
bool deffound = false;
|
bool deffound = false;
|
||||||
if (se_istraversable(tree))
|
if (se_istraversable(tree))
|
||||||
if (((tSyntaxElement*)tree->content)->type == TOKEN && ((tSyntaxElement*)tree->content)->next != NULL) {
|
if (tree->content.syntax->type == TOKEN && tree->content.syntax->next != NULL) {
|
||||||
tSyntaxElement* token = (tSyntaxElement*)tree->content;
|
tSyntaxElement* token = tree->content.syntax;
|
||||||
if (strcmp((char*)token->content,"def") == 0) {
|
if (strcmp(token->content.string,"def") == 0) {
|
||||||
char* replacethis = token->next->content;
|
char* replacethis = token->next->content.string;
|
||||||
tSyntaxElement* replacewiththis = token->next->next;
|
tSyntaxElement* replacewiththis = token->next->next;
|
||||||
tProcessingData* npd = malloc(sizeof(tSyntaxElement));
|
tProcessingData* npd = malloc(sizeof(tSyntaxElement));
|
||||||
npd->replacethis = replacethis;
|
npd->type = REPLACE;
|
||||||
npd->replacewiththis = replacewiththis;
|
npd->data.replace.replacethis = replacethis;
|
||||||
|
npd->data.replace.replacewiththis = replacewiththis;
|
||||||
npd->prev = pd;
|
npd->prev = pd;
|
||||||
*p_pdata = npd;
|
*p_pdata = npd;
|
||||||
deffound = true;
|
deffound = true;
|
||||||
@ -59,8 +60,14 @@ void process(tSyntaxElement* tree, tProcessingData* pdata) {
|
|||||||
}
|
}
|
||||||
if (!deffound) {
|
if (!deffound) {
|
||||||
if (se_istraversable(tree)) {
|
if (se_istraversable(tree)) {
|
||||||
process((tSyntaxElement*)tree->content, pd);
|
process(tree->content.syntax, pd);
|
||||||
}
|
}
|
||||||
|
switch (pd->type) {
|
||||||
|
case REPLACE:
|
||||||
replace(tree, pd);
|
replace(tree, pd);
|
||||||
|
break;
|
||||||
|
case FUNCTION:
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,34 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include "syntax.h"
|
#include "syntax.h"
|
||||||
|
|
||||||
typedef struct ProcessingData {
|
typedef enum {
|
||||||
struct ProcessingData* prev;
|
REPLACE,
|
||||||
|
FUNCTION
|
||||||
|
} tProcessingType;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
char* replacethis;
|
char* replacethis;
|
||||||
tSyntaxElement* replacewiththis;
|
tSyntaxElement* replacewiththis;
|
||||||
|
} tProcessingReplace;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char** args;
|
||||||
|
tSyntaxElement* body;
|
||||||
|
} tProcessingFunction;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
tProcessingReplace replace;
|
||||||
|
tProcessingFunction function;
|
||||||
|
} tuProcessingData;
|
||||||
|
|
||||||
|
typedef struct ProcessingData {
|
||||||
|
struct ProcessingData* prev;
|
||||||
|
tProcessingType type;
|
||||||
|
tuProcessingData data;
|
||||||
} tProcessingData;
|
} tProcessingData;
|
||||||
|
|
||||||
void process(tSyntaxElement*, tProcessingData*);
|
void process(tSyntaxElement*, tProcessingData*);
|
||||||
|
24
src/syntax.c
24
src/syntax.c
@ -4,13 +4,13 @@
|
|||||||
#include "syntax.h"
|
#include "syntax.h"
|
||||||
|
|
||||||
bool se_istraversable(tSyntaxElement* syntaxtree) {
|
bool se_istraversable(tSyntaxElement* syntaxtree) {
|
||||||
return syntaxtree->type == TREE && syntaxtree->content != NULL;
|
return syntaxtree->type == TREE && syntaxtree->content.syntax != NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
tSyntaxElement* se_create(void) {
|
tSyntaxElement* se_create(void) {
|
||||||
tSyntaxElement* syntaxelement = malloc(sizeof(tSyntaxElement));
|
tSyntaxElement* syntaxelement = malloc(sizeof(tSyntaxElement));
|
||||||
syntaxelement->type = NONE;
|
syntaxelement->type = NONE;
|
||||||
syntaxelement->content = NULL;
|
syntaxelement->content.none = NULL;
|
||||||
syntaxelement->next = NULL;
|
syntaxelement->next = NULL;
|
||||||
syntaxelement->top = NULL;
|
syntaxelement->top = NULL;
|
||||||
return syntaxelement;
|
return syntaxelement;
|
||||||
@ -18,9 +18,9 @@ tSyntaxElement* se_create(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tSyntaxElement* se_bottom(tSyntaxElement* syntaxelement) {
|
tSyntaxElement* se_bottom(tSyntaxElement* syntaxelement) {
|
||||||
syntaxelement->content = se_create();
|
syntaxelement->content.syntax = se_create();
|
||||||
((tSyntaxElement*)syntaxelement->content)->top = syntaxelement;
|
syntaxelement->content.syntax->top = syntaxelement;
|
||||||
return ((tSyntaxElement*)syntaxelement->content);
|
return syntaxelement->content.syntax;
|
||||||
}
|
}
|
||||||
|
|
||||||
tSyntaxElement* se_next(tSyntaxElement* syntaxelement) {
|
tSyntaxElement* se_next(tSyntaxElement* syntaxelement) {
|
||||||
@ -39,12 +39,12 @@ void se_free(tSyntaxElement* syntaxtree) {
|
|||||||
free(syntaxtree);
|
free(syntaxtree);
|
||||||
return;
|
return;
|
||||||
case TREE:
|
case TREE:
|
||||||
if (syntaxtree->content != NULL) {
|
if (syntaxtree->content.syntax != NULL) {
|
||||||
se_free((tSyntaxElement*)syntaxtree->content);
|
se_free(syntaxtree->content.syntax);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
free(syntaxtree->content);
|
free(syntaxtree->content.none);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (syntaxtree->next != NULL) {
|
if (syntaxtree->next != NULL) {
|
||||||
@ -59,8 +59,8 @@ tSyntaxElement* se_clone(tSyntaxElement* syntaxelement, tSyntaxElement* newtop)
|
|||||||
case TREE:
|
case TREE:
|
||||||
clone = se_create();
|
clone = se_create();
|
||||||
clone->type = TREE;
|
clone->type = TREE;
|
||||||
if (syntaxelement->content != NULL)
|
if (syntaxelement->content.syntax != NULL)
|
||||||
clone->content = se_clone(syntaxelement->content, clone);
|
clone->content.syntax = se_clone(syntaxelement->content.syntax, clone);
|
||||||
if (syntaxelement->next != NULL)
|
if (syntaxelement->next != NULL)
|
||||||
clone->next = se_clone(syntaxelement->next, newtop);
|
clone->next = se_clone(syntaxelement->next, newtop);
|
||||||
break;
|
break;
|
||||||
@ -68,8 +68,8 @@ tSyntaxElement* se_clone(tSyntaxElement* syntaxelement, tSyntaxElement* newtop)
|
|||||||
case STRING:
|
case STRING:
|
||||||
clone = se_create();
|
clone = se_create();
|
||||||
clone->type = syntaxelement->type;
|
clone->type = syntaxelement->type;
|
||||||
clone->content = malloc(strlen(syntaxelement->content));
|
clone->content.string = malloc(strlen(syntaxelement->content.string));
|
||||||
strcpy(clone->content, syntaxelement->content);
|
strcpy(clone->content.string, syntaxelement->content.string);
|
||||||
if (syntaxelement->next != NULL)
|
if (syntaxelement->next != NULL)
|
||||||
clone->next = se_clone(syntaxelement->next, newtop);
|
clone->next = se_clone(syntaxelement->next, newtop);
|
||||||
break;
|
break;
|
||||||
|
@ -10,9 +10,15 @@ enum SyntaxElementType {
|
|||||||
NONE
|
NONE
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
struct SyntaxElement* syntax;
|
||||||
|
char* string;
|
||||||
|
void* none;
|
||||||
|
} tSyntaxContent;
|
||||||
|
|
||||||
typedef struct SyntaxElement {
|
typedef struct SyntaxElement {
|
||||||
enum SyntaxElementType type;
|
enum SyntaxElementType type;
|
||||||
void* content;
|
tSyntaxContent content;
|
||||||
struct SyntaxElement* next;
|
struct SyntaxElement* next;
|
||||||
struct SyntaxElement* top;
|
struct SyntaxElement* top;
|
||||||
} tSyntaxElement;
|
} tSyntaxElement;
|
||||||
|
Loading…
Reference in New Issue
Block a user