summaryrefslogtreecommitdiffstats
path: root/src/expr.h
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2024-01-07 12:19:17 -0500
committerTavian Barnes <tavianator@tavianator.com>2024-01-07 12:19:17 -0500
commit4a36bb92a5bbdc41965a6d2c6eae6cdca5983474 (patch)
tree1f2767e349ece3229a8da22ba58d905309e99dfa /src/expr.h
parent70acbc194fa1cc4972293d4e3affee5ba6fe5539 (diff)
downloadbfs-4a36bb92a5bbdc41965a6d2c6eae6cdca5983474.tar.xz
expr: Make expressions variadic
Rather than only unary/binary expressions, we now support an arbitrary number of children. The optimizer has been re-written almost completely and now supports optimal reordering of longer expression chains, rather than just arm-swapping. Fixes #85.
Diffstat (limited to 'src/expr.h')
-rw-r--r--src/expr.h28
1 files changed, 20 insertions, 8 deletions
diff --git a/src/expr.h b/src/expr.h
index 290f1f8..4d607a4 100644
--- a/src/expr.h
+++ b/src/expr.h
@@ -86,8 +86,10 @@ struct bfs_exprs {
* A command line expression.
*/
struct bfs_expr {
- /** The next allocated expression. */
+ /** This expression's next sibling, if any. */
struct bfs_expr *next;
+ /** The next allocated expression. */
+ struct { struct bfs_expr *next; } freelist;
/** The function that evaluates this expression. */
bfs_eval_fn *eval_fn;
@@ -123,12 +125,7 @@ struct bfs_expr {
/** Auxilliary data for the evaluation function. */
union {
/** Child expressions. */
- struct {
- /** The left hand side of the expression. */
- struct bfs_expr *lhs;
- /** The right hand side of the expression. */
- struct bfs_expr *rhs;
- };
+ struct bfs_exprs children;
/** Integer comparisons. */
struct {
@@ -218,11 +215,26 @@ struct bfs_ctx;
struct bfs_expr *bfs_expr_new(struct bfs_ctx *ctx, bfs_eval_fn *eval, size_t argc, char **argv);
/**
- * @return Whether the expression has child expressions.
+ * @return Whether this type of expression has children.
*/
bool bfs_expr_is_parent(const struct bfs_expr *expr);
/**
+ * @return The first child of this expression, or NULL if it has none.
+ */
+struct bfs_expr *bfs_expr_children(const struct bfs_expr *expr);
+
+/**
+ * Add a child to an expression.
+ */
+void bfs_expr_append(struct bfs_expr *expr, struct bfs_expr *child);
+
+/**
+ * Add a list of children to an expression.
+ */
+void bfs_expr_extend(struct bfs_expr *expr, struct bfs_exprs *children);
+
+/**
* @return Whether expr is known to always quit.
*/
bool bfs_expr_never_returns(const struct bfs_expr *expr);