summaryrefslogtreecommitdiff
path: root/comp/lucas-standen-NEA/code/global
diff options
context:
space:
mode:
Diffstat (limited to 'comp/lucas-standen-NEA/code/global')
-rw-r--r--comp/lucas-standen-NEA/code/global/types.h13
-rw-r--r--comp/lucas-standen-NEA/code/global/util.c111
-rw-r--r--comp/lucas-standen-NEA/code/global/util.h8
3 files changed, 78 insertions, 54 deletions
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);