diff options
author | thing1 <thing1@seacrossedlovers.xyz> | 2024-07-01 11:36:45 +0100 |
---|---|---|
committer | thing1 <thing1@seacrossedlovers.xyz> | 2024-07-01 11:36:45 +0100 |
commit | d933ce70bd2c497e4af26483abafebfce436986e (patch) | |
tree | b15f4f2c297734108ff0717334b912b87a0f995b /comp/lucas-standen-NEA/code | |
parent | 42047fea26b14edc67b394db18ce7edb0c6399f8 (diff) |
updated work
Diffstat (limited to 'comp/lucas-standen-NEA/code')
29 files changed, 280 insertions, 136 deletions
diff --git a/comp/lucas-standen-NEA/code/Makefile b/comp/lucas-standen-NEA/code/Makefile index e69de29..d8330a0 100644 --- a/comp/lucas-standen-NEA/code/Makefile +++ b/comp/lucas-standen-NEA/code/Makefile @@ -0,0 +1,8 @@ +all: + export FLAGS="-ggdb" + cd global; make + cd execution; make + cd tokenizer; make + cd ads; make + cd proto; make + $(info done!) diff --git a/comp/lucas-standen-NEA/code/TODO b/comp/lucas-standen-NEA/code/TODO new file mode 100644 index 0000000..5d3078a --- /dev/null +++ b/comp/lucas-standen-NEA/code/TODO @@ -0,0 +1,10 @@ +MAKE LITERAL ARGS A UNION +something like this +union { + i32 i32Value; + u32 u32Value; + 164 i32Value; + u64 u32Value; + char charValue; + float floatValue; +} diff --git a/comp/lucas-standen-NEA/code/ads/ll/Makefile b/comp/lucas-standen-NEA/code/ads/ll/Makefile index aaf0241..e91e445 100644 --- a/comp/lucas-standen-NEA/code/ads/ll/Makefile +++ b/comp/lucas-standen-NEA/code/ads/ll/Makefile @@ -1,5 +1,5 @@ all: ll.c - cc ll.c -c -o ll.o + cc ll.c -c -o ll.o test: all cc ll.o lltest.c -o lltest diff --git a/comp/lucas-standen-NEA/code/execution/Makefile b/comp/lucas-standen-NEA/code/execution/Makefile index e69de29..405f9de 100644 --- a/comp/lucas-standen-NEA/code/execution/Makefile +++ b/comp/lucas-standen-NEA/code/execution/Makefile @@ -0,0 +1,6 @@ +all: exec builtin + $(info done execution!) +exec: exec.c builtin + cc exec.c builtin.o ../tokenizer/tokenizer.o ../global/util.o -o exec -ggdb +builtin: builtin.c + cc builtin.c -c -o builtin.o -ggdb diff --git a/comp/lucas-standen-NEA/code/execution/builtin.c b/comp/lucas-standen-NEA/code/execution/builtin.c new file mode 100644 index 0000000..df3ca03 --- /dev/null +++ b/comp/lucas-standen-NEA/code/execution/builtin.c @@ -0,0 +1,45 @@ +#include <stdlib.h> +#include <stdio.h> +#include "../global/types.h" +#include "../global/util.h" + +#define MAXARGS 8 + +void *doCall(ast_node *node){ + builtInFuncs id = node->func->builtInFunc; + for (int i = 0; i < MAXARGS; i++){ + if (node->args[i] != NULL){ + node->literalArgs[i] = doCall(node->args[i]); + } + } + + char *str = CheckedMalloc(20); + switch (id){ + case ADD: + snprintf(str, 20, "%d", atoi(node->literalArgs[0]) + atoi(node->literalArgs[1])); + return str; + break; + case SUB: + snprintf(str, 20, "%d", atoi(node->literalArgs[0]) - atoi(node->literalArgs[1])); + return str; + break; + case DIV: + snprintf(str, 20, "%d", atoi(node->literalArgs[0]) / atoi(node->literalArgs[1])); + return str; + break; + case MUL: + snprintf(str, 20, "%d", atoi(node->literalArgs[0]) * atoi(node->literalArgs[1])); + return str; + break; + + case WRITE: + fputs(node->literalArgs[0], stdout); + break; + + case EXIT: + int returnValue = atoi(node->literalArgs[0]); + CheckedFreeALL(); + exit(returnValue); + break; + } +} diff --git a/comp/lucas-standen-NEA/code/execution/builtin.h b/comp/lucas-standen-NEA/code/execution/builtin.h new file mode 100644 index 0000000..46ffde5 --- /dev/null +++ b/comp/lucas-standen-NEA/code/execution/builtin.h @@ -0,0 +1,7 @@ +#include <stdlib.h> +#include "../global/types.h" +#include "../global/util.h" + +#define MAXARGS 8 + +void *doCall(ast_node *node); diff --git a/comp/lucas-standen-NEA/code/execution/exec b/comp/lucas-standen-NEA/code/execution/exec Binary files differnew file mode 100755 index 0000000..ca6eb33 --- /dev/null +++ b/comp/lucas-standen-NEA/code/execution/exec diff --git a/comp/lucas-standen-NEA/code/execution/exec.c b/comp/lucas-standen-NEA/code/execution/exec.c index e69de29..fb85d3f 100644 --- a/comp/lucas-standen-NEA/code/execution/exec.c +++ b/comp/lucas-standen-NEA/code/execution/exec.c @@ -0,0 +1,24 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "./builtin.h" + +#include "../global/util.h" + +#include "../tokenizer/tokenizer.h" + +int main(){ + char *sample = "(write (* 2 5))"; + ast_node *root = tokenize(sample); + doCall(root); + sample = "(write \n)"; + root = tokenize(sample); + doCall(root); + sample = "(exit 0)"; + root = tokenize(sample); + doCall(root); + + CheckedFreeALL(); + + return 0; +} diff --git a/comp/lucas-standen-NEA/code/execution/types.c b/comp/lucas-standen-NEA/code/execution/types.c deleted file mode 100644 index e69de29..0000000 --- a/comp/lucas-standen-NEA/code/execution/types.c +++ /dev/null diff --git a/comp/lucas-standen-NEA/code/execution/types.h b/comp/lucas-standen-NEA/code/execution/types.h deleted file mode 100644 index e69de29..0000000 --- a/comp/lucas-standen-NEA/code/execution/types.h +++ /dev/null diff --git a/comp/lucas-standen-NEA/code/global/Makefile b/comp/lucas-standen-NEA/code/global/Makefile new file mode 100644 index 0000000..dbff2b0 --- /dev/null +++ b/comp/lucas-standen-NEA/code/global/Makefile @@ -0,0 +1,4 @@ +all: util + $(info done!) +util: util.c + cc util.c -c -o util.o diff --git a/comp/lucas-standen-NEA/code/tokenizer/types.h b/comp/lucas-standen-NEA/code/global/types.h index 8c79bd9..e73d55d 100644 --- a/comp/lucas-standen-NEA/code/tokenizer/types.h +++ b/comp/lucas-standen-NEA/code/global/types.h @@ -1,17 +1,6 @@ #include <stdint.h> #include "../ads/ll/ll.h" -// all language types -typedef enum types { - I32_T = 0, - I64_T = 1, - U32_T = 2, - U64_T = 3, - FLOAT_t = 4, - CHAR_T = 5, - FUNCTION_T = 6, -} types; - // int types typedef int32_t i32; typedef int64_t i64; @@ -54,15 +43,14 @@ typedef enum builtInFuncs { TYPEOF = 21, EXIT = 22, RETURN = 23, + WRITE = 24, NIL = -1, } builtInFuncs; // function type typedef struct functionToken { int id; // a function id to avoid strings - types returnType; // what the function returns - types *args; // the types of args a function takes - ll_t *func; // the code for the function + char *name; // the code for the function builtInFuncs builtInFunc; // a built in functions } functionToken; @@ -70,7 +58,7 @@ typedef struct ast_node ast_node; typedef struct ast_node { functionToken *func; // if it's not builtin then use this - void **literalArgs; // the args of the node, this will be an array of litteral values + char **literalArgs; // the args of the node, this will be an array of litteral values ast_node **args; // the non litteral tokens // if litteralArgs[x] is real then args[x] should be NULL, and vice versa } ast_node; diff --git a/comp/lucas-standen-NEA/code/global/util.c b/comp/lucas-standen-NEA/code/global/util.c new file mode 100644 index 0000000..736619b --- /dev/null +++ b/comp/lucas-standen-NEA/code/global/util.c @@ -0,0 +1,73 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <stdbool.h> +#include <errno.h> +#include <error.h> + +// functions for user +void Die(); // brings down the program +void *CheckedMalloc(long size); // malloc checked with autofree +void *CheckedRealloc(void *out, long size); // realloc checked with autofree +int CheckedFree(void *ptr); // frees a pointer if it is in the array MEMptrs +void CheckedFreeALL(); // frees all pointers in the array MEMptrs + +#define MAXPTRS 30000 // maximum allocs done by user + +void *MEMptrs[MAXPTRS] = { NULL }; +size_t currentPtr = 0; + +void Die(){ + perror("zpy parser"); + exit(errno); +} + +void *CheckedMalloc(long size){ + void *out = malloc(size); + if (out == NULL) + Die(); + MEMptrs[currentPtr] = out; + currentPtr++; + if (currentPtr > MAXPTRS){ + printf("used %d ptrs!\n", MAXPTRS); + Die(); + } + return out; +} + +void *CheckedRealloc(void *orig, long size){ + void *out = realloc(orig, size); + if (out == NULL) + Die(); + MEMptrs[currentPtr] = out; + currentPtr++; + if (currentPtr > MAXPTRS){ + printf("used %d ptrs!\n", MAXPTRS); + Die(); + } + + for (int i = 0; i < MAXPTRS; i++) + if (MEMptrs[i] == orig && MEMptrs[i] != NULL) MEMptrs[i] = NULL; + + return out; +} + +int CheckedFree(void *ptr){ + if (ptr == NULL) return 1; + for (int i = 0; i < MAXPTRS; i++){ + if (MEMptrs[i] == ptr){ + free(MEMptrs[i]); + MEMptrs[i] = NULL; + return 0; + } + } + return 1; +} + +void CheckedFreeALL(){ + for (int i = 0; i < MAXPTRS; i++){ + if (MEMptrs[i] != NULL){ + free(MEMptrs[i]); + } + } +} diff --git a/comp/lucas-standen-NEA/code/global/util.h b/comp/lucas-standen-NEA/code/global/util.h new file mode 100644 index 0000000..8feed88 --- /dev/null +++ b/comp/lucas-standen-NEA/code/global/util.h @@ -0,0 +1,13 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <stdbool.h> +#include <errno.h> +#include <error.h> + +// functions for user +void Die(); // brings down the program +void *CheckedMalloc(long size); // malloc checked with autofree +void *CheckedRealloc(void *out, long size); // realloc checked with autofree +int CheckedFree(void *ptr); // frees a pointer if it is in the array MEMptrs +void CheckedFreeALL(); // frees all pointers in the array MEMptrs diff --git a/comp/lucas-standen-NEA/code/proto/ast/Makefile b/comp/lucas-standen-NEA/code/proto/Makefile index 6074b00..6074b00 100644 --- a/comp/lucas-standen-NEA/code/proto/ast/Makefile +++ b/comp/lucas-standen-NEA/code/proto/Makefile diff --git a/comp/lucas-standen-NEA/code/proto/ast/ast b/comp/lucas-standen-NEA/code/proto/ast Binary files differindex 817c30e..f922f0d 100755 --- a/comp/lucas-standen-NEA/code/proto/ast/ast +++ b/comp/lucas-standen-NEA/code/proto/ast diff --git a/comp/lucas-standen-NEA/code/proto/ast/ast.c b/comp/lucas-standen-NEA/code/proto/ast.c index d76107b..d76107b 100644 --- a/comp/lucas-standen-NEA/code/proto/ast/ast.c +++ b/comp/lucas-standen-NEA/code/proto/ast.c diff --git a/comp/lucas-standen-NEA/code/proto/ast/astg.c b/comp/lucas-standen-NEA/code/proto/astg.c index e96b771..e96b771 100644 --- a/comp/lucas-standen-NEA/code/proto/ast/astg.c +++ b/comp/lucas-standen-NEA/code/proto/astg.c diff --git a/comp/lucas-standen-NEA/code/proto/ast/astg.h b/comp/lucas-standen-NEA/code/proto/astg.h index 16250b2..16250b2 100644 --- a/comp/lucas-standen-NEA/code/proto/ast/astg.h +++ b/comp/lucas-standen-NEA/code/proto/astg.h diff --git a/comp/lucas-standen-NEA/code/proto/ast/left b/comp/lucas-standen-NEA/code/proto/left index e69de29..e69de29 100644 --- a/comp/lucas-standen-NEA/code/proto/ast/left +++ b/comp/lucas-standen-NEA/code/proto/left diff --git a/comp/lucas-standen-NEA/code/proto/ast/right b/comp/lucas-standen-NEA/code/proto/right index e69de29..e69de29 100644 --- a/comp/lucas-standen-NEA/code/proto/ast/right +++ b/comp/lucas-standen-NEA/code/proto/right diff --git a/comp/lucas-standen-NEA/code/tokenizer/Makefile b/comp/lucas-standen-NEA/code/tokenizer/Makefile index b09f177..73e4033 100644 --- a/comp/lucas-standen-NEA/code/tokenizer/Makefile +++ b/comp/lucas-standen-NEA/code/tokenizer/Makefile @@ -1,10 +1,9 @@ -tokenizer: parser util tokenizer.c - cc -O3 tokenizer.c parser.o util.o -o tokenizer -ggdb +all: tokenizer parser + $(info done tokenizer!) +tokenizer: tokenizer.c + cc tokenizer.c -c -o tokenizer.o parser: parser.c - cc -O3 parser.c -c -o parser.o -ggdb -util: util.c - cc -O3 util.c -c -o util.o -ggdb + cc parser.c -c -o parser.o clean: rm -rf *.o - rm -rf tokenizer diff --git a/comp/lucas-standen-NEA/code/tokenizer/parser.c b/comp/lucas-standen-NEA/code/tokenizer/parser.c index 9ac9fde..69ec458 100644 --- a/comp/lucas-standen-NEA/code/tokenizer/parser.c +++ b/comp/lucas-standen-NEA/code/tokenizer/parser.c @@ -1,11 +1,10 @@ #include <stdio.h> #include <stdlib.h> -#include "util.h" - -char *Parse(char *fileName); // general parser function +#include "../global/util.h" char *ReadFile(char *fileName); // reads the file into a single var +char *Parse(char *fileName); // general parser function char *ReadFile(char *filename){ FILE *f = fopen(filename, "r"); diff --git a/comp/lucas-standen-NEA/code/tokenizer/sample.zpy b/comp/lucas-standen-NEA/code/tokenizer/sample.zpy deleted file mode 100644 index f0d9700..0000000 --- a/comp/lucas-standen-NEA/code/tokenizer/sample.zpy +++ /dev/null @@ -1,5 +0,0 @@ -(let x:char[] "he -llo") - -(let y:i32 20) - diff --git a/comp/lucas-standen-NEA/code/tokenizer/tokenizer b/comp/lucas-standen-NEA/code/tokenizer/tokenizer Binary files differdeleted file mode 100755 index 2d2f1c0..0000000 --- a/comp/lucas-standen-NEA/code/tokenizer/tokenizer +++ /dev/null diff --git a/comp/lucas-standen-NEA/code/tokenizer/tokenizer.c b/comp/lucas-standen-NEA/code/tokenizer/tokenizer.c index 080951b..a76760b 100644 --- a/comp/lucas-standen-NEA/code/tokenizer/tokenizer.c +++ b/comp/lucas-standen-NEA/code/tokenizer/tokenizer.c @@ -1,12 +1,24 @@ #include <stdio.h> #include <string.h> -#include "types.h" -#include "util.h" +#include "../global/types.h" +#include "../global/util.h" #define MAXARGS 8 +#define MAXFUNCS 2048 +#define MAXVARS 8192 -int getBuiltIn(char *func, ast_node *node){ +char *userDefinedFunctions[MAXFUNCS]; +char *userDefinedVars[MAXVARS]; +size_t userFuncCount = 0; +size_t userVarCount = 0; + +int getBuiltIn(char *func, ast_node *node); // checks if a function is built in to zippy +void expressFunction(char *function, ast_node *node); // puts a string into the ast_node struct +ast_node *tokenize(char *input); // does the tokenization +void printAst(ast_node *root); // shows an ast and its sub nodes + +int getBuiltIn(char *func, ast_node *node){ // returns NIL when the function doesn't exist if (strcmp(func, "defun") == 0){ node->func->builtInFunc= DEFUN; }else if (strcmp(func, "let") == 0){ @@ -53,6 +65,8 @@ int getBuiltIn(char *func, ast_node *node){ node->func->builtInFunc = EXIT; }else if (strcmp(func, "return") == 0){ node->func->builtInFunc = RETURN; + }else if (strcmp(func, "write") == 0){ + node->func->builtInFunc = WRITE; }else { node->func->builtInFunc = NIL; return -1; @@ -60,24 +74,10 @@ int getBuiltIn(char *func, ast_node *node){ return 0; } -ll_t *getUserDefinedFunction(char *function); - void expressFunction(char *function, ast_node *node){ node->func = CheckedMalloc(sizeof(functionToken)); - if ((getBuiltIn(function, node)) == -1){ - //node->func->func = getUserDefinedFunction(function); - } else { - node->func->func = NULL; - } -} - -void expressArgs(char **args, ast_node *node){ - for (int i = 0; i < MAXARGS; i++){ - if (node->args[i] == NULL){ - memcpy(node->literalArgs[i], args[i], strlen(args[i]) + 1); - } - } - + if ((getBuiltIn(function, node)) == NIL) // non user defined function + node->func->name = function; } ast_node *tokenize(char *input){ @@ -113,7 +113,6 @@ ast_node *tokenize(char *input){ } exp[i-2] = '\0'; exp = CheckedRealloc(exp, strlen(exp) + 1); - printf("%s\n", exp); }else if (input[i] == '"'){ i++; while (input[i] != '"') i++; @@ -129,23 +128,60 @@ ast_node *tokenize(char *input){ function[i] = '\0'; function = CheckedRealloc(function, i); - printf("%s\n", function); expressFunction(function, node); - i++; - args = Split(&input[i], ' '); - // need a length - expressArgs(args, node /* length */ ); + char *tok; + tok = strtok(strstr(exp, " ") + 1, " "); + argCount = 0; + depth = 0; + do { + if (node->args[argCount] != NULL){ + argCount++; + } + if (tok[0] != '(' && tok[strlen(tok)-1] != ')' && depth == 0){ + if (node->args[argCount] == NULL){ + node->literalArgs[argCount] = malloc(strlen(tok)+1); + node->literalArgs[argCount] = tok; + } + argCount++; + } + + if (tok[0] == '(') depth++; + if (tok[strlen(tok)-1] == ')') depth--; + tok = strtok(NULL, " "); + } while (tok != NULL); + + if (strcmp(function, "set") == 0 || strcmp(function, "let") == 0){ + char *varName; + char *varType; + varName = strtok(node->literalArgs[0], ":"); + varType = strtok(NULL, ":"); + if (strcmp(varType, "function") == 0){ + userDefinedFunctions[userFuncCount] = CheckedMalloc(25); + userDefinedFunctions[userFuncCount] = varName; + userFuncCount++; + }else { + userDefinedVars[userVarCount] = CheckedMalloc(15); + userDefinedVars[userVarCount] = varName; + userVarCount++; + } + } - free(exp); + CheckedFree(exp); return node; } -int main(){ - char sample[] = "(+ (- 2 2) 1)"; - ast_node *root = tokenize(sample); - printf("%d", root->args[0]->func->builtInFunc); - free(root); +void printAst(ast_node *root){ + printf("-----------\n"); + if (root->func->builtInFunc == -1) printf("function: %s\n", root->func->name); + else printf("function (built in): %d\n", root->func->builtInFunc); + for (int i = 0; i < MAXARGS + 1; i++){ + if (root->args[i] != NULL) printAst(root->args[i]); + else { + if (root->literalArgs[i] != NULL) printf("%s\n", root->literalArgs[i]); + } + } + printf("-----------\n"); } diff --git a/comp/lucas-standen-NEA/code/tokenizer/tokenizer.h b/comp/lucas-standen-NEA/code/tokenizer/tokenizer.h new file mode 100644 index 0000000..3cfaaf2 --- /dev/null +++ b/comp/lucas-standen-NEA/code/tokenizer/tokenizer.h @@ -0,0 +1,12 @@ +#include <stdio.h> +#include <string.h> + +#define MAXARGS 8 +#define MAXFUNCS 2048 +#define MAXVARS 8192 + +int getBuiltIn(char *func, ast_node *node); // checks if a function is built in to zippy +void expressFunction(char *function, ast_node *node); // puts a string into the ast_node struct +ast_node *tokenize(char *input); // does the tokenization +void printAst(ast_node *root); // shows an ast and its sub nodes + diff --git a/comp/lucas-standen-NEA/code/tokenizer/util.c b/comp/lucas-standen-NEA/code/tokenizer/util.c deleted file mode 100644 index 46deba8..0000000 --- a/comp/lucas-standen-NEA/code/tokenizer/util.c +++ /dev/null @@ -1,65 +0,0 @@ -#include <stdio.h> -#include <string.h> -#include <stdlib.h> -#include <errno.h> -#include <error.h> - -void Die(); // brings down the program -void *CheckedMalloc(long size); // malloc checked -void *CheckedRealloc(void *out, long size); // realloc checked -char **Split(char *s, char c); // splits a string into an array of strings around c - -void Die(){ - perror("zpy parser"); - exit(errno); -} - -void *CheckedMalloc(long size){ - void *out = malloc(size); - if (out == NULL) - Die(); - return out; -} - -void *CheckedRealloc(void *orig, long size){ - void *out = realloc(orig, size); - if (out == NULL) - Die(); - return out; -} - -static size_t countSegment(char const *s, char c){ - size_t counter = 0; - int i = 0; - while (s[i]){ - if (s[i] == c){ - i++; - continue; - } - counter++; - while (s[i] && s[i] != c) i++; - } - return counter; -} - -char **Split(char *s, char c){ - char **strs; - size_t tab_counter; - size_t i; - size_t j; - - if (s == NULL) return NULL; - tab_counter = countSegment(s, c); - if ((strs = (char**)CheckedMalloc(sizeof(char*) * (tab_counter + 1))) == NULL) return NULL; - tab_counter = 0; - j = -1; - while (s[++j]) { - if (s[j] == c) continue; - i = 0; - while (s[j + i] && s[j + i] != c) i++; - if ((strs[tab_counter++] = strndup(&s[j], i)) == NULL) return NULL; - j += i - 1; - } - strs[tab_counter] = NULL; - return strs; -} diff --git a/comp/lucas-standen-NEA/code/tokenizer/util.h b/comp/lucas-standen-NEA/code/tokenizer/util.h deleted file mode 100644 index c25ebec..0000000 --- a/comp/lucas-standen-NEA/code/tokenizer/util.h +++ /dev/null @@ -1,10 +0,0 @@ -#include <stdio.h> -#include <string.h> -#include <stdlib.h> -#include <errno.h> -#include <error.h> - -void Die(); // brings down the program -void *CheckedMalloc(long size); // malloc checked -void *CheckedRealloc(void *out, long size); // realloc checked -char **Split(char *s, char c); // splits a string into an array of strings around c |