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/Makefile | 8 ++ comp/lucas-standen-NEA/code/TODO | 10 ++ comp/lucas-standen-NEA/code/ads/ll/Makefile | 2 +- comp/lucas-standen-NEA/code/execution/Makefile | 6 + comp/lucas-standen-NEA/code/execution/builtin.c | 45 +++++++ comp/lucas-standen-NEA/code/execution/builtin.h | 7 + comp/lucas-standen-NEA/code/execution/exec | Bin 0 -> 30040 bytes comp/lucas-standen-NEA/code/execution/exec.c | 24 ++++ comp/lucas-standen-NEA/code/execution/types.c | 0 comp/lucas-standen-NEA/code/execution/types.h | 0 comp/lucas-standen-NEA/code/global/Makefile | 4 + comp/lucas-standen-NEA/code/global/types.h | 64 +++++++++ comp/lucas-standen-NEA/code/global/util.c | 73 +++++++++++ comp/lucas-standen-NEA/code/global/util.h | 13 ++ 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 comp/lucas-standen-NEA/code/tokenizer/Makefile | 11 +- comp/lucas-standen-NEA/code/tokenizer/parser.c | 5 +- comp/lucas-standen-NEA/code/tokenizer/sample.zpy | 5 - comp/lucas-standen-NEA/code/tokenizer/tokenizer | Bin 42264 -> 0 bytes comp/lucas-standen-NEA/code/tokenizer/tokenizer.c | 98 +++++++++----- comp/lucas-standen-NEA/code/tokenizer/tokenizer.h | 12 ++ comp/lucas-standen-NEA/code/tokenizer/types.h | 76 ----------- comp/lucas-standen-NEA/code/tokenizer/util.c | 65 --------- comp/lucas-standen-NEA/code/tokenizer/util.h | 10 -- comp/lucas-standen-NEA/writeup/coverpage.ps | 2 +- .../lucas-standen-NEA/writeup/questions-for-amy.ps | 2 +- 39 files changed, 564 insertions(+), 420 deletions(-) create mode 100644 comp/lucas-standen-NEA/code/TODO create mode 100644 comp/lucas-standen-NEA/code/execution/builtin.c create mode 100644 comp/lucas-standen-NEA/code/execution/builtin.h create mode 100755 comp/lucas-standen-NEA/code/execution/exec delete mode 100644 comp/lucas-standen-NEA/code/execution/types.c delete mode 100644 comp/lucas-standen-NEA/code/execution/types.h create mode 100644 comp/lucas-standen-NEA/code/global/Makefile create mode 100644 comp/lucas-standen-NEA/code/global/types.h create mode 100644 comp/lucas-standen-NEA/code/global/util.c create mode 100644 comp/lucas-standen-NEA/code/global/util.h 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 delete mode 100644 comp/lucas-standen-NEA/code/tokenizer/sample.zpy delete mode 100755 comp/lucas-standen-NEA/code/tokenizer/tokenizer create mode 100644 comp/lucas-standen-NEA/code/tokenizer/tokenizer.h delete mode 100644 comp/lucas-standen-NEA/code/tokenizer/types.h delete mode 100644 comp/lucas-standen-NEA/code/tokenizer/util.c delete mode 100644 comp/lucas-standen-NEA/code/tokenizer/util.h diff --git a/comp/lucas-standen-NEA/code/Makefile b/comp/lucas-standen-NEA/code/Makefile index e69de29..d8330a0 100644 --- a/comp/lucas-standen-NEA/code/Makefile +++ b/comp/lucas-standen-NEA/code/Makefile @@ -0,0 +1,8 @@ +all: + export FLAGS="-ggdb" + cd global; make + cd execution; make + cd tokenizer; make + cd ads; make + cd proto; make + $(info done!) diff --git a/comp/lucas-standen-NEA/code/TODO b/comp/lucas-standen-NEA/code/TODO new file mode 100644 index 0000000..5d3078a --- /dev/null +++ b/comp/lucas-standen-NEA/code/TODO @@ -0,0 +1,10 @@ +MAKE LITERAL ARGS A UNION +something like this +union { + i32 i32Value; + u32 u32Value; + 164 i32Value; + u64 u32Value; + char charValue; + float floatValue; +} diff --git a/comp/lucas-standen-NEA/code/ads/ll/Makefile b/comp/lucas-standen-NEA/code/ads/ll/Makefile index aaf0241..e91e445 100644 --- a/comp/lucas-standen-NEA/code/ads/ll/Makefile +++ b/comp/lucas-standen-NEA/code/ads/ll/Makefile @@ -1,5 +1,5 @@ all: ll.c - cc ll.c -c -o ll.o + cc ll.c -c -o ll.o test: all cc ll.o lltest.c -o lltest diff --git a/comp/lucas-standen-NEA/code/execution/Makefile b/comp/lucas-standen-NEA/code/execution/Makefile index e69de29..405f9de 100644 --- a/comp/lucas-standen-NEA/code/execution/Makefile +++ b/comp/lucas-standen-NEA/code/execution/Makefile @@ -0,0 +1,6 @@ +all: exec builtin + $(info done execution!) +exec: exec.c builtin + cc exec.c builtin.o ../tokenizer/tokenizer.o ../global/util.o -o exec -ggdb +builtin: builtin.c + cc builtin.c -c -o builtin.o -ggdb diff --git a/comp/lucas-standen-NEA/code/execution/builtin.c b/comp/lucas-standen-NEA/code/execution/builtin.c new file mode 100644 index 0000000..df3ca03 --- /dev/null +++ b/comp/lucas-standen-NEA/code/execution/builtin.c @@ -0,0 +1,45 @@ +#include +#include +#include "../global/types.h" +#include "../global/util.h" + +#define MAXARGS 8 + +void *doCall(ast_node *node){ + builtInFuncs id = node->func->builtInFunc; + for (int i = 0; i < MAXARGS; i++){ + if (node->args[i] != NULL){ + node->literalArgs[i] = doCall(node->args[i]); + } + } + + char *str = CheckedMalloc(20); + switch (id){ + case ADD: + snprintf(str, 20, "%d", atoi(node->literalArgs[0]) + atoi(node->literalArgs[1])); + return str; + break; + case SUB: + snprintf(str, 20, "%d", atoi(node->literalArgs[0]) - atoi(node->literalArgs[1])); + return str; + break; + case DIV: + snprintf(str, 20, "%d", atoi(node->literalArgs[0]) / atoi(node->literalArgs[1])); + return str; + break; + case MUL: + snprintf(str, 20, "%d", atoi(node->literalArgs[0]) * atoi(node->literalArgs[1])); + return str; + break; + + case WRITE: + fputs(node->literalArgs[0], stdout); + break; + + case EXIT: + int returnValue = atoi(node->literalArgs[0]); + CheckedFreeALL(); + exit(returnValue); + break; + } +} diff --git a/comp/lucas-standen-NEA/code/execution/builtin.h b/comp/lucas-standen-NEA/code/execution/builtin.h new file mode 100644 index 0000000..46ffde5 --- /dev/null +++ b/comp/lucas-standen-NEA/code/execution/builtin.h @@ -0,0 +1,7 @@ +#include +#include "../global/types.h" +#include "../global/util.h" + +#define MAXARGS 8 + +void *doCall(ast_node *node); diff --git a/comp/lucas-standen-NEA/code/execution/exec b/comp/lucas-standen-NEA/code/execution/exec new file mode 100755 index 0000000..ca6eb33 Binary files /dev/null and b/comp/lucas-standen-NEA/code/execution/exec differ diff --git a/comp/lucas-standen-NEA/code/execution/exec.c b/comp/lucas-standen-NEA/code/execution/exec.c index e69de29..fb85d3f 100644 --- a/comp/lucas-standen-NEA/code/execution/exec.c +++ b/comp/lucas-standen-NEA/code/execution/exec.c @@ -0,0 +1,24 @@ +#include +#include + +#include "./builtin.h" + +#include "../global/util.h" + +#include "../tokenizer/tokenizer.h" + +int main(){ + char *sample = "(write (* 2 5))"; + ast_node *root = tokenize(sample); + doCall(root); + sample = "(write \n)"; + root = tokenize(sample); + doCall(root); + sample = "(exit 0)"; + root = tokenize(sample); + doCall(root); + + CheckedFreeALL(); + + return 0; +} diff --git a/comp/lucas-standen-NEA/code/execution/types.c b/comp/lucas-standen-NEA/code/execution/types.c deleted file mode 100644 index e69de29..0000000 diff --git a/comp/lucas-standen-NEA/code/execution/types.h b/comp/lucas-standen-NEA/code/execution/types.h deleted file mode 100644 index e69de29..0000000 diff --git a/comp/lucas-standen-NEA/code/global/Makefile b/comp/lucas-standen-NEA/code/global/Makefile new file mode 100644 index 0000000..dbff2b0 --- /dev/null +++ b/comp/lucas-standen-NEA/code/global/Makefile @@ -0,0 +1,4 @@ +all: util + $(info done!) +util: util.c + cc util.c -c -o util.o diff --git a/comp/lucas-standen-NEA/code/global/types.h b/comp/lucas-standen-NEA/code/global/types.h new file mode 100644 index 0000000..e73d55d --- /dev/null +++ b/comp/lucas-standen-NEA/code/global/types.h @@ -0,0 +1,64 @@ +#include +#include "../ads/ll/ll.h" + +// int types +typedef int32_t i32; +typedef int64_t i64; + +// uint types +typedef uint32_t u32; +typedef uint64_t u64; + +// char and float types are still called char and float so no typedef needed + +// built in functions +typedef enum builtInFuncs { + // general + DEFUN = 0, + LET = 1, + SET = 2, + IF = 3, + ELIF = 4, + ELSE = 5, + FOR = 6, + WHILE = 7, + SYMBOL = 8, + + // arithmetic + ADD = 10, + SUB = 11, + MUL = 12, + DIV = 13, + + // comparison + EQ = 14, + NEQ = 15, + GT = 16, + LT = 17, + GTEQ = 18, + LTEQ = 19, + + // misc + CAST = 20, + TYPEOF = 21, + EXIT = 22, + RETURN = 23, + WRITE = 24, + NIL = -1, +} builtInFuncs; + +// function type +typedef struct functionToken { + int id; // a function id to avoid strings + char *name; // the code for the function + builtInFuncs builtInFunc; // a built in functions +} functionToken; + +typedef struct ast_node ast_node; + +typedef struct ast_node { + functionToken *func; // if it's not builtin then use this + char **literalArgs; // the args of the node, this will be an array of litteral values + ast_node **args; // the non litteral tokens + // if litteralArgs[x] is real then args[x] should be NULL, and vice versa +} ast_node; diff --git a/comp/lucas-standen-NEA/code/global/util.c b/comp/lucas-standen-NEA/code/global/util.c new file mode 100644 index 0000000..736619b --- /dev/null +++ b/comp/lucas-standen-NEA/code/global/util.c @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include +#include + +// functions for user +void Die(); // brings down the program +void *CheckedMalloc(long size); // malloc checked with autofree +void *CheckedRealloc(void *out, long size); // realloc checked with autofree +int CheckedFree(void *ptr); // frees a pointer if it is in the array MEMptrs +void CheckedFreeALL(); // frees all pointers in the array MEMptrs + +#define MAXPTRS 30000 // maximum allocs done by user + +void *MEMptrs[MAXPTRS] = { NULL }; +size_t currentPtr = 0; + +void Die(){ + perror("zpy parser"); + exit(errno); +} + +void *CheckedMalloc(long size){ + void *out = malloc(size); + if (out == NULL) + Die(); + MEMptrs[currentPtr] = out; + currentPtr++; + if (currentPtr > MAXPTRS){ + printf("used %d ptrs!\n", MAXPTRS); + Die(); + } + return out; +} + +void *CheckedRealloc(void *orig, long size){ + void *out = realloc(orig, size); + if (out == NULL) + Die(); + MEMptrs[currentPtr] = out; + currentPtr++; + if (currentPtr > MAXPTRS){ + printf("used %d ptrs!\n", MAXPTRS); + Die(); + } + + for (int i = 0; i < MAXPTRS; i++) + if (MEMptrs[i] == orig && MEMptrs[i] != NULL) MEMptrs[i] = NULL; + + return out; +} + +int CheckedFree(void *ptr){ + if (ptr == NULL) return 1; + for (int i = 0; i < MAXPTRS; i++){ + if (MEMptrs[i] == ptr){ + free(MEMptrs[i]); + MEMptrs[i] = NULL; + return 0; + } + } + return 1; +} + +void CheckedFreeALL(){ + for (int i = 0; i < MAXPTRS; i++){ + if (MEMptrs[i] != NULL){ + free(MEMptrs[i]); + } + } +} diff --git a/comp/lucas-standen-NEA/code/global/util.h b/comp/lucas-standen-NEA/code/global/util.h new file mode 100644 index 0000000..8feed88 --- /dev/null +++ b/comp/lucas-standen-NEA/code/global/util.h @@ -0,0 +1,13 @@ +#include +#include +#include +#include +#include +#include + +// functions for user +void Die(); // brings down the program +void *CheckedMalloc(long size); // malloc checked with autofree +void *CheckedRealloc(void *out, long size); // realloc checked with autofree +int CheckedFree(void *ptr); // frees a pointer if it is in the array MEMptrs +void CheckedFreeALL(); // frees all pointers in the array MEMptrs 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 diff --git a/comp/lucas-standen-NEA/code/tokenizer/Makefile b/comp/lucas-standen-NEA/code/tokenizer/Makefile index b09f177..73e4033 100644 --- a/comp/lucas-standen-NEA/code/tokenizer/Makefile +++ b/comp/lucas-standen-NEA/code/tokenizer/Makefile @@ -1,10 +1,9 @@ -tokenizer: parser util tokenizer.c - cc -O3 tokenizer.c parser.o util.o -o tokenizer -ggdb +all: tokenizer parser + $(info done tokenizer!) +tokenizer: tokenizer.c + cc tokenizer.c -c -o tokenizer.o parser: parser.c - cc -O3 parser.c -c -o parser.o -ggdb -util: util.c - cc -O3 util.c -c -o util.o -ggdb + cc parser.c -c -o parser.o clean: rm -rf *.o - rm -rf tokenizer diff --git a/comp/lucas-standen-NEA/code/tokenizer/parser.c b/comp/lucas-standen-NEA/code/tokenizer/parser.c index 9ac9fde..69ec458 100644 --- a/comp/lucas-standen-NEA/code/tokenizer/parser.c +++ b/comp/lucas-standen-NEA/code/tokenizer/parser.c @@ -1,11 +1,10 @@ #include #include -#include "util.h" - -char *Parse(char *fileName); // general parser function +#include "../global/util.h" char *ReadFile(char *fileName); // reads the file into a single var +char *Parse(char *fileName); // general parser function char *ReadFile(char *filename){ FILE *f = fopen(filename, "r"); diff --git a/comp/lucas-standen-NEA/code/tokenizer/sample.zpy b/comp/lucas-standen-NEA/code/tokenizer/sample.zpy deleted file mode 100644 index f0d9700..0000000 --- a/comp/lucas-standen-NEA/code/tokenizer/sample.zpy +++ /dev/null @@ -1,5 +0,0 @@ -(let x:char[] "he -llo") - -(let y:i32 20) - diff --git a/comp/lucas-standen-NEA/code/tokenizer/tokenizer b/comp/lucas-standen-NEA/code/tokenizer/tokenizer deleted file mode 100755 index 2d2f1c0..0000000 Binary files a/comp/lucas-standen-NEA/code/tokenizer/tokenizer and /dev/null differ diff --git a/comp/lucas-standen-NEA/code/tokenizer/tokenizer.c b/comp/lucas-standen-NEA/code/tokenizer/tokenizer.c index 080951b..a76760b 100644 --- a/comp/lucas-standen-NEA/code/tokenizer/tokenizer.c +++ b/comp/lucas-standen-NEA/code/tokenizer/tokenizer.c @@ -1,12 +1,24 @@ #include #include -#include "types.h" -#include "util.h" +#include "../global/types.h" +#include "../global/util.h" #define MAXARGS 8 +#define MAXFUNCS 2048 +#define MAXVARS 8192 -int getBuiltIn(char *func, ast_node *node){ +char *userDefinedFunctions[MAXFUNCS]; +char *userDefinedVars[MAXVARS]; +size_t userFuncCount = 0; +size_t userVarCount = 0; + +int getBuiltIn(char *func, ast_node *node); // checks if a function is built in to zippy +void expressFunction(char *function, ast_node *node); // puts a string into the ast_node struct +ast_node *tokenize(char *input); // does the tokenization +void printAst(ast_node *root); // shows an ast and its sub nodes + +int getBuiltIn(char *func, ast_node *node){ // returns NIL when the function doesn't exist if (strcmp(func, "defun") == 0){ node->func->builtInFunc= DEFUN; }else if (strcmp(func, "let") == 0){ @@ -53,6 +65,8 @@ int getBuiltIn(char *func, ast_node *node){ node->func->builtInFunc = EXIT; }else if (strcmp(func, "return") == 0){ node->func->builtInFunc = RETURN; + }else if (strcmp(func, "write") == 0){ + node->func->builtInFunc = WRITE; }else { node->func->builtInFunc = NIL; return -1; @@ -60,24 +74,10 @@ int getBuiltIn(char *func, ast_node *node){ return 0; } -ll_t *getUserDefinedFunction(char *function); - void expressFunction(char *function, ast_node *node){ node->func = CheckedMalloc(sizeof(functionToken)); - if ((getBuiltIn(function, node)) == -1){ - //node->func->func = getUserDefinedFunction(function); - } else { - node->func->func = NULL; - } -} - -void expressArgs(char **args, ast_node *node){ - for (int i = 0; i < MAXARGS; i++){ - if (node->args[i] == NULL){ - memcpy(node->literalArgs[i], args[i], strlen(args[i]) + 1); - } - } - + if ((getBuiltIn(function, node)) == NIL) // non user defined function + node->func->name = function; } ast_node *tokenize(char *input){ @@ -113,7 +113,6 @@ ast_node *tokenize(char *input){ } exp[i-2] = '\0'; exp = CheckedRealloc(exp, strlen(exp) + 1); - printf("%s\n", exp); }else if (input[i] == '"'){ i++; while (input[i] != '"') i++; @@ -129,23 +128,60 @@ ast_node *tokenize(char *input){ function[i] = '\0'; function = CheckedRealloc(function, i); - printf("%s\n", function); expressFunction(function, node); - i++; - args = Split(&input[i], ' '); - // need a length - expressArgs(args, node /* length */ ); + char *tok; + tok = strtok(strstr(exp, " ") + 1, " "); + argCount = 0; + depth = 0; + do { + if (node->args[argCount] != NULL){ + argCount++; + } + if (tok[0] != '(' && tok[strlen(tok)-1] != ')' && depth == 0){ + if (node->args[argCount] == NULL){ + node->literalArgs[argCount] = malloc(strlen(tok)+1); + node->literalArgs[argCount] = tok; + } + argCount++; + } + + if (tok[0] == '(') depth++; + if (tok[strlen(tok)-1] == ')') depth--; + tok = strtok(NULL, " "); + } while (tok != NULL); + + if (strcmp(function, "set") == 0 || strcmp(function, "let") == 0){ + char *varName; + char *varType; + varName = strtok(node->literalArgs[0], ":"); + varType = strtok(NULL, ":"); + if (strcmp(varType, "function") == 0){ + userDefinedFunctions[userFuncCount] = CheckedMalloc(25); + userDefinedFunctions[userFuncCount] = varName; + userFuncCount++; + }else { + userDefinedVars[userVarCount] = CheckedMalloc(15); + userDefinedVars[userVarCount] = varName; + userVarCount++; + } + } - free(exp); + CheckedFree(exp); return node; } -int main(){ - char sample[] = "(+ (- 2 2) 1)"; - ast_node *root = tokenize(sample); - printf("%d", root->args[0]->func->builtInFunc); - free(root); +void printAst(ast_node *root){ + printf("-----------\n"); + if (root->func->builtInFunc == -1) printf("function: %s\n", root->func->name); + else printf("function (built in): %d\n", root->func->builtInFunc); + for (int i = 0; i < MAXARGS + 1; i++){ + if (root->args[i] != NULL) printAst(root->args[i]); + else { + if (root->literalArgs[i] != NULL) printf("%s\n", root->literalArgs[i]); + } + } + printf("-----------\n"); } diff --git a/comp/lucas-standen-NEA/code/tokenizer/tokenizer.h b/comp/lucas-standen-NEA/code/tokenizer/tokenizer.h new file mode 100644 index 0000000..3cfaaf2 --- /dev/null +++ b/comp/lucas-standen-NEA/code/tokenizer/tokenizer.h @@ -0,0 +1,12 @@ +#include +#include + +#define MAXARGS 8 +#define MAXFUNCS 2048 +#define MAXVARS 8192 + +int getBuiltIn(char *func, ast_node *node); // checks if a function is built in to zippy +void expressFunction(char *function, ast_node *node); // puts a string into the ast_node struct +ast_node *tokenize(char *input); // does the tokenization +void printAst(ast_node *root); // shows an ast and its sub nodes + diff --git a/comp/lucas-standen-NEA/code/tokenizer/types.h b/comp/lucas-standen-NEA/code/tokenizer/types.h deleted file mode 100644 index 8c79bd9..0000000 --- a/comp/lucas-standen-NEA/code/tokenizer/types.h +++ /dev/null @@ -1,76 +0,0 @@ -#include -#include "../ads/ll/ll.h" - -// all language types -typedef enum types { - I32_T = 0, - I64_T = 1, - U32_T = 2, - U64_T = 3, - FLOAT_t = 4, - CHAR_T = 5, - FUNCTION_T = 6, -} types; - -// int types -typedef int32_t i32; -typedef int64_t i64; - -// uint types -typedef uint32_t u32; -typedef uint64_t u64; - -// char and float types are still called char and float so no typedef needed - -// built in functions -typedef enum builtInFuncs { - // general - DEFUN = 0, - LET = 1, - SET = 2, - IF = 3, - ELIF = 4, - ELSE = 5, - FOR = 6, - WHILE = 7, - SYMBOL = 8, - - // arithmetic - ADD = 10, - SUB = 11, - MUL = 12, - DIV = 13, - - // comparison - EQ = 14, - NEQ = 15, - GT = 16, - LT = 17, - GTEQ = 18, - LTEQ = 19, - - // misc - CAST = 20, - TYPEOF = 21, - EXIT = 22, - RETURN = 23, - NIL = -1, -} builtInFuncs; - -// function type -typedef struct functionToken { - int id; // a function id to avoid strings - types returnType; // what the function returns - types *args; // the types of args a function takes - ll_t *func; // the code for the function - builtInFuncs builtInFunc; // a built in functions -} functionToken; - -typedef struct ast_node ast_node; - -typedef struct ast_node { - functionToken *func; // if it's not builtin then use this - void **literalArgs; // the args of the node, this will be an array of litteral values - ast_node **args; // the non litteral tokens - // if litteralArgs[x] is real then args[x] should be NULL, and vice versa -} ast_node; diff --git a/comp/lucas-standen-NEA/code/tokenizer/util.c b/comp/lucas-standen-NEA/code/tokenizer/util.c deleted file mode 100644 index 46deba8..0000000 --- a/comp/lucas-standen-NEA/code/tokenizer/util.c +++ /dev/null @@ -1,65 +0,0 @@ -#include -#include -#include -#include -#include - -void Die(); // brings down the program -void *CheckedMalloc(long size); // malloc checked -void *CheckedRealloc(void *out, long size); // realloc checked -char **Split(char *s, char c); // splits a string into an array of strings around c - -void Die(){ - perror("zpy parser"); - exit(errno); -} - -void *CheckedMalloc(long size){ - void *out = malloc(size); - if (out == NULL) - Die(); - return out; -} - -void *CheckedRealloc(void *orig, long size){ - void *out = realloc(orig, size); - if (out == NULL) - Die(); - return out; -} - -static size_t countSegment(char const *s, char c){ - size_t counter = 0; - int i = 0; - while (s[i]){ - if (s[i] == c){ - i++; - continue; - } - counter++; - while (s[i] && s[i] != c) i++; - } - return counter; -} - -char **Split(char *s, char c){ - char **strs; - size_t tab_counter; - size_t i; - size_t j; - - if (s == NULL) return NULL; - tab_counter = countSegment(s, c); - if ((strs = (char**)CheckedMalloc(sizeof(char*) * (tab_counter + 1))) == NULL) return NULL; - tab_counter = 0; - j = -1; - while (s[++j]) { - if (s[j] == c) continue; - i = 0; - while (s[j + i] && s[j + i] != c) i++; - if ((strs[tab_counter++] = strndup(&s[j], i)) == NULL) return NULL; - j += i - 1; - } - strs[tab_counter] = NULL; - return strs; -} diff --git a/comp/lucas-standen-NEA/code/tokenizer/util.h b/comp/lucas-standen-NEA/code/tokenizer/util.h deleted file mode 100644 index c25ebec..0000000 --- a/comp/lucas-standen-NEA/code/tokenizer/util.h +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include -#include -#include -#include - -void Die(); // brings down the program -void *CheckedMalloc(long size); // malloc checked -void *CheckedRealloc(void *out, long size); // realloc checked -char **Split(char *s, char c); // splits a string into an array of strings around c diff --git a/comp/lucas-standen-NEA/writeup/coverpage.ps b/comp/lucas-standen-NEA/writeup/coverpage.ps index a5f55b6..049b9a3 100644 --- a/comp/lucas-standen-NEA/writeup/coverpage.ps +++ b/comp/lucas-standen-NEA/writeup/coverpage.ps @@ -1,6 +1,6 @@ %!PS-Adobe-3.0 %%Creator: groff version 1.23.0 -%%CreationDate: Thu Jun 6 13:22:18 2024 +%%CreationDate: Thu Jun 20 12:51:08 2024 %%DocumentNeededResources: font Times-Bold %%+ font Times-Italic %%+ font Times-Roman diff --git a/comp/lucas-standen-NEA/writeup/questions-for-amy.ps b/comp/lucas-standen-NEA/writeup/questions-for-amy.ps index 9644dca..eaa8d07 100644 --- a/comp/lucas-standen-NEA/writeup/questions-for-amy.ps +++ b/comp/lucas-standen-NEA/writeup/questions-for-amy.ps @@ -1,6 +1,6 @@ %!PS-Adobe-3.0 %%Creator: groff version 1.23.0 -%%CreationDate: Thu Jun 6 13:22:20 2024 +%%CreationDate: Thu Jun 20 12:51:10 2024 %%DocumentNeededResources: font Times-Bold %%+ font Times-Roman %%DocumentSuppliedResources: procset grops 1.23 0 -- cgit v1.2.3