Filter refactoring: Converted condition to three-args instruction

This commit is contained in:
Maria Matejka 2018-12-20 14:34:35 +01:00
parent 8e8b1fe48c
commit 224b77d4f7
3 changed files with 12 additions and 16 deletions

View file

@ -559,7 +559,7 @@ filter:
where_filter: where_filter:
WHERE term { WHERE term {
/* Construct 'IF term THEN ACCEPT; REJECT;' */ /* Construct 'IF term THEN { ACCEPT; } ELSE { REJECT; }' */
struct filter *f = cfg_alloc(sizeof(struct filter)); struct filter *f = cfg_alloc(sizeof(struct filter));
struct f_inst *i, *acc, *rej; struct f_inst *i, *acc, *rej;
acc = f_new_inst(FI_PRINT_AND_DIE); /* ACCEPT */ acc = f_new_inst(FI_PRINT_AND_DIE); /* ACCEPT */
@ -571,7 +571,7 @@ where_filter:
i = f_new_inst(FI_CONDITION); /* IF */ i = f_new_inst(FI_CONDITION); /* IF */
i->a1.p = $2; i->a1.p = $2;
i->a2.p = acc; i->a2.p = acc;
i->next = rej; i->a3.p = rej;
f->name = NULL; f->name = NULL;
f->root = i; f->root = i;
$$ = f; $$ = f;
@ -1005,12 +1005,10 @@ cmd:
$$->a2.p = $4; $$->a2.p = $4;
} }
| IF term THEN block ELSE block { | IF term THEN block ELSE block {
struct f_inst *i = f_new_inst(FI_CONDITION);
i->a1.p = $2;
i->a2.p = $4;
$$ = f_new_inst(FI_CONDITION); $$ = f_new_inst(FI_CONDITION);
$$->a1.p = i; $$->a1.p = $2;
$$->a2.p = $6; $$->a2.p = $4;
$$->a3.p = $6;
} }
| SYM '=' term ';' { | SYM '=' term ';' {
DBG( "Ook, we'll set value\n" ); DBG( "Ook, we'll set value\n" );

View file

@ -259,14 +259,12 @@
ARG_ANY(1); ARG_ANY(1);
val_format(v1, &fs->buf); val_format(v1, &fs->buf);
break; break;
case FI_CONDITION: /* ? has really strange error value, so we can implement if ... else nicely :-) */ case FI_CONDITION:
ARG(1, T_BOOL); ARG_T(1, 0, T_BOOL);
if (v1.val.i) { if (res.val.i)
ARG_ANY(2); ARG_ANY_T(2,0);
res.val.i = 0; else
} else ARG_ANY_T(3,0);
res.val.i = 1;
res.type = T_BOOL;
break; break;
case FI_NOP: case FI_NOP:
debug( "No operation\n" ); debug( "No operation\n" );

View file

@ -793,7 +793,7 @@ i_same(struct f_inst *f1, struct f_inst *f2)
return 0; return 0;
break; break;
case FI_PRINT: case FI_LENGTH: ONEARG; break; case FI_PRINT: case FI_LENGTH: ONEARG; break;
case FI_CONDITION: TWOARGS; break; case FI_CONDITION: THREEARGS; break;
case FI_NOP: case FI_EMPTY: break; case FI_NOP: case FI_EMPTY: break;
case FI_PRINT_AND_DIE: ONEARG; A2_SAME; break; case FI_PRINT_AND_DIE: ONEARG; A2_SAME; break;
case FI_PREF_GET: case FI_PREF_GET: