From 4db750bbf03c9d7c18291fc63b7cf23cf966d6c8 Mon Sep 17 00:00:00 2001 From: thing1 Date: Mon, 8 Jul 2024 11:36:32 +0100 Subject: i forgot to commit last time i think --- comp/lucas-standen-NEA/code/proto/AST/astg.c | 52 ++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 comp/lucas-standen-NEA/code/proto/AST/astg.c (limited to 'comp/lucas-standen-NEA/code/proto/AST/astg.c') 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..e96b771 --- /dev/null +++ b/comp/lucas-standen-NEA/code/proto/AST/astg.c @@ -0,0 +1,52 @@ +#include +#include +#include + +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; +} + -- cgit v1.2.3