Format code better
This commit is contained in:
parent
3128d3c646
commit
65e360984d
@ -2,53 +2,81 @@
|
|||||||
#include "extstring.h"
|
#include "extstring.h"
|
||||||
|
|
||||||
void es_addsymbol(tExtstring* string, char symbol) {
|
void es_addsymbol(tExtstring* string, char symbol) {
|
||||||
|
|
||||||
if (string == NULL)
|
if (string == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
tExtstring* ptr = string;
|
tExtstring* ptr = string;
|
||||||
|
|
||||||
while (ptr->next != NULL) {
|
while (ptr->next != NULL) {
|
||||||
ptr = ptr->next;
|
ptr = ptr->next;
|
||||||
};
|
};
|
||||||
|
|
||||||
ptr->next = malloc(sizeof(tExtstring));
|
ptr->next = malloc(sizeof(tExtstring));
|
||||||
ptr->next->symbol = symbol;
|
ptr->next->symbol = symbol;
|
||||||
ptr->next->next = NULL;
|
ptr->next->next = NULL;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int es_size(tExtstring* string) {
|
int es_size(tExtstring* string) {
|
||||||
|
|
||||||
int size = 0;
|
int size = 0;
|
||||||
|
|
||||||
tExtstring* ptr = string;
|
tExtstring* ptr = string;
|
||||||
|
|
||||||
for (; ptr != NULL; size++) {
|
for (; ptr != NULL; size++) {
|
||||||
ptr = ptr->next;
|
ptr = ptr->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
void es_free(tExtstring* string) {
|
void es_free(tExtstring* string) {
|
||||||
|
|
||||||
if (string->next == NULL) {
|
if (string->next == NULL) {
|
||||||
|
|
||||||
free(string);
|
free(string);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tExtstring* ptr = string;
|
tExtstring* ptr = string;
|
||||||
tExtstring* nextptr = ptr->next;
|
tExtstring* nextptr = ptr->next;
|
||||||
|
|
||||||
while (ptr->next != NULL) {
|
while (ptr->next != NULL) {
|
||||||
|
|
||||||
nextptr = ptr->next;
|
nextptr = ptr->next;
|
||||||
free(ptr);
|
free(ptr);
|
||||||
ptr = nextptr;
|
ptr = nextptr;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free(ptr);
|
free(ptr);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char* es_tostring(tExtstring* string) {
|
char* es_tostring(tExtstring* string) {
|
||||||
|
|
||||||
tExtstring* ptr = string;
|
tExtstring* ptr = string;
|
||||||
|
|
||||||
int size = es_size(string) + 1;
|
int size = es_size(string) + 1;
|
||||||
|
|
||||||
char* cstring = malloc(size);
|
char* cstring = malloc(size);
|
||||||
|
|
||||||
for (int i = 0; i != size; i++) {
|
for (int i = 0; i != size; i++) {
|
||||||
|
|
||||||
if (ptr != NULL) {
|
if (ptr != NULL) {
|
||||||
cstring[i] = ptr->symbol;
|
cstring[i] = ptr->symbol;
|
||||||
ptr = ptr->next;
|
ptr = ptr->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
else
|
else
|
||||||
cstring[i] = 0;
|
cstring[i] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return cstring;
|
return cstring;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,11 @@ typedef struct Extstring {
|
|||||||
struct Extstring* next;
|
struct Extstring* next;
|
||||||
} tExtstring;
|
} tExtstring;
|
||||||
|
|
||||||
|
|
||||||
void es_addsymbol(tExtstring* string, char symbol);
|
void es_addsymbol(tExtstring* string, char symbol);
|
||||||
|
|
||||||
int es_size(tExtstring* string);
|
int es_size(tExtstring* string);
|
||||||
|
|
||||||
void es_free(tExtstring* string);
|
void es_free(tExtstring* string);
|
||||||
|
|
||||||
char* es_tostring(tExtstring* string);
|
char* es_tostring(tExtstring* string);
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
#include "process.h"
|
#include "process.h"
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
FILE *file;
|
FILE *file;
|
||||||
if (argc != 2) {
|
if (argc != 2) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
@ -14,23 +15,30 @@ int main(int argc, char *argv[]) {
|
|||||||
"%s FILENAME\n", argv[0]);
|
"%s FILENAME\n", argv[0]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
file = fopen(argv[1],"r");
|
file = fopen(argv[1],"r");
|
||||||
if (file == NULL) {
|
if (file == NULL) {
|
||||||
fprintf(stderr,"Unable to open file.\n");
|
fprintf(stderr,"Unable to open file.\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
tSyntaxElement* code = parse(file);
|
tSyntaxElement* code = parse(file);
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
|
||||||
if (code == NULL) {
|
if (code == NULL) {
|
||||||
fprintf(stderr, "Error during parsing.\n");
|
fprintf(stderr, "Error during parsing.\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Parsed: \n");
|
printf("Parsed: \n");
|
||||||
printtree(code,1);
|
printtree(code,1);
|
||||||
|
|
||||||
printf("\nProcessed: \n");
|
printf("\nProcessed: \n");
|
||||||
process(code, NULL);
|
process(code, NULL);
|
||||||
printtree(code,1);
|
printtree(code,1);
|
||||||
|
|
||||||
se_free(code);
|
se_free(code);
|
||||||
code = NULL;
|
code = NULL;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
76
src/parser.c
76
src/parser.c
@ -3,130 +3,190 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
#ifdef DEBUG
|
|
||||||
#include "printtree.h"
|
|
||||||
#endif
|
|
||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
#include "extstring.h"
|
#include "extstring.h"
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
#include "printtree.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
tSyntaxElement* parse(FILE* file) {
|
tSyntaxElement* parse(FILE* file) {
|
||||||
|
|
||||||
tSyntaxElement* syntaxtree = se_create();
|
tSyntaxElement* syntaxtree = se_create();
|
||||||
tSyntaxElement* syntaxtreestart = syntaxtree;
|
tSyntaxElement* syntaxtreestart = syntaxtree;
|
||||||
syntaxtree->type = NEWTREE;
|
syntaxtree->type = NEWTREE;
|
||||||
|
|
||||||
char symbol;
|
char symbol;
|
||||||
bool intree = false;
|
bool intree = false;
|
||||||
bool incomment = false;
|
bool incomment = false;
|
||||||
bool instring = false;
|
bool instring = false;
|
||||||
bool intoken = false;
|
bool intoken = false;
|
||||||
|
|
||||||
tExtstring* exttoken = NULL;
|
tExtstring* exttoken = NULL;
|
||||||
tExtstring* extstring = NULL;
|
tExtstring* extstring = NULL;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
symbol = fgetc(file);
|
symbol = fgetc(file);
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
if (!isspace(symbol))
|
|
||||||
printf("In parsing. Current symbol: %c\n", symbol);
|
if (!isspace(symbol))
|
||||||
else
|
printf("In parsing. Current symbol: %c\n", symbol);
|
||||||
printf("In parsing. Current symbol: %d\n", symbol);
|
else
|
||||||
printtree(syntaxtreestart, 1);
|
printf("In parsing. Current symbol: %d\n", symbol);
|
||||||
|
|
||||||
|
printtree(syntaxtreestart, 1);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (feof(file))
|
if (feof(file))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (incomment) {
|
if (incomment) {
|
||||||
if (symbol == '\n')
|
if (symbol == '\n')
|
||||||
incomment = false;
|
incomment = false;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (instring) {
|
if (instring) {
|
||||||
if (symbol == '"') {
|
if (symbol == '"') {
|
||||||
|
|
||||||
if (extstring == NULL) {
|
if (extstring == NULL) {
|
||||||
extstring = malloc(sizeof(tExtstring));
|
extstring = malloc(sizeof(tExtstring));
|
||||||
extstring->next = NULL;
|
extstring->next = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
instring = false;
|
instring = false;
|
||||||
|
|
||||||
syntaxtree->type = STRING;
|
syntaxtree->type = STRING;
|
||||||
char* string = es_tostring(extstring);
|
char* string = es_tostring(extstring);
|
||||||
syntaxtree->content.string = string;
|
syntaxtree->content.string = string;
|
||||||
|
|
||||||
es_free(extstring);
|
es_free(extstring);
|
||||||
extstring = NULL;
|
extstring = NULL;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
if (extstring == NULL) {
|
if (extstring == NULL) {
|
||||||
|
|
||||||
extstring = malloc(sizeof(tExtstring));
|
extstring = malloc(sizeof(tExtstring));
|
||||||
extstring->symbol = symbol;
|
extstring->symbol = symbol;
|
||||||
extstring->next = NULL;
|
extstring->next = NULL;
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
es_addsymbol(extstring, symbol);
|
es_addsymbol(extstring, symbol);
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (intoken) {
|
if (intoken) {
|
||||||
if (isspace(symbol) || symbol == '(' || symbol == ')') {
|
if (isspace(symbol) || symbol == '(' || symbol == ')') {
|
||||||
|
|
||||||
intoken = false;
|
intoken = false;
|
||||||
|
|
||||||
syntaxtree->type = TOKEN;
|
syntaxtree->type = TOKEN;
|
||||||
char* string = es_tostring(exttoken);
|
char* string = es_tostring(exttoken);
|
||||||
syntaxtree->content.string = string;
|
syntaxtree->content.string = string;
|
||||||
|
|
||||||
es_free(exttoken);
|
es_free(exttoken);
|
||||||
exttoken = NULL;
|
exttoken = NULL;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
es_addsymbol(exttoken, symbol);
|
es_addsymbol(exttoken, symbol);
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isspace(symbol))
|
if (isspace(symbol))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (intree) {
|
if (intree) {
|
||||||
switch (symbol) {
|
switch (symbol) {
|
||||||
|
|
||||||
case '(':
|
case '(':
|
||||||
|
|
||||||
syntaxtree = se_next(syntaxtree);
|
syntaxtree = se_next(syntaxtree);
|
||||||
syntaxtree->type = TREE;
|
syntaxtree->type = TREE;
|
||||||
|
|
||||||
syntaxtree = se_bottom(syntaxtree);
|
syntaxtree = se_bottom(syntaxtree);
|
||||||
syntaxtree->type = NEWTREE;
|
syntaxtree->type = NEWTREE;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ')':
|
case ')':
|
||||||
|
|
||||||
syntaxtree = syntaxtree->top;
|
syntaxtree = syntaxtree->top;
|
||||||
|
|
||||||
if (syntaxtree->top == NULL)
|
if (syntaxtree->top == NULL)
|
||||||
intree = false;
|
intree = false;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ';':
|
case ';':
|
||||||
|
|
||||||
incomment = true;
|
incomment = true;
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
case '"':
|
case '"':
|
||||||
|
|
||||||
syntaxtree = se_next(syntaxtree);
|
syntaxtree = se_next(syntaxtree);
|
||||||
instring = true;
|
instring = true;
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
||||||
syntaxtree = se_next(syntaxtree);
|
syntaxtree = se_next(syntaxtree);
|
||||||
|
|
||||||
exttoken = malloc(sizeof(tExtstring));
|
exttoken = malloc(sizeof(tExtstring));
|
||||||
exttoken->symbol = symbol;
|
exttoken->symbol = symbol;
|
||||||
exttoken->next = NULL;
|
exttoken->next = NULL;
|
||||||
|
|
||||||
intoken = true;
|
intoken = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
switch (symbol) {
|
switch (symbol) {
|
||||||
|
|
||||||
case ')':
|
case ')':
|
||||||
|
|
||||||
fprintf(stderr, "Unbalanced brackets.\n");
|
fprintf(stderr, "Unbalanced brackets.\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
case ';':
|
case ';':
|
||||||
|
|
||||||
incomment = true;
|
incomment = true;
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
case '(':
|
case '(':
|
||||||
|
|
||||||
intree = true;
|
intree = true;
|
||||||
|
|
||||||
syntaxtree = se_next(syntaxtree);
|
syntaxtree = se_next(syntaxtree);
|
||||||
syntaxtree->type = TREE;
|
syntaxtree->type = TREE;
|
||||||
|
|
||||||
syntaxtree = se_bottom(syntaxtree);
|
syntaxtree = se_bottom(syntaxtree);
|
||||||
syntaxtree->type = NEWTREE;
|
syntaxtree->type = NEWTREE;
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
||||||
fprintf(stderr, "Expression outside of brackets.\n");
|
fprintf(stderr, "Expression outside of brackets.\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (intree) {
|
if (intree) {
|
||||||
|
|
||||||
fprintf(stderr, "Unexpected EOF.\n");
|
fprintf(stderr, "Unexpected EOF.\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return syntaxtreestart;
|
return syntaxtreestart;
|
||||||
}
|
}
|
||||||
|
@ -5,29 +5,47 @@
|
|||||||
#include "printtree.h"
|
#include "printtree.h"
|
||||||
|
|
||||||
void printtree(tSyntaxElement* syntaxtree, int depth) {
|
void printtree(tSyntaxElement* syntaxtree, int depth) {
|
||||||
|
|
||||||
if (syntaxtree == NULL)
|
if (syntaxtree == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for (int i = 1; i <= depth; i++)
|
for (int i = 1; i <= depth; i++)
|
||||||
putc(' ',stdout);
|
putc(' ',stdout);
|
||||||
|
|
||||||
switch (syntaxtree->type) {
|
switch (syntaxtree->type) {
|
||||||
|
|
||||||
case TREE:
|
case TREE:
|
||||||
|
|
||||||
printf("TREE\n");
|
printf("TREE\n");
|
||||||
printtree(syntaxtree->content.syntax, depth + 1);
|
printtree(syntaxtree->content.syntax, depth + 1);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case TOKEN:
|
case TOKEN:
|
||||||
|
|
||||||
printf("TOKEN: %s\n", syntaxtree->content.string);
|
printf("TOKEN: %s\n", syntaxtree->content.string);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case STRING:
|
case STRING:
|
||||||
|
|
||||||
printf("STRING: %s\n", syntaxtree->content.string);
|
printf("STRING: %s\n", syntaxtree->content.string);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case NONE:
|
case NONE:
|
||||||
|
|
||||||
printf("NONE\n");
|
printf("NONE\n");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case NEWTREE:
|
case NEWTREE:
|
||||||
|
|
||||||
printf("NEWTREE\n");
|
printf("NEWTREE\n");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (syntaxtree->next != NULL)
|
if (syntaxtree->next != NULL)
|
||||||
printtree(syntaxtree->next, depth);
|
printtree(syntaxtree->next, depth);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,2 +1,3 @@
|
|||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
|
|
||||||
void printtree(tSyntaxElement*, int);
|
void printtree(tSyntaxElement*, int);
|
||||||
|
@ -6,76 +6,124 @@
|
|||||||
#include "process.h"
|
#include "process.h"
|
||||||
|
|
||||||
bool replace(tSyntaxElement* tree, tProcessingData* pdata) {
|
bool replace(tSyntaxElement* tree, tProcessingData* pdata) {
|
||||||
|
|
||||||
tProcessingData* pd = pdata;
|
tProcessingData* pd = pdata;
|
||||||
|
|
||||||
bool didreplace = false;
|
bool didreplace = false;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
|
|
||||||
if (pd->data.replace.replacethis == NULL)
|
if (pd->data.replace.replacethis == NULL)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (tree->type == TOKEN)
|
if (tree->type == TOKEN)
|
||||||
|
|
||||||
if (strcmp(tree->content.string, pd->data.replace.replacethis) == 0) {
|
if (strcmp(tree->content.string, pd->data.replace.replacethis) == 0) {
|
||||||
|
|
||||||
didreplace = true;
|
didreplace = true;
|
||||||
|
|
||||||
free(tree->content.none);
|
free(tree->content.none);
|
||||||
|
|
||||||
tSyntaxElement* clone = se_clone_no_next(pd->data.replace.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;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pd->prev != NULL)
|
if (pd->prev != NULL)
|
||||||
pd = pd->prev;
|
pd = pd->prev;
|
||||||
else
|
|
||||||
break;
|
else break;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return didreplace;
|
return didreplace;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool process_find(tSyntaxElement* tree, tProcessingData** p_pdata) {
|
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 (tree->content.syntax->type == TOKEN &&
|
if (tree->content.syntax->type == TOKEN &&
|
||||||
tree->content.syntax->next != NULL &&
|
tree->content.syntax->next != NULL &&
|
||||||
tree->content.syntax->next->next != NULL) {
|
tree->content.syntax->next->next != NULL) {
|
||||||
|
|
||||||
tSyntaxElement* token = tree->content.syntax;
|
tSyntaxElement* token = tree->content.syntax;
|
||||||
|
|
||||||
if (strcmp(token->content.string,"def") == 0) {
|
if (strcmp(token->content.string,"def") == 0) {
|
||||||
|
|
||||||
char* replacethis = token->next->content.string;
|
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->type = REPLACE;
|
npd->type = REPLACE;
|
||||||
npd->data.replace.replacethis = replacethis;
|
npd->data.replace.replacethis = replacethis;
|
||||||
npd->data.replace.replacewiththis = replacewiththis;
|
npd->data.replace.replacewiththis = replacewiththis;
|
||||||
npd->prev = pd;
|
npd->prev = pd;
|
||||||
|
|
||||||
*p_pdata = npd;
|
*p_pdata = npd;
|
||||||
|
|
||||||
deffound = true;
|
deffound = true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return deffound;
|
return deffound;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void process(tSyntaxElement* tree, tProcessingData* pdata) {
|
void process(tSyntaxElement* tree, tProcessingData* pdata) {
|
||||||
|
|
||||||
tProcessingData* pd = pdata;
|
tProcessingData* pd = pdata;
|
||||||
|
|
||||||
bool deffound = false;
|
bool deffound = false;
|
||||||
|
|
||||||
tProcessingData* firstpd = NULL;
|
tProcessingData* firstpd = NULL;
|
||||||
|
|
||||||
if (pd == NULL) {
|
if (pd == NULL) {
|
||||||
|
|
||||||
pd = calloc(1,sizeof(tProcessingData));
|
pd = calloc(1,sizeof(tProcessingData));
|
||||||
firstpd = pd;
|
firstpd = pd;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
deffound = process_find(tree, &pd);
|
deffound = process_find(tree, &pd);
|
||||||
|
|
||||||
if (tree->next != NULL) {
|
if (tree->next != NULL) {
|
||||||
process(tree->next, pd);
|
process(tree->next, pd);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!deffound) {
|
if (!deffound) {
|
||||||
if (se_istraversable(tree)) {
|
|
||||||
|
if (se_istraversable(tree))
|
||||||
process(tree->content.syntax, pd);
|
process(tree->content.syntax, pd);
|
||||||
}
|
|
||||||
switch (pd->type) {
|
switch (pd->type) {
|
||||||
|
|
||||||
case REPLACE:
|
case REPLACE:
|
||||||
replace(tree, pd);
|
replace(tree, pd);
|
||||||
break;
|
break;
|
||||||
case FUNCTION:
|
|
||||||
|
case FUNCTION: // TODO
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pd != pdata)
|
if (pd != pdata)
|
||||||
free(pd);
|
free(pd);
|
||||||
|
|
||||||
if (firstpd != pd && firstpd != NULL)
|
if (firstpd != pd && firstpd != NULL)
|
||||||
free(firstpd);
|
free(firstpd);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,33 +2,45 @@
|
|||||||
#include "syntax.h"
|
#include "syntax.h"
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
|
|
||||||
REPLACE,
|
REPLACE,
|
||||||
FUNCTION
|
FUNCTION
|
||||||
|
|
||||||
} tProcessingType;
|
} tProcessingType;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
||||||
char* replacethis;
|
char* replacethis;
|
||||||
tSyntaxElement* replacewiththis;
|
tSyntaxElement* replacewiththis;
|
||||||
|
|
||||||
} tProcessingReplace;
|
} tProcessingReplace;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
||||||
char** args;
|
char** args;
|
||||||
tSyntaxElement* body;
|
tSyntaxElement* body;
|
||||||
|
|
||||||
} tProcessingFunction;
|
} tProcessingFunction;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
typedef union {
|
typedef union {
|
||||||
|
|
||||||
tProcessingReplace replace;
|
tProcessingReplace replace;
|
||||||
tProcessingFunction function;
|
tProcessingFunction function;
|
||||||
|
|
||||||
} tuProcessingData;
|
} tuProcessingData;
|
||||||
|
|
||||||
typedef struct ProcessingData {
|
typedef struct ProcessingData {
|
||||||
|
|
||||||
struct ProcessingData* prev;
|
struct ProcessingData* prev;
|
||||||
tProcessingType type;
|
tProcessingType type;
|
||||||
tuProcessingData data;
|
tuProcessingData data;
|
||||||
|
|
||||||
} tProcessingData;
|
} tProcessingData;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void process(tSyntaxElement*, tProcessingData*);
|
void process(tSyntaxElement*, tProcessingData*);
|
||||||
|
51
src/syntax.c
51
src/syntax.c
@ -4,94 +4,141 @@
|
|||||||
#include "syntax.h"
|
#include "syntax.h"
|
||||||
|
|
||||||
bool se_istraversable(tSyntaxElement* syntaxtree) {
|
bool se_istraversable(tSyntaxElement* syntaxtree) {
|
||||||
|
|
||||||
return syntaxtree->type == TREE && syntaxtree->content.syntax != 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.none = NULL;
|
syntaxelement->content.none = NULL;
|
||||||
syntaxelement->next = NULL;
|
syntaxelement->next = NULL;
|
||||||
syntaxelement->top = NULL;
|
syntaxelement->top = NULL;
|
||||||
|
|
||||||
return syntaxelement;
|
return syntaxelement;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tSyntaxElement* se_bottom(tSyntaxElement* syntaxelement) {
|
tSyntaxElement* se_bottom(tSyntaxElement* syntaxelement) {
|
||||||
|
|
||||||
syntaxelement->content.syntax = se_create();
|
syntaxelement->content.syntax = se_create();
|
||||||
syntaxelement->content.syntax->top = syntaxelement;
|
syntaxelement->content.syntax->top = syntaxelement;
|
||||||
|
|
||||||
return syntaxelement->content.syntax;
|
return syntaxelement->content.syntax;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tSyntaxElement* se_next(tSyntaxElement* syntaxelement) {
|
tSyntaxElement* se_next(tSyntaxElement* syntaxelement) {
|
||||||
|
|
||||||
if (syntaxelement->type != NEWTREE) {
|
if (syntaxelement->type != NEWTREE) {
|
||||||
|
|
||||||
syntaxelement->next = se_create();
|
syntaxelement->next = se_create();
|
||||||
syntaxelement->next->top = syntaxelement->top;
|
syntaxelement->next->top = syntaxelement->top;
|
||||||
|
|
||||||
return syntaxelement->next;
|
return syntaxelement->next;
|
||||||
} else {
|
|
||||||
|
} else
|
||||||
return syntaxelement;
|
return syntaxelement;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void se_free(tSyntaxElement* syntaxtree) {
|
void se_free(tSyntaxElement* syntaxtree) {
|
||||||
|
|
||||||
switch (syntaxtree->type) {
|
switch (syntaxtree->type) {
|
||||||
|
|
||||||
case NONE:
|
case NONE:
|
||||||
|
|
||||||
free(syntaxtree);
|
free(syntaxtree);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case TREE:
|
case TREE:
|
||||||
|
|
||||||
if (syntaxtree->content.syntax != NULL) {
|
if (syntaxtree->content.syntax != NULL) {
|
||||||
se_free(syntaxtree->content.syntax);
|
se_free(syntaxtree->content.syntax);
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
||||||
free(syntaxtree->content.none);
|
free(syntaxtree->content.none);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (syntaxtree->next != NULL) {
|
if (syntaxtree->next != NULL) {
|
||||||
se_free(syntaxtree->next);
|
se_free(syntaxtree->next);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(syntaxtree);
|
free(syntaxtree);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tSyntaxElement* se_clone(tSyntaxElement* syntaxelement, tSyntaxElement* newtop) {
|
tSyntaxElement* se_clone(tSyntaxElement* syntaxelement, tSyntaxElement* newtop) {
|
||||||
|
|
||||||
tSyntaxElement* clone = NULL;
|
tSyntaxElement* clone = NULL;
|
||||||
|
|
||||||
switch (syntaxelement->type) {
|
switch (syntaxelement->type) {
|
||||||
|
|
||||||
case TREE:
|
case TREE:
|
||||||
|
|
||||||
clone = se_create();
|
clone = se_create();
|
||||||
clone->type = TREE;
|
clone->type = TREE;
|
||||||
|
|
||||||
if (syntaxelement->content.syntax != NULL)
|
if (syntaxelement->content.syntax != NULL)
|
||||||
clone->content.syntax = se_clone(syntaxelement->content.syntax, 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;
|
||||||
|
|
||||||
case TOKEN:
|
case TOKEN:
|
||||||
case STRING:
|
case STRING:
|
||||||
|
|
||||||
clone = se_create();
|
clone = se_create();
|
||||||
clone->type = syntaxelement->type;
|
clone->type = syntaxelement->type;
|
||||||
|
|
||||||
clone->content.string = malloc(strlen(syntaxelement->content.string));
|
clone->content.string = malloc(strlen(syntaxelement->content.string));
|
||||||
strcpy(clone->content.string, syntaxelement->content.string);
|
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;
|
||||||
|
|
||||||
case NONE:
|
case NONE:
|
||||||
case NEWTREE:
|
case NEWTREE:
|
||||||
|
|
||||||
clone = se_create();
|
clone = se_create();
|
||||||
clone->type = syntaxelement->type;
|
clone->type = syntaxelement->type;
|
||||||
clone->top = newtop;
|
clone->top = newtop;
|
||||||
|
|
||||||
if (syntaxelement->next != NULL)
|
if (syntaxelement->next != NULL)
|
||||||
clone->next = se_clone(syntaxelement->next, newtop);
|
clone->next = se_clone(syntaxelement->next, newtop);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
tSyntaxElement* se_clone_no_next(tSyntaxElement* syntaxelement, tSyntaxElement* newtop) {
|
tSyntaxElement* se_clone_no_next(tSyntaxElement* syntaxelement, tSyntaxElement* newtop) {
|
||||||
|
|
||||||
tSyntaxElement* se = malloc(sizeof(tSyntaxElement));
|
tSyntaxElement* se = malloc(sizeof(tSyntaxElement));
|
||||||
|
|
||||||
se->content = syntaxelement->content;
|
se->content = syntaxelement->content;
|
||||||
se->top = syntaxelement->top;
|
se->top = syntaxelement->top;
|
||||||
se->type = syntaxelement->type;
|
se->type = syntaxelement->type;
|
||||||
se->next = NULL;
|
se->next = NULL;
|
||||||
|
|
||||||
tSyntaxElement* clone = se_clone(se, newtop);
|
tSyntaxElement* clone = se_clone(se, newtop);
|
||||||
|
|
||||||
free(se);
|
free(se);
|
||||||
|
|
||||||
return clone;
|
return clone;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
16
src/syntax.h
16
src/syntax.h
@ -1,34 +1,50 @@
|
|||||||
#ifndef SYNTAX_H
|
#ifndef SYNTAX_H
|
||||||
#define SYNTAX_H
|
#define SYNTAX_H
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
enum SyntaxElementType {
|
enum SyntaxElementType {
|
||||||
|
|
||||||
NEWTREE,
|
NEWTREE,
|
||||||
TREE,
|
TREE,
|
||||||
TOKEN,
|
TOKEN,
|
||||||
STRING,
|
STRING,
|
||||||
NONE
|
NONE
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef union {
|
typedef union {
|
||||||
|
|
||||||
struct SyntaxElement* syntax;
|
struct SyntaxElement* syntax;
|
||||||
char* string;
|
char* string;
|
||||||
void* none;
|
void* none;
|
||||||
|
|
||||||
} tSyntaxContent;
|
} tSyntaxContent;
|
||||||
|
|
||||||
typedef struct SyntaxElement {
|
typedef struct SyntaxElement {
|
||||||
|
|
||||||
enum SyntaxElementType type;
|
enum SyntaxElementType type;
|
||||||
tSyntaxContent content;
|
tSyntaxContent content;
|
||||||
struct SyntaxElement* next;
|
struct SyntaxElement* next;
|
||||||
struct SyntaxElement* top;
|
struct SyntaxElement* top;
|
||||||
|
|
||||||
} tSyntaxElement;
|
} tSyntaxElement;
|
||||||
|
|
||||||
|
|
||||||
void se_free(tSyntaxElement*);
|
void se_free(tSyntaxElement*);
|
||||||
|
|
||||||
bool se_istraversable(tSyntaxElement*);
|
bool se_istraversable(tSyntaxElement*);
|
||||||
|
|
||||||
tSyntaxElement* se_create(void);
|
tSyntaxElement* se_create(void);
|
||||||
|
|
||||||
tSyntaxElement* se_init(void);
|
tSyntaxElement* se_init(void);
|
||||||
|
|
||||||
tSyntaxElement* se_bottom(tSyntaxElement*);
|
tSyntaxElement* se_bottom(tSyntaxElement*);
|
||||||
|
|
||||||
tSyntaxElement* se_next(tSyntaxElement*);
|
tSyntaxElement* se_next(tSyntaxElement*);
|
||||||
|
|
||||||
tSyntaxElement* se_clone(tSyntaxElement*, tSyntaxElement*);
|
tSyntaxElement* se_clone(tSyntaxElement*, tSyntaxElement*);
|
||||||
|
|
||||||
tSyntaxElement* se_clone_no_next(tSyntaxElement*, tSyntaxElement*);
|
tSyntaxElement* se_clone_no_next(tSyntaxElement*, tSyntaxElement*);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user