From 1f5da5a858d1dfe1052b9f0ba8daef827b3e0c9e Mon Sep 17 00:00:00 2001 From: thing1 Date: Thu, 12 Sep 2024 14:39:18 +0000 Subject: IT WORKS: first full build --- comp/lucas-standen-NEA/code2/Makefile | 4 +- comp/lucas-standen-NEA/code2/appendsnprintf.c | 1 + comp/lucas-standen-NEA/code2/comp.c | 103 ++++++++++++++++---------- comp/lucas-standen-NEA/code2/comp.h | 2 +- comp/lucas-standen-NEA/code2/parser.c | 2 +- comp/lucas-standen-NEA/code2/sample | Bin 0 -> 15256 bytes comp/lucas-standen-NEA/code2/sample.zpy | 12 ++- comp/lucas-standen-NEA/code2/zpy | Bin 33048 -> 21336 bytes comp/lucas-standen-NEA/code2/zpy.c | 6 +- comp/lucas-standen-NEA/code2/zpy.sh | 2 +- 10 files changed, 82 insertions(+), 50 deletions(-) create mode 100755 comp/lucas-standen-NEA/code2/sample diff --git a/comp/lucas-standen-NEA/code2/Makefile b/comp/lucas-standen-NEA/code2/Makefile index 02af075..a3aa9b2 100644 --- a/comp/lucas-standen-NEA/code2/Makefile +++ b/comp/lucas-standen-NEA/code2/Makefile @@ -1,5 +1,5 @@ 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} @@ -21,4 +21,4 @@ _debug: ${CC} debug.c -c -o debug.o ${CFLAGS} clean: - rm -rf zpy *.o *.core + rm -rf zpy *.o *.core sample diff --git a/comp/lucas-standen-NEA/code2/appendsnprintf.c b/comp/lucas-standen-NEA/code2/appendsnprintf.c index 637d214..ffa8349 100644 --- a/comp/lucas-standen-NEA/code2/appendsnprintf.c +++ b/comp/lucas-standen-NEA/code2/appendsnprintf.c @@ -17,6 +17,7 @@ char *genfmt(char *buf, char *fmt){ i++; j++; } + out[i] = '\0'; return out; } diff --git a/comp/lucas-standen-NEA/code2/comp.c b/comp/lucas-standen-NEA/code2/comp.c index 402f797..f37ffed 100644 --- a/comp/lucas-standen-NEA/code2/comp.c +++ b/comp/lucas-standen-NEA/code2/comp.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "tokenizer.h" #include "appendsnprintf.h" @@ -16,26 +17,28 @@ char *names[] = { "endif", // takes no args // 5 "elif", // same as if, but only executes if the prev statment didnt // 6 "else", // its else! // 7 - "for", // takes a iterator and type, a condition, and an increment // 8 + "for", // takes a iterator and type, a start point, a condition, and an increment // 8 "endfor", // takes no args // 9 - "write", // takes an int and puts it on the screen // 10 - "symbol", // takes a name and return type and args - "+", - "-", - "*", - "/", - "=", - "!=", - "<", - ">", - "<=", - ">=", - "cast", - "typeof", - "exit", - "return", + "symbol", // takes a name and return type and args // 10 + // arithmetic + "+", // 11 + "-", // 12 + "*", // 13 + "/", // 14 + // comparison + "=", // 15 + "!=", // 16 + "<", // 17 + ">", // 18 + "<=", // 19 + ">=", // 20 + + "exit", // 21 + "return", // 22 }; +char *compile(astNode *node); + char *vartypeToC(char *str, char *out){ char *name = malloc(strlen(str)); char *type = malloc(strlen(str)); @@ -60,7 +63,7 @@ char *vartypeToC(char *str, char *out){ } type[j] = '\0'; - char *outbuf = malloc(64); + char *outbuf = calloc(0, 64); outbuf = appendsnprintf(outbuf, MAXOUTLEN, "%s %s", type, name); out = appendsnprintf(out, MAXOUTLEN, "%s", outbuf); free(type); @@ -84,12 +87,25 @@ char *reversepolishToC(astNode *exp, char *out){ return out; } +astNode *processChildren(astNode *node){ + for (int i = 0; i < 8; i++){ + if (node->children[i] != NULL){ + node->args[i] = compile(node->children[i]); + node->children[i] = NULL; + } + } + return node; +} + + char *compile(astNode *node){ - char *out = malloc(MAXOUTLEN); + char *out = calloc(0, MAXOUTLEN); + node = processChildren(node); if (strcmp(names[0], node->func) == 0){ out = appendsnprintf(out, MAXOUTLEN, "%s %s(", node->args[1], node->args[0]); int i = 2; while (node->args[i] != NULL){ + if (i != 2) out = appendsnprintf(out, MAXOUTLEN, ",", node->args[i]); out = vartypeToC(node->args[i], out); i++; } @@ -108,17 +124,14 @@ char *compile(astNode *node){ out = appendsnprintf(out, MAXOUTLEN, " = %s;\n", node->args[1]); } else if (strcmp(names[4], node->func) == 0){ - out = appendsnprintf(out, MAXOUTLEN, "if ("); - out = reversepolishToC(node->children[0], out); + out = appendsnprintf(out, MAXOUTLEN, "if (%s", node->args[0]); out = appendsnprintf(out, MAXOUTLEN, "){\n"); } else if (strcmp(names[5], node->func) == 0){ out = appendsnprintf(out, MAXOUTLEN, "}\n"); } else if (strcmp(names[6], node->func) == 0){ - out = appendsnprintf(out, MAXOUTLEN, "}\n"); - out = appendsnprintf(out, MAXOUTLEN, "else if ("); - out = reversepolishToC(node->children[0], out); + out = appendsnprintf(out, MAXOUTLEN, "else if (%s", node->args[0]); out = appendsnprintf(out, MAXOUTLEN, "){\n"); } else if (strcmp(names[7], node->func) == 0){ @@ -128,20 +141,14 @@ char *compile(astNode *node){ else if (strcmp(names[8], node->func) == 0){ out = appendsnprintf(out, MAXOUTLEN, "for ("); out = vartypeToC(node->args[0], out); - out = appendsnprintf(out, MAXOUTLEN, " = 0;"); - out = reversepolishToC(node->children[1], out); - out = appendsnprintf(out, MAXOUTLEN, "; %s+=%s){", getVarName(node->args[0]), node->args[2]); + out = appendsnprintf(out, MAXOUTLEN, " = %s;", node->args[1]); + out = appendsnprintf(out, MAXOUTLEN, "%s", node->args[2]); + out = appendsnprintf(out, MAXOUTLEN, "; %s+=%s){", getVarName(node->args[0]), node->args[3]); } else if (strcmp(names[9], node->func) == 0){ out = appendsnprintf(out, MAXOUTLEN, "}\n"); } else if (strcmp(names[10], node->func) == 0){ - out = appendsnprintf(out, MAXOUTLEN, "printf(\""); - out = appendsnprintf(out, MAXOUTLEN, "%%d\\n"); - out = appendsnprintf(out, MAXOUTLEN, "\", %s);", node->args[0]); - - } - else if (strcmp(names[11], node->func) == 0){ out = appendsnprintf(out, MAXOUTLEN, "%s %s(", node->args[1], node->args[0]); int i = 2; while (node->args[i] != NULL){ @@ -150,19 +157,39 @@ char *compile(astNode *node){ } out = appendsnprintf(out, MAXOUTLEN, ");\n"); } - else if (strcmp(names[12], node->func) == 0){ - out = reversepolishToC(node->children[0], out); - } - + else if (strcmp(names[21], node->func) == 0){ + out = appendsnprintf(out, MAXOUTLEN, "exit(%s);\n", node->args[0]); + } + else if (strcmp(names[22], node->func) == 0){ + out = appendsnprintf(out, MAXOUTLEN, "return %s;\n", node->args[0]); + } + else { + // arithmetic operators and comparitors + for (int i = 0; i < 9; i++){ + if (strcmp(names[11+i], node->func) == 0){ + out = reversepolishToC(node, out); + goto end; + } + } + out = appendsnprintf(out, MAXOUTLEN, "%s(", node->func); int i = 0; while (node->args[i] != NULL){ + if (i != 0) out = appendsnprintf(out, MAXOUTLEN, ",", node->args[i]); out = appendsnprintf(out, MAXOUTLEN, "%s", node->args[i]); i++; } - out = appendsnprintf(out, MAXOUTLEN, ");\n"); + out = appendsnprintf(out, MAXOUTLEN, ")"); } +end: return out; } +void Compile(astNode *line, FILE *f){ + char *code = compile(line); + int len = strlen(code); + if (code[len-2] == ';' || code[len-2] == '{' || code[len-2] == '}') fprintf(f, "%s", code); + else fprintf(f, "%s;\n", code); + free(code); +} diff --git a/comp/lucas-standen-NEA/code2/comp.h b/comp/lucas-standen-NEA/code2/comp.h index e6a6d9e..c2f1942 100644 --- a/comp/lucas-standen-NEA/code2/comp.h +++ b/comp/lucas-standen-NEA/code2/comp.h @@ -2,5 +2,5 @@ #include #define MAXOUTLEN 512 -char *compile(astNode *node); +void Compile(astNode *node, FILE *f); diff --git a/comp/lucas-standen-NEA/code2/parser.c b/comp/lucas-standen-NEA/code2/parser.c index e15e545..7d177ca 100644 --- a/comp/lucas-standen-NEA/code2/parser.c +++ b/comp/lucas-standen-NEA/code2/parser.c @@ -26,7 +26,7 @@ strings *parse(FILE *f){ int count = 0; while (fgets(line, 256, f) != NULL){ if (line[0] != '\n'){ - if (line[0] == '\t') line++; + while (line[0] == '\t') line++; line[strlen(line)-1] = '\0'; strs->strs[count] = malloc(256); memcpy(strs->strs[count], line, 256); diff --git a/comp/lucas-standen-NEA/code2/sample b/comp/lucas-standen-NEA/code2/sample new file mode 100755 index 0000000..411368d Binary files /dev/null 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 c8a1f80..98bbf44 100644 --- a/comp/lucas-standen-NEA/code2/sample.zpy +++ b/comp/lucas-standen-NEA/code2/sample.zpy @@ -1,7 +1,11 @@ -(symbol putchar int c:int) +(defun fib int n:int) + (if (<= n 1)) + (return n) + (endif) + (return (+ (fib (- n 1)) (fib (- n 2)))) +(endfun) (defun main int) - (if (= 1 1)) - (putchar 65) - (endif) + (fib 43) + (return 0) (endfun) diff --git a/comp/lucas-standen-NEA/code2/zpy b/comp/lucas-standen-NEA/code2/zpy index 6618f07..2251a3f 100755 Binary files a/comp/lucas-standen-NEA/code2/zpy and b/comp/lucas-standen-NEA/code2/zpy differ diff --git a/comp/lucas-standen-NEA/code2/zpy.c b/comp/lucas-standen-NEA/code2/zpy.c index 4c328a0..f09576d 100644 --- a/comp/lucas-standen-NEA/code2/zpy.c +++ b/comp/lucas-standen-NEA/code2/zpy.c @@ -20,13 +20,13 @@ int main(int argc, char **argv){ if (stringTokens == NULL) die("couldn't parse file, is it formated properly?"); - + for (int i = 0; i < stringTokens->count; i++){ stringTokens->strs[i]++; stringTokens->strs[i][strlen(stringTokens->strs[i]) - 1] = '\0'; astNode *line = tokenize(stringTokens->strs[i]); - - printf("%s", compile(line)); + + Compile(line, stdout); } diff --git a/comp/lucas-standen-NEA/code2/zpy.sh b/comp/lucas-standen-NEA/code2/zpy.sh index dbbbdbd..1db747f 100755 --- a/comp/lucas-standen-NEA/code2/zpy.sh +++ b/comp/lucas-standen-NEA/code2/zpy.sh @@ -1,3 +1,3 @@ #!/bin/sh CC=cc -./zpy $1 | ${CC} -x c - -Wno-implicit -Wno-missing-declarations -o $2 +./zpy $1 | ${CC} -O3 -x c - -Wno-implicit -Wno-missing-declarations -o $2 -- cgit v1.2.3