diff options
author | Pedro Souza <pedro@masba.net> | 2024-04-04 01:52:22 -0300 |
---|---|---|
committer | Pedro Souza <pedro@masba.net> | 2024-04-04 01:52:22 -0300 |
commit | 5a2f6687f681fa0ceff19a42ec9ddce479e67c48 (patch) | |
tree | 97fbf0acee446c267c2a71e243b239c4b61c8632 /tree.c | |
parent | c5921aec313cefa8bd6e8f4c70f057c137133028 (diff) |
Diffstat (limited to 'tree.c')
-rw-r--r-- | tree.c | 23 |
1 files changed, 23 insertions, 0 deletions
@@ -1,5 +1,6 @@ #include <stdio.h> #include <stdlib.h> +#include <stdint.h> #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; + } + } +} + |