Somewhat working

main
Øyvind Skaaden 2022-02-27 16:41:57 +01:00
parent e8482f39ea
commit a701db4a19
3 changed files with 60 additions and 6 deletions

View File

@ -13,7 +13,7 @@
%left '*' '/' %left '*' '/'
%nonassoc UMINUS %nonassoc UMINUS
%right '~' %right '~'
%expect 1 //%expect 1
%nonassoc IF THEN %nonassoc IF THEN
%nonassoc ELSE %nonassoc ELSE
@ -250,13 +250,13 @@ expression:
$$ = NODE(EXPRESSION, strdup("~"), 1, $2); $$ = NODE(EXPRESSION, strdup("~"), 1, $2);
} }
| '(' expression ')' { | '(' expression ')' {
$$ = NODE(EXPRESSION, /*NULL*/ strdup("group"), 1, $2); $$ = NODE(EXPRESSION, NULL /*strdup("group")*/, 1, $2);
} }
| number { | number {
$$ = NODE(EXPRESSION, /*NULL*/ strdup("number"), 1, $1); $$ = NODE(EXPRESSION, NULL /*strdup("number")*/, 1, $1);
} }
| identifier { | identifier {
$$ = NODE(EXPRESSION, /*NULL*/ strdup("identifier"), 1, $1); $$ = NODE(EXPRESSION, NULL /*strdup("identifier")*/, 1, $1);
} }
| identifier '(' argument_list ')' { | identifier '(' argument_list ')' {
$$ = NODE(EXPRESSION, /*NULL*/ strdup("function_call"), 2, $1, $3); $$ = NODE(EXPRESSION, /*NULL*/ strdup("function_call"), 2, $1, $3);

View File

@ -12,6 +12,10 @@ struct stem_t { const char *str; stem next; };
static void static void
tree_print(node_t* root, stem head) tree_print(node_t* root, stem head)
{ {
if (!root)
return;
static const char *sdown = "", *slast = "", *snone = " "; static const char *sdown = "", *slast = "", *snone = " ";
struct stem_t col = {0, 0}, *tail; 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 static void
simplify_tree ( node_t **simplified, node_t *root ) simplify_tree ( node_t **simplified, node_t *root )
@ -174,4 +220,12 @@ simplify_tree ( node_t **simplified, node_t *root )
node_finalize(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);
} }

View File

@ -3,12 +3,12 @@ VSLC := ../src/vslc
PS2_EXAMPLES := $(patsubst ps2-parser/%.vsl, ps2-parser/%.ast, $(wildcard ps2-parser/*.vsl)) 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)) 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) ps2: $(PS2_EXAMPLES)
ps3: $(PS3_EXAMPLES) ps3: $(PS3_EXAMPLES)
%.ast: %.vsl %.ast: %.vsl
$(VSLC) -t < $^ > $@ $(VSLC) -T -t < $^ > $@
clean: clean:
-rm -r */*.ast -rm -r */*.ast