From 5a2f6687f681fa0ceff19a42ec9ddce479e67c48 Mon Sep 17 00:00:00 2001 From: Pedro Souza Date: Thu, 4 Apr 2024 01:52:22 -0300 Subject: added truth table and expression evaluator --- tree.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'tree.c') diff --git a/tree.c b/tree.c index fbb561a..00d5eb9 100644 --- a/tree.c +++ b/tree.c @@ -1,5 +1,6 @@ #include #include +#include #include "tree.h" int strichar(const char *str, char c){ @@ -69,3 +70,25 @@ void free_node(node *n) { free(n); } +char eval_tree(node *root, uint32_t alph_bools) { + switch (root->type) { + case BIOP: { + char lhs = eval_tree(root->lhs, alph_bools); + char rhs = eval_tree(root->rhs, alph_bools); + switch (root->el) { + case '+': return lhs || rhs; + case '*': return lhs && rhs; + default: fprintf(stderr, "%s: unknown binary operation \"%c\"\n", __func__, root->el); + } + } + case UNOP: { + // We only have the not, FIXME if more are added + return !eval_tree(root->value, alph_bools); + } + case LTTR: { + char alph_pos = root->el - 'a'; + return (alph_bools & (1 << alph_pos)) > 0; + } + } +} + -- cgit v1.2.3