summaryrefslogtreecommitdiff
path: root/tree.c
diff options
context:
space:
mode:
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;
+ }
+ }
+}
+