Skip to content

Commit

Permalink
filterx/expr-conditional: use optimize()
Browse files Browse the repository at this point in the history
Signed-off-by: Balazs Scheidler <[email protected]>
  • Loading branch information
bazsi committed Dec 28, 2024
1 parent 6090295 commit af26e95
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 64 deletions.
87 changes: 44 additions & 43 deletions lib/filterx/expr-condition.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,6 @@ struct _FilterXConditional
FilterXExpr *false_branch;
};

static FilterXExpr *
_optimize(FilterXExpr *s)
{
FilterXConditional *self = (FilterXConditional *) s;

self->true_branch = filterx_expr_optimize(self->true_branch);
self->false_branch = filterx_expr_optimize(self->false_branch);
return NULL;
}

static gboolean
_init(FilterXExpr *s, GlobalConfig *cfg)
Expand Down Expand Up @@ -155,6 +146,50 @@ _eval(FilterXExpr *s)
return result;
}

static void
_optimize_branches(FilterXExpr *s)
{
FilterXConditional *self = (FilterXConditional *) s;

self->condition = filterx_expr_optimize(self->condition);
self->true_branch = filterx_expr_optimize(self->true_branch);
self->false_branch = filterx_expr_optimize(self->false_branch);
}

static FilterXExpr *
_optimize(FilterXExpr *s)
{
FilterXConditional *self = (FilterXConditional *) s;

_optimize_branches(s);

if (!filterx_expr_is_literal(self->condition))
return FALSE;

FilterXObject *condition_value = filterx_expr_eval(self->condition);

g_assert(condition_value);
gboolean condition_truthy = filterx_object_truthy(condition_value);
filterx_object_unref(condition_value);

if (condition_truthy)
{
if (self->true_branch)
return filterx_expr_ref(self->true_branch);
else
return filterx_expr_ref(self->condition);
}
else
{
if (self->false_branch)
return filterx_expr_ref(self->false_branch);
else
return filterx_literal_new(filterx_boolean_new(TRUE));
}

return NULL;
}

void
filterx_conditional_set_true_branch(FilterXExpr *s, FilterXExpr *true_branch)
{
Expand Down Expand Up @@ -207,37 +242,3 @@ filterx_conditional_find_tail(FilterXExpr *s)
}
return s;
}

FilterXExpr *
filterx_literal_conditional(FilterXExpr *condition, FilterXExpr *true_branch, FilterXExpr *false_branch)
{
g_assert(filterx_expr_is_literal(condition));

FilterXObject *condition_value = filterx_expr_eval(condition);
g_assert(condition_value);

FilterXExpr *optimized = NULL;

if (filterx_object_truthy(condition_value))
{
if (true_branch)
optimized = true_branch;
else
optimized = filterx_expr_ref(condition);

filterx_expr_unref(false_branch);
}
else
{
if (false_branch)
optimized = false_branch;
else
optimized = filterx_literal_new(filterx_boolean_new(TRUE));

filterx_expr_unref(true_branch);
}

filterx_object_unref(condition_value);
filterx_expr_unref(condition);
return optimized;
}
2 changes: 0 additions & 2 deletions lib/filterx/expr-condition.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,4 @@ void filterx_conditional_set_false_branch(FilterXExpr *s, FilterXExpr *false_bra
FilterXExpr *filterx_conditional_find_tail(FilterXExpr *s);
FilterXExpr *filterx_conditional_new(FilterXExpr *condition);

FilterXExpr *filterx_literal_conditional(FilterXExpr *condition, FilterXExpr *true_branch, FilterXExpr *false_branch);

#endif
24 changes: 5 additions & 19 deletions lib/filterx/filterx-grammar.ym
Original file line number Diff line number Diff line change
Expand Up @@ -641,29 +641,15 @@ ternary
: expr '?' expr ':' expr
{
filterx_expr_set_location($1, lexer, &@1);
if (filterx_expr_is_literal($1))
{
$$ = filterx_literal_conditional($1, $3, $5);
}
else
{
$$ = filterx_conditional_new($1);
filterx_conditional_set_true_branch($$, $3);
filterx_conditional_set_false_branch($$, $5);
}
$$ = filterx_conditional_new($1);
filterx_conditional_set_true_branch($$, $3);
filterx_conditional_set_false_branch($$, $5);
}
| expr '?' ':' expr
{
filterx_expr_set_location($1, lexer, &@1);
if (filterx_expr_is_literal($1))
{
$$ = filterx_literal_conditional($1, NULL, $4);
}
else
{
$$ = filterx_conditional_new($1);
filterx_conditional_set_false_branch($$, $4);
}
$$ = filterx_conditional_new($1);
filterx_conditional_set_false_branch($$, $4);
}
;

Expand Down

0 comments on commit af26e95

Please sign in to comment.