Skip to content

Commit

Permalink
expr: Tell expressions what kind of expression they are
Browse files Browse the repository at this point in the history
  • Loading branch information
tavianator committed Aug 15, 2024
1 parent 50dd5af commit 9749af0
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 200 deletions.
22 changes: 14 additions & 8 deletions src/color.c
Original file line number Diff line number Diff line change
Expand Up @@ -1136,14 +1136,20 @@ static int print_expr(CFILE *cfile, const struct bfs_expr *expr, bool verbose, i
return -1;
}

if (bfs_expr_is_parent(expr)) {
if (cbuff(cfile, "${red}%pq${rs}", expr->argv[0]) < 0) {
return -1;
}
} else {
if (cbuff(cfile, "${blu}%pq${rs}", expr->argv[0]) < 0) {
return -1;
}
int ret;
switch (expr->kind) {
case BFS_FLAG:
ret = cbuff(cfile, "${cyn}%pq${rs}", expr->argv[0]);
break;
case BFS_OPERATOR:
ret = cbuff(cfile, "${red}%pq${rs}", expr->argv[0]);
break;
default:
ret = cbuff(cfile, "${blu}%pq${rs}", expr->argv[0]);
break;
}
if (ret < 0) {
return -1;
}

for (size_t i = 1; i < expr->argc; ++i) {
Expand Down
5 changes: 4 additions & 1 deletion src/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
#include "xregex.h"
#include <string.h>

struct bfs_expr *bfs_expr_new(struct bfs_ctx *ctx, bfs_eval_fn *eval_fn, size_t argc, char **argv) {
struct bfs_expr *bfs_expr_new(struct bfs_ctx *ctx, bfs_eval_fn *eval_fn, size_t argc, char **argv, enum bfs_kind kind) {
bfs_assert(kind != BFS_PATH);

struct bfs_expr *expr = arena_alloc(&ctx->expr_arena);
if (!expr) {
return NULL;
Expand All @@ -22,6 +24,7 @@ struct bfs_expr *bfs_expr_new(struct bfs_ctx *ctx, bfs_eval_fn *eval_fn, size_t
expr->eval_fn = eval_fn;
expr->argc = argc;
expr->argv = argv;
expr->kind = kind;
expr->probability = 0.5;
SLIST_PREPEND(&ctx->expr_list, expr, freelist);

Expand Down
25 changes: 24 additions & 1 deletion src/expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,27 @@
#include <sys/types.h>
#include <time.h>

/**
* Argument/token/expression kinds.
*/
enum bfs_kind {
/** A flag (-H, -L, etc.). */
BFS_FLAG,

/** A root path. */
BFS_PATH,

/** An option (-follow, -mindepth, etc.). */
BFS_OPTION,
/** A test (-name, -size, etc.). */
BFS_TEST,
/** An action (-print, -exec, etc.). */
BFS_ACTION,

/** An operator (-and, -or, etc.). */
BFS_OPERATOR,
};

/**
* Integer comparison modes.
*/
Expand Down Expand Up @@ -97,6 +118,8 @@ struct bfs_expr {
size_t argc;
/** The command line arguments comprising this expression. */
char **argv;
/** The kind of expression this is. */
enum bfs_kind kind;

/** The number of files this expression keeps open between evaluations. */
int persistent_fds;
Expand Down Expand Up @@ -207,7 +230,7 @@ struct bfs_ctx;
/**
* Create a new expression.
*/
struct bfs_expr *bfs_expr_new(struct bfs_ctx *ctx, bfs_eval_fn *eval, size_t argc, char **argv);
struct bfs_expr *bfs_expr_new(struct bfs_ctx *ctx, bfs_eval_fn *eval, size_t argc, char **argv, enum bfs_kind kind);

/**
* @return Whether this type of expression has children.
Expand Down
4 changes: 2 additions & 2 deletions src/opt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,7 @@ static struct bfs_expr *opt_const(struct bfs_opt *opt, bool value) {
static bfs_eval_fn *const fns[] = {eval_false, eval_true};
static char *fake_args[] = {"-false", "-true"};

struct bfs_expr *expr = bfs_expr_new(opt->ctx, fns[value], 1, &fake_args[value]);
struct bfs_expr *expr = bfs_expr_new(opt->ctx, fns[value], 1, &fake_args[value], BFS_TEST);
return visit_shallow(opt, expr, &annotate);
}

Expand All @@ -1351,7 +1351,7 @@ static struct bfs_expr *negate_expr(struct bfs_opt *opt, struct bfs_expr *expr,
return opt_const(opt, true);
}

struct bfs_expr *ret = bfs_expr_new(opt->ctx, eval_not, 1, argv);
struct bfs_expr *ret = bfs_expr_new(opt->ctx, eval_not, 1, argv, BFS_OPERATOR);
if (!ret) {
return NULL;
}
Expand Down
Loading

0 comments on commit 9749af0

Please sign in to comment.