summaryrefslogtreecommitdiff
path: root/comp/lucas-standen-NEA/code/proto/astg.c
diff options
context:
space:
mode:
authorthing1 <thing1@seacrossedlovers.xyz>2024-07-01 11:36:45 +0100
committerthing1 <thing1@seacrossedlovers.xyz>2024-07-01 11:36:45 +0100
commitd933ce70bd2c497e4af26483abafebfce436986e (patch)
treeb15f4f2c297734108ff0717334b912b87a0f995b /comp/lucas-standen-NEA/code/proto/astg.c
parent42047fea26b14edc67b394db18ce7edb0c6399f8 (diff)
updated work
Diffstat (limited to 'comp/lucas-standen-NEA/code/proto/astg.c')
-rw-r--r--comp/lucas-standen-NEA/code/proto/astg.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/comp/lucas-standen-NEA/code/proto/astg.c b/comp/lucas-standen-NEA/code/proto/astg.c
new file mode 100644
index 0000000..e96b771
--- /dev/null
+++ b/comp/lucas-standen-NEA/code/proto/astg.c
@@ -0,0 +1,52 @@
+#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;
+
+void freeAst(ast_node *head){
+ if (head->left != NULL)
+ freeAst(head->left);
+ if (head->right != NULL)
+ freeAst(head->left);
+ free(head);
+}
+
+int exec(ast_node *exp){
+ if (exp->left != NULL){
+ exp->realLeft = exec(exp->left);
+ freeAst(exp->left);
+ exp->left = NULL;
+ }
+ if (exp->right != NULL){
+ exp->realRight = exec(exp->right);
+ freeAst(exp->right);
+ exp->right = NULL;
+ }
+
+ 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;
+}
+