Copying stack items in certain cases.
h3rald h3rald@h3rald.com
Thu, 26 Dec 2024 07:35:27 +0000
3 files changed,
58 insertions(+),
4 deletions(-)
M
src/hex.h
→
src/hex.h
@@ -276,6 +276,7 @@ int hex_push_string(hex_context_t *ctx, const char *value);
int hex_push_quotation(hex_context_t *ctx, hex_item_t **quotation, size_t size); int hex_push_symbol(hex_context_t *ctx, hex_token_t *token); hex_item_t hex_pop(hex_context_t *ctx); +hex_item_t hex_copy_item(const hex_item_t *item); // Parser and interpreter hex_token_t *hex_next_token(hex_context_t *ctx, const char **input, hex_file_position_t *position);
M
src/stack.c
→
src/stack.c
@@ -190,3 +190,49 @@ {
HEX_FREE(ctx, *quotation[i]); } } + +hex_item_t hex_copy_item(const hex_item_t *item) +{ + hex_item_t 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 + } + break; + + case HEX_TYPE_QUOTATION: + if (item->data.quotation_value) + { + copy.data.quotation_value = malloc(item->quotation_size * sizeof(hex_item_t *)); + for (size_t i = 0; i < item->quotation_size; i++) + { + copy.data.quotation_value[i] = malloc(sizeof(hex_item_t)); + *copy.data.quotation_value[i] = hex_copy_item(item->data.quotation_value[i]); // Deep copy each item + } + } + 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)); + *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 + } + } + break; + + default: + break; + } + + return copy; +}
M
src/symbols.c
→
src/symbols.c
@@ -1208,6 +1208,7 @@ HEX_FREE(ctx, index);
return 1; } int result = 0; + hex_item_t copy; if (list.type == HEX_TYPE_QUOTATION) { if (index.type != HEX_TYPE_INTEGER)@@ -1222,7 +1223,8 @@ result = 1;
} else { - result = hex_push(ctx, *list.data.quotation_value[index.data.int_value]); + copy = hex_copy_item(list.data.quotation_value[index.data.int_value]); + result = hex_push(ctx, copy); } } else if (list.type == HEX_TYPE_STRING)@@ -1240,7 +1242,8 @@ }
else { char str[2] = {list.data.str_value[index.data.int_value], '\0'}; - result = hex_push_string(ctx, str); + copy = hex_string_item(ctx, str); + result = hex_push(ctx, copy); } } else@@ -1253,6 +1256,7 @@ {
HEX_FREE(ctx, list); HEX_FREE(ctx, index); + HEX_FREE(ctx, copy); } return result; }@@ -2448,11 +2452,14 @@ {
HEX_FREE(ctx, item); return 1; } - if (HEX_PUSH(ctx, item) == 0 && HEX_PUSH(ctx, item) == 0) + hex_item_t copy = hex_copy_item(&item); + if (HEX_PUSH(ctx, copy) == 0 && HEX_PUSH(ctx, item) == 0) { + HEX_FREE(ctx, item); return 0; } HEX_FREE(ctx, item); + HEX_FREE(ctx, copy); return 1; }@@ -2475,7 +2482,7 @@ hex_error(ctx, "[symbol stack] Memory allocation failed");
hex_free_list(ctx, quotation, count); return 1; } - *quotation[i] = ctx->stack.entries[i]; + *quotation[i] = hex_copy_item(&ctx->stack.entries[i]); count++; }