diff options
author | standenboy <standenboy@seacrossedlovers.xyz> | 2024-06-15 17:06:28 +0100 |
---|---|---|
committer | standenboy <standenboy@seacrossedlovers.xyz> | 2024-06-15 17:06:28 +0100 |
commit | 42047fea26b14edc67b394db18ce7edb0c6399f8 (patch) | |
tree | e66ca6451ca842eae0d263aabc82435fb7987e92 /comp/lucas-standen-NEA/code | |
parent | f6d4ab1521fe2427b2a43f92c4ecf163bbd889dc (diff) | |
parent | a944e593ad7fe54d007ad44d5bf9dc7ad2ddf103 (diff) |
added so much code
Diffstat (limited to 'comp/lucas-standen-NEA/code')
-rwxr-xr-x | comp/lucas-standen-NEA/code/tokenizer/tokenizer | bin | 28928 -> 42264 bytes | |||
-rw-r--r-- | comp/lucas-standen-NEA/code/tokenizer/tokenizer.c | 99 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/code/tokenizer/types.h | 5 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/code/tokenizer/util.c | 38 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/code/tokenizer/util.h | 7 |
5 files changed, 109 insertions, 40 deletions
diff --git a/comp/lucas-standen-NEA/code/tokenizer/tokenizer b/comp/lucas-standen-NEA/code/tokenizer/tokenizer Binary files differindex ab76521..2d2f1c0 100755 --- a/comp/lucas-standen-NEA/code/tokenizer/tokenizer +++ b/comp/lucas-standen-NEA/code/tokenizer/tokenizer diff --git a/comp/lucas-standen-NEA/code/tokenizer/tokenizer.c b/comp/lucas-standen-NEA/code/tokenizer/tokenizer.c index f94b640..080951b 100644 --- a/comp/lucas-standen-NEA/code/tokenizer/tokenizer.c +++ b/comp/lucas-standen-NEA/code/tokenizer/tokenizer.c @@ -4,9 +4,11 @@ #include "types.h" #include "util.h" -void getBuiltIn(char *func, ast_node *node){ +#define MAXARGS 8 + +int getBuiltIn(char *func, ast_node *node){ if (strcmp(func, "defun") == 0){ - node->func->builtInFunc = DEFUN; + node->func->builtInFunc= DEFUN; }else if (strcmp(func, "let") == 0){ node->func->builtInFunc = LET; }else if (strcmp(func, "set") == 0){ @@ -51,57 +53,72 @@ void getBuiltIn(char *func, ast_node *node){ node->func->builtInFunc = EXIT; }else if (strcmp(func, "return") == 0){ node->func->builtInFunc = RETURN; + }else { + node->func->builtInFunc = NIL; + return -1; } - else { - node->func->builtInFunc = -1; - } + return 0; } ll_t *getUserDefinedFunction(char *function); void expressFunction(char *function, ast_node *node){ - if ((node->func->builtInFunc = getBuiltIn(function)) == -1){ - node->func->func = getUserDefinedFunction(function); + 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); + } + } + +} + ast_node *tokenize(char *input){ - ast_node *node; + ast_node *node, *child; char *exp, *function, **args; - size_t i, j; - int depth; - - for (int i = 0; i < strlen(input); i++){ - if (input[i] == '('){ - depth = 1; - j = i; - exp = CheckedMalloc(strlen(input)); - while (depth != 0){ - if (input[j] == '('){ - depth++; - } else if (input[j] == ')'){ - depth--; - } - exp[j - i] = input[j+1]; - j++; - if (input[j] == '\0'){ - fprintf(stderr, "error brace not closed"); - exit(1); - } + size_t i = 0, argCount = -1; + int depth = 0; + + node = CheckedMalloc(sizeof(ast_node)); + node->args = CheckedMalloc(sizeof(ast_node) * MAXARGS); + node->literalArgs = CheckedMalloc(sizeof(void *) * MAXARGS); + + if (input[i] == '('){ + depth = 1; + i++; + exp = CheckedMalloc(strlen(input)); + while (depth != 0){ + if (input[i] == ' ') argCount++; + if (input[i] == '('){ + child = tokenize(&input[i]); + node->args[argCount] = child; + depth++; + } else if (input[i] == ')'){ + depth--; + } + exp[i - 1] = input[i]; + if (input[i] == '\0'){ + fprintf(stderr, "error brace not closed\n"); + exit(1); } - j -= 2; - exp[j] = '\0'; - printf("%s\n", exp); - }else if (input[i] == '"'){ i++; - while (input[i] != '"') i++; } + exp[i-2] = '\0'; + exp = CheckedRealloc(exp, strlen(exp) + 1); + printf("%s\n", exp); + }else if (input[i] == '"'){ + i++; + while (input[i] != '"') i++; } - node = CheckedMalloc(sizeof(ast_node)); i = 0; function = CheckedMalloc(strlen(exp)); @@ -116,13 +133,19 @@ ast_node *tokenize(char *input){ expressFunction(function, node); - free(function); + i++; + args = Split(&input[i], ' '); + // need a length + expressArgs(args, node /* length */ ); + free(exp); - return NULL; + return node; } int main(){ - char sample[] = "(+ \"hello(\" 1)"; - tokenize(sample); + char sample[] = "(+ (- 2 2) 1)"; + ast_node *root = tokenize(sample); + printf("%d", root->args[0]->func->builtInFunc); + free(root); } diff --git a/comp/lucas-standen-NEA/code/tokenizer/types.h b/comp/lucas-standen-NEA/code/tokenizer/types.h index 82eb3df..8c79bd9 100644 --- a/comp/lucas-standen-NEA/code/tokenizer/types.h +++ b/comp/lucas-standen-NEA/code/tokenizer/types.h @@ -24,6 +24,7 @@ typedef uint64_t u64; // built in functions typedef enum builtInFuncs { + // general DEFUN = 0, LET = 1, SET = 2, @@ -48,10 +49,12 @@ typedef enum builtInFuncs { GTEQ = 18, LTEQ = 19, + // misc CAST = 20, TYPEOF = 21, EXIT = 22, RETURN = 23, + NIL = -1, } builtInFuncs; // function type @@ -63,8 +66,6 @@ typedef struct functionToken { builtInFuncs builtInFunc; // a built in functions } functionToken; -// built in functions - typedef struct ast_node ast_node; typedef struct ast_node { diff --git a/comp/lucas-standen-NEA/code/tokenizer/util.c b/comp/lucas-standen-NEA/code/tokenizer/util.c index de5b6b2..46deba8 100644 --- a/comp/lucas-standen-NEA/code/tokenizer/util.c +++ b/comp/lucas-standen-NEA/code/tokenizer/util.c @@ -1,4 +1,5 @@ #include <stdio.h> +#include <string.h> #include <stdlib.h> #include <errno.h> #include <error.h> @@ -6,6 +7,7 @@ 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"); @@ -25,3 +27,39 @@ void *CheckedRealloc(void *orig, long size){ 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 index cbcbdfa..c25ebec 100644 --- a/comp/lucas-standen-NEA/code/tokenizer/util.h +++ b/comp/lucas-standen-NEA/code/tokenizer/util.h @@ -1,3 +1,10 @@ +#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 |