From d933ce70bd2c497e4af26483abafebfce436986e Mon Sep 17 00:00:00 2001 From: thing1 Date: Mon, 1 Jul 2024 11:36:45 +0100 Subject: updated work --- comp/lucas-standen-NEA/code/proto/Makefile | 4 + comp/lucas-standen-NEA/code/proto/ast | Bin 0 -> 24200 bytes comp/lucas-standen-NEA/code/proto/ast.c | 146 +++++++++++++++++++++++++ comp/lucas-standen-NEA/code/proto/ast/Makefile | 4 - comp/lucas-standen-NEA/code/proto/ast/ast | Bin 24200 -> 0 bytes comp/lucas-standen-NEA/code/proto/ast/ast.c | 146 ------------------------- comp/lucas-standen-NEA/code/proto/ast/astg.c | 52 --------- comp/lucas-standen-NEA/code/proto/ast/astg.h | 19 ---- comp/lucas-standen-NEA/code/proto/ast/left | 0 comp/lucas-standen-NEA/code/proto/ast/right | 0 comp/lucas-standen-NEA/code/proto/astg.c | 52 +++++++++ comp/lucas-standen-NEA/code/proto/astg.h | 19 ++++ comp/lucas-standen-NEA/code/proto/left | 0 comp/lucas-standen-NEA/code/proto/right | 0 14 files changed, 221 insertions(+), 221 deletions(-) create mode 100644 comp/lucas-standen-NEA/code/proto/Makefile create mode 100755 comp/lucas-standen-NEA/code/proto/ast create mode 100644 comp/lucas-standen-NEA/code/proto/ast.c delete mode 100644 comp/lucas-standen-NEA/code/proto/ast/Makefile delete mode 100755 comp/lucas-standen-NEA/code/proto/ast/ast delete mode 100644 comp/lucas-standen-NEA/code/proto/ast/ast.c delete mode 100644 comp/lucas-standen-NEA/code/proto/ast/astg.c delete mode 100644 comp/lucas-standen-NEA/code/proto/ast/astg.h delete mode 100644 comp/lucas-standen-NEA/code/proto/ast/left delete mode 100644 comp/lucas-standen-NEA/code/proto/ast/right create mode 100644 comp/lucas-standen-NEA/code/proto/astg.c create mode 100644 comp/lucas-standen-NEA/code/proto/astg.h create mode 100644 comp/lucas-standen-NEA/code/proto/left create mode 100644 comp/lucas-standen-NEA/code/proto/right (limited to 'comp/lucas-standen-NEA/code/proto') diff --git a/comp/lucas-standen-NEA/code/proto/Makefile b/comp/lucas-standen-NEA/code/proto/Makefile new file mode 100644 index 0000000..6074b00 --- /dev/null +++ b/comp/lucas-standen-NEA/code/proto/Makefile @@ -0,0 +1,4 @@ +all: astg ast.c + cc -ggdb ast.c astg.o -o ast +astg: astg.c + cc -ggdb astg.c -c -o astg.o diff --git a/comp/lucas-standen-NEA/code/proto/ast b/comp/lucas-standen-NEA/code/proto/ast new file mode 100755 index 0000000..f922f0d Binary files /dev/null and b/comp/lucas-standen-NEA/code/proto/ast differ diff --git a/comp/lucas-standen-NEA/code/proto/ast.c b/comp/lucas-standen-NEA/code/proto/ast.c new file mode 100644 index 0000000..d76107b --- /dev/null +++ b/comp/lucas-standen-NEA/code/proto/ast.c @@ -0,0 +1,146 @@ +#include +#include +#include + +#include "astg.h" + +void getBrackets(char *in, int bpos, char *out){ + // gets the content of the brackets that open at bpos + if (in[0] != '(' && in[0] != '[') + out = NULL; + + char *input = malloc(strlen(in) + 1); // cpy in for mem safety + char *Pinput = input; + memcpy(input, in, strlen(in) + 1); + + int i = 0; + while (i != bpos){ + input++; + i++; + } + i = 0; + int depth = 0; + while (input[0] != '\0'){ // loop through input + out[i] = input[0]; + if (input[0] == '(' || input[0] == '[') + depth++; + if (input[0] == ')' || input[0] == ']') + depth--; + if (depth == 0){ + i++; + break; + } + + input++; + i++; + } + + out[i] = '\0'; + free(Pinput); +} + +int getContents(char *brackets){ // get all the content in brackets + int i = 0; + char *num = malloc(strlen(brackets)); + while (brackets[0] != '\0'){ + if (brackets[0] != '[' && brackets[0] != ']'){ + num[i] = brackets[0]; + i++; + } + brackets++; + } + num[i] = '\0'; + int out = atoi(num); + free(num); + return out; +} + +ast_node *genAst(char *expression){ + ast_node *out = malloc(sizeof(ast_node)); + + /* take the expression + * get the first operation from it + * get the first number from after the expression + * if that number is another expression{ + * grab everything inside its braket and then call this function on it + * } + * get the second number from the expression + * if that number is another expression{ + * grab everything inside its braket and then call this function on it + * } + */ + + int i = 0, j = 0; + int currentTokenVal = -1; + + char *currentToken = malloc(strlen(expression)+1); + while (expression[i] != '\0'){ + if (expression[i] == '(' || expression[i] == '['){ + getBrackets(expression, i, currentToken); + currentTokenVal++; + + int depth = 0; + + if (i != 0){ + while (expression[i] != '\0'){ + if (expression[i] == '(' || expression[i] == '[') + depth++; + if (expression[i] == ')' || expression[i] == ']') + depth--; + if (depth == 0){ + break; + } + i++; + } + } + + if (currentTokenVal == 0){ + switch (currentToken[1]) { + case '+': + out->operation= ADD; + break; + case '-': + out->operation = SUB; + break; + case '*': + out->operation = MUL; + break; + case '/': + out->operation = DIV; + break; + } + } + + if (currentTokenVal == 1){ + if (currentToken[0] == '(') + out->left = genAst(currentToken); + else { + out->realLeft = getContents(currentToken); + out->left = NULL; + } + } else if (currentTokenVal == 2){ + if (currentToken[0] == '(') + out->right= genAst(currentToken); + else { + out->realRight = getContents(currentToken); + out->right = NULL; + } + } + } + i++; + } + free(currentToken); + + return out; +} + +int main(int argc, char **argv){ + if (argc < 2) // die if no argument given + exit(1); + + ast_node *head = genAst(argv[1]); + + printf("%d\n", exec(head)); + + freeAst(head); +} diff --git a/comp/lucas-standen-NEA/code/proto/ast/Makefile b/comp/lucas-standen-NEA/code/proto/ast/Makefile deleted file mode 100644 index 6074b00..0000000 --- a/comp/lucas-standen-NEA/code/proto/ast/Makefile +++ /dev/null @@ -1,4 +0,0 @@ -all: astg ast.c - cc -ggdb ast.c astg.o -o ast -astg: astg.c - cc -ggdb astg.c -c -o astg.o diff --git a/comp/lucas-standen-NEA/code/proto/ast/ast b/comp/lucas-standen-NEA/code/proto/ast/ast deleted file mode 100755 index 817c30e..0000000 Binary files a/comp/lucas-standen-NEA/code/proto/ast/ast and /dev/null differ diff --git a/comp/lucas-standen-NEA/code/proto/ast/ast.c b/comp/lucas-standen-NEA/code/proto/ast/ast.c deleted file mode 100644 index d76107b..0000000 --- a/comp/lucas-standen-NEA/code/proto/ast/ast.c +++ /dev/null @@ -1,146 +0,0 @@ -#include -#include -#include - -#include "astg.h" - -void getBrackets(char *in, int bpos, char *out){ - // gets the content of the brackets that open at bpos - if (in[0] != '(' && in[0] != '[') - out = NULL; - - char *input = malloc(strlen(in) + 1); // cpy in for mem safety - char *Pinput = input; - memcpy(input, in, strlen(in) + 1); - - int i = 0; - while (i != bpos){ - input++; - i++; - } - i = 0; - int depth = 0; - while (input[0] != '\0'){ // loop through input - out[i] = input[0]; - if (input[0] == '(' || input[0] == '[') - depth++; - if (input[0] == ')' || input[0] == ']') - depth--; - if (depth == 0){ - i++; - break; - } - - input++; - i++; - } - - out[i] = '\0'; - free(Pinput); -} - -int getContents(char *brackets){ // get all the content in brackets - int i = 0; - char *num = malloc(strlen(brackets)); - while (brackets[0] != '\0'){ - if (brackets[0] != '[' && brackets[0] != ']'){ - num[i] = brackets[0]; - i++; - } - brackets++; - } - num[i] = '\0'; - int out = atoi(num); - free(num); - return out; -} - -ast_node *genAst(char *expression){ - ast_node *out = malloc(sizeof(ast_node)); - - /* take the expression - * get the first operation from it - * get the first number from after the expression - * if that number is another expression{ - * grab everything inside its braket and then call this function on it - * } - * get the second number from the expression - * if that number is another expression{ - * grab everything inside its braket and then call this function on it - * } - */ - - int i = 0, j = 0; - int currentTokenVal = -1; - - char *currentToken = malloc(strlen(expression)+1); - while (expression[i] != '\0'){ - if (expression[i] == '(' || expression[i] == '['){ - getBrackets(expression, i, currentToken); - currentTokenVal++; - - int depth = 0; - - if (i != 0){ - while (expression[i] != '\0'){ - if (expression[i] == '(' || expression[i] == '[') - depth++; - if (expression[i] == ')' || expression[i] == ']') - depth--; - if (depth == 0){ - break; - } - i++; - } - } - - if (currentTokenVal == 0){ - switch (currentToken[1]) { - case '+': - out->operation= ADD; - break; - case '-': - out->operation = SUB; - break; - case '*': - out->operation = MUL; - break; - case '/': - out->operation = DIV; - break; - } - } - - if (currentTokenVal == 1){ - if (currentToken[0] == '(') - out->left = genAst(currentToken); - else { - out->realLeft = getContents(currentToken); - out->left = NULL; - } - } else if (currentTokenVal == 2){ - if (currentToken[0] == '(') - out->right= genAst(currentToken); - else { - out->realRight = getContents(currentToken); - out->right = NULL; - } - } - } - i++; - } - free(currentToken); - - return out; -} - -int main(int argc, char **argv){ - if (argc < 2) // die if no argument given - exit(1); - - ast_node *head = genAst(argv[1]); - - printf("%d\n", exec(head)); - - freeAst(head); -} diff --git a/comp/lucas-standen-NEA/code/proto/ast/astg.c b/comp/lucas-standen-NEA/code/proto/ast/astg.c deleted file mode 100644 index e96b771..0000000 --- a/comp/lucas-standen-NEA/code/proto/ast/astg.c +++ /dev/null @@ -1,52 +0,0 @@ -#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; -} - diff --git a/comp/lucas-standen-NEA/code/proto/ast/astg.h b/comp/lucas-standen-NEA/code/proto/ast/astg.h deleted file mode 100644 index 16250b2..0000000 --- a/comp/lucas-standen-NEA/code/proto/ast/astg.h +++ /dev/null @@ -1,19 +0,0 @@ -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); -void freeAst(ast_node *head); diff --git a/comp/lucas-standen-NEA/code/proto/ast/left b/comp/lucas-standen-NEA/code/proto/ast/left deleted file mode 100644 index e69de29..0000000 diff --git a/comp/lucas-standen-NEA/code/proto/ast/right b/comp/lucas-standen-NEA/code/proto/ast/right deleted file mode 100644 index e69de29..0000000 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 +#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; +} + diff --git a/comp/lucas-standen-NEA/code/proto/astg.h b/comp/lucas-standen-NEA/code/proto/astg.h new file mode 100644 index 0000000..16250b2 --- /dev/null +++ b/comp/lucas-standen-NEA/code/proto/astg.h @@ -0,0 +1,19 @@ +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); +void freeAst(ast_node *head); diff --git a/comp/lucas-standen-NEA/code/proto/left b/comp/lucas-standen-NEA/code/proto/left new file mode 100644 index 0000000..e69de29 diff --git a/comp/lucas-standen-NEA/code/proto/right b/comp/lucas-standen-NEA/code/proto/right new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3