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/TODO | 4 +- comp/lucas-standen-NEA/code/proto/AST/Makefile | 4 + comp/lucas-standen-NEA/code/proto/AST/ast | Bin 0 -> 24200 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/Makefile | 7 +- comp/lucas-standen-NEA/code/proto/ast | Bin 24200 -> 0 bytes comp/lucas-standen-NEA/code/proto/ast.c | 146 --------------------- 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/parser/Makefile | 2 + .../lucas-standen-NEA/code/proto/parser/sample.zpy | 1 + comp/lucas-standen-NEA/code/proto/parser/test | Bin 0 -> 28680 bytes comp/lucas-standen-NEA/code/proto/parser/test.c | 7 + comp/lucas-standen-NEA/code/proto/right | 0 comp/lucas-standen-NEA/code/tokenizer/parser.c | 36 ++++- comp/lucas-standen-NEA/code/tokenizer/parser.h | 6 +- comp/lucas-standen-NEA/code/tokenizer/test | Bin 28320 -> 0 bytes comp/lucas-standen-NEA/code/tokenizer/test.c | 6 - 23 files changed, 269 insertions(+), 238 deletions(-) create mode 100644 comp/lucas-standen-NEA/code/proto/AST/Makefile create mode 100755 comp/lucas-standen-NEA/code/proto/AST/ast create mode 100644 comp/lucas-standen-NEA/code/proto/AST/ast.c create mode 100644 comp/lucas-standen-NEA/code/proto/AST/astg.c create mode 100644 comp/lucas-standen-NEA/code/proto/AST/astg.h create mode 100644 comp/lucas-standen-NEA/code/proto/AST/left create mode 100644 comp/lucas-standen-NEA/code/proto/AST/right delete mode 100755 comp/lucas-standen-NEA/code/proto/ast delete mode 100644 comp/lucas-standen-NEA/code/proto/ast.c delete mode 100644 comp/lucas-standen-NEA/code/proto/astg.c delete mode 100644 comp/lucas-standen-NEA/code/proto/astg.h delete mode 100644 comp/lucas-standen-NEA/code/proto/left create mode 100644 comp/lucas-standen-NEA/code/proto/parser/Makefile create mode 100644 comp/lucas-standen-NEA/code/proto/parser/sample.zpy create mode 100755 comp/lucas-standen-NEA/code/proto/parser/test create mode 100644 comp/lucas-standen-NEA/code/proto/parser/test.c delete mode 100644 comp/lucas-standen-NEA/code/proto/right delete mode 100755 comp/lucas-standen-NEA/code/tokenizer/test delete mode 100644 comp/lucas-standen-NEA/code/tokenizer/test.c diff --git a/comp/lucas-standen-NEA/code/TODO b/comp/lucas-standen-NEA/code/TODO index 25766b5..0ec1882 100644 --- a/comp/lucas-standen-NEA/code/TODO +++ b/comp/lucas-standen-NEA/code/TODO @@ -1,3 +1 @@ -finish the pre processor, it should read through a given input and convert all string litterals to arrays - -make it possible to index arrays +add variables diff --git a/comp/lucas-standen-NEA/code/proto/AST/Makefile b/comp/lucas-standen-NEA/code/proto/AST/Makefile new file mode 100644 index 0000000..6074b00 --- /dev/null +++ b/comp/lucas-standen-NEA/code/proto/AST/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/ast b/comp/lucas-standen-NEA/code/proto/AST/ast new file mode 100755 index 0000000..69ccc93 Binary files /dev/null and b/comp/lucas-standen-NEA/code/proto/AST/ast differ diff --git a/comp/lucas-standen-NEA/code/proto/AST/ast.c b/comp/lucas-standen-NEA/code/proto/AST/ast.c new file mode 100644 index 0000000..d76107b --- /dev/null +++ b/comp/lucas-standen-NEA/code/proto/AST/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/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; +} + diff --git a/comp/lucas-standen-NEA/code/proto/AST/astg.h b/comp/lucas-standen-NEA/code/proto/AST/astg.h new file mode 100644 index 0000000..16250b2 --- /dev/null +++ b/comp/lucas-standen-NEA/code/proto/AST/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/AST/left b/comp/lucas-standen-NEA/code/proto/AST/left new file mode 100644 index 0000000..e69de29 diff --git a/comp/lucas-standen-NEA/code/proto/AST/right b/comp/lucas-standen-NEA/code/proto/AST/right new file mode 100644 index 0000000..e69de29 diff --git a/comp/lucas-standen-NEA/code/proto/Makefile b/comp/lucas-standen-NEA/code/proto/Makefile index 6074b00..adb84a1 100644 --- a/comp/lucas-standen-NEA/code/proto/Makefile +++ b/comp/lucas-standen-NEA/code/proto/Makefile @@ -1,4 +1,3 @@ -all: astg ast.c - cc -ggdb ast.c astg.o -o ast -astg: astg.c - cc -ggdb astg.c -c -o astg.o +all: + cd AST; make + cd parser; make diff --git a/comp/lucas-standen-NEA/code/proto/ast b/comp/lucas-standen-NEA/code/proto/ast deleted file mode 100755 index f922f0d..0000000 Binary files a/comp/lucas-standen-NEA/code/proto/ast and /dev/null differ diff --git a/comp/lucas-standen-NEA/code/proto/ast.c b/comp/lucas-standen-NEA/code/proto/ast.c deleted file mode 100644 index d76107b..0000000 --- a/comp/lucas-standen-NEA/code/proto/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/astg.c b/comp/lucas-standen-NEA/code/proto/astg.c deleted file mode 100644 index e96b771..0000000 --- a/comp/lucas-standen-NEA/code/proto/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/astg.h b/comp/lucas-standen-NEA/code/proto/astg.h deleted file mode 100644 index 16250b2..0000000 --- a/comp/lucas-standen-NEA/code/proto/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/left b/comp/lucas-standen-NEA/code/proto/left deleted file mode 100644 index e69de29..0000000 diff --git a/comp/lucas-standen-NEA/code/proto/parser/Makefile b/comp/lucas-standen-NEA/code/proto/parser/Makefile new file mode 100644 index 0000000..e8ab981 --- /dev/null +++ b/comp/lucas-standen-NEA/code/proto/parser/Makefile @@ -0,0 +1,2 @@ +all: + cc test.c ../../global/util.o ../../tokenizer/parser.o -o test diff --git a/comp/lucas-standen-NEA/code/proto/parser/sample.zpy b/comp/lucas-standen-NEA/code/proto/parser/sample.zpy new file mode 100644 index 0000000..fb998be --- /dev/null +++ b/comp/lucas-standen-NEA/code/proto/parser/sample.zpy @@ -0,0 +1 @@ +(write "hello!") diff --git a/comp/lucas-standen-NEA/code/proto/parser/test b/comp/lucas-standen-NEA/code/proto/parser/test new file mode 100755 index 0000000..bc47f65 Binary files /dev/null and b/comp/lucas-standen-NEA/code/proto/parser/test differ diff --git a/comp/lucas-standen-NEA/code/proto/parser/test.c b/comp/lucas-standen-NEA/code/proto/parser/test.c new file mode 100644 index 0000000..fcc8f3c --- /dev/null +++ b/comp/lucas-standen-NEA/code/proto/parser/test.c @@ -0,0 +1,7 @@ +#include +#include "../../tokenizer/parser.h" + +int main(){ + char *buf = parser("sample.zpy"); + printf("%s", buf); +} diff --git a/comp/lucas-standen-NEA/code/proto/right b/comp/lucas-standen-NEA/code/proto/right deleted file mode 100644 index e69de29..0000000 diff --git a/comp/lucas-standen-NEA/code/tokenizer/parser.c b/comp/lucas-standen-NEA/code/tokenizer/parser.c index 4bb9c3a..7d827f9 100644 --- a/comp/lucas-standen-NEA/code/tokenizer/parser.c +++ b/comp/lucas-standen-NEA/code/tokenizer/parser.c @@ -5,7 +5,7 @@ #include "../global/util.h" char *readFile(char *fileName); // reads the file into a single var -char *parse(char *fileName); // general parser function +char *parser(char *fileName); // general parser function char *readFile(char *filename){ FILE *f = fopen(filename, "r"); @@ -32,13 +32,39 @@ char *readFile(char *filename){ return out; } -char *preProcess(char *contents){ - char *out = CheckedMalloc(strlen(contents)+1); +FILE *preProcess(char *contents){ + int currentSize = strlen(contents)+1; + char *out = CheckedMalloc(currentSize); + + FILE *tmp = fopen("/tmp/zpy.tmp", "w+"); + for (char c = contents[0]; c != '\0'; c = (contents += 1)[0]){ - printf("%c", c); + if (c == '"'){ + fprintf(tmp, "["); + c = (contents += 1)[0]; + do { + fprintf(tmp, "'%c'", c); + if ((contents+1)[0] != '"') { + fprintf(tmp, ","); + } + c = (contents += 1)[0]; + } while (c != '"'); + c = (contents += 1)[0]; + fprintf(tmp, "]"); + } + fprintf(tmp, "%c", c); } + fprintf(tmp, "\n"); + return tmp; } char *parser(char *fileName){ - return readFile(fileName); + FILE *tmp = preProcess(readFile(fileName)); + fseek(tmp, 0, SEEK_END); + int len = ftell(tmp); + rewind(tmp); + char *buf = CheckedMalloc(len); + fgets(buf, len, tmp); + fclose(tmp); + return buf; } diff --git a/comp/lucas-standen-NEA/code/tokenizer/parser.h b/comp/lucas-standen-NEA/code/tokenizer/parser.h index f90b777..993d257 100644 --- a/comp/lucas-standen-NEA/code/tokenizer/parser.h +++ b/comp/lucas-standen-NEA/code/tokenizer/parser.h @@ -1,3 +1,3 @@ -char *Parse(char *fileName); // general parser function -char *ReadFile(char *fileName); // reads the file into a single var -char *preProcess(char *contents); +char *parser(char *fileName); // general parser function +char *readFile(char *fileName); // reads the file into a single var +FILE *preProcess(char *contents); diff --git a/comp/lucas-standen-NEA/code/tokenizer/test b/comp/lucas-standen-NEA/code/tokenizer/test deleted file mode 100755 index 797a447..0000000 Binary files a/comp/lucas-standen-NEA/code/tokenizer/test and /dev/null differ diff --git a/comp/lucas-standen-NEA/code/tokenizer/test.c b/comp/lucas-standen-NEA/code/tokenizer/test.c deleted file mode 100644 index 49d6160..0000000 --- a/comp/lucas-standen-NEA/code/tokenizer/test.c +++ /dev/null @@ -1,6 +0,0 @@ -#include "parser.h" - -int main(){ - char *sample = "(write \"hello\")"; - preProcess(sample); -} -- cgit v1.2.3