#include #include #include "syntax.h" bool se_istraversable(tSyntaxElement* syntaxtree) { return syntaxtree->type == TREE && syntaxtree->content.syntax != NULL; } tSyntaxElement* se_create(void) { tSyntaxElement* syntaxelement = malloc(sizeof(tSyntaxElement)); syntaxelement->type = NONE; syntaxelement->content.none = NULL; syntaxelement->next = NULL; syntaxelement->top = NULL; return syntaxelement; } tSyntaxElement* se_bottom(tSyntaxElement* syntaxelement) { syntaxelement->content.syntax = se_create(); syntaxelement->content.syntax->top = syntaxelement; return syntaxelement->content.syntax; } tSyntaxElement* se_next(tSyntaxElement* syntaxelement) { if (syntaxelement->type != NEWTREE) { syntaxelement->next = se_create(); syntaxelement->next->top = syntaxelement->top; return syntaxelement->next; } else return syntaxelement; } void se_free(tSyntaxElement* syntaxtree) { switch (syntaxtree->type) { case NONE: free(syntaxtree); return; case TREE: if (syntaxtree->content.syntax != NULL) { se_free(syntaxtree->content.syntax); } break; default: free(syntaxtree->content.none); break; } if (syntaxtree->next != NULL) { se_free(syntaxtree->next); } free(syntaxtree); } tSyntaxElement* se_clone(tSyntaxElement* syntaxelement, tSyntaxElement* newtop) { tSyntaxElement* clone = NULL; switch (syntaxelement->type) { case TREE: clone = se_create(); clone->type = TREE; 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; case TOKEN: case STRING: clone = se_create(); clone->type = syntaxelement->type; 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; case NONE: case NEWTREE: clone = se_create(); clone->type = syntaxelement->type; clone->top = newtop; if (syntaxelement->next != NULL) clone->next = se_clone(syntaxelement->next, newtop); break; } return clone; } tSyntaxElement* se_clone_no_next(tSyntaxElement* syntaxelement, tSyntaxElement* newtop) { tSyntaxElement* se = malloc(sizeof(tSyntaxElement)); se->content = syntaxelement->content; se->top = syntaxelement->top; se->type = syntaxelement->type; se->next = NULL; tSyntaxElement* clone = se_clone(se, newtop); free(se); return clone; }