diff options
Diffstat (limited to 'comp/lucas-standen-NEA/code/global')
-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 |
4 files changed, 72 insertions, 8 deletions
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); |