From ce61e41c6a14b84808aceb37d5118f23e3affaa0 Mon Sep 17 00:00:00 2001 From: thing1 Date: Mon, 7 Oct 2024 12:59:43 +0100 Subject: added comments to all of zpy and made the compiler have useful error msgs and for the most part not segv fault --- comp/lucas-standen-NEA/code2/appendsnprintf.c | 2 + comp/lucas-standen-NEA/code2/autodoc/Makefile | 11 ++++ comp/lucas-standen-NEA/code2/autodoc/autodoc | Bin 0 -> 19584 bytes comp/lucas-standen-NEA/code2/autodoc/autodoc.c | 73 +++++++++++++++++++++ comp/lucas-standen-NEA/code2/comp.c | 73 ++++++++++++--------- comp/lucas-standen-NEA/code2/debug.c | 1 + comp/lucas-standen-NEA/code2/examples/fib_example | Bin 25496 -> 0 bytes .../code2/examples/raylib_example | Bin 1086488 -> 0 bytes .../lucas-standen-NEA/code2/examples/spaceinvaders | Bin 1086520 -> 0 bytes comp/lucas-standen-NEA/code2/examples/str_example | Bin 25952 -> 0 bytes comp/lucas-standen-NEA/code2/examples/tmp.zpy.c | 2 - comp/lucas-standen-NEA/code2/parser.c | 4 +- comp/lucas-standen-NEA/code2/tokenizer.c | 7 +- comp/lucas-standen-NEA/code2/util.c | 2 +- comp/lucas-standen-NEA/code2/zpy | Bin 42536 -> 0 bytes comp/lucas-standen-NEA/code2/zpy.c | 3 +- 16 files changed, 137 insertions(+), 41 deletions(-) create mode 100644 comp/lucas-standen-NEA/code2/autodoc/Makefile create mode 100755 comp/lucas-standen-NEA/code2/autodoc/autodoc create mode 100644 comp/lucas-standen-NEA/code2/autodoc/autodoc.c delete mode 100755 comp/lucas-standen-NEA/code2/examples/fib_example delete mode 100755 comp/lucas-standen-NEA/code2/examples/raylib_example delete mode 100755 comp/lucas-standen-NEA/code2/examples/spaceinvaders delete mode 100755 comp/lucas-standen-NEA/code2/examples/str_example delete mode 100644 comp/lucas-standen-NEA/code2/examples/tmp.zpy.c delete mode 100755 comp/lucas-standen-NEA/code2/zpy (limited to 'comp/lucas-standen-NEA/code2') diff --git a/comp/lucas-standen-NEA/code2/appendsnprintf.c b/comp/lucas-standen-NEA/code2/appendsnprintf.c index ffa8349..a638c99 100644 --- a/comp/lucas-standen-NEA/code2/appendsnprintf.c +++ b/comp/lucas-standen-NEA/code2/appendsnprintf.c @@ -3,6 +3,7 @@ #include #include +//# this function will generate the fmt specifier for appendsnprintf, should only be used for this use case char *genfmt(char *buf, char *fmt){ int len = strlen(buf) + strlen(fmt) + 1; char *out = malloc(len); @@ -21,6 +22,7 @@ char *genfmt(char *buf, char *fmt){ return out; } +//# this function will append size number of bytes, onto the end of buf, a format string in the form of vaargs char *appendsnprintf(char *buf, int size, char *format, ...){ va_list ap; char *outputbuf = malloc(size); diff --git a/comp/lucas-standen-NEA/code2/autodoc/Makefile b/comp/lucas-standen-NEA/code2/autodoc/Makefile new file mode 100644 index 0000000..76ef5c1 --- /dev/null +++ b/comp/lucas-standen-NEA/code2/autodoc/Makefile @@ -0,0 +1,11 @@ +autodoc: autodoc.c + cc autodoc.c -o autodoc -ggdb + +install: autodoc + cp autodoc /usr/local/bin/autodoc + +uninstall: + rm /usr/local/bin/autodoc + +clean: + rm autodoc diff --git a/comp/lucas-standen-NEA/code2/autodoc/autodoc b/comp/lucas-standen-NEA/code2/autodoc/autodoc new file mode 100755 index 0000000..0ef6fad Binary files /dev/null and b/comp/lucas-standen-NEA/code2/autodoc/autodoc differ diff --git a/comp/lucas-standen-NEA/code2/autodoc/autodoc.c b/comp/lucas-standen-NEA/code2/autodoc/autodoc.c new file mode 100644 index 0000000..d8e7583 --- /dev/null +++ b/comp/lucas-standen-NEA/code2/autodoc/autodoc.c @@ -0,0 +1,73 @@ +#include +#include +#include +#include + +typedef struct docpair { + char *doc; + char *func; +} docpair; + +docpair funcs[2048]; +int count = 0; + +//# returns a bool as to weather s2 is contained fully in s1 +bool contains(char *s1, char *s2){ + int j = 0; + if (strlen(s1) < strlen(s2)) return false; + for (int i = 0; i < strlen(s1); i++){ + if (s1[i] == s2[j]){ + if (j+1 == strlen(s2)) return true; + j++; + } + } + return false; +} + +int main(int argc, char **argv){ + FILE *in; + if (argv[1][0] == '-') in = stdin; + else in = fopen(argv[1], "r"); + + char *search = argv[2]; + + char *line = malloc(512); + char *tmp = malloc(512); + + bool nextIsFunc = false; + + while (fgets(line, 512, in) != NULL){ + strcpy(tmp, line); + tmp[4] = '\0'; + if (strcmp(tmp, "//# ") == 0){ + funcs[count].doc = malloc(strlen(line)+1); + strcpy(funcs[count].doc, line); + nextIsFunc = true; + } + else if (nextIsFunc == true){ + funcs[count].func = malloc(strlen(line)+1); + strcpy(funcs[count].func, line); + nextIsFunc = false; + count++; + } + } + + if (search != NULL){ + for (int i = 0; i < 2048; i++){ + if (funcs[i].doc != NULL) + if (contains(funcs[i].func, search)) + printf("%s \e[1m%s\e[0m\n", funcs[i].doc, funcs[i].func); + } + } + else { + for (int i = 0; i < 2048; i++){ + if (funcs[i].doc != NULL) + printf("%s \e[1m%s\e[0m\n", funcs[i].doc, funcs[i].func); + } + } + + free(line); + free(tmp); + + +} diff --git a/comp/lucas-standen-NEA/code2/comp.c b/comp/lucas-standen-NEA/code2/comp.c index 087c536..d49148e 100644 --- a/comp/lucas-standen-NEA/code2/comp.c +++ b/comp/lucas-standen-NEA/code2/comp.c @@ -11,11 +11,21 @@ #define MAXOUTLEN 512 +// globals for memory management +char *tofree[256]; +int freeptr = 0; +bool neededmemptr = false; + +// globals for structs +char *structname; + +// globals for errors pid_t pid; int linecount = 0; char *currentLine; char *errmsg; +//# error handler that will be triggered by signal when there is a syntax error and will print information void errorhandle(int type){ fprintf(stderr, "err:%d (%s)\n", linecount, currentLine); fprintf(stderr, "%s\n", errmsg); @@ -63,17 +73,10 @@ char *names[] = { "defunptr" // takes the same stuff as defun but outputs it in fuction ptr form }; -// function prototype for recursion +// function prototype for recursion of the compile function char *compile(astNode *node); -// globals for memory management -char *tofree[256]; -int freeptr = 0; -bool neededmemptr = false; - -// globals for stucts -char *structname; - +//# this function will convert the zpy style variable defintion, to the c style char *vartypeToC(char *str, char *out){ char *name = malloc(strlen(str)); char *type = malloc(strlen(str)); @@ -106,6 +109,7 @@ char *vartypeToC(char *str, char *out){ return out; } +//# this function will give the user the variable name, without its type, which is useful for translation char *getVarName(char *exp){ char *out = malloc(strlen(exp)); memcpy(out, exp, strlen(exp)); @@ -114,13 +118,14 @@ char *getVarName(char *exp){ return out; } +//# this function will give the user the type, without its name, which is useful for translation char *getVarType(char *exp){ char *out = malloc(strlen(exp)); char *pos = strchr(exp, ':')+1; memcpy(out, pos, strlen(pos) + 1); return out; } - +//# this will convert mathmatical expressions, to the c style of maths char *reversepolishToC(astNode *exp, char *out){ out = appendsnprintf(out, MAXOUTLEN, "%s ", exp->args[0]); if (exp->func[0] == '=') out = appendsnprintf(out, MAXOUTLEN, "=="); @@ -129,6 +134,7 @@ char *reversepolishToC(astNode *exp, char *out){ return out; } +//# this is a recursive loop that will call the compile function recursivly to flatten the tree astNode *processChildren(astNode *node){ for (int i = 0; i < 8; i++){ if (node->children[i] != NULL){ @@ -139,6 +145,7 @@ astNode *processChildren(astNode *node){ return node; } +//# this function will check if the value given is null, if it is, it will cause a sig segv and set the error msg void checkNULL(void *value, char *msg){ if (value == NULL) { errmsg = msg; @@ -146,10 +153,11 @@ void checkNULL(void *value, char *msg){ } } +//# this function will do the bulk of converting from zpy into c code char *compile(astNode *node){ char *out = calloc(0, MAXOUTLEN); node = processChildren(node); - if (strcmp(names[0], node->func) == 0){ + if (strcmp(names[0], node->func) == 0){ // converting function definitions checkNULL(node->args[0], "expected func name"); checkNULL(node->args[1], "expected return type"); out = appendsnprintf(out, MAXOUTLEN, "%s %s(", node->args[1], node->args[0]); @@ -161,38 +169,38 @@ char *compile(astNode *node){ } out = appendsnprintf(out, MAXOUTLEN, "){\n"); } - else if (strcmp(names[1], node->func) == 0){ + else if (strcmp(names[1], node->func) == 0){ // converting ending function definitions out = appendsnprintf(out, MAXOUTLEN, "}\n"); } - else if (strcmp(names[2], node->func) == 0){ + else if (strcmp(names[2], node->func) == 0){ // converting variable declarations checkNULL(node->args[0], "expected var name"); checkNULL(node->args[1], "expected var value"); out = vartypeToC(node->args[0], out); out = appendsnprintf(out, MAXOUTLEN, " = %s;\n", node->args[1]); } - else if (strcmp(names[3], node->func) == 0){ + else if (strcmp(names[3], node->func) == 0){ // converting vairable reasignments checkNULL(node->args[0], "expected var name"); checkNULL(node->args[1], "expected var value"); out = appendsnprintf(out, MAXOUTLEN, "%s = %s;\n", node->args[0], node->args[1]); } - else if (strcmp(names[4], node->func) == 0){ + else if (strcmp(names[4], node->func) == 0){ // converting if statments checkNULL(node->args[0], "expected sub expression"); out = appendsnprintf(out, MAXOUTLEN, "if (%s", node->args[0]); out = appendsnprintf(out, MAXOUTLEN, "){\n"); } - else if (strcmp(names[5], node->func) == 0){ + else if (strcmp(names[5], node->func) == 0){ // converting ending if statments out = appendsnprintf(out, MAXOUTLEN, "}\n"); } - else if (strcmp(names[6], node->func) == 0){ + else if (strcmp(names[6], node->func) == 0){ // converting elif (else if) statments checkNULL(node->args[0], "expected sub expression"); out = appendsnprintf(out, MAXOUTLEN, "}else if (%s", node->args[0]); out = appendsnprintf(out, MAXOUTLEN, "){\n"); } - else if (strcmp(names[7], node->func) == 0){ + else if (strcmp(names[7], node->func) == 0){ // converting else statments out = appendsnprintf(out, MAXOUTLEN, "}\n"); out = appendsnprintf(out, MAXOUTLEN, "else{"); } - else if (strcmp(names[8], node->func) == 0){ + else if (strcmp(names[8], node->func) == 0){ // converting for loop statments checkNULL(node->args[0], "expected iterator"); checkNULL(node->args[1], "expected iterator value"); checkNULL(node->args[2], "expected condition"); @@ -203,10 +211,10 @@ char *compile(astNode *node){ 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){ + else if (strcmp(names[9], node->func) == 0){ // converting end for loop statments out = appendsnprintf(out, MAXOUTLEN, "}\n"); } - else if (strcmp(names[10], node->func) == 0){ + else if (strcmp(names[10], node->func) == 0){ // converting symbol definition statments checkNULL(node->args[0], "expected symbol type"); checkNULL(node->args[1], "expected symbol name"); out = appendsnprintf(out, MAXOUTLEN, "%s %s(", node->args[1], node->args[0]); @@ -218,7 +226,7 @@ char *compile(astNode *node){ } out = appendsnprintf(out, MAXOUTLEN, ");\n"); } - else if (strcmp(names[21], node->func) == 0){ + else if (strcmp(names[21], node->func) == 0){ //converting exit statments checkNULL(node->args[0], "expected exit code"); out = appendsnprintf(out, MAXOUTLEN, "exit(%s);\n", node->args[0]); } @@ -234,33 +242,33 @@ char *compile(astNode *node){ break; } } - checkNULL(node->args[0], "expected return value"); + checkNULL(node->args[0], "expected return value"); // converting return statments out = appendsnprintf(out, MAXOUTLEN, "return %s;\n", node->args[0]); } - else if (strcmp(names[23], node->func) == 0){ + else if (strcmp(names[23], node->func) == 0){ // converting memory allocation statments checkNULL(node->args[0], "expected alloc size"); out = appendsnprintf(out, MAXOUTLEN, "malloc(%s)", node->args[0]); neededmemptr = true; } - else if (strcmp(names[24], node->func) == 0){ + else if (strcmp(names[24], node->func) == 0){ // converting struct definiton statments checkNULL(node->args[0], "expected type name"); 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){ + else if (strcmp(names[25], node->func) == 0){ // converting struct end statments out = appendsnprintf(out, MAXOUTLEN, "} %s", structname); structname = NULL; } - else if (strcmp(names[26], node->func) == 0){ + else if (strcmp(names[26], node->func) == 0){ // converting variable definition statments checkNULL(node->args[0], "expected variable definition"); out = vartypeToC(node->args[0], out); } - else if (strcmp(names[27], node->func) == 0){ + else if (strcmp(names[27], node->func) == 0){ // converting sizeof statments checkNULL(node->args[0], "expected variable type"); out = appendsnprintf(out, MAXOUTLEN, "sizeof(%s)", node->args[0]); } - else if (strcmp(names[28], node->func) == 0){ + else if (strcmp(names[28], node->func) == 0){ // converting function pointer definition staments checkNULL(node->args[0], "expected function ptr type"); checkNULL(node->args[0], "expected function ptr name"); out = appendsnprintf(out, MAXOUTLEN, "%s (*%s)", node->args[1], node->args[0]); @@ -274,7 +282,7 @@ char *compile(astNode *node){ out = appendsnprintf(out, MAXOUTLEN, ")"); } - else { + else { // arithmetic operators and comparitors for (int i = 0; i < 9; i++){ checkNULL(node->func, "expected func name"); @@ -284,7 +292,8 @@ char *compile(astNode *node){ } } - checkNULL(node->func, "expected func name"); + // converting user defined function calls + checkNULL(node->func, "expected func name"); out = appendsnprintf(out, MAXOUTLEN, "%s(", node->func); int i = 0; while (node->args[i] != NULL){ @@ -298,11 +307,13 @@ end: return out; } +//# this function sets up the signal handler for the error msgs void CompilerInit(){ signal(SIGSEGV, &errorhandle); pid = getpid(); } +//# the exposed compiler function that will fully process an astNode tree void Compile(astNode *line, FILE *f, char* strline){ currentLine = strline; char *code = compile(line); diff --git a/comp/lucas-standen-NEA/code2/debug.c b/comp/lucas-standen-NEA/code2/debug.c index 3d2d75b..14b717f 100644 --- a/comp/lucas-standen-NEA/code2/debug.c +++ b/comp/lucas-standen-NEA/code2/debug.c @@ -3,6 +3,7 @@ #include "util.h" +//# a simple util function to visulize an astNode, never used but nice for debugging void printAST(astNode *head){ printf("\n>>>\nfunc: %s\n", head->func); printf("args: "); diff --git a/comp/lucas-standen-NEA/code2/examples/fib_example b/comp/lucas-standen-NEA/code2/examples/fib_example deleted file mode 100755 index bdd94ec..0000000 Binary files a/comp/lucas-standen-NEA/code2/examples/fib_example and /dev/null differ diff --git a/comp/lucas-standen-NEA/code2/examples/raylib_example b/comp/lucas-standen-NEA/code2/examples/raylib_example deleted file mode 100755 index 08ea8fd..0000000 Binary files a/comp/lucas-standen-NEA/code2/examples/raylib_example and /dev/null differ diff --git a/comp/lucas-standen-NEA/code2/examples/spaceinvaders b/comp/lucas-standen-NEA/code2/examples/spaceinvaders deleted file mode 100755 index ec88002..0000000 Binary files a/comp/lucas-standen-NEA/code2/examples/spaceinvaders and /dev/null differ diff --git a/comp/lucas-standen-NEA/code2/examples/str_example b/comp/lucas-standen-NEA/code2/examples/str_example deleted file mode 100755 index c543ab7..0000000 Binary files a/comp/lucas-standen-NEA/code2/examples/str_example and /dev/null differ diff --git a/comp/lucas-standen-NEA/code2/examples/tmp.zpy.c b/comp/lucas-standen-NEA/code2/examples/tmp.zpy.c deleted file mode 100644 index 2305f24..0000000 --- a/comp/lucas-standen-NEA/code2/examples/tmp.zpy.c +++ /dev/null @@ -1,2 +0,0 @@ -#include -int main(){ diff --git a/comp/lucas-standen-NEA/code2/parser.c b/comp/lucas-standen-NEA/code2/parser.c index 7d177ca..62f32fa 100644 --- a/comp/lucas-standen-NEA/code2/parser.c +++ b/comp/lucas-standen-NEA/code2/parser.c @@ -10,7 +10,8 @@ typedef struct strings { int count; } strings; -int countChars(char *s, char c){ // counts the number of times c ocurrs in s +//# counts the number of times c ocurrs in s +int countChars(char *s, char c){ int count = 0; for (int i = 0; i < strlen(s); i++){ if (s[i] == c) count++; @@ -18,6 +19,7 @@ int countChars(char *s, char c){ // counts the number of times c ocurrs in s return count; } +//# returns an array of strings (type strings) of the file contents, split by line strings *parse(FILE *f){ strings *strs = malloc(sizeof(strings)); strs->strs = malloc(sizeof(char **)); diff --git a/comp/lucas-standen-NEA/code2/tokenizer.c b/comp/lucas-standen-NEA/code2/tokenizer.c index c756c0c..0326057 100644 --- a/comp/lucas-standen-NEA/code2/tokenizer.c +++ b/comp/lucas-standen-NEA/code2/tokenizer.c @@ -11,9 +11,8 @@ typedef struct astNode { struct astNode *children[8]; } astNode; -int readuntil(char *src, char c, char *dst){ // returns how many chars read, will read until - // the end of an expression, not the first - // occurence +//# reads a block of code from src, until char, outputting to dst, it allows for brackets so it stays on the same depth +int readuntil(char *src, char c, char *dst){ int ptr = 0; int depth = 0; int i = 0; @@ -36,7 +35,7 @@ int readuntil(char *src, char c, char *dst){ // returns how many chars read, wil return i; } - +//# this function will converts one line of zpy into an astNode, which can be compiled astNode *tokenize(char *line){ // asume the first set of brackets have been stripped astNode *head = malloc(sizeof(astNode)); head->func = NULL; diff --git a/comp/lucas-standen-NEA/code2/util.c b/comp/lucas-standen-NEA/code2/util.c index a1e8f15..6fdb8ad 100644 --- a/comp/lucas-standen-NEA/code2/util.c +++ b/comp/lucas-standen-NEA/code2/util.c @@ -1,7 +1,7 @@ #include #include -#include +//# a simple die function to bring down the program in the case of an error void die(char *msg){ fprintf(stderr, "zpy: %s\n", msg); exit(1); diff --git a/comp/lucas-standen-NEA/code2/zpy b/comp/lucas-standen-NEA/code2/zpy deleted file mode 100755 index dac40be..0000000 Binary files a/comp/lucas-standen-NEA/code2/zpy and /dev/null differ diff --git a/comp/lucas-standen-NEA/code2/zpy.c b/comp/lucas-standen-NEA/code2/zpy.c index 923d5c8..40a497e 100644 --- a/comp/lucas-standen-NEA/code2/zpy.c +++ b/comp/lucas-standen-NEA/code2/zpy.c @@ -16,6 +16,7 @@ bool omitc = false; char compilerflags[64][128]; int compilerflagscount = 0; +//# this function return will deal with the args given to stdin, and put the needed values into globals void processargs(int argc, char **argv){ for (int i = 1; i < argc; i++){ if (argv[i][0] == '-'){ @@ -94,6 +95,4 @@ int main(int argc, char **argv){ system(cmd); remove("./tmp.zpy.c"); } - - } -- cgit v1.2.3