diff options
author | thing1 <thing1@seacrossedlovers.xyz> | 2024-07-02 11:51:10 +0100 |
---|---|---|
committer | thing1 <thing1@seacrossedlovers.xyz> | 2024-07-02 11:51:10 +0100 |
commit | 478eed4ef9ece1b86b2fed7bfe57d62180b12c92 (patch) | |
tree | b541dda71f7f3deb5d94ece604b86fa18a1527e9 | |
parent | d933ce70bd2c497e4af26483abafebfce436986e (diff) |
started type system
-rw-r--r-- | comp/lucas-standen-NEA/code/TODO | 11 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/code/execution/Makefile | 2 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/code/execution/builtin.c | 20 | ||||
-rwxr-xr-x | comp/lucas-standen-NEA/code/execution/exec | bin | 30040 -> 33904 bytes | |||
-rw-r--r-- | comp/lucas-standen-NEA/code/execution/exec.c | 8 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/code/global/Makefile | 2 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/code/global/types.h | 38 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/code/global/util.c | 34 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/code/global/util.h | 6 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/code/tokenizer/parser.c | 1 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/code/tokenizer/tokenizer.c | 26 |
11 files changed, 95 insertions, 53 deletions
diff --git a/comp/lucas-standen-NEA/code/TODO b/comp/lucas-standen-NEA/code/TODO index 5d3078a..d15d029 100644 --- a/comp/lucas-standen-NEA/code/TODO +++ b/comp/lucas-standen-NEA/code/TODO @@ -1,10 +1 @@ -MAKE LITERAL ARGS A UNION -something like this -union { - i32 i32Value; - u32 u32Value; - 164 i32Value; - u64 u32Value; - char charValue; - float floatValue; -} +FIX THE SEGFAULT ON ADDING / SUBTRACTING diff --git a/comp/lucas-standen-NEA/code/execution/Makefile b/comp/lucas-standen-NEA/code/execution/Makefile index 405f9de..00c18b6 100644 --- a/comp/lucas-standen-NEA/code/execution/Makefile +++ b/comp/lucas-standen-NEA/code/execution/Makefile @@ -4,3 +4,5 @@ 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 +clean: + rm -rf *.o exec diff --git a/comp/lucas-standen-NEA/code/execution/builtin.c b/comp/lucas-standen-NEA/code/execution/builtin.c index df3ca03..99aca8c 100644 --- a/comp/lucas-standen-NEA/code/execution/builtin.c +++ b/comp/lucas-standen-NEA/code/execution/builtin.c @@ -13,23 +13,23 @@ void *doCall(ast_node *node){ } } - char *str = CheckedMalloc(20); + I64 *outi64 = CheckedMalloc(sizeof(I64)); switch (id){ case ADD: - snprintf(str, 20, "%d", atoi(node->literalArgs[0]) + atoi(node->literalArgs[1])); - return str; + outi64->data = node->literalArgs[0]->i64->data + node->literalArgs[1]->i64->data; + return outi64; break; case SUB: - snprintf(str, 20, "%d", atoi(node->literalArgs[0]) - atoi(node->literalArgs[1])); - return str; + outi64->data = node->literalArgs[0]->i64->data - node->literalArgs[1]->i64->data; + return outi64; break; case DIV: - snprintf(str, 20, "%d", atoi(node->literalArgs[0]) / atoi(node->literalArgs[1])); - return str; + outi64->data = node->literalArgs[0]->i64->data / node->literalArgs[1]->i64->data; + return outi64; break; case MUL: - snprintf(str, 20, "%d", atoi(node->literalArgs[0]) * atoi(node->literalArgs[1])); - return str; + outi64->data = node->literalArgs[0]->i64->data * node->literalArgs[1]->i64->data; + return outi64; break; case WRITE: @@ -37,7 +37,7 @@ void *doCall(ast_node *node){ break; case EXIT: - int returnValue = atoi(node->literalArgs[0]); + int returnValue = node->literalArgs[0]->i64->data; CheckedFreeALL(); exit(returnValue); break; diff --git a/comp/lucas-standen-NEA/code/execution/exec b/comp/lucas-standen-NEA/code/execution/exec Binary files differindex ca6eb33..e1cfbf1 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 fb85d3f..adb6f25 100644 --- a/comp/lucas-standen-NEA/code/execution/exec.c +++ b/comp/lucas-standen-NEA/code/execution/exec.c @@ -8,15 +8,9 @@ #include "../tokenizer/tokenizer.h" int main(){ - char *sample = "(write (* 2 5))"; + char *sample = "(exit (+ 1 1))"; ast_node *root = tokenize(sample); doCall(root); - sample = "(write \n)"; - root = tokenize(sample); - doCall(root); - sample = "(exit 0)"; - root = tokenize(sample); - doCall(root); CheckedFreeALL(); diff --git a/comp/lucas-standen-NEA/code/global/Makefile b/comp/lucas-standen-NEA/code/global/Makefile index dbff2b0..4f9828e 100644 --- a/comp/lucas-standen-NEA/code/global/Makefile +++ b/comp/lucas-standen-NEA/code/global/Makefile @@ -1,4 +1,4 @@ all: util $(info done!) util: util.c - cc util.c -c -o util.o + cc util.c -c -o util.o -ggdb diff --git a/comp/lucas-standen-NEA/code/global/types.h b/comp/lucas-standen-NEA/code/global/types.h index e73d55d..0d27996 100644 --- a/comp/lucas-standen-NEA/code/global/types.h +++ b/comp/lucas-standen-NEA/code/global/types.h @@ -1,15 +1,39 @@ #include <stdint.h> -#include "../ads/ll/ll.h" // int types -typedef int32_t i32; -typedef int64_t i64; +typedef struct I32 { + int32_t data; +} I32; + +typedef struct I64 { + int64_t data; +} I64; // uint types -typedef uint32_t u32; -typedef uint64_t u64; +typedef struct U32 { + uint32_t data; +} U32; + +typedef struct U64 { + uint64_t data; +} U64; + +typedef struct Char { + char data; +} Char; + +typedef struct Float { + float data; +} Float; -// char and float types are still called char and float so no typedef needed +typedef union litteral { + I32 *i32; + I64 *i64; + U32 *u32; + I64 *u64; + Char *ch; + Float *fl; +} litteral; // built in functions typedef enum builtInFuncs { @@ -58,7 +82,7 @@ typedef struct ast_node ast_node; typedef struct ast_node { functionToken *func; // if it's not builtin then use this - char **literalArgs; // the args of the node, this will be an array of litteral values + litteral **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 index 736619b..3b1f97c 100644 --- a/comp/lucas-standen-NEA/code/global/util.c +++ b/comp/lucas-standen-NEA/code/global/util.c @@ -4,6 +4,10 @@ #include <stdbool.h> #include <errno.h> #include <error.h> +#include <ctype.h> + +#include "types.h" + // functions for user void Die(); // brings down the program @@ -71,3 +75,33 @@ void CheckedFreeALL(){ } } } + +I64 *isNum(char *str){ + for (int i = 0; i < strlen(str); i++){ + if (isdigit(str[i]) == 0 && str[i] != '-'){ + return NULL; + } + } + I64 *out = CheckedMalloc(sizeof(I64)); + out->data = strtol(str, NULL, 10); + return out; +} + +Float *isFloat(char *str){ + for (int i = 0; i < strlen(str); i++){ + if (isdigit(str[i]) == 0 && str[i] != '-' && str[i] != '.'){ + return NULL; + } + } + Float *out = CheckedMalloc(sizeof(Float)); + 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; + } + else return NULL; +} diff --git a/comp/lucas-standen-NEA/code/global/util.h b/comp/lucas-standen-NEA/code/global/util.h index 8feed88..b10a1f4 100644 --- a/comp/lucas-standen-NEA/code/global/util.h +++ b/comp/lucas-standen-NEA/code/global/util.h @@ -5,9 +5,15 @@ #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 + +//checking functions, return NULL if the data is not of their type, else return the value. +I64 *isNum(char *str); +Float *isFloat(char *str); +Char *isChar(char *str); diff --git a/comp/lucas-standen-NEA/code/tokenizer/parser.c b/comp/lucas-standen-NEA/code/tokenizer/parser.c index 69ec458..6ce8c5b 100644 --- a/comp/lucas-standen-NEA/code/tokenizer/parser.c +++ b/comp/lucas-standen-NEA/code/tokenizer/parser.c @@ -1,6 +1,7 @@ #include <stdio.h> #include <stdlib.h> +#include "../global/types.h" #include "../global/util.h" char *ReadFile(char *fileName); // reads the file into a single var diff --git a/comp/lucas-standen-NEA/code/tokenizer/tokenizer.c b/comp/lucas-standen-NEA/code/tokenizer/tokenizer.c index a76760b..0c30d36 100644 --- a/comp/lucas-standen-NEA/code/tokenizer/tokenizer.c +++ b/comp/lucas-standen-NEA/code/tokenizer/tokenizer.c @@ -141,8 +141,14 @@ ast_node *tokenize(char *input){ } 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; + 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); + } } argCount++; } @@ -152,22 +158,6 @@ ast_node *tokenize(char *input){ 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++; - } - } - CheckedFree(exp); return node; |