diff options
author | thing1 <thing1@seacrossedlovers.xyz> | 2024-07-04 13:11:04 +0100 |
---|---|---|
committer | thing1 <thing1@seacrossedlovers.xyz> | 2024-07-04 13:11:04 +0100 |
commit | 3dad2f4c5ce7fd44015cbd921318b3512c36326e (patch) | |
tree | e43f3b1d0080e9de96c2866743e521ecbd2770f5 /comp/lucas-standen-NEA/code | |
parent | 478eed4ef9ece1b86b2fed7bfe57d62180b12c92 (diff) |
zippy supports arrays!
Diffstat (limited to 'comp/lucas-standen-NEA/code')
-rw-r--r-- | comp/lucas-standen-NEA/code/TODO | 4 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/code/execution/builtin.c | 33 | ||||
-rwxr-xr-x | comp/lucas-standen-NEA/code/execution/exec | bin | 33904 -> 39376 bytes | |||
-rw-r--r-- | comp/lucas-standen-NEA/code/execution/exec.c | 8 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/code/global/types.h | 13 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/code/global/util.c | 111 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/code/global/util.h | 8 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/code/tokenizer/Makefile | 4 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/code/tokenizer/parser.c | 19 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/code/tokenizer/parser.h | 2 | ||||
-rwxr-xr-x | comp/lucas-standen-NEA/code/tokenizer/test | bin | 0 -> 28320 bytes | |||
-rw-r--r-- | comp/lucas-standen-NEA/code/tokenizer/test.c | 6 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/code/tokenizer/tokenizer.c | 12 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/code/tokenizer/tokenizer.h | 2 |
14 files changed, 129 insertions, 93 deletions
diff --git a/comp/lucas-standen-NEA/code/TODO b/comp/lucas-standen-NEA/code/TODO index d15d029..25766b5 100644 --- a/comp/lucas-standen-NEA/code/TODO +++ b/comp/lucas-standen-NEA/code/TODO @@ -1 +1,3 @@ -FIX THE SEGFAULT ON ADDING / SUBTRACTING +finish the pre processor, it should read through a given input and convert all string litterals to arrays + +make it possible to index arrays diff --git a/comp/lucas-standen-NEA/code/execution/builtin.c b/comp/lucas-standen-NEA/code/execution/builtin.c index 99aca8c..12441cd 100644 --- a/comp/lucas-standen-NEA/code/execution/builtin.c +++ b/comp/lucas-standen-NEA/code/execution/builtin.c @@ -13,32 +13,37 @@ void *doCall(ast_node *node){ } } - I64 *outi64 = CheckedMalloc(sizeof(I64)); + literal *out = CheckedMalloc(sizeof(literal)); switch (id){ case ADD: - outi64->data = node->literalArgs[0]->i64->data + node->literalArgs[1]->i64->data; - return outi64; + out->i64 = CheckedMalloc(sizeof(I64)); + out->i64->data = node->literalArgs[0]->i64->data + node->literalArgs[1]->i64->data; + return out; break; - case SUB: - outi64->data = node->literalArgs[0]->i64->data - node->literalArgs[1]->i64->data; - return outi64; + + case SUB: + out->i64 = CheckedMalloc(sizeof(I64)); + out->i64->data = node->literalArgs[0]->i64->data - node->literalArgs[1]->i64->data; + return out; break; case DIV: - outi64->data = node->literalArgs[0]->i64->data / node->literalArgs[1]->i64->data; - return outi64; + out->i64 = CheckedMalloc(sizeof(I64)); + out->i64->data = node->literalArgs[0]->i64->data / node->literalArgs[1]->i64->data; + return out; break; case MUL: - outi64->data = node->literalArgs[0]->i64->data * node->literalArgs[1]->i64->data; - return outi64; + out->i64 = CheckedMalloc(sizeof(I64)); + out->i64->data = node->literalArgs[0]->i64->data * node->literalArgs[1]->i64->data; + return out; break; case WRITE: - fputs(node->literalArgs[0], stdout); + for (int i = 0; i < node->literalArgs[0]->arr->len; i++) + fputc(node->literalArgs[0]->arr->arr[i].ch->data, stdout); break; - + case EXIT: - int returnValue = node->literalArgs[0]->i64->data; - CheckedFreeALL(); + int returnValue = (int)node->literalArgs[0]->i64->data; exit(returnValue); break; } diff --git a/comp/lucas-standen-NEA/code/execution/exec b/comp/lucas-standen-NEA/code/execution/exec Binary files differindex e1cfbf1..744535a 100755 --- a/comp/lucas-standen-NEA/code/execution/exec +++ 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 adb6f25..8775526 100644 --- a/comp/lucas-standen-NEA/code/execution/exec.c +++ b/comp/lucas-standen-NEA/code/execution/exec.c @@ -2,17 +2,15 @@ #include <stdlib.h> #include "./builtin.h" - #include "../global/util.h" - #include "../tokenizer/tokenizer.h" int main(){ - char *sample = "(exit (+ 1 1))"; + char *sample = "(write ['h','e','l','l','o','\n'])"; ast_node *root = tokenize(sample); doCall(root); - CheckedFreeALL(); - + free(root); + return 0; } diff --git a/comp/lucas-standen-NEA/code/global/types.h b/comp/lucas-standen-NEA/code/global/types.h index 0d27996..2c3c4be 100644 --- a/comp/lucas-standen-NEA/code/global/types.h +++ b/comp/lucas-standen-NEA/code/global/types.h @@ -26,14 +26,21 @@ typedef struct Float { float data; } Float; -typedef union litteral { +typedef struct Arr { + union literal *arr; + long len; +} Arr; + +typedef union literal { I32 *i32; I64 *i64; U32 *u32; I64 *u64; Char *ch; Float *fl; -} litteral; + + Arr *arr; +} literal; // built in functions typedef enum builtInFuncs { @@ -82,7 +89,7 @@ typedef struct ast_node ast_node; typedef struct ast_node { functionToken *func; // if it's not builtin then use this - litteral **literalArgs; // the args of the node, this will be an array of litteral values + literal **literalArgs; // the args of the node, this will be an array of literal 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 index 3b1f97c..d1f2b84 100644 --- a/comp/lucas-standen-NEA/code/global/util.c +++ b/comp/lucas-standen-NEA/code/global/util.c @@ -11,15 +11,10 @@ // functions for user void Die(); // brings down the program -void *CheckedMalloc(long size); // malloc checked with autofree +void *CheckedMalloc(long size); // CheckedMalloc 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 +literal *giveType(char *tok); -#define MAXPTRS 30000 // maximum allocs done by user - -void *MEMptrs[MAXPTRS] = { NULL }; -size_t currentPtr = 0; void Die(){ perror("zpy parser"); @@ -30,12 +25,6 @@ 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; } @@ -43,39 +32,9 @@ 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]); - } - } -} - I64 *isNum(char *str){ for (int i = 0; i < strlen(str); i++){ if (isdigit(str[i]) == 0 && str[i] != '-'){ @@ -97,11 +56,67 @@ Float *isFloat(char *str){ out->data = strtod(str, NULL); return out; } + Char *isChar(char *str){ - if (strlen(str) == 1){ - Char *out = malloc(sizeof(Char)); - out->data = str[0]; - return out; + if (str[0] == '\'' && str[strlen(str)-1] == '\''){ + str++; + str[strlen(str)-1] = '\0'; + if (strlen(str) == 1){ + Char *out = CheckedMalloc(sizeof(Char)); + out->data = str[0]; + return out; + } + else return NULL; + } +} + +Arr *isArr(char *str){ + char *strbak = CheckedMalloc(strlen(str)); + memcpy(strbak, str, strlen(str)+1); + if (strbak[0] == '[' && strbak[strlen(strbak)-1] == ']'){ + Arr *arr = CheckedMalloc(sizeof(Arr)); + arr->arr = CheckedMalloc(sizeof(literal)); + strbak++; + strbak[strlen(strbak)-1] = '\0'; + char *tok; + int length = 0; + size_t cCount = 1; + tok = strtok(strbak, ","); + while (tok != NULL){ + cCount++; + arr = CheckedRealloc(arr, sizeof(Arr) * cCount); + arr->arr[cCount - 2] = *giveType(tok); + tok = strtok(NULL, ","); + length++; + } + arr->len = length; + free(strbak - 1); + return arr; + } else { + free(strbak); + return NULL; } - else return NULL; +} + +literal *giveType(char *tok){ + literal *out = CheckedMalloc(sizeof(literal)); + Arr *arr = isArr(tok); + I64 *i64 = isNum(tok); + Float *fl = isFloat(tok); + Char *ch = isChar(tok); + + if (arr != NULL){ + out->arr = arr; + } else if (i64 != NULL){ + out->i64 = i64; + } else if (fl != NULL){ + out->fl = fl; + } else if (ch != NULL){ + out->ch = ch; + } else { + fprintf(stderr, "data %s could not be typed\n", tok); + errno = 22; + Die(); + } + return out; } diff --git a/comp/lucas-standen-NEA/code/global/util.h b/comp/lucas-standen-NEA/code/global/util.h index b10a1f4..bf2a46c 100644 --- a/comp/lucas-standen-NEA/code/global/util.h +++ b/comp/lucas-standen-NEA/code/global/util.h @@ -4,16 +4,18 @@ #include <stdbool.h> #include <errno.h> #include <error.h> +#include <ctype.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 -//checking functions, return NULL if the data is not of their type, else return the value. +literal *giveType(char *tok); // gives a string a type + +// gives strings types I64 *isNum(char *str); Float *isFloat(char *str); Char *isChar(char *str); +literal *isArr(char *str); diff --git a/comp/lucas-standen-NEA/code/tokenizer/Makefile b/comp/lucas-standen-NEA/code/tokenizer/Makefile index 73e4033..38f92bf 100644 --- a/comp/lucas-standen-NEA/code/tokenizer/Makefile +++ b/comp/lucas-standen-NEA/code/tokenizer/Makefile @@ -1,9 +1,9 @@ all: tokenizer parser $(info done tokenizer!) tokenizer: tokenizer.c - cc tokenizer.c -c -o tokenizer.o + cc tokenizer.c -c -o tokenizer.o -ggdb parser: parser.c - cc parser.c -c -o parser.o + cc parser.c -c -o parser.o -ggdb clean: rm -rf *.o diff --git a/comp/lucas-standen-NEA/code/tokenizer/parser.c b/comp/lucas-standen-NEA/code/tokenizer/parser.c index 6ce8c5b..4bb9c3a 100644 --- a/comp/lucas-standen-NEA/code/tokenizer/parser.c +++ b/comp/lucas-standen-NEA/code/tokenizer/parser.c @@ -4,10 +4,10 @@ #include "../global/types.h" #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); // reads the file into a single var +char *parse(char *fileName); // general parser function -char *ReadFile(char *filename){ +char *readFile(char *filename){ FILE *f = fopen(filename, "r"); if (f == NULL) Die(); @@ -16,7 +16,7 @@ char *ReadFile(char *filename){ size_t len = ftell(f); rewind(f); - char *out = malloc(len+1); + char *out = CheckedMalloc(len+1); char c; for (int i = 0; i < len; i++){ @@ -32,6 +32,13 @@ char *ReadFile(char *filename){ return out; } -char *Parser(char *fileName){ - return ReadFile(fileName); +char *preProcess(char *contents){ + char *out = CheckedMalloc(strlen(contents)+1); + for (char c = contents[0]; c != '\0'; c = (contents += 1)[0]){ + printf("%c", c); + } +} + +char *parser(char *fileName){ + return readFile(fileName); } diff --git a/comp/lucas-standen-NEA/code/tokenizer/parser.h b/comp/lucas-standen-NEA/code/tokenizer/parser.h index e4f69a8..f90b777 100644 --- a/comp/lucas-standen-NEA/code/tokenizer/parser.h +++ b/comp/lucas-standen-NEA/code/tokenizer/parser.h @@ -1,3 +1,3 @@ char *Parse(char *fileName); // general parser function - char *ReadFile(char *fileName); // reads the file into a single var +char *preProcess(char *contents); diff --git a/comp/lucas-standen-NEA/code/tokenizer/test b/comp/lucas-standen-NEA/code/tokenizer/test Binary files differnew file mode 100755 index 0000000..797a447 --- /dev/null +++ b/comp/lucas-standen-NEA/code/tokenizer/test diff --git a/comp/lucas-standen-NEA/code/tokenizer/test.c b/comp/lucas-standen-NEA/code/tokenizer/test.c new file mode 100644 index 0000000..49d6160 --- /dev/null +++ b/comp/lucas-standen-NEA/code/tokenizer/test.c @@ -0,0 +1,6 @@ +#include "parser.h" + +int main(){ + char *sample = "(write \"hello\")"; + preProcess(sample); +} diff --git a/comp/lucas-standen-NEA/code/tokenizer/tokenizer.c b/comp/lucas-standen-NEA/code/tokenizer/tokenizer.c index 0c30d36..3a59d88 100644 --- a/comp/lucas-standen-NEA/code/tokenizer/tokenizer.c +++ b/comp/lucas-standen-NEA/code/tokenizer/tokenizer.c @@ -141,14 +141,8 @@ ast_node *tokenize(char *input){ } if (tok[0] != '(' && tok[strlen(tok)-1] != ')' && depth == 0){ if (node->args[argCount] == NULL){ - node->literalArgs[argCount] = CheckedMalloc(sizeof(litteral)); - if (isNum(tok) != NULL){ - node->literalArgs[argCount]->i64 = isNum(tok); - } else if (isFloat(tok) != NULL){ - node->literalArgs[argCount]->fl = isFloat(tok); - } else if (isChar(tok) != NULL){ - node->literalArgs[argCount]->ch = isChar(tok); - } + node->literalArgs[argCount] = giveType(tok); + } argCount++; } @@ -158,7 +152,7 @@ ast_node *tokenize(char *input){ tok = strtok(NULL, " "); } while (tok != NULL); - CheckedFree(exp); + free(exp); return node; } diff --git a/comp/lucas-standen-NEA/code/tokenizer/tokenizer.h b/comp/lucas-standen-NEA/code/tokenizer/tokenizer.h index 3cfaaf2..9e07921 100644 --- a/comp/lucas-standen-NEA/code/tokenizer/tokenizer.h +++ b/comp/lucas-standen-NEA/code/tokenizer/tokenizer.h @@ -9,4 +9,4 @@ int getBuiltIn(char *func, ast_node *node); // checks if a function is built in 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 - +void freeAst(ast_node *head); // frees most of the ast; |