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