school

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

commit 2ab1f5cf5a759f0982eea3951c47e0477cb46561
parent fe89cc56e2aa7fa119e2251d104acde37d632cd9
Author: Thing1 <thing1@seacrossedlovers.xyz>
Date:   Thu, 12 Sep 2024 07:45:34 +0100

new

Diffstat:
Mcomp/lucas-standen-NEA/code2/Makefile | 6++++--
Acomp/lucas-standen-NEA/code2/appendsnprintf.c | 35+++++++++++++++++++++++++++++++++++
Acomp/lucas-standen-NEA/code2/appendsnprintf.h | 1+
Mcomp/lucas-standen-NEA/code2/comp.c | 93++++++++++++++++++++++++++++++++++++++++++-------------------------------------
Mcomp/lucas-standen-NEA/code2/comp.h | 2+-
Dcomp/lucas-standen-NEA/code2/sample | 0
Mcomp/lucas-standen-NEA/code2/sample.zpy | 6+++++-
Mcomp/lucas-standen-NEA/code2/zpy | 0
Mcomp/lucas-standen-NEA/code2/zpy.c | 6++----
9 files changed, 98 insertions(+), 51 deletions(-)

diff --git a/comp/lucas-standen-NEA/code2/Makefile b/comp/lucas-standen-NEA/code2/Makefile @@ -1,8 +1,8 @@ CC = cc CFLAGS = -O0 -ggdb -all: _zpy _parser _tokenizer _comp _util _debug - ${CC} zpy.o parser.o tokenizer.o comp.o util.o debug.o -o zpy ${CFLAGS} +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} _zpy: zpy.c ${CC} zpy.c -c -o zpy.o ${CFLAGS} @@ -12,6 +12,8 @@ _tokenizer: tokenizer.c ${CC} tokenizer.c -c -o tokenizer.o ${CFLAGS} _comp: comp.c ${CC} comp.c -c -o comp.o ${CFLAGS} +_appendsnprintf: appendsnprintf.c + ${CC} appendsnprintf.c -c -o appendsnprintf.o ${CFLAGS} _util: util.c ${CC} util.c -c -o util.o ${CFLAGS} diff --git a/comp/lucas-standen-NEA/code2/appendsnprintf.c b/comp/lucas-standen-NEA/code2/appendsnprintf.c @@ -0,0 +1,35 @@ +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +char *genfmt(char *buf, char *fmt){ + int len = strlen(buf) + strlen(fmt) + 1; + char *out = malloc(len); + + int j = 0, i = 0; + while (buf[i] != '\0'){ + out[i] = buf[i]; + i++; + } + while (fmt[j] != '\0'){ + out[i] = fmt[j]; + i++; + j++; + } + return out; +} + +char *appendsnprintf(char *buf, int size, char *format, ...){ + va_list ap; + char *outputbuf = malloc(size); + va_start(ap, format); + char *fmt = genfmt(buf, format); + vsnprintf(outputbuf, size, fmt, ap); + free(fmt); + + buf = realloc(outputbuf, strlen(outputbuf) + 1); + va_end(ap); + + return buf; +} diff --git a/comp/lucas-standen-NEA/code2/appendsnprintf.h b/comp/lucas-standen-NEA/code2/appendsnprintf.h @@ -0,0 +1 @@ +char *appendsnprintf(char *buf, int size, char *format, ...); diff --git a/comp/lucas-standen-NEA/code2/comp.c b/comp/lucas-standen-NEA/code2/comp.c @@ -3,6 +3,7 @@ #include <string.h> #include "tokenizer.h" +#include "appendsnprintf.h" #define MAXOUTLEN 512 @@ -35,7 +36,7 @@ char *names[] = { "return", }; -void vartypeToC(char *str, char *out){ +char *vartypeToC(char *str, char *out){ char *name = malloc(strlen(str)); char *type = malloc(strlen(str)); @@ -59,9 +60,12 @@ void vartypeToC(char *str, char *out){ } type[j] = '\0'; - snprintf(out, MAXOUTLEN, "%s%s %s", out, type, name); + char *outbuf = malloc(64); + outbuf = appendsnprintf(outbuf, MAXOUTLEN, "%s %s", type, name); + out = appendsnprintf(out, MAXOUTLEN, "%s", outbuf); free(type); free(name); + return out; } char *getVarName(char *exp){ @@ -72,90 +76,93 @@ char *getVarName(char *exp){ return out; } -void reversepolishToC(astNode *exp, char *out){ - snprintf(out, MAXOUTLEN, "%s ", exp->args[0]); - if (exp->func[0] == '=') snprintf(out, MAXOUTLEN, "=="); - else snprintf(out, MAXOUTLEN, "%s", exp->func); - snprintf(out, MAXOUTLEN, " %s", exp->args[1]); +char *reversepolishToC(astNode *exp, char *out){ + out = appendsnprintf(out, MAXOUTLEN, "%s ", exp->args[0]); + if (exp->func[0] == '=') out = appendsnprintf(out, MAXOUTLEN, "=="); + else out = appendsnprintf(out, MAXOUTLEN, "%s", exp->func); + out = appendsnprintf(out, MAXOUTLEN, " %s", exp->args[1]); + return out; } -void compile(astNode *node, char *out){ +char *compile(astNode *node){ + char *out = malloc(MAXOUTLEN); if (strcmp(names[0], node->func) == 0){ - snprintf(out, MAXOUTLEN, "%s %s(", node->args[1], node->args[0]); + out = appendsnprintf(out, MAXOUTLEN, "%s %s(", node->args[1], node->args[0]); int i = 2; while (node->args[i] != NULL){ - vartypeToC(node->args[i], out); + out = vartypeToC(node->args[i], out); i++; } - snprintf(out, MAXOUTLEN, "%s){\n", out); + out = appendsnprintf(out, MAXOUTLEN, "){\n"); } else if (strcmp(names[1], node->func) == 0){ - snprintf(out, MAXOUTLEN, "}\n"); + out = appendsnprintf(out, MAXOUTLEN, "}\n"); } else if (strcmp(names[2], node->func) == 0){ - snprintf(out, MAXOUTLEN, "const "); - vartypeToC(node->args[0], out); - snprintf(out, MAXOUTLEN, " = %s;\n", node->args[1]); + out = appendsnprintf(out, MAXOUTLEN, "const "); + out = vartypeToC(node->args[0], out); + out = appendsnprintf(out, MAXOUTLEN, " = %s;\n", node->args[1]); } else if (strcmp(names[3], node->func) == 0){ - vartypeToC(node->args[0], out); - snprintf(out, MAXOUTLEN, " = %s;\n", node->args[1]); + out = vartypeToC(node->args[0], out); + out = appendsnprintf(out, MAXOUTLEN, " = %s;\n", node->args[1]); } else if (strcmp(names[4], node->func) == 0){ - snprintf(out, MAXOUTLEN, "if ("); - reversepolishToC(node->children[0], out); - snprintf(out, MAXOUTLEN, "){\n"); + out = appendsnprintf(out, MAXOUTLEN, "if ("); + out = reversepolishToC(node->children[0], out); + out = appendsnprintf(out, MAXOUTLEN, "){\n"); } else if (strcmp(names[5], node->func) == 0){ - snprintf(out, MAXOUTLEN, "}\n"); + out = appendsnprintf(out, MAXOUTLEN, "}\n"); } else if (strcmp(names[6], node->func) == 0){ - snprintf(out, MAXOUTLEN, "}\n"); - snprintf(out, MAXOUTLEN, "else if ("); - reversepolishToC(node->children[0], out); - snprintf(out, MAXOUTLEN, "){\n"); + out = appendsnprintf(out, MAXOUTLEN, "}\n"); + out = appendsnprintf(out, MAXOUTLEN, "else if ("); + out = reversepolishToC(node->children[0], out); + out = appendsnprintf(out, MAXOUTLEN, "){\n"); } else if (strcmp(names[7], node->func) == 0){ - snprintf(out, MAXOUTLEN, "}\n"); - snprintf(out, MAXOUTLEN, "else{"); + out = appendsnprintf(out, MAXOUTLEN, "}\n"); + out = appendsnprintf(out, MAXOUTLEN, "else{"); } else if (strcmp(names[8], node->func) == 0){ - snprintf(out, MAXOUTLEN, "for ("); - vartypeToC(node->args[0], out); - snprintf(out, MAXOUTLEN, " = 0;"); - reversepolishToC(node->children[1], out); - snprintf(out, MAXOUTLEN, "; %s+=%s){", getVarName(node->args[0]), node->args[2]); + 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]); } else if (strcmp(names[9], node->func) == 0){ - snprintf(out, MAXOUTLEN, "}\n"); + out = appendsnprintf(out, MAXOUTLEN, "}\n"); } else if (strcmp(names[10], node->func) == 0){ - snprintf(out, MAXOUTLEN, "printf(\""); - snprintf(out, MAXOUTLEN, "%%d\\n"); - snprintf(out, MAXOUTLEN, "\", %s);", node->args[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){ - snprintf(out, MAXOUTLEN, "%s %s(", node->args[1], node->args[0]); + out = appendsnprintf(out, MAXOUTLEN, "%s %s(", node->args[1], node->args[0]); int i = 2; while (node->args[i] != NULL){ - vartypeToC(node->args[i], out); + out = vartypeToC(node->args[i], out); i++; } - snprintf(out, MAXOUTLEN, ");\n"); + out = appendsnprintf(out, MAXOUTLEN, ");\n"); } else if (strcmp(names[12], node->func) == 0){ - reversepolishToC(node->children[0], out); + out = reversepolishToC(node->children[0], out); } else { - snprintf(out, MAXOUTLEN, "%s(", node->func); + out = appendsnprintf(out, MAXOUTLEN, "%s(", node->func); int i = 0; while (node->args[i] != NULL){ - snprintf(out, MAXOUTLEN, "%s", node->args[i]); + out = appendsnprintf(out, MAXOUTLEN, "%s", node->args[i]); i++; } - snprintf(out, MAXOUTLEN, ");\n"); + out = appendsnprintf(out, MAXOUTLEN, ");\n"); } + return out; } diff --git a/comp/lucas-standen-NEA/code2/comp.h b/comp/lucas-standen-NEA/code2/comp.h @@ -2,5 +2,5 @@ #include <stdio.h> #define MAXOUTLEN 512 -void compile(astNode *node, char *out); +char *compile(astNode *node); diff --git a/comp/lucas-standen-NEA/code2/sample b/comp/lucas-standen-NEA/code2/sample Binary files differ. diff --git a/comp/lucas-standen-NEA/code2/sample.zpy b/comp/lucas-standen-NEA/code2/sample.zpy @@ -1,3 +1,7 @@ +(symbol putchar int c:int) + (defun main int) - (putchar a) + (if (= 1 1)) + (putchar 65) + (endif) (endfun) diff --git a/comp/lucas-standen-NEA/code2/zpy b/comp/lucas-standen-NEA/code2/zpy Binary files differ. diff --git a/comp/lucas-standen-NEA/code2/zpy.c b/comp/lucas-standen-NEA/code2/zpy.c @@ -25,10 +25,8 @@ int main(int argc, char **argv){ stringTokens->strs[i]++; stringTokens->strs[i][strlen(stringTokens->strs[i]) - 1] = '\0'; astNode *line = tokenize(stringTokens->strs[i]); - char *out = malloc(MAXOUTLEN); - compile(line, out); - printf("%s", out); - free(out); + + printf("%s", compile(line)); }