diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2023-07-21 10:11:20 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2023-12-20 20:19:18 -0500 |
commit | 8f004e73238c5ee4be40c044827138eb5895ce88 (patch) | |
tree | c93e247d9adc7074033f9e88c8071981092ea8b1 /src | |
parent | cbbcb340c974fca7312713d2515e3b32bacb176f (diff) | |
download | bfs-8f004e73238c5ee4be40c044827138eb5895ce88.tar.xz |
expr: Move some implementation into expr.c
Diffstat (limited to 'src')
-rw-r--r-- | src/expr.c | 56 | ||||
-rw-r--r-- | src/parse.c | 45 |
2 files changed, 56 insertions, 45 deletions
diff --git a/src/expr.c b/src/expr.c new file mode 100644 index 0000000..380038c --- /dev/null +++ b/src/expr.c @@ -0,0 +1,56 @@ +// Copyright © Tavian Barnes <tavianator@tavianator.com> +// SPDX-License-Identifier: 0BSD + +#include "expr.h" +#include "alloc.h" +#include "eval.h" +#include "exec.h" +#include "printf.h" +#include "xregex.h" +#include <stdio.h> +#include <stdlib.h> + +struct bfs_expr *bfs_expr_new(bfs_eval_fn *eval_fn, size_t argc, char **argv) { + struct bfs_expr *expr = ZALLOC(struct bfs_expr); + if (!expr) { + perror("zalloc()"); + return NULL; + } + + expr->eval_fn = eval_fn; + expr->argc = argc; + expr->argv = argv; + expr->probability = 0.5; + return expr; +} + +bool bfs_expr_is_parent(const struct bfs_expr *expr) { + return expr->eval_fn == eval_and + || expr->eval_fn == eval_or + || expr->eval_fn == eval_not + || expr->eval_fn == eval_comma; +} + +bool bfs_expr_never_returns(const struct bfs_expr *expr) { + // Expressions that never return are vacuously both always true and always false + return expr->always_true && expr->always_false; +} + +void bfs_expr_free(struct bfs_expr *expr) { + if (!expr) { + return; + } + + if (bfs_expr_is_parent(expr)) { + bfs_expr_free(expr->rhs); + bfs_expr_free(expr->lhs); + } else if (expr->eval_fn == eval_exec) { + bfs_exec_free(expr->exec); + } else if (expr->eval_fn == eval_fprintf) { + bfs_printf_free(expr->printf); + } else if (expr->eval_fn == eval_regex) { + bfs_regfree(expr->regex); + } + + free(expr); +} diff --git a/src/parse.c b/src/parse.c index 8d9c3f0..2d7b3a9 100644 --- a/src/parse.c +++ b/src/parse.c @@ -54,51 +54,6 @@ static char *fake_or_arg = "-o"; static char *fake_print_arg = "-print"; static char *fake_true_arg = "-true"; -struct bfs_expr *bfs_expr_new(bfs_eval_fn *eval_fn, size_t argc, char **argv) { - struct bfs_expr *expr = ZALLOC(struct bfs_expr); - if (!expr) { - perror("zalloc()"); - return NULL; - } - - expr->eval_fn = eval_fn; - expr->argc = argc; - expr->argv = argv; - expr->probability = 0.5; - return expr; -} - -bool bfs_expr_is_parent(const struct bfs_expr *expr) { - return expr->eval_fn == eval_and - || expr->eval_fn == eval_or - || expr->eval_fn == eval_not - || expr->eval_fn == eval_comma; -} - -bool bfs_expr_never_returns(const struct bfs_expr *expr) { - // Expressions that never return are vacuously both always true and always false - return expr->always_true && expr->always_false; -} - -void bfs_expr_free(struct bfs_expr *expr) { - if (!expr) { - return; - } - - if (bfs_expr_is_parent(expr)) { - bfs_expr_free(expr->rhs); - bfs_expr_free(expr->lhs); - } else if (expr->eval_fn == eval_exec) { - bfs_exec_free(expr->exec); - } else if (expr->eval_fn == eval_fprintf) { - bfs_printf_free(expr->printf); - } else if (expr->eval_fn == eval_regex) { - bfs_regfree(expr->regex); - } - - free(expr); -} - /** * Create a new unary expression. */ |