diff --git a/exercises/03/vslc/src/parser.y b/exercises/03/vslc/src/parser.y index ab5b7bd..1057d47 100644 --- a/exercises/03/vslc/src/parser.y +++ b/exercises/03/vslc/src/parser.y @@ -13,7 +13,7 @@ %left '*' '/' %nonassoc UMINUS %right '~' -%expect 1 +//%expect 1 %nonassoc IF THEN %nonassoc ELSE @@ -250,13 +250,13 @@ expression: $$ = NODE(EXPRESSION, strdup("~"), 1, $2); } | '(' expression ')' { - $$ = NODE(EXPRESSION, /*NULL*/ strdup("group"), 1, $2); + $$ = NODE(EXPRESSION, NULL /*strdup("group")*/, 1, $2); } | number { - $$ = NODE(EXPRESSION, /*NULL*/ strdup("number"), 1, $1); + $$ = NODE(EXPRESSION, NULL /*strdup("number")*/, 1, $1); } | identifier { - $$ = NODE(EXPRESSION, /*NULL*/ strdup("identifier"), 1, $1); + $$ = NODE(EXPRESSION, NULL /*strdup("identifier")*/, 1, $1); } | identifier '(' argument_list ')' { $$ = NODE(EXPRESSION, /*NULL*/ strdup("function_call"), 2, $1, $3); diff --git a/exercises/03/vslc/src/tree.c b/exercises/03/vslc/src/tree.c index 072100f..5e58deb 100644 --- a/exercises/03/vslc/src/tree.c +++ b/exercises/03/vslc/src/tree.c @@ -12,6 +12,10 @@ struct stem_t { const char *str; stem next; }; static void tree_print(node_t* root, stem head) { + if (!root) + return; + + static const char *sdown = " │", *slast = " └", *snone = " "; struct stem_t col = {0, 0}, *tail; @@ -139,6 +143,48 @@ destroy_subtree ( node_t *discard ) } } +static void +prune_children(node_t *parent) +{ + if (!parent) + return; + + for (int i = 0; i < parent->n_children; i++) + prune_children(parent->children[i]); + + printf("Trying to remove node!\n"); + if (parent->data != NULL || parent->n_children != 1) + return; + printf("Only one child found. Purging...\n"); + + node_t *child = parent->children[0]; + switch (parent->type) + { + case PROGRAM: + case STATEMENT: + case EXPRESSION: + case PRINT_ITEM: + printf("Removing node!\n"); + //parent->children = NULL; + free(parent->children); + + //node_finalize(parent); + parent = child; + default: + break; + } + + /* + if (parent->n_children != 1 || parent->data) + return; + + printf("delete parent\n"); + node_t *child = parent->children[0]; + //parent->children = NULL; + node_finalize(parent); + parent = child;*/ +} + static void simplify_tree ( node_t **simplified, node_t *root ) @@ -174,4 +220,12 @@ simplify_tree ( node_t **simplified, node_t *root ) node_finalize(root); */ + if (!root) + return; + + for (int i = 0; i < root->n_children; i++) + simplify_tree(&root->children[i], root->children[i]); + + prune_children(root); + } diff --git a/exercises/03/vslc/vsl_programs/Makefile b/exercises/03/vslc/vsl_programs/Makefile index 8f22c0f..b020f93 100644 --- a/exercises/03/vslc/vsl_programs/Makefile +++ b/exercises/03/vslc/vsl_programs/Makefile @@ -3,12 +3,12 @@ VSLC := ../src/vslc PS2_EXAMPLES := $(patsubst ps2-parser/%.vsl, ps2-parser/%.ast, $(wildcard ps2-parser/*.vsl)) PS3_EXAMPLES := $(patsubst ps3-simplify/%.vsl, ps3-simplify/%.ast, $(wildcard ps3-simplify/*.vsl)) -all: $(PS2_EXAMPLES) $(PS2_EXAMPLES) +all: $(PS2_EXAMPLES) $(PS3_EXAMPLES) ps2: $(PS2_EXAMPLES) ps3: $(PS3_EXAMPLES) %.ast: %.vsl - $(VSLC) -t < $^ > $@ + $(VSLC) -T -t < $^ > $@ clean: -rm -r */*.ast \ No newline at end of file