diff options
Diffstat (limited to 'comp/lucas-standen-NEA/code/global')
-rw-r--r-- | comp/lucas-standen-NEA/code/global/types.h | 18 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/code/global/util.c | 28 | ||||
-rw-r--r-- | comp/lucas-standen-NEA/code/global/util.h | 1 |
3 files changed, 47 insertions, 0 deletions
diff --git a/comp/lucas-standen-NEA/code/global/types.h b/comp/lucas-standen-NEA/code/global/types.h index 2c3c4be..67c4a58 100644 --- a/comp/lucas-standen-NEA/code/global/types.h +++ b/comp/lucas-standen-NEA/code/global/types.h @@ -1,5 +1,17 @@ #include <stdint.h> +typedef enum types { + TI32 = 0, + TI64 = 1, + TU32 = 2, + TU64 = 3, + + TChar = 4, + Tfloat = 5, + TArr = 6, + TVDef = 7, +} types; + // int types typedef struct I32 { int32_t data; @@ -31,6 +43,11 @@ typedef struct Arr { long len; } Arr; +typedef struct Vdef { + char *id; + types type; +} Vdef; + typedef union literal { I32 *i32; I64 *i64; @@ -40,6 +57,7 @@ typedef union literal { Float *fl; Arr *arr; + Vdef *vdef; } literal; // built in functions diff --git a/comp/lucas-standen-NEA/code/global/util.c b/comp/lucas-standen-NEA/code/global/util.c index d1f2b84..a29bbbc 100644 --- a/comp/lucas-standen-NEA/code/global/util.c +++ b/comp/lucas-standen-NEA/code/global/util.c @@ -68,6 +68,7 @@ Char *isChar(char *str){ } else return NULL; } + return NULL; } Arr *isArr(char *str){ @@ -98,12 +99,37 @@ Arr *isArr(char *str){ } } +Vdef *isVdef(char *str){ + if (strchr(str, ':') != NULL){ + Vdef *out = CheckedMalloc(sizeof(Vdef)); + char *type; + type = strtok(str, ":"); + if (strcmp(type, "i64") == 0) out->type = TI64; + else if (strcmp(type, "i32") == 0) out->type = TI32; + else if (strcmp(type, "u64") == 0) out->type = TU64; + else if (strcmp(type, "u32") == 0) out->type = TU32; + + else if (strcmp(type, "char") == 0) out->type = TChar; + else if (strcmp(type, "float") == 0) out->type = Tfloat; + // do something with arrays here + else { + fprintf(stderr, "%s is not a valid data type\n", type); + Die(); + } + + out->id = strtok(NULL, ":"); + return out; + } + 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); + Vdef *vdef = isVdef(tok); if (arr != NULL){ out->arr = arr; @@ -113,6 +139,8 @@ literal *giveType(char *tok){ out->fl = fl; } else if (ch != NULL){ out->ch = ch; + }else if (vdef != NULL){ + out->vdef = vdef; } else { fprintf(stderr, "data %s could not be typed\n", tok); errno = 22; diff --git a/comp/lucas-standen-NEA/code/global/util.h b/comp/lucas-standen-NEA/code/global/util.h index bf2a46c..dc59460 100644 --- a/comp/lucas-standen-NEA/code/global/util.h +++ b/comp/lucas-standen-NEA/code/global/util.h @@ -19,3 +19,4 @@ I64 *isNum(char *str); Float *isFloat(char *str); Char *isChar(char *str); literal *isArr(char *str); +Vdef *isVdef(char *str); |