diff options
Diffstat (limited to '')
-rw-r--r-- | src/stack.c | 47 |
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); +} |