src/stack.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
#ifndef HEX_H #include "hex.h" #endif //////////////////////////////////////// // Stack Implementation // //////////////////////////////////////// // Free a token void hex_free_token(hex_token_t *token) { if (token) { free(token->value); free(token); } } // Push functions int hex_push(hex_context_t *ctx, hex_item_t item) { if (ctx->stack.top >= HEX_STACK_SIZE - 1) { hex_error(ctx, "[push] Stack overflow"); return 1; } hex_debug_item(ctx, "PUSH", item); int result = 0; if (item.type == HEX_TYPE_USER_SYMBOL) { hex_item_t value; if (hex_get_symbol(ctx, item.token->value, &value)) { add_to_stack_trace(ctx, item.token); result = HEX_PUSH(ctx, value); } else { hex_error(ctx, "[push] Undefined user symbol: %s", item.token->value); HEX_FREE(ctx, value); result = 1; } } else if (item.type == HEX_TYPE_NATIVE_SYMBOL) { add_to_stack_trace(ctx, item.token); hex_debug_item(ctx, "CALL", item); result = item.data.fn_value(ctx); } else { ctx->stack.entries[++ctx->stack.top] = item; } if (result == 0) { hex_debug_item(ctx, "DONE", item); } else { hex_debug_item(ctx, "FAIL", item); } return result; } hex_item_t hex_string_item(hex_context_t *ctx, const char *value) { char *str = hex_process_string(value); if (str == NULL) { hex_error(ctx, "[create string] Failed to allocate memory for string"); return (hex_item_t){.type = HEX_TYPE_INVALID}; } hex_item_t item = {.type = HEX_TYPE_STRING, .data.str_value = str}; return item; } hex_item_t hex_integer_item(hex_context_t *ctx, int value) { (void)(ctx); hex_item_t item = {.type = HEX_TYPE_INTEGER, .data.int_value = value}; return item; } hex_item_t hex_quotation_item(hex_context_t *ctx, hex_item_t **quotation, size_t size) { (void)(ctx); hex_item_t item = {.type = HEX_TYPE_QUOTATION, .data.quotation_value = quotation, .quotation_size = size}; return item; } int hex_push_string(hex_context_t *ctx, const char *value) { return HEX_PUSH(ctx, hex_string_item(ctx, value)); } int hex_push_integer(hex_context_t *ctx, int value) { return HEX_PUSH(ctx, hex_integer_item(ctx, value)); } int hex_push_quotation(hex_context_t *ctx, hex_item_t **quotation, size_t size) { return HEX_PUSH(ctx, hex_quotation_item(ctx, quotation, size)); } int hex_push_symbol(hex_context_t *ctx, hex_token_t *token) { hex_item_t value; if (hex_get_symbol(ctx, token->value, &value)) { value.token = token; return HEX_PUSH(ctx, value); } else { hex_error(ctx, "[push] Undefined symbol: %s", token->value); return 1; } } // Pop function hex_item_t hex_pop(hex_context_t *ctx) { if (ctx->stack.top < 0) { hex_error(ctx, "[pop] Insufficient items on the stack"); return (hex_item_t){.type = HEX_TYPE_INVALID}; } hex_debug_item(ctx, " POP", ctx->stack.entries[ctx->stack.top]); return ctx->stack.entries[ctx->stack.top--]; } // Free a stack item void hex_free_item(hex_context_t *ctx, hex_item_t item) { hex_debug_item(ctx, "FREE", item); if (item.type == HEX_TYPE_STRING && item.data.str_value != NULL) { hex_debug(ctx, "FREE: ** string start"); item.data.str_value = NULL; free(item.data.str_value); hex_debug(ctx, "FREE: ** string end"); } else if (item.type == HEX_TYPE_QUOTATION && item.data.quotation_value != NULL) { hex_debug(ctx, "FREE: ** quotation start"); hex_free_list(ctx, item.data.quotation_value, item.quotation_size); item.data.quotation_value = NULL; hex_debug(ctx, "FREE: ** quotation end"); } else if (item.type == HEX_TYPE_NATIVE_SYMBOL && item.token->value != NULL) { hex_debug(ctx, "FREE: ** native symbol start"); item.token = NULL; hex_free_token(item.token); hex_debug(ctx, "FREE: ** native symbol end"); } else if (item.type == HEX_TYPE_USER_SYMBOL && item.token->value != NULL) { hex_debug(ctx, "FREE: ** user symbol start"); item.token = NULL; hex_free_token(item.token); hex_debug(ctx, "FREE: ** user symbol end"); } else { hex_debug(ctx, "FREE: ** nothing to free"); } } void hex_free_list(hex_context_t *ctx, hex_item_t **quotation, size_t size) { for (size_t i = 0; i < size; i++) { HEX_FREE(ctx, *quotation[i]); } } |