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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
#ifndef HEX_H #include "hex.h" #endif //////////////////////////////////////// // Stack Implementation // //////////////////////////////////////// // Free a token void hex_free_token(hex_token_t *token) { if (token == NULL) return; free(token->value); token->value = NULL; free(token); // Free the token itself } // 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 = malloc(sizeof(hex_item_t)); if (value == NULL) { hex_error(ctx, "[push] Failed to allocate memory for value"); return 1; } if (hex_get_symbol(ctx, item->token->value, value)) { add_to_stack_trace(ctx, item->token); if (value->type == HEX_TYPE_QUOTATION && value->operator) { for (size_t i = 0; i < value->quotation_size; i++) { if (hex_push(ctx, value->data.quotation_value[i]) != 0) { HEX_FREE(ctx, value); free(value); hex_debug_item(ctx, "FAIL", item); return 1; } } } else { result = hex_push(ctx, value); } } else { hex_error(ctx, "[push] Undefined user symbol: %s", item->token->value); HEX_FREE(ctx, value); result = 1; } free(value); } else if (item->type == HEX_TYPE_NATIVE_SYMBOL) { hex_item_t *value = malloc(sizeof(hex_item_t)); if (value == NULL) { hex_error(ctx, "[push] Failed to allocate memory for value"); return 1; } if (hex_get_symbol(ctx, item->token->value, value)) { add_to_stack_trace(ctx, item->token); hex_debug_item(ctx, "CALL", item); result = value->data.fn_value(ctx); } else { hex_error(ctx, "[push] Undefined native symbol: %s", item->token->value); HEX_FREE(ctx, value); result = 1; } free(value); } 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 NULL; } hex_item_t *item = malloc(sizeof(hex_item_t)); if (item == NULL) { hex_error(ctx, "[create string] Failed to allocate memory for item"); free(str); return NULL; } item->type = HEX_TYPE_STRING; item->data.str_value = str; return item; } hex_item_t *hex_integer_item(hex_context_t *ctx, int value) { (void)(ctx); hex_item_t *item = malloc(sizeof(hex_item_t)); if (item == NULL) { hex_error(ctx, "[create integer] Failed to allocate memory for item"); return NULL; } item->type = HEX_TYPE_INTEGER; item->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 = malloc(sizeof(hex_item_t)); if (item == NULL) { hex_error(ctx, "[create quotation] Failed to allocate memory for item"); return NULL; } item->type = HEX_TYPE_QUOTATION; item->data.quotation_value = quotation; item->quotation_size = size; return item; } hex_item_t *hex_symbol_item(hex_context_t *ctx, hex_token_t *token) { hex_item_t *item = malloc(sizeof(hex_item_t)); if (item == NULL) { hex_error(ctx, "[create symbol] Failed to allocate memory for item"); return NULL; } item->type = hex_valid_native_symbol(ctx, token->value) ? HEX_TYPE_NATIVE_SYMBOL : HEX_TYPE_USER_SYMBOL; item->token = token; return item; } int hex_push_string(hex_context_t *ctx, const char *value) { hex_item_t *item = hex_string_item(ctx, value); if (item == NULL) { return 1; } int result = HEX_PUSH(ctx, item); free(item); return result; } int hex_push_integer(hex_context_t *ctx, int value) { hex_item_t *item = hex_integer_item(ctx, value); if (item == NULL) { return 1; } int result = HEX_PUSH(ctx, item); free(item); return result; } int hex_push_quotation(hex_context_t *ctx, hex_item_t **quotation, size_t size) { hex_item_t *item = hex_quotation_item(ctx, quotation, size); if (item == NULL) { return 1; } int result = HEX_PUSH(ctx, item); free(item); return result; } int hex_push_symbol(hex_context_t *ctx, hex_token_t *token) { hex_item_t *item = hex_symbol_item(ctx, token); if (item == NULL) { return 1; } int result = HEX_PUSH(ctx, item); free(item); return result; } // 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 NULL; } hex_debug_item(ctx, " POP", ctx->stack.entries[ctx->stack.top]); hex_item_t *item = malloc(sizeof(hex_item_t)); if (item == NULL) { hex_error(ctx, "[pop] Failed to allocate memory for item"); return NULL; } *item = *ctx->stack.entries[ctx->stack.top--]; return item; } void hex_free_list(hex_context_t *ctx, hex_item_t **quotation, size_t size) { if (!quotation) return; for (size_t i = 0; i < size; i++) { if (quotation[i]) { hex_free_item(ctx, quotation[i]); // Free each item free(quotation[i]); // Free the pointer itself quotation[i] = NULL; // Nullify after freeing } } } void hex_free_item(hex_context_t *ctx, hex_item_t *item) { if (item == NULL) return; hex_debug_item(ctx, "FREE", item); switch (item->type) { case HEX_TYPE_STRING: if (item->data.str_value) { free(item->data.str_value); item->data.str_value = NULL; // Prevent double free } break; case HEX_TYPE_QUOTATION: if (item->data.quotation_value) { hex_free_list(ctx, item->data.quotation_value, item->quotation_size); free(item->data.quotation_value); item->data.quotation_value = NULL; // Prevent double free } break; case HEX_TYPE_NATIVE_SYMBOL: case HEX_TYPE_USER_SYMBOL: if (item->token) { hex_free_token(item->token); item->token = NULL; // Prevent double free } break; default: break; } free(item); // Free the item itself } hex_item_t *hex_copy_item(const hex_item_t *item) { hex_item_t *copy = malloc(sizeof(hex_item_t)); if (copy == NULL) { return NULL; } *copy = *item; // Shallow copy the structure switch (item->type) { case HEX_TYPE_STRING: if (item->data.str_value) { copy->data.str_value = strdup(item->data.str_value); // Duplicate the string if (copy->data.str_value == NULL) { free(copy); return NULL; } } break; case HEX_TYPE_QUOTATION: if (item->data.quotation_value) { copy->data.quotation_value = malloc(item->quotation_size * sizeof(hex_item_t *)); if (copy->data.quotation_value == NULL) { free(copy); return NULL; } for (size_t i = 0; i < item->quotation_size; i++) { copy->data.quotation_value[i] = hex_copy_item(item->data.quotation_value[i]); // Deep copy each item if (copy->data.quotation_value[i] == NULL) { hex_free_list(NULL, copy->data.quotation_value, i); free(copy->data.quotation_value); free(copy); return NULL; } } } copy->quotation_size = item->quotation_size; break; case HEX_TYPE_NATIVE_SYMBOL: case HEX_TYPE_USER_SYMBOL: if (item->token) { copy->token = malloc(sizeof(hex_token_t)); if (copy->token == NULL) { free(copy); return NULL; } *copy->token = *item->token; // Shallow copy the token structure if (item->token->value) { copy->token->value = strdup(item->token->value); // Deep copy the token's value if (copy->token->value == NULL) { free(copy->token); free(copy); return NULL; } } } break; default: break; } return copy; } |