summaryrefslogtreecommitdiff
path: root/comp/lucas-standen-NEA/code/proto/ast/astg.c
diff options
context:
space:
mode:
authorstandenboy <standenboy@seacrossedlovers.xyz>2024-05-06 09:32:48 +0100
committerstandenboy <standenboy@seacrossedlovers.xyz>2024-05-06 09:32:48 +0100
commit319e7ea0a724cd97041c1aaf1281c4ca6aa688d1 (patch)
tree7411f4636ce9ce9733760666f3311a17b6e4d17c /comp/lucas-standen-NEA/code/proto/ast/astg.c
parent0ca35b27de52a5d3acf5f2eb877a440c1103e928 (diff)
added a load of stuff, and fixed a git conflict
Diffstat (limited to 'comp/lucas-standen-NEA/code/proto/ast/astg.c')
-rw-r--r--comp/lucas-standen-NEA/code/proto/ast/astg.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/comp/lucas-standen-NEA/code/proto/ast/astg.c b/comp/lucas-standen-NEA/code/proto/ast/astg.c
new file mode 100644
index 0000000..fc6fc8f
--- /dev/null
+++ b/comp/lucas-standen-NEA/code/proto/ast/astg.c
@@ -0,0 +1,45 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+typedef struct ast_node ast_node;
+
+typedef enum op {
+ ADD = 0,
+ SUB = 1,
+ MUL = 2,
+ DIV = 3,
+} op;
+
+typedef struct ast_node {
+ op operation;
+ int realLeft;
+ int realRight;
+ ast_node *right;
+ ast_node *left;
+} ast_node;
+
+int exec(ast_node *exp){
+ if (exp->left != NULL)
+ exp->realLeft = exec(exp->left);
+ if (exp->right != NULL)
+ exp->realRight = exec(exp->right);
+
+ if (exp->operation == ADD)
+ return exp->realLeft+ exp->realRight;
+ if (exp->operation == SUB)
+ return exp->realLeft - exp->realRight;
+ if (exp->operation == MUL)
+ return exp->realLeft * exp->realRight;
+ if (exp->operation == DIV)
+ return exp->realLeft/ exp->realRight;
+ return 0;
+}
+
+void freeAst(ast_node *head){
+ if (head->left != NULL)
+ freeAst(head->left);
+ if (head->right != NULL)
+ freeAst(head->left);
+ free(head);
+}