diff --git a/src/extstring.c b/src/extstring.c index d4de042..f2fb558 100644 --- a/src/extstring.c +++ b/src/extstring.c @@ -14,9 +14,9 @@ void es_addsymbol(struct Extstring* string, char symbol) { } int es_size(struct Extstring* string) { - int size; + int size = 0; struct Extstring* ptr = string; - for (size = 0; ptr != NULL; size++) { + for (; ptr != NULL; size++) { ptr = ptr->next; } return size; @@ -37,19 +37,17 @@ void es_free(struct Extstring* string) { free(ptr); } -struct SString* es_tostring(struct Extstring* string) { +char* es_tostring(struct Extstring* string) { struct Extstring* ptr = string; int size = es_size(string) + 1; - struct SString* sstring = malloc(sizeof(struct SString)); - sstring->string = malloc(size); - sstring->size = size; + char* cstring = malloc(size); for (int i = 0; i != size; i++) { if (ptr != NULL) { - sstring->string[i] = ptr->symbol; + cstring[i] = ptr->symbol; ptr = ptr->next; } else - sstring->string[i] = 0; + cstring[i] = 0; } - return sstring; + return cstring; } diff --git a/src/extstring.h b/src/extstring.h index 623ef45..b4d7a16 100644 --- a/src/extstring.h +++ b/src/extstring.h @@ -3,12 +3,7 @@ struct Extstring { struct Extstring* next; }; -struct SString { - int size; - char* string; -}; - void es_addsymbol(struct Extstring* string, char symbol); int es_size(struct Extstring* string); void es_free(struct Extstring* string); -struct SString* es_tostring(struct Extstring* string); +char* es_tostring(struct Extstring* string); diff --git a/src/main.c b/src/main.c index 8e53609..2822ade 100644 --- a/src/main.c +++ b/src/main.c @@ -15,10 +15,13 @@ int main(int argc, char *argv[]) { } file = fopen(argv[1],"r"); struct SyntaxElement* code = parse(file); + fclose(file); if (code == NULL) { fprintf(stderr, "Error during parsing.\n"); return 1; } printtree(code,0); + se_free(code); + code = NULL; return 0; } diff --git a/src/parser.c b/src/parser.c index 30beed9..c9d2014 100644 --- a/src/parser.c +++ b/src/parser.c @@ -28,6 +28,27 @@ struct SyntaxElement* se_next(struct SyntaxElement* syntaxelement) { return syntaxelement->next; } +void se_free(struct SyntaxElement* syntaxtree) { + switch (syntaxtree->type) { + case NONE: + free(syntaxtree); + return; + case TREE: + case TOPTREE: + if (syntaxtree->content != NULL) { + se_free((struct SyntaxElement*)syntaxtree->content); + } + break; + default: + free(syntaxtree->content); + break; + } + if (syntaxtree->next != NULL) { + se_free(syntaxtree->next); + } + free(syntaxtree); +} + void se_clean(struct SyntaxElement* syntaxtree) { if (syntaxtree->next != NULL) { switch (syntaxtree->next->type) { @@ -80,7 +101,7 @@ struct SyntaxElement* parse(FILE* file) { } instring = false; syntaxtree->type = STRING; - char* sstring = es_tostring(string)->string; + char* sstring = es_tostring(string); syntaxtree->content = sstring; es_free(string); string = NULL; @@ -99,7 +120,7 @@ struct SyntaxElement* parse(FILE* file) { if (isspace(symbol) || symbol == '(' || symbol == ')') { intoken = false; syntaxtree->type = TOKEN; - char* sstring = es_tostring(token)->string; + char* sstring = es_tostring(token); syntaxtree->content = sstring; es_free(token); token = NULL; diff --git a/src/parser.h b/src/parser.h index ac60f55..55cb73f 100644 --- a/src/parser.h +++ b/src/parser.h @@ -2,6 +2,11 @@ #ifndef PARSER_H #define PARSER_H +struct SyntaxElement* se_init(void); +struct SyntaxElement* se_bottom(struct SyntaxElement* syntaxelement); +struct SyntaxElement* se_next(struct SyntaxElement* syntaxelement); +void se_free(struct SyntaxElement* syntaxtree); + enum SyntaxElementType { TOPTREE, TREE,