diff options
author | Thing1 <thing1@seacrossedlovers.xyz> | 2024-09-04 19:59:54 +0100 |
---|---|---|
committer | Thing1 <thing1@seacrossedlovers.xyz> | 2024-09-04 19:59:54 +0100 |
commit | c818366f33527bbd0d23d7ff983bb1eaf82fa34b (patch) | |
tree | 0c76b016135e4f34b97c46eaa3aa1bd80fd83cc6 | |
parent | d28f618c0e4c3da57c856a31d9ce3003a086e7ed (diff) |
got some real exec going
-rw-r--r-- | comp/lucas-standen-NEA/code2/Makefile | 19 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/code2/comp.c | 137 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/code2/comp.h | 2 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/code2/parser.c | 45 | ||||
-rwxr-xr-x | comp/lucas-standen-NEA/code2/sample | bin | 0 -> 15464 bytes | |||
-rw-r--r-- | comp/lucas-standen-NEA/code2/sample.zpy | 10 | ||||
-rwxr-xr-x | comp/lucas-standen-NEA/code2/zpy | bin | 16352 -> 0 bytes | |||
-rw-r--r-- | comp/lucas-standen-NEA/code2/zpy.c | 7 | ||||
-rwxr-xr-x | comp/lucas-standen-NEA/code2/zpy.sh | 2 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/writeup/coverpage.ms | 9 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/writeup/coverpage.ps | 2 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/writeup/questions-for-amy.ps | 2 | ||||
-rw-r--r-- | electronics.mp4 | bin | 0 -> 5038951 bytes |
13 files changed, 186 insertions, 49 deletions
diff --git a/comp/lucas-standen-NEA/code2/Makefile b/comp/lucas-standen-NEA/code2/Makefile index d9aa963..50e44c2 100644 --- a/comp/lucas-standen-NEA/code2/Makefile +++ b/comp/lucas-standen-NEA/code2/Makefile @@ -1,19 +1,22 @@ -CFLAGS= -O3 +CC = gcc +CFLAGS = -O0 -ggdb -all: _zpy _parser _tokenizer _util _debug - cc zpy.o parser.o tokenizer.o util.o debug.o -o zpy ${CFLAGS} +all: _zpy _parser _tokenizer _comp _util _debug + ${CC} zpy.o parser.o tokenizer.o comp.o util.o debug.o -o zpy ${CFLAGS} _zpy: zpy.c - cc zpy.c -c -o zpy.o ${CFLAGS} + ${CC} zpy.c -c -o zpy.o ${CFLAGS} _parser: parser.c - cc parser.c -c -o parser.o ${CFLAGS} + ${CC} parser.c -c -o parser.o ${CFLAGS} _tokenizer: tokenizer.c - cc tokenizer.c -c -o tokenizer.o ${CFLAGS} + ${CC} tokenizer.c -c -o tokenizer.o ${CFLAGS} +_comp: comp.c + ${CC} comp.c -c -o comp.o ${CFLAGS} _util: util.c - cc util.c -c -o util.o ${CFLAGS} + ${CC} util.c -c -o util.o ${CFLAGS} _debug: - cc debug.c -c -o debug.o ${CFLAGS} + ${CC} debug.c -c -o debug.o ${CFLAGS} clean: rm -rf zpy *.o diff --git a/comp/lucas-standen-NEA/code2/comp.c b/comp/lucas-standen-NEA/code2/comp.c new file mode 100644 index 0000000..c11fdd8 --- /dev/null +++ b/comp/lucas-standen-NEA/code2/comp.c @@ -0,0 +1,137 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "tokenizer.h" + +char *names[] = { + "defun", // takes a func name, func return type, and args // 0 + "endfun", // takes no args // 1 + "let", // takes a name and type, then a value (immutable) // 2 + "set", // same as above but mutable values // 3 + "if", // takes a condition // 4 + "endif", // takes no args // 5 + "elif", // same as if, but only executes if the prev statment didnt // 6 + "else", // its else! // + "for", // takes a iterator and type, a condition, and an increment // 8 + "endfor", // takes no args // 9 + "write", // takes an int and puts it on the screen // 10 + "symbol", + "+", + "-", + "*", + "/", + "=", + "!=", + "<", + ">", + "<=", + ">=", + "cast", + "typeof", + "exit", + "return", +}; + +void vartypeToC(char *str){ + char *name = malloc(strlen(str)); + char *type = malloc(strlen(str)); + + int j = 0, i = 0; + + for (; i < strlen(str); i++){ + if (str[i] == ':'){ + break; + } + name[i] = str[i]; + } + name[i] = '\0'; + i++; + + for (; i < strlen(str); i++){ + if (str[i] == ':'){ + break; + } + type[j] = str[i]; + j++; + } + type[j] = '\0'; + + printf("%s %s", type, name); + free(type); + free(name); +} + +char *getVarName(char *exp){ + char *out = malloc(strlen(exp)); + memcpy(out, exp, strlen(exp)); + char *pos = strchr(out, ':'); + pos[0] = '\0'; + return out; +} + +void reversepolishToC(astNode *exp){ + printf("%s ", exp->args[0]); + if (exp->func[0] == '=') printf("=="); + else printf("%s", exp->func); + printf(" %s", exp->args[1]); +} + +void compile(astNode *node){ + if (strcmp(names[0], node->func) == 0){ + printf("%s %s(", node->args[1], node->args[0]); + int i = 2; + while (node->args[i] != NULL){ + vartypeToC(node->args[i]); + i++; + } + printf("){\n"); + } + else if (strcmp(names[1], node->func) == 0){ + printf("\n}\n"); + } + else if (strcmp(names[2], node->func) == 0){ + printf("const "); + vartypeToC(node->args[0]); + printf(" = %s;\n", node->args[1]); + } + else if (strcmp(names[3], node->func) == 0){ + vartypeToC(node->args[0]); + printf(" = %s;\n", node->args[1]); + } + else if (strcmp(names[4], node->func) == 0){ + printf("if ("); + reversepolishToC(node->children[0]); + printf("){\n"); + } + else if (strcmp(names[5], node->func) == 0){ + printf("\n}\n"); + } + else if (strcmp(names[6], node->func) == 0){ + printf("\n}\n"); + printf("else if ("); + reversepolishToC(node->children[0]); + printf("){\n"); + } + else if (strcmp(names[7], node->func) == 0){ + printf("\n}\n"); + printf("else{"); + } + else if (strcmp(names[8], node->func) == 0){ + printf("for ("); + vartypeToC(node->args[0]); + printf(" = 0;"); + reversepolishToC(node->children[1]); + printf("; %s+=%s){", getVarName(node->args[0]), node->args[2]); + } + else if (strcmp(names[9], node->func) == 0){ + printf("\n}\n"); + } + else if (strcmp(names[10], node->func) == 0){ + printf("printf(\""); + printf("%%d\\n"); + printf("\", %s);", node->args[0]); + + } + +} diff --git a/comp/lucas-standen-NEA/code2/comp.h b/comp/lucas-standen-NEA/code2/comp.h new file mode 100644 index 0000000..840cd16 --- /dev/null +++ b/comp/lucas-standen-NEA/code2/comp.h @@ -0,0 +1,2 @@ +#include "tokenizer.h" +void compile(astNode *node); diff --git a/comp/lucas-standen-NEA/code2/parser.c b/comp/lucas-standen-NEA/code2/parser.c index d9cb7bd..7650c4f 100644 --- a/comp/lucas-standen-NEA/code2/parser.c +++ b/comp/lucas-standen-NEA/code2/parser.c @@ -19,41 +19,20 @@ int countChars(char *s, char c){ // counts the number of times c ocurrs in s } strings *parse(FILE *f){ - fseek(f, 0, SEEK_END); - int len = ftell(f); - rewind(f); - - char *contents = malloc(len); - - if (fread(contents, 1, len, f) == 0){ - die("failed to read file, is it formated properly"); - } - - char **tokens = malloc(countChars(contents, '\n')); - - int tokCount = 0; - int charCount = 0; - char *line = malloc(strlen(contents)); - for (int i = 0; i < len; i++){ - line[charCount] = contents[i]; - charCount++; - - if (contents[i] == '\n'){ - charCount--; - line[charCount] = '\0'; - tokens[tokCount] = malloc(strlen(line)+1); + strings *strs = malloc(sizeof(strings)); + strs->strs = malloc(sizeof(char **)); - memcpy(tokens[tokCount], line, strlen(line)+1); - charCount = 0; - tokCount++; - } + char *line = alloca(256); + int count = 0; + while (fgets(line, 256, f) != NULL){ + if (line[0] != '\n'){ + line[strlen(line)-1] = '\0'; + strs->strs[count] = malloc(256); + memcpy(strs->strs[count], line, 256); + count++; + } } - strings *strs = malloc(sizeof(strings)); - strs->strs = tokens; - strs->count = tokCount; - - free(line); - free(contents); + strs->count = count; return strs; } diff --git a/comp/lucas-standen-NEA/code2/sample b/comp/lucas-standen-NEA/code2/sample Binary files differnew file mode 100755 index 0000000..08dda04 --- /dev/null +++ b/comp/lucas-standen-NEA/code2/sample diff --git a/comp/lucas-standen-NEA/code2/sample.zpy b/comp/lucas-standen-NEA/code2/sample.zpy index fe7e1ae..c8eaba1 100644 --- a/comp/lucas-standen-NEA/code2/sample.zpy +++ b/comp/lucas-standen-NEA/code2/sample.zpy @@ -1 +1,9 @@ -(let a:i64 (+ (- 1 4) 3)) +(defun main int) +(let i:int 10) +(let a:int 10) + +(if (= i a)) +(write a) +(endif) + +(endfun) diff --git a/comp/lucas-standen-NEA/code2/zpy b/comp/lucas-standen-NEA/code2/zpy Binary files differdeleted file mode 100755 index 640c7fc..0000000 --- a/comp/lucas-standen-NEA/code2/zpy +++ /dev/null diff --git a/comp/lucas-standen-NEA/code2/zpy.c b/comp/lucas-standen-NEA/code2/zpy.c index e88ee2c..69eb6c3 100644 --- a/comp/lucas-standen-NEA/code2/zpy.c +++ b/comp/lucas-standen-NEA/code2/zpy.c @@ -3,7 +3,7 @@ #include "util.h" #include "parser.h" -#include "tokenizer.h" +#include "comp.h" #include "debug.h" @@ -20,12 +20,11 @@ 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])] = '\0'; + stringTokens->strs[i][strlen(stringTokens->strs[i]) - 1] = '\0'; astNode *line = tokenize(stringTokens->strs[i]); + compile(line); } diff --git a/comp/lucas-standen-NEA/code2/zpy.sh b/comp/lucas-standen-NEA/code2/zpy.sh new file mode 100755 index 0000000..72259c2 --- /dev/null +++ b/comp/lucas-standen-NEA/code2/zpy.sh @@ -0,0 +1,2 @@ +#!/bin/sh +./zpy $1 | cc -x c - -Wno-implicit -Wno-builtin-declaration-mismatch -o $2 diff --git a/comp/lucas-standen-NEA/writeup/coverpage.ms b/comp/lucas-standen-NEA/writeup/coverpage.ms index 5099c18..e18f9c0 100644 --- a/comp/lucas-standen-NEA/writeup/coverpage.ms +++ b/comp/lucas-standen-NEA/writeup/coverpage.ms @@ -1,4 +1,11 @@ -.2C +.TL +The solution To bad code +.AU +Lucas Standen +.AI +7949 +.AB + .NH 1 Reading this document .LP diff --git a/comp/lucas-standen-NEA/writeup/coverpage.ps b/comp/lucas-standen-NEA/writeup/coverpage.ps index 5f0ee77..d4cc359 100644 --- a/comp/lucas-standen-NEA/writeup/coverpage.ps +++ b/comp/lucas-standen-NEA/writeup/coverpage.ps @@ -1,6 +1,6 @@ %!PS-Adobe-3.0 %%Creator: groff version 1.23.0 -%%CreationDate: Tue Aug 27 22:07:55 2024 +%%CreationDate: Fri Aug 30 13:51:17 2024 %%DocumentNeededResources: font Times-Bold %%+ font Times-Italic %%+ font Times-Roman diff --git a/comp/lucas-standen-NEA/writeup/questions-for-amy.ps b/comp/lucas-standen-NEA/writeup/questions-for-amy.ps index d4475aa..4332f59 100644 --- a/comp/lucas-standen-NEA/writeup/questions-for-amy.ps +++ b/comp/lucas-standen-NEA/writeup/questions-for-amy.ps @@ -1,6 +1,6 @@ %!PS-Adobe-3.0 %%Creator: groff version 1.23.0 -%%CreationDate: Tue Aug 27 22:07:55 2024 +%%CreationDate: Fri Aug 30 13:51:18 2024 %%DocumentNeededResources: font Times-Bold %%+ font Times-Roman %%DocumentSuppliedResources: procset grops 1.23 0 diff --git a/electronics.mp4 b/electronics.mp4 Binary files differnew file mode 100644 index 0000000..351df31 --- /dev/null +++ b/electronics.mp4 |