From f35cd6c1d15ce31cd5f1d1d578a7a2a4880c2179 Mon Sep 17 00:00:00 2001 From: Thing1 Date: Fri, 13 Sep 2024 17:41:04 +0100 Subject: did structs, and layed the groundwork for function pointers --- comp/lucas-standen-NEA/code2/Makefile | 9 ++--- comp/lucas-standen-NEA/code2/TODO | 1 + comp/lucas-standen-NEA/code2/comp.c | 56 ++++++++++++++++++++++++-------- comp/lucas-standen-NEA/code2/sample | Bin 15624 -> 15512 bytes comp/lucas-standen-NEA/code2/sample.zpy | 25 ++++---------- comp/lucas-standen-NEA/code2/zpy | Bin 34792 -> 21288 bytes 6 files changed, 53 insertions(+), 38 deletions(-) create mode 100644 comp/lucas-standen-NEA/code2/TODO (limited to 'comp') diff --git a/comp/lucas-standen-NEA/code2/Makefile b/comp/lucas-standen-NEA/code2/Makefile index 728fa1d..bcfe9f5 100644 --- a/comp/lucas-standen-NEA/code2/Makefile +++ b/comp/lucas-standen-NEA/code2/Makefile @@ -1,8 +1,8 @@ CC = cc -CFLAGS = -O0 -ggdb +CFLAGS = -O3 -all: _zpy _parser _tokenizer _comp _appendsnprintf _util _debug - ${CC} zpy.o parser.o tokenizer.o comp.o appendsnprintf.o util.o debug.o -o zpy ${CFLAGS} +all: _zpy _parser _tokenizer _comp _appendsnprintf _util + ${CC} zpy.o parser.o tokenizer.o comp.o appendsnprintf.o util.o -o zpy ${CFLAGS} _zpy: zpy.c ${CC} zpy.c -c -o zpy.o ${CFLAGS} @@ -17,8 +17,5 @@ _appendsnprintf: appendsnprintf.c _util: util.c ${CC} util.c -c -o util.o ${CFLAGS} -_debug: - ${CC} debug.c -c -o debug.o ${CFLAGS} - clean: rm -rf zpy *.o *.core sample diff --git a/comp/lucas-standen-NEA/code2/TODO b/comp/lucas-standen-NEA/code2/TODO new file mode 100644 index 0000000..2e7e13d --- /dev/null +++ b/comp/lucas-standen-NEA/code2/TODO @@ -0,0 +1 @@ +make the defunptr function diff --git a/comp/lucas-standen-NEA/code2/comp.c b/comp/lucas-standen-NEA/code2/comp.c index 7fb975a..bdca47f 100644 --- a/comp/lucas-standen-NEA/code2/comp.c +++ b/comp/lucas-standen-NEA/code2/comp.c @@ -33,18 +33,32 @@ char *names[] = { "<=", // 19 ">=", // 20 - "exit", // 21 - "return", // 22 + "exit", // takes an int // 21 + "return", // takes an int // 22 "alloc", // takes a size and allocates a block of it // 23 + + "struct", // takes a name for the struct // 24 + "endstruct", // takes nothing // 25 + + "def", // takes a name and type does not asign // 26 + + "sizeof", // takes a datatype and returns its size // 27 + + "defunptr" // takes the same stuff as defun but outputs it in fuction ptr form }; +// function prototype for recursion char *compile(astNode *node); +// globals for memory management char *tofree[256]; int freeptr = 0; bool neededmemptr = false; +// globals for stucts +char *structname; + char *vartypeToC(char *str, char *out){ char *name = malloc(strlen(str)); char *type = malloc(strlen(str)); @@ -118,17 +132,7 @@ char *compile(astNode *node){ out = appendsnprintf(out, MAXOUTLEN, "){\n"); } else if (strcmp(names[1], node->func) == 0){ - for (int i = 0; i < 256; i++){ - if (tofree[i] != NULL){ - out = appendsnprintf(out, MAXOUTLEN, "free(%s);\n", tofree[i]); - } else{ - for (int j = 0; j < 256; j++){ - tofree[j] = NULL; - } - freeptr = 0; - break; - } - } + out = appendsnprintf(out, MAXOUTLEN, "}\n"); } else if (strcmp(names[2], node->func) == 0){ @@ -177,12 +181,38 @@ char *compile(astNode *node){ out = appendsnprintf(out, MAXOUTLEN, "exit(%s);\n", node->args[0]); } else if (strcmp(names[22], node->func) == 0){ + for (int i = 0; i < 256; i++){ + if (tofree[i] != NULL){ + out = appendsnprintf(out, MAXOUTLEN, "free(%s);\n", tofree[i]); + } else{ + for (int j = 0; j < 256; j++){ + tofree[j] = NULL; + } + freeptr = 0; + break; + } + } out = appendsnprintf(out, MAXOUTLEN, "return %s;\n", node->args[0]); } else if (strcmp(names[23], node->func) == 0){ out = appendsnprintf(out, MAXOUTLEN, "calloc(0, %s)", node->args[0]); neededmemptr = true; } + else if (strcmp(names[24], node->func) == 0){ + out = appendsnprintf(out, MAXOUTLEN, "typedef struct %s %s;\n", node->args[0], node->args[0]); + out = appendsnprintf(out, MAXOUTLEN, "typedef struct %s {", node->args[0]); + structname = node->args[0]; + } + else if (strcmp(names[25], node->func) == 0){ + out = appendsnprintf(out, MAXOUTLEN, "} %s", structname); + structname = NULL; + } + else if (strcmp(names[26], node->func) == 0){ + out = vartypeToC(node->args[0], out); + } + else if (strcmp(names[27], node->func) == 0){ + out = appendsnprintf(out, MAXOUTLEN, "sizeof(%s)", node->args[0]); + } else { // arithmetic operators and comparitors diff --git a/comp/lucas-standen-NEA/code2/sample b/comp/lucas-standen-NEA/code2/sample index 96a8c07..7516796 100755 Binary files a/comp/lucas-standen-NEA/code2/sample and b/comp/lucas-standen-NEA/code2/sample differ diff --git a/comp/lucas-standen-NEA/code2/sample.zpy b/comp/lucas-standen-NEA/code2/sample.zpy index 1eea0f4..57b954f 100644 --- a/comp/lucas-standen-NEA/code2/sample.zpy +++ b/comp/lucas-standen-NEA/code2/sample.zpy @@ -1,22 +1,9 @@ -(symbol putchar int c:int) -(symbol getchar int) - -(defun read char* str:char*) - (let count:int 0) - (for c:char (getchar) (!= c '\n') 0) - (set str[count] c) - (set c (getchar)) - (set count (+ count 1)) - (endfor) - (return str) -(endfun) +(struct ll) + (def data:void*) + (def next:ll*) +(endstruct) (defun main int) - (let str:char* (alloc 10)) - - (set str (read str)) - - (for i:int 0 (< i 10) 1) - (putchar str[i]) - (endfor) + (let node:ll* (alloc (sizeof ll*))) + (return 0) (endfun) diff --git a/comp/lucas-standen-NEA/code2/zpy b/comp/lucas-standen-NEA/code2/zpy index 5bbab13..4191bbf 100755 Binary files a/comp/lucas-standen-NEA/code2/zpy and b/comp/lucas-standen-NEA/code2/zpy differ -- cgit v1.2.3