summaryrefslogtreecommitdiff
path: root/src/stack.c
diff options
context:
space:
mode:
authorTyler Murphy <tylerm@tylerm.dev>2023-04-14 22:55:43 -0400
committerTyler Murphy <tylerm@tylerm.dev>2023-04-14 22:55:43 -0400
commitcb76bcf7d2358ad04281d123141acfa175e31841 (patch)
tree98d770bbc21b9767ae2b7b5186cfe9c445f40236 /src/stack.c
parentthing (diff)
downloadbrainfucked-cb76bcf7d2358ad04281d123141acfa175e31841.tar.gz
brainfucked-cb76bcf7d2358ad04281d123141acfa175e31841.tar.bz2
brainfucked-cb76bcf7d2358ad04281d123141acfa175e31841.zip
i made things faster
Diffstat (limited to '')
-rw-r--r--src/stack.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/stack.c b/src/stack.c
new file mode 100644
index 0000000..3491581
--- /dev/null
+++ b/src/stack.c
@@ -0,0 +1,47 @@
+#include "types.h"
+
+#include <stdlib.h>
+
+void stack_init(Stack* stack, uint32_t len) {
+ stack->index = 0;
+ stack->len = len;
+ stack->data = malloc(stack->len * sizeof(union stored));
+}
+
+void stack_push(Stack* stack, void* value) {
+ if (stack->index == stack->len) {
+ stack->len *= 2;
+ stack->data = realloc(stack->data, stack->len * sizeof(union stored));
+ }
+ stack->data[stack->index].p = value;
+ stack->index++;
+}
+
+void* stack_pop(Stack* stack) {
+ if (stack->index == 0) {
+ return NULL;
+ }
+ stack->index--;
+ return stack->data[stack->index].p;
+}
+
+void stack_pushi(Stack* stack, uint32_t value) {
+ if (stack->index == stack->len) {
+ stack->len *= 2;
+ stack->data = realloc(stack->data, stack->len * sizeof(union stored));
+ }
+ stack->data[stack->index].i = value;
+ stack->index++;
+}
+
+uint32_t stack_popi(Stack* stack) {
+ if (stack->index == 0) {
+ return 0;
+ }
+ stack->index--;
+ return stack->data[stack->index].i;
+}
+
+void stack_free(Stack* stack) {
+ free(stack->data);
+}