#include "toy_stack.h" #include "toy_console_colors.h" #include #include //a good chunk of space - 'count' actually tracks the number of values #define MIN_CAPACITY 64 Toy_Stack* Toy_allocateStack() { Toy_Stack* stack = malloc(MIN_CAPACITY * sizeof(Toy_Value) + sizeof(Toy_Stack)); if (stack == NULL) { fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to allocate a 'Toy_Stack' of %d capacity (%d space in memory)\n" TOY_CC_RESET, MIN_CAPACITY, (int)(MIN_CAPACITY * sizeof(Toy_Value) + sizeof(Toy_Stack))); exit(1); } stack->capacity = MIN_CAPACITY; stack->count = 0; return stack; } void Toy_freeStack(Toy_Stack* stack) { //TODO: slip in a call to free the complex values here if (stack != NULL) { free(stack); } } void Toy_pushStack(Toy_Stack** stackHandle, Toy_Value value) { //don't go overboard - limit to 1mb of capacity used if ((*stackHandle)->count >= 1024 * 1024 / sizeof(Toy_Value)) { fprintf(stderr, TOY_CC_ERROR "ERROR: Stack overflow\n" TOY_CC_RESET); exit(-1); } //expand the capacity if needed if ((*stackHandle)->count + 1 > (*stackHandle)->capacity) { while ((*stackHandle)->count + 1 > (*stackHandle)->capacity) { (*stackHandle)->capacity = (*stackHandle)->capacity < MIN_CAPACITY ? MIN_CAPACITY : (*stackHandle)->capacity * 2; } unsigned int newCapacity = (*stackHandle)->capacity; (*stackHandle) = realloc((*stackHandle), newCapacity * sizeof(Toy_Value) + sizeof(Toy_Stack)); if ((*stackHandle) == NULL) { fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to reallocate a 'Toy_Stack' of %d capacity (%d space in memory)\n" TOY_CC_RESET, (int)newCapacity, (int)(newCapacity * sizeof(Toy_Value) + sizeof(Toy_Stack))); exit(1); } } //Note: "pointer arithmetic in C/C++ is type-relative" ((Toy_Value*)((*stackHandle) + 1))[(*stackHandle)->count++] = value; } Toy_Value Toy_peekStack(Toy_Stack** stackHandle) { if ((*stackHandle)->count == 0) { fprintf(stderr, TOY_CC_ERROR "ERROR: Stack underflow\n" TOY_CC_RESET); exit(-1); } return ((Toy_Value*)((*stackHandle) + 1))[(*stackHandle)->count - 1]; } Toy_Value Toy_popStack(Toy_Stack** stackHandle) { if ((*stackHandle)->count == 0) { fprintf(stderr, TOY_CC_ERROR "ERROR: Stack underflow\n" TOY_CC_RESET); exit(-1); } //shrink if possible if ((*stackHandle)->count > MIN_CAPACITY && (*stackHandle)->count < (*stackHandle)->capacity / 4) { (*stackHandle)->capacity /= 2; unsigned int newCapacity = (*stackHandle)->capacity; (*stackHandle) = realloc((*stackHandle), (*stackHandle)->capacity * sizeof(Toy_Value) + sizeof(Toy_Stack)); if ((*stackHandle) == NULL) { fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to reallocate a 'Toy_Stack' of %d capacity (%d space in memory)\n" TOY_CC_RESET, (int)newCapacity, (int)(newCapacity * sizeof(Toy_Value) + sizeof(Toy_Stack))); exit(1); } } return ((Toy_Value*)((*stackHandle) + 1))[--(*stackHandle)->count]; }