summaryrefslogtreecommitdiff
path: root/tree.c
diff options
context:
space:
mode:
authorPedro Souza <pedro@masba.net>2024-04-04 01:52:22 -0300
committerPedro Souza <pedro@masba.net>2024-04-04 01:52:22 -0300
commit5a2f6687f681fa0ceff19a42ec9ddce479e67c48 (patch)
tree97fbf0acee446c267c2a71e243b239c4b61c8632 /tree.c
parentc5921aec313cefa8bd6e8f4c70f057c137133028 (diff)
added truth table and expression evaluatorHEADmaster
Diffstat (limited to 'tree.c')
-rw-r--r--tree.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/tree.c b/tree.c
index fbb561a..00d5eb9 100644
--- a/tree.c
+++ b/tree.c
@@ -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;
+ }
+ }
+}
+