school

thing1's amazing school repo
Log | Files | Refs | Submodules | README

commit fec2f4d9d1989feb0290dfcff416385ec4b97586
parent 3d0db47fa403229bced868f0b59e024b95cd19b6
Author: thing1 <thing1@seacrossedlovers.xyz>
Date:   Thu, 11 Jul 2024 13:33:42 +0100

finished vars, started work on the final zpy executable, still need to fix the parser though

Diffstat:
Mcomp/lucas-standen-NEA/code/Makefile | 4++--
Mcomp/lucas-standen-NEA/code/TODO | 2+-
Mcomp/lucas-standen-NEA/code/execution/exec | 0
Mcomp/lucas-standen-NEA/code/execution/exec.c | 2++
Mcomp/lucas-standen-NEA/code/execution/vars.h | 1-
Mcomp/lucas-standen-NEA/code/global/util.c | 4+++-
Mcomp/lucas-standen-NEA/code/global/util.h | 8--------
Mcomp/lucas-standen-NEA/code/proto/parser/test | 0
Mcomp/lucas-standen-NEA/code/tokenizer/parser.c | 27++++++++++++++++++++++++---
Mcomp/lucas-standen-NEA/code/tokenizer/parser.h | 2--
Mcomp/lucas-standen-NEA/code/tokenizer/tokenizer.c | 1+
Mcomp/lucas-standen-NEA/code/zpy/Makefile | 2++
Acomp/lucas-standen-NEA/code/zpy/sample.zpy | 3+++
Acomp/lucas-standen-NEA/code/zpy/zpy | 0
Mcomp/lucas-standen-NEA/code/zpy/zpy.c | 14++++++++++++++
15 files changed, 52 insertions(+), 18 deletions(-)

diff --git a/comp/lucas-standen-NEA/code/Makefile b/comp/lucas-standen-NEA/code/Makefile @@ -1,8 +1,8 @@ all: export FLAGS="-ggdb" cd global; make - cd execution; make cd tokenizer; make + cd execution; make + cd zpy; 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 @@ -1 +1 @@ -fix the variable definiton->id in the function newVar +FIX THE parser to make it work properly. diff --git a/comp/lucas-standen-NEA/code/execution/exec b/comp/lucas-standen-NEA/code/execution/exec Binary files differ. diff --git a/comp/lucas-standen-NEA/code/execution/exec.c b/comp/lucas-standen-NEA/code/execution/exec.c @@ -2,6 +2,7 @@ #include <stdlib.h> #include "./builtin.h" + #include "../global/util.h" #include "../tokenizer/tokenizer.h" @@ -10,6 +11,7 @@ int main(){ ast_node *root = tokenize(sample); doCall(root); + free(root); return 0; diff --git a/comp/lucas-standen-NEA/code/execution/vars.h b/comp/lucas-standen-NEA/code/execution/vars.h @@ -1,4 +1,3 @@ #include "../global/types.h" - void newVar(Vdef *definiton, literal *value); literal *getVarCalled(char *name); diff --git a/comp/lucas-standen-NEA/code/global/util.c b/comp/lucas-standen-NEA/code/global/util.c @@ -104,7 +104,9 @@ Vdef *isVdef(char *str){ Vdef *out = CheckedMalloc(sizeof(Vdef)); char *type; - out->id = strtok(str, ":"); + char *tmp = strtok(str, ":"); + out->id = CheckedMalloc(strlen(tmp)+1); + memcpy(out->id, tmp, strlen(tmp)+1); type = strtok(NULL, ":"); if (strcmp(type, "i64") == 0) out->type = TI64; diff --git a/comp/lucas-standen-NEA/code/global/util.h b/comp/lucas-standen-NEA/code/global/util.h @@ -1,11 +1,3 @@ -#include <stdio.h> -#include <string.h> -#include <stdlib.h> -#include <stdbool.h> -#include <errno.h> -#include <error.h> -#include <ctype.h> - // functions for user void Die(); // brings down the program diff --git a/comp/lucas-standen-NEA/code/proto/parser/test b/comp/lucas-standen-NEA/code/proto/parser/test Binary files differ. diff --git a/comp/lucas-standen-NEA/code/tokenizer/parser.c b/comp/lucas-standen-NEA/code/tokenizer/parser.c @@ -1,12 +1,10 @@ #include <stdio.h> #include <stdlib.h> +#include <string.h> #include "../global/types.h" #include "../global/util.h" -char *readFile(char *fileName); // reads the file into a single var -char *parser(char *fileName); // general parser function - char *readFile(char *filename){ FILE *f = fopen(filename, "r"); if (f == NULL) @@ -58,6 +56,28 @@ FILE *preProcess(char *contents){ return tmp; } +char **getExpressions(char *file){ + int depth = 0; + char *str = CheckedMalloc(strlen(file)+1); + int pos = 0; + for (int i = 0; i < strlen(file); i++){ + str[pos] = str[i]; + pos++; + if (file[i] == '(') + depth++; + if (file[i] == ')') + depth--; + + if (depth == 0) { + str[pos] = '\0'; + printf("%s\n", str); + pos = 0; + } + } + + return NULL; +} + char *parser(char *fileName){ FILE *tmp = preProcess(readFile(fileName)); fseek(tmp, 0, SEEK_END); @@ -66,5 +86,6 @@ char *parser(char *fileName){ char *buf = CheckedMalloc(len); fgets(buf, len, tmp); fclose(tmp); + getExpressions(buf); return buf; } diff --git a/comp/lucas-standen-NEA/code/tokenizer/parser.h b/comp/lucas-standen-NEA/code/tokenizer/parser.h @@ -1,3 +1 @@ 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/tokenizer.c b/comp/lucas-standen-NEA/code/tokenizer/tokenizer.c @@ -1,4 +1,5 @@ #include <stdio.h> +#include <stdlib.h> #include <string.h> #include "../global/types.h" diff --git a/comp/lucas-standen-NEA/code/zpy/Makefile b/comp/lucas-standen-NEA/code/zpy/Makefile @@ -0,0 +1,2 @@ +zpy: + cc zpy.c ../global/util.o ../tokenizer/parser.o -o zpy -ggdb diff --git a/comp/lucas-standen-NEA/code/zpy/sample.zpy b/comp/lucas-standen-NEA/code/zpy/sample.zpy @@ -0,0 +1,3 @@ +(let a:i64 4) +(let b:i64 2) +(+ a b) diff --git a/comp/lucas-standen-NEA/code/zpy/zpy b/comp/lucas-standen-NEA/code/zpy/zpy Binary files differ. diff --git a/comp/lucas-standen-NEA/code/zpy/zpy.c b/comp/lucas-standen-NEA/code/zpy/zpy.c @@ -0,0 +1,14 @@ +#include <stdio.h> + +#include "../tokenizer/parser.h" +#include "../global/types.h" +#include "../global/util.h" + +int main(int argc, char **argv){ + if (argc < 2) { + printf("repl not yet implemented\n"); + Die(); + } + parser(argv[1]); + return 0; +}