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/Makefile4
-rw-r--r--comp/lucas-standen-NEA/code/global/types.h64
-rw-r--r--comp/lucas-standen-NEA/code/global/util.c73
-rw-r--r--comp/lucas-standen-NEA/code/global/util.h13
4 files changed, 154 insertions, 0 deletions
diff --git a/comp/lucas-standen-NEA/code/global/Makefile b/comp/lucas-standen-NEA/code/global/Makefile
new file mode 100644
index 0000000..dbff2b0
--- /dev/null
+++ b/comp/lucas-standen-NEA/code/global/Makefile
@@ -0,0 +1,4 @@
+all: util
+ $(info done!)
+util: util.c
+ cc util.c -c -o util.o
diff --git a/comp/lucas-standen-NEA/code/global/types.h b/comp/lucas-standen-NEA/code/global/types.h
new file mode 100644
index 0000000..e73d55d
--- /dev/null
+++ b/comp/lucas-standen-NEA/code/global/types.h
@@ -0,0 +1,64 @@
+#include <stdint.h>
+#include "../ads/ll/ll.h"
+
+// int types
+typedef int32_t i32;
+typedef int64_t i64;
+
+// uint types
+typedef uint32_t u32;
+typedef uint64_t u64;
+
+// char and float types are still called char and float so no typedef needed
+
+// built in functions
+typedef enum builtInFuncs {
+ // general
+ DEFUN = 0,
+ LET = 1,
+ SET = 2,
+ IF = 3,
+ ELIF = 4,
+ ELSE = 5,
+ FOR = 6,
+ WHILE = 7,
+ SYMBOL = 8,
+
+ // arithmetic
+ ADD = 10,
+ SUB = 11,
+ MUL = 12,
+ DIV = 13,
+
+ // comparison
+ EQ = 14,
+ NEQ = 15,
+ GT = 16,
+ LT = 17,
+ GTEQ = 18,
+ LTEQ = 19,
+
+ // misc
+ CAST = 20,
+ TYPEOF = 21,
+ EXIT = 22,
+ RETURN = 23,
+ WRITE = 24,
+ NIL = -1,
+} builtInFuncs;
+
+// function type
+typedef struct functionToken {
+ int id; // a function id to avoid strings
+ char *name; // the code for the function
+ builtInFuncs builtInFunc; // a built in functions
+} functionToken;
+
+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
+ 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
new file mode 100644
index 0000000..736619b
--- /dev/null
+++ b/comp/lucas-standen-NEA/code/global/util.c
@@ -0,0 +1,73 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#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
+
+#define MAXPTRS 30000 // maximum allocs done by user
+
+void *MEMptrs[MAXPTRS] = { NULL };
+size_t currentPtr = 0;
+
+void Die(){
+ perror("zpy parser");
+ exit(errno);
+}
+
+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;
+}
+
+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]);
+ }
+ }
+}
diff --git a/comp/lucas-standen-NEA/code/global/util.h b/comp/lucas-standen-NEA/code/global/util.h
new file mode 100644
index 0000000..8feed88
--- /dev/null
+++ b/comp/lucas-standen-NEA/code/global/util.h
@@ -0,0 +1,13 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#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