Moved all data to heap; segfaults.
@@ -10,7 +10,7 @@ void hex_set_doc(hex_doc_dictionary_t *dict, const char *name, const char *input, const char *output, const char *description)
{ hex_doc_entry_t doc = {.name = name, .description = description, .input = input, .output = output}; // No overflow check as the dictionary is fixed size - dict->entries[dict->size] = doc; + *(dict->entries[dict->size]) = doc; dict->size++; }@@ -18,9 +18,9 @@ int hex_get_doc(hex_doc_dictionary_t *docs, const char *key, hex_doc_entry_t *result)
{ for (size_t i = 0; i < docs->size; i++) { - if (strcmp(docs->entries[i].name, key) == 0) + if (strcmp(docs->entries[i]->name, key) == 0) { - *result = docs->entries[i]; + *result = *(docs->entries[i]); return 1; } }
@@ -53,7 +53,7 @@ return "unknown";
} } -void hex_debug_item(hex_context_t *ctx, const char *message, hex_item_t item) +void hex_debug_item(hex_context_t *ctx, const char *message, hex_item_t *item) { if (ctx->settings.debugging_enabled) {@@ -75,13 +75,13 @@
if (ctx->stack_trace.size < HEX_STACK_TRACE_SIZE) { // Buffer is not full; add item - ctx->stack_trace.entries[index] = *token; + ctx->stack_trace.entries[index] = token; ctx->stack_trace.size++; } else { // Buffer is full; overwrite the oldest item - ctx->stack_trace.entries[index] = *token; + ctx->stack_trace.entries[index] = token; ctx->stack_trace.start = (ctx->stack_trace.start + 1) % HEX_STACK_TRACE_SIZE; } }@@ -98,7 +98,7 @@
for (size_t i = 0; i < ctx->stack_trace.size; i++) { int index = (ctx->stack_trace.start + ctx->stack_trace.size - 1 - i) % HEX_STACK_TRACE_SIZE; - hex_token_t token = ctx->stack_trace.entries[index]; - fprintf(stderr, " %s (%s:%d:%d)\n", token.value, token.position.filename, token.position.line, token.position.column); + hex_token_t token = *ctx->stack_trace.entries[index]; + fprintf(stderr, " %s (%s:%d:%d)\n", token.value, token.position->filename, token.position->line, token.position->column); } }
@@ -63,7 +63,7 @@ {
hex_token_type_t type; char *value; size_t quotation_size; - hex_file_position_t position; + hex_file_position_t *position; } hex_token_t; typedef struct hex_context_t hex_context_t;@@ -86,25 +86,26 @@
typedef struct hex_registry_entry { char *key; - hex_item_t value; + hex_item_t *value; } hex_registry_entry_t; typedef struct hex_stack_trace_t { - hex_token_t entries[HEX_STACK_TRACE_SIZE]; + hex_token_t **entries; int start; // Index of the oldest item size_t size; // Current number of items in the buffer } hex_stack_trace_t; typedef struct hex_stack_t { - hex_item_t entries[HEX_STACK_SIZE]; + hex_item_t **entries; int top; + size_t capacity; } hex_stack_t; typedef struct hex_registry_t { - hex_registry_entry_t entries[HEX_REGISTRY_SIZE]; + hex_registry_entry_t **entries; size_t size; } hex_registry_t;@@ -118,7 +119,7 @@ } hex_doc_entry_t;
typedef struct hex_doc_dictionary_t { - hex_doc_entry_t entries[HEX_NATIVE_SYMBOLS]; + hex_doc_entry_t **entries; size_t size; } hex_doc_dictionary_t;@@ -245,38 +246,38 @@ void hex_print_help();
void hex_print_docs(hex_doc_dictionary_t *docs); // Free data -void hex_free_item(hex_context_t *ctx, hex_item_t item); +void hex_free_item(hex_context_t *ctx, hex_item_t *item); void hex_free_token(hex_token_t *token); void hex_free_list(hex_context_t *ctx, hex_item_t **quotation, size_t size); // Symbol management int hex_valid_user_symbol(hex_context_t *ctx, const char *symbol); int hex_valid_native_symbol(hex_context_t *ctx, const char *symbol); -int hex_set_symbol(hex_context_t *ctx, const char *key, hex_item_t value, int native); +int hex_set_symbol(hex_context_t *ctx, const char *key, hex_item_t *value, int native); void hex_set_native_symbol(hex_context_t *ctx, const char *name, int (*func)()); int hex_get_symbol(hex_context_t *ctx, const char *key, hex_item_t *result); // Errors and debugging void hex_error(hex_context_t *ctx, const char *format, ...); void hex_debug(hex_context_t *ctx, const char *format, ...); -void hex_debug_item(hex_context_t *ctx, const char *message, hex_item_t item); -void hex_print_item(FILE *stream, hex_item_t item); +void hex_debug_item(hex_context_t *ctx, const char *message, hex_item_t *item); +void hex_print_item(FILE *stream, hex_item_t *item); void add_to_stack_trace(hex_context_t *ctx, hex_token_t *token); void print_stack_trace(hex_context_t *ctx); // Item constructors -hex_item_t hex_string_item(hex_context_t *ctx, const char *value); -hex_item_t hex_integer_item(hex_context_t *ctx, int value); -hex_item_t hex_quotation_item(hex_context_t *ctx, hex_item_t **quotation, size_t size); +hex_item_t *hex_string_item(hex_context_t *ctx, const char *value); +hex_item_t *hex_integer_item(hex_context_t *ctx, int value); +hex_item_t *hex_quotation_item(hex_context_t *ctx, hex_item_t **quotation, size_t size); // Stack management -int hex_push(hex_context_t *ctx, hex_item_t item); +int hex_push(hex_context_t *ctx, hex_item_t *item); int hex_push_integer(hex_context_t *ctx, int value); 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); +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);@@ -401,8 +402,9 @@ int hex_write_bytecode_file(hex_context_t *ctx, char *filename, uint8_t *bytecode, size_t size);
char *hex_read_file(hex_context_t *ctx, const char *filename); // Common operations -#define HEX_POP(ctx, x) hex_item_t x = hex_pop(ctx) +#define HEX_POP(ctx, x) x = hex_pop(ctx) #define HEX_FREE(ctx, x) hex_free_item(ctx, x) #define HEX_PUSH(ctx, x) hex_push(ctx, x) +#define HEX_ALLOC(x) hex_item_t *x = (hex_item_t *)malloc(sizeof(hex_item_t)); #endif // HEX_H
@@ -44,7 +44,7 @@ result = hex_push_string(ctx, token->value);
} else if (token->type == HEX_TOKEN_SYMBOL) { - token->position.filename = strdup(filename); + token->position->filename = strdup(filename); result = hex_push_symbol(ctx, token); } else if (token->type == HEX_TOKEN_QUOTATION_END)@@ -80,7 +80,7 @@ token = hex_next_token(ctx, &input, &position);
} if (token != NULL && token->type == HEX_TOKEN_INVALID) { - token->position.filename = strdup(filename); + token->position->filename = strdup(filename); add_to_stack_trace(ctx, token); print_stack_trace(ctx); return 1;
@@ -124,7 +124,7 @@ hex_interpret(ctx, line, "<repl>", 1, 1);
// Print the top item of the stack if (ctx->stack.top >= 0) { - hex_print_item(stdout, ctx->stack.entries[ctx->stack.top]); + hex_print_item(stdout, *ctx->stack.entries[ctx->stack.top]); // hex_print_item(stdout, HEX_STACK[HEX_TOP]); printf("\n"); }@@ -262,13 +262,13 @@ " +---------+----------------------------+-------------------------------------------------+\n");
for (size_t i = 0; i < docs->size; i++) { printf(" | "); - hex_rpad(docs->entries[i].name, 7); + hex_rpad(docs->entries[i]->name, 7); printf(" | "); - hex_lpad(docs->entries[i].input, 15); + hex_lpad(docs->entries[i]->input, 15); printf(" -> "); - hex_rpad(docs->entries[i].output, 7); + hex_rpad(docs->entries[i]->output, 7); printf(" | "); - hex_rpad(docs->entries[i].description, 47); + hex_rpad(docs->entries[i]->description, 47); printf(" |\n"); } printf(" +---------+----------------------------+-------------------------------------------------+\n");
@@ -33,8 +33,8 @@ }
hex_token_t *token = (hex_token_t *)malloc(sizeof(hex_token_t)); token->value = NULL; - token->position.line = position->line; - token->position.column = position->column; + token->position->line = position->line; + token->position->column = position->column; if (*ptr == ';') {@@ -73,8 +73,8 @@ }
if (*ptr == '\0') { token->type = HEX_TOKEN_INVALID; - token->position.line = position->line; - token->position.column = position->column; + token->position->line = position->line; + token->position->column = position->column; hex_error(ctx, "(%d,%d) Unterminated block comment", position->line, position->column); return token; }@@ -114,8 +114,8 @@ }
else if (*ptr == '\n') { token->type = HEX_TOKEN_INVALID; - token->position.line = position->line; - token->position.column = position->column; + token->position->line = position->line; + token->position->column = position->column; hex_error(ctx, "(%d,%d) Unescaped new line in string", position->line, position->column); return token; }@@ -130,8 +130,8 @@
if (*ptr != '"') { token->type = HEX_TOKEN_INVALID; - token->position.line = position->line; - token->position.column = position->column; + token->position->line = position->line; + token->position->column = position->column; hex_error(ctx, "(%d,%d) Unterminated string", position->line, position->column); return token; }@@ -213,8 +213,8 @@ }
else { token->type = HEX_TOKEN_INVALID; - token->position.line = position->line; - token->position.column = position->column; + token->position->line = position->line; + token->position->column = position->column; } }@@ -278,17 +278,17 @@ return 1;
} } - hex_item_t *item = (hex_item_t *)malloc(sizeof(hex_item_t)); + HEX_ALLOC(item); if (token->type == HEX_TOKEN_INTEGER) { - *item = hex_integer_item(ctx, hex_parse_integer(token->value)); + *item = *hex_integer_item(ctx, hex_parse_integer(token->value)); quotation[size] = item; size++; } else if (token->type == HEX_TOKEN_STRING) { - *item = hex_string_item(ctx, token->value); + *item = *hex_string_item(ctx, token->value); quotation[size] = item; size++; }@@ -297,12 +297,12 @@ {
if (hex_valid_native_symbol(ctx, token->value)) { item->type = HEX_TYPE_NATIVE_SYMBOL; - hex_item_t value; - if (hex_get_symbol(ctx, token->value, &value)) + HEX_ALLOC(value); + if (hex_get_symbol(ctx, token->value, value)) { item->token = token; item->type = HEX_TYPE_NATIVE_SYMBOL; - item->data.fn_value = value.data.fn_value; + item->data.fn_value = value->data.fn_value; } else {@@ -316,7 +316,7 @@ else
{ item->type = HEX_TYPE_USER_SYMBOL; } - token->position.filename = strdup(position->filename); + token->position->filename = strdup(position->filename); item->token = token; quotation[size] = item; size++;
@@ -37,7 +37,7 @@ return 1;
} // Add a symbol to the registry -int hex_set_symbol(hex_context_t *ctx, const char *key, hex_item_t value, int native) +int hex_set_symbol(hex_context_t *ctx, const char *key, hex_item_t *value, int native) { if (!native && hex_valid_user_symbol(ctx, key) == 0)@@ -46,16 +46,16 @@ return 1;
} for (size_t i = 0; i < ctx->registry.size; i++) { - if (strcmp(ctx->registry.entries[i].key, key) == 0) + if (strcmp(ctx->registry.entries[i]->key, key) == 0) { - if (ctx->registry.entries[i].value.type == HEX_TYPE_NATIVE_SYMBOL) + if (ctx->registry.entries[i]->value->type == HEX_TYPE_NATIVE_SYMBOL) { hex_error(ctx, "[set symbol] Cannot overwrite native symbol '%s'", key); return 1; } - free(ctx->registry.entries[i].key); - ctx->registry.entries[i].key = strdup(key); - ctx->registry.entries[i].value = value; + free(ctx->registry.entries[i]->key); + ctx->registry.entries[i]->key = strdup(key); + ctx->registry.entries[i]->value = value; return 0; } }@@ -63,12 +63,12 @@
if (ctx->registry.size >= HEX_REGISTRY_SIZE) { hex_error(ctx, "Registry overflow"); - hex_free_token(value.token); + hex_free_token(value->token); return 1; } - ctx->registry.entries[ctx->registry.size].key = strdup(key); - ctx->registry.entries[ctx->registry.size].value = value; + ctx->registry.entries[ctx->registry.size]->key = strdup(key); + ctx->registry.entries[ctx->registry.size]->value = value; ctx->registry.size++; return 0; }@@ -76,25 +76,30 @@
// Register a native symbol void hex_set_native_symbol(hex_context_t *ctx, const char *name, int (*func)()) { - hex_item_t func_item; - func_item.type = HEX_TYPE_NATIVE_SYMBOL; - func_item.data.fn_value = func; + hex_item_t *func_item = malloc(sizeof(hex_item_t)); + if (func_item == NULL) + { + hex_error(ctx, "Error: Memory allocation failed for native symbol '%s'", name); + return; + } + func_item->type = HEX_TYPE_NATIVE_SYMBOL; + func_item->data.fn_value = func; if (hex_set_symbol(ctx, name, func_item, 1) != 0) { hex_error(ctx, "Error: Failed to register native symbol '%s'", name); + free(func_item); } } // Get a symbol value from the registry int hex_get_symbol(hex_context_t *ctx, const char *key, hex_item_t *result) { - for (size_t i = 0; i < ctx->registry.size; i++) { - if (strcmp(ctx->registry.entries[i].key, key) == 0) + if (strcmp(ctx->registry.entries[i]->key, key) == 0) { - *result = ctx->registry.entries[i].value; + *result = *ctx->registry.entries[i]->value; return 1; } }
@@ -9,17 +9,16 @@
// Free a token void hex_free_token(hex_token_t *token) { - if (token == NULL) return; + 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) +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");@@ -27,19 +26,25 @@ return 1;
} hex_debug_item(ctx, "PUSH", item); int result = 0; - if (item.type == HEX_TYPE_USER_SYMBOL) + if (item->type == HEX_TYPE_USER_SYMBOL) { - hex_item_t value; - if (hex_get_symbol(ctx, item.token->value, &value)) + 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) + 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++) + for (size_t i = 0; i < value->quotation_size; i++) { - if (hex_push(ctx, *value.data.quotation_value[i]) != 0) + if (hex_push(ctx, value->data.quotation_value[i]) != 0) { HEX_FREE(ctx, value); + free(value); hex_debug_item(ctx, "FAIL", item); return 1; }@@ -47,31 +52,38 @@ }
} else { - result = HEX_PUSH(ctx, value); + result = hex_push(ctx, value); } } else { - hex_error(ctx, "[push] Undefined user symbol: %s", item.token->value); + 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) + else if (item->type == HEX_TYPE_NATIVE_SYMBOL) { - hex_item_t value; - if (hex_get_symbol(ctx, item.token->value, &value)) + hex_item_t *value = malloc(sizeof(hex_item_t)); + if (value == NULL) { - add_to_stack_trace(ctx, item.token); + 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); + result = value->data.fn_value(ctx); } else { - hex_error(ctx, "[push] Undefined native symbol: %s", item.token->value); + hex_error(ctx, "[push] Undefined native symbol: %s", item->token->value); HEX_FREE(ctx, value); result = 1; } + free(value); } else {@@ -88,163 +100,267 @@ }
return result; } -hex_item_t hex_string_item(hex_context_t *ctx, const char *value) +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}; + 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; } - hex_item_t item = {.type = HEX_TYPE_STRING, .data.str_value = str}; + item->type = HEX_TYPE_STRING; + item->data.str_value = str; return item; } -hex_item_t hex_integer_item(hex_context_t *ctx, int value) +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}; + 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) +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}; + 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 *hex_symbol_item(hex_context_t *ctx, hex_token_t *token) { - hex_item_t item = {.type = hex_valid_native_symbol(ctx, token->value) ? HEX_TYPE_NATIVE_SYMBOL : HEX_TYPE_USER_SYMBOL, .token = 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) { - return HEX_PUSH(ctx, hex_string_item(ctx, 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) { - return HEX_PUSH(ctx, hex_integer_item(ctx, 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) { - return HEX_PUSH(ctx, hex_quotation_item(ctx, quotation, 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) { - return HEX_PUSH(ctx, hex_symbol_item(ctx, 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) +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}; + return NULL; } hex_debug_item(ctx, " POP", ctx->stack.entries[ctx->stack.top]); - return 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; + 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 + 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) +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) + 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_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_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; + 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; + default: + break; } + + free(item); // Free the item itself } -hex_item_t hex_copy_item(const hex_item_t *item) +hex_item_t *hex_copy_item(const hex_item_t *item) { - hex_item_t copy = *item; // Shallow copy the structure + 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) + 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) { - copy.data.str_value = strdup(item->data.str_value); // Duplicate the string + free(copy); + return NULL; } - break; + } + break; - case HEX_TYPE_QUOTATION: - if (item->data.quotation_value) + 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) { - copy.data.quotation_value = malloc(item->quotation_size * sizeof(hex_item_t *)); - for (size_t i = 0; i < item->quotation_size; i++) + 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) { - 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 + 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; + } + copy->quotation_size = item->quotation_size; + break; - case HEX_TYPE_NATIVE_SYMBOL: - case HEX_TYPE_USER_SYMBOL: - if (item->token) + 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 = 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 + if (copy->token->value == NULL) { - copy.token->value = strdup(item->token->value); // Deep copy the token's value + free(copy->token); + free(copy); + return NULL; } } - break; + } + break; - default: - break; + default: + break; } return copy;
@@ -10,30 +10,47 @@ // Definition symbols
int hex_symbol_store(hex_context_t *ctx) { + HEX_ALLOC(name); + if (!name) + { + hex_error(ctx, "[symbol :] Memory allocation failed"); + return 1; + } + HEX_POP(ctx, name); + ; + if (name->type == HEX_TYPE_INVALID) + { + HEX_FREE(ctx, name); + return 1; + } - HEX_POP(ctx, name); - if (name.type == HEX_TYPE_INVALID) + HEX_ALLOC(value); + if (!value) { + hex_error(ctx, "[symbol :] Memory allocation failed"); HEX_FREE(ctx, name); return 1; } HEX_POP(ctx, value); - if (value.type == HEX_TYPE_INVALID) + ; + if (value->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, name); HEX_FREE(ctx, value); return 1; } - if (name.type != HEX_TYPE_STRING) + + if (name->type != HEX_TYPE_STRING) { hex_error(ctx, "[symbol :] Symbol name must be a string"); HEX_FREE(ctx, name); HEX_FREE(ctx, value); return 1; } - if (hex_set_symbol(ctx, name.data.str_value, value, 0) != 0) + + if (hex_set_symbol(ctx, name->data.str_value, value, 0) != 0) { - hex_error(ctx, "[symbol :] Failed to store symbol '%s'", name.data.str_value); + hex_error(ctx, "[symbol :] Failed to store symbol '%s'", name->data.str_value); HEX_FREE(ctx, name); HEX_FREE(ctx, value); return 1;@@ -43,68 +60,93 @@ }
int hex_symbol_define(hex_context_t *ctx) { - + HEX_ALLOC(name); + if (!name) + { + hex_error(ctx, "[symbol ::] Memory allocation failed"); + return 1; + } HEX_POP(ctx, name); - if (name.type == HEX_TYPE_INVALID) + ; + if (name->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, name); return 1; } + + HEX_ALLOC(value); + if (!value) + { + hex_error(ctx, "[symbol ::] Memory allocation failed"); + HEX_FREE(ctx, name); + return 1; + } HEX_POP(ctx, value); - if (value.type == HEX_TYPE_INVALID) + ; + if (value->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, name); HEX_FREE(ctx, value); return 1; } - if (name.type != HEX_TYPE_STRING) + + if (name->type != HEX_TYPE_STRING) { hex_error(ctx, "[symbol ::] Symbol name must be a string"); HEX_FREE(ctx, name); HEX_FREE(ctx, value); return 1; } - if (value.type == HEX_TYPE_QUOTATION) + + if (value->type == HEX_TYPE_QUOTATION) { - value.operator= 1; + value->operator= 1; } - if (hex_set_symbol(ctx, name.data.str_value, value, 0) != 0) + + if (hex_set_symbol(ctx, name->data.str_value, value, 0) != 0) { - hex_error(ctx, "[symbol ::] Failed to store symbol '%s'", name.data.str_value); + hex_error(ctx, "[symbol ::] Failed to store symbol '%s'", name->data.str_value); HEX_FREE(ctx, name); HEX_FREE(ctx, value); return 1; } + return 0; } int hex_symbol_free(hex_context_t *ctx) { - + HEX_ALLOC(item); + if (!item) + { + hex_error(ctx, "[symbol #] Memory allocation failed"); + return 1; + } HEX_POP(ctx, item); - if (item.type == HEX_TYPE_INVALID) + ; + if (item->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, item); return 1; } - if (item.type != HEX_TYPE_STRING) + if (item->type != HEX_TYPE_STRING) { HEX_FREE(ctx, item); hex_error(ctx, "[symbol #] Symbol name must be a string"); return 1; } - if (hex_valid_native_symbol(ctx, item.data.str_value)) + if (hex_valid_native_symbol(ctx, item->data.str_value)) { - hex_error(ctx, "[symbol #] Cannot free native symbol '%s'", item.data.str_value); + hex_error(ctx, "[symbol #] Cannot free native symbol '%s'", item->data.str_value); HEX_FREE(ctx, item); return 1; } for (size_t i = 0; i < ctx->registry.size; i++) { - if (strcmp(ctx->registry.entries[i].key, item.data.str_value) == 0) + if (strcmp(ctx->registry.entries[i]->key, item->data.str_value) == 0) { - free(ctx->registry.entries[i].key); - HEX_FREE(ctx, ctx->registry.entries[i].value); + free(ctx->registry.entries[i]->key); + HEX_FREE(ctx, ctx->registry.entries[i]->value); for (size_t j = i; j < ctx->registry.size - 1; j++) { ctx->registry.entries[j] = ctx->registry.entries[j + 1];@@ -121,12 +163,29 @@
int hex_symbol_symbols(hex_context_t *ctx) { hex_item_t **quotation = (hex_item_t **)malloc(ctx->registry.size * sizeof(hex_item_t *)); + if (!quotation) + { + hex_error(ctx, "[symbol symbols] Memory allocation failed"); + return 1; + } for (size_t i = 0; i < ctx->registry.size; i++) { - char *id = malloc(strlen(ctx->registry.entries[i].key) * sizeof(char *)); - strcpy(id, ctx->registry.entries[i].key); + char *id = malloc(strlen(ctx->registry.entries[i]->key) + 1); + if (!id) + { + hex_error(ctx, "[symbol symbols] Memory allocation failed"); + hex_free_list(ctx, quotation, i); + return 1; + } + strcpy(id, ctx->registry.entries[i]->key); quotation[i] = (hex_item_t *)malloc(sizeof(hex_item_t)); - *quotation[i] = hex_string_item(ctx, id); + if (!quotation[i]) + { + hex_error(ctx, "[symbol symbols] Memory allocation failed"); + hex_free_list(ctx, quotation, i); + return 1; + } + *quotation[i] = *hex_string_item(ctx, id); } if (hex_push_quotation(ctx, quotation, ctx->registry.size) != 0) {@@ -138,88 +197,105 @@ }
int hex_symbol_type(hex_context_t *ctx) { - + HEX_ALLOC(item); + if (!item) + { + hex_error(ctx, "[symbol type] Memory allocation failed"); + return 1; + } HEX_POP(ctx, item); - if (item.type == HEX_TYPE_INVALID) + ; + if (item->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, item); return 1; } - return hex_push_string(ctx, hex_type(item.type)); + int result = hex_push_string(ctx, hex_type(item->type)); + HEX_FREE(ctx, item); + return result; } // Evaluation symbols int hex_symbol_i(hex_context_t *ctx) { - + HEX_ALLOC(item); + if (!item) + { + hex_error(ctx, "[symbol .] Memory allocation failed"); + return 1; + } HEX_POP(ctx, item); - if (item.type == HEX_TYPE_INVALID) + ; + if (item->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, item); return 1; } - if (item.type != HEX_TYPE_QUOTATION) + if (item->type != HEX_TYPE_QUOTATION) { hex_error(ctx, "[symbol .] Quotation required"); HEX_FREE(ctx, item); return 1; } - for (size_t i = 0; i < item.quotation_size; i++) + for (size_t i = 0; i < item->quotation_size; i++) { - if (hex_push(ctx, *item.data.quotation_value[i]) != 0) + if (hex_push(ctx, item->data.quotation_value[i]) != 0) { HEX_FREE(ctx, item); return 1; } } + HEX_FREE(ctx, item); return 0; } // evaluate a string or bytecode array int hex_symbol_eval(hex_context_t *ctx) { + HEX_ALLOC(item); + if (!item) + { + hex_error(ctx, "[symbol !] Memory allocation failed"); + return 1; + } HEX_POP(ctx, item); - if (item.type == HEX_TYPE_INVALID) + ; + if (item->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, item); return 1; } - if (item.type == HEX_TYPE_STRING) + if (item->type == HEX_TYPE_STRING) { - return hex_interpret(ctx, item.data.str_value, "<!>", 1, 1); + int result = hex_interpret(ctx, item->data.str_value, "<!>", 1, 1); + HEX_FREE(ctx, item); + return result; } - else if (item.type == HEX_TYPE_QUOTATION) + else if (item->type == HEX_TYPE_QUOTATION) { - for (size_t i = 0; i < item.quotation_size; i++) + for (size_t i = 0; i < item->quotation_size; i++) { - if (item.data.quotation_value[i]->type != HEX_TYPE_INTEGER) + if (item->data.quotation_value[i]->type != HEX_TYPE_INTEGER) { hex_error(ctx, "[symbol !] Quotation must contain only integers"); HEX_FREE(ctx, item); return 1; } } - uint8_t *bytecode = (uint8_t *)malloc(item.quotation_size * sizeof(uint8_t)); + uint8_t *bytecode = (uint8_t *)malloc(item->quotation_size * sizeof(uint8_t)); if (!bytecode) { hex_error(ctx, "[symbol !] Memory allocation failed"); HEX_FREE(ctx, item); return 1; } - for (size_t i = 0; i < item.quotation_size; i++) + for (size_t i = 0; i < item->quotation_size; i++) { - if (item.data.quotation_value[i]->type != HEX_TYPE_INTEGER) - { - hex_error(ctx, "[symbol !] Quotation must contain only integers"); - free(bytecode); - HEX_FREE(ctx, item); - return 1; - } - bytecode[i] = (uint8_t)item.data.quotation_value[i]->data.int_value; + bytecode[i] = (uint8_t)item->data.quotation_value[i]->data.int_value; } - int result = hex_interpret_bytecode(ctx, bytecode, item.quotation_size); - free(bytecode); + int result = hex_interpret_bytecode(ctx, bytecode, item->quotation_size); + HEX_FREE(ctx, item); return result; } else@@ -234,49 +310,69 @@ // IO Symbols
int hex_symbol_puts(hex_context_t *ctx) { - + HEX_ALLOC(item); + if (!item) + { + hex_error(ctx, "[symbol puts] Memory allocation failed"); + return 1; + } HEX_POP(ctx, item); - if (item.type == HEX_TYPE_INVALID) + ; + if (item->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, item); return 1; } - hex_raw_print_item(stdout, item); + hex_raw_print_item(stdout, *item); printf("\n"); + HEX_FREE(ctx, item); return 0; } int hex_symbol_warn(hex_context_t *ctx) { - + HEX_ALLOC(item); + if (!item) + { + hex_error(ctx, "[symbol warn] Memory allocation failed"); + return 1; + } HEX_POP(ctx, item); - if (item.type == HEX_TYPE_INVALID) + ; + if (item->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, item); return 1; } - hex_raw_print_item(stderr, item); + hex_raw_print_item(stderr, *item); printf("\n"); + HEX_FREE(ctx, item); return 0; } int hex_symbol_print(hex_context_t *ctx) { - + HEX_ALLOC(item); + if (!item) + { + hex_error(ctx, "[symbol print] Memory allocation failed"); + return 1; + } HEX_POP(ctx, item); - if (item.type == HEX_TYPE_INVALID) + ; + if (item->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, item); return 1; } - hex_raw_print_item(stdout, item); + hex_raw_print_item(stdout, *item); fflush(stdout); + HEX_FREE(ctx, item); return 0; } int hex_symbol_gets(hex_context_t *ctx) { - char input[HEX_STDIN_BUFFER_SIZE]; // Buffer to store the input (adjust size if needed) char *p = input; #if defined(__EMSCRIPTEN__)@@ -303,22 +399,40 @@
// Mathematical symbols int hex_symbol_add(hex_context_t *ctx) { + HEX_ALLOC(b); + if (!b) + { + hex_error(ctx, "[symbol +] Memory allocation failed"); + return 1; + } HEX_POP(ctx, b); - if (b.type == HEX_TYPE_INVALID) + ; + if (b->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, b); return 1; } + HEX_ALLOC(a); + if (!a) + { + hex_error(ctx, "[symbol +] Memory allocation failed"); + HEX_FREE(ctx, b); + return 1; + } HEX_POP(ctx, a); - if (a.type == HEX_TYPE_INVALID) + ; + if (a->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, a); HEX_FREE(ctx, b); return 1; } - if (a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) + if (a->type == HEX_TYPE_INTEGER && b->type == HEX_TYPE_INTEGER) { - return hex_push_integer(ctx, a.data.int_value + b.data.int_value); + int result = hex_push_integer(ctx, a->data.int_value + b->data.int_value); + HEX_FREE(ctx, a); + HEX_FREE(ctx, b); + return result; } hex_error(ctx, "[symbol +] Two integers required"); HEX_FREE(ctx, a);@@ -328,23 +442,40 @@ }
int hex_symbol_subtract(hex_context_t *ctx) { - + HEX_ALLOC(b); + if (!b) + { + hex_error(ctx, "[symbol -] Memory allocation failed"); + return 1; + } HEX_POP(ctx, b); - if (b.type == HEX_TYPE_INVALID) + ; + if (b->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, b); return 1; } + HEX_ALLOC(a); + if (!a) + { + hex_error(ctx, "[symbol -] Memory allocation failed"); + HEX_FREE(ctx, b); + return 1; + } HEX_POP(ctx, a); - if (a.type == HEX_TYPE_INVALID) + ; + if (a->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, a); HEX_FREE(ctx, b); return 1; } - if (a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) + if (a->type == HEX_TYPE_INTEGER && b->type == HEX_TYPE_INTEGER) { - return hex_push_integer(ctx, a.data.int_value - b.data.int_value); + int result = hex_push_integer(ctx, a->data.int_value - b->data.int_value); + HEX_FREE(ctx, a); + HEX_FREE(ctx, b); + return result; } hex_error(ctx, "[symbol -] Two integers required"); HEX_FREE(ctx, a);@@ -354,23 +485,40 @@ }
int hex_symbol_multiply(hex_context_t *ctx) { - + HEX_ALLOC(b); + if (!b) + { + hex_error(ctx, "[symbol *] Memory allocation failed"); + return 1; + } HEX_POP(ctx, b); - if (b.type == HEX_TYPE_INVALID) + ; + if (b->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, b); return 1; } + HEX_ALLOC(a); + if (!a) + { + hex_error(ctx, "[symbol *] Memory allocation failed"); + HEX_FREE(ctx, b); + return 1; + } HEX_POP(ctx, a); - if (a.type == HEX_TYPE_INVALID) + ; + if (a->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, a); HEX_FREE(ctx, b); return 1; } - if (a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) + if (a->type == HEX_TYPE_INTEGER && b->type == HEX_TYPE_INTEGER) { - return hex_push_integer(ctx, a.data.int_value * b.data.int_value); + int result = hex_push_integer(ctx, a->data.int_value * b->data.int_value); + HEX_FREE(ctx, a); + HEX_FREE(ctx, b); + return result; } hex_error(ctx, "[symbol *] Two integers required"); HEX_FREE(ctx, a);@@ -380,28 +528,47 @@ }
int hex_symbol_divide(hex_context_t *ctx) { - + HEX_ALLOC(b); + if (!b) + { + hex_error(ctx, "[symbol /] Memory allocation failed"); + return 1; + } HEX_POP(ctx, b); - if (b.type == HEX_TYPE_INVALID) + ; + if (b->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, b); return 1; } + HEX_ALLOC(a); + if (!a) + { + hex_error(ctx, "[symbol /] Memory allocation failed"); + HEX_FREE(ctx, b); + return 1; + } HEX_POP(ctx, a); - if (a.type == HEX_TYPE_INVALID) + ; + if (a->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, a); HEX_FREE(ctx, b); return 1; } - if (a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) + if (a->type == HEX_TYPE_INTEGER && b->type == HEX_TYPE_INTEGER) { - if (b.data.int_value == 0) + if (b->data.int_value == 0) { hex_error(ctx, "[symbol /] Division by zero"); + HEX_FREE(ctx, a); + HEX_FREE(ctx, b); return 1; } - return hex_push_integer(ctx, a.data.int_value / b.data.int_value); + int result = hex_push_integer(ctx, a->data.int_value / b->data.int_value); + HEX_FREE(ctx, a); + HEX_FREE(ctx, b); + return result; } hex_error(ctx, "[symbol /] Two integers required"); HEX_FREE(ctx, a);@@ -411,28 +578,47 @@ }
int hex_symbol_modulo(hex_context_t *ctx) { - + HEX_ALLOC(b); + if (!b) + { + hex_error(ctx, "[symbol %%] Memory allocation failed"); + return 1; + } HEX_POP(ctx, b); - if (b.type == HEX_TYPE_INVALID) + ; + if (b->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, b); return 1; } + HEX_ALLOC(a); + if (!a) + { + hex_error(ctx, "[symbol %%] Memory allocation failed"); + HEX_FREE(ctx, b); + return 1; + } HEX_POP(ctx, a); - if (a.type == HEX_TYPE_INVALID) + ; + if (a->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, a); HEX_FREE(ctx, b); return 1; } - if (a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) + if (a->type == HEX_TYPE_INTEGER && b->type == HEX_TYPE_INTEGER) { - if (b.data.int_value == 0) + if (b->data.int_value == 0) { hex_error(ctx, "[symbol %%] Division by zero"); + HEX_FREE(ctx, a); + HEX_FREE(ctx, b); return 1; } - return hex_push_integer(ctx, a.data.int_value % b.data.int_value); + int result = hex_push_integer(ctx, a->data.int_value % b->data.int_value); + HEX_FREE(ctx, a); + HEX_FREE(ctx, b); + return result; } hex_error(ctx, "[symbol %%] Two integers required"); HEX_FREE(ctx, a);@@ -444,23 +630,40 @@ // Bit symbols
int hex_symbol_bitand(hex_context_t *ctx) { - + HEX_ALLOC(right); + if (!right) + { + hex_error(ctx, "[symbol &] Memory allocation failed"); + return 1; + } HEX_POP(ctx, right); - if (right.type == HEX_TYPE_INVALID) + ; + if (right->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, right); return 1; } + HEX_ALLOC(left); + if (!left) + { + hex_error(ctx, "[symbol &] Memory allocation failed"); + HEX_FREE(ctx, right); + return 1; + } HEX_POP(ctx, left); - if (left.type == HEX_TYPE_INVALID) + ; + if (left->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, left); HEX_FREE(ctx, right); return 1; } - if (left.type == HEX_TYPE_INTEGER && right.type == HEX_TYPE_INTEGER) + if (left->type == HEX_TYPE_INTEGER && right->type == HEX_TYPE_INTEGER) { - return hex_push_integer(ctx, left.data.int_value & right.data.int_value); + int result = hex_push_integer(ctx, left->data.int_value & right->data.int_value); + HEX_FREE(ctx, left); + HEX_FREE(ctx, right); + return result; } hex_error(ctx, "[symbol &] Two integers required"); HEX_FREE(ctx, left);@@ -470,23 +673,40 @@ }
int hex_symbol_bitor(hex_context_t *ctx) { - + HEX_ALLOC(right); + if (!right) + { + hex_error(ctx, "[symbol |] Memory allocation failed"); + return 1; + } HEX_POP(ctx, right); - if (right.type == HEX_TYPE_INVALID) + ; + if (right->type == HEX_TYPE_INVALID) + { + HEX_FREE(ctx, right); + return 1; + } + HEX_ALLOC(left); + if (!left) { + hex_error(ctx, "[symbol |] Memory allocation failed"); HEX_FREE(ctx, right); return 1; } HEX_POP(ctx, left); - if (left.type == HEX_TYPE_INVALID) + ; + if (left->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, left); HEX_FREE(ctx, right); return 1; } - if (left.type == HEX_TYPE_INTEGER && right.type == HEX_TYPE_INTEGER) + if (left->type == HEX_TYPE_INTEGER && right->type == HEX_TYPE_INTEGER) { - return hex_push_integer(ctx, left.data.int_value | right.data.int_value); + int result = hex_push_integer(ctx, left->data.int_value | right->data.int_value); + HEX_FREE(ctx, left); + HEX_FREE(ctx, right); + return result; } hex_error(ctx, "[symbol |] Two integers required"); HEX_FREE(ctx, left);@@ -496,23 +716,40 @@ }
int hex_symbol_bitxor(hex_context_t *ctx) { - + HEX_ALLOC(right); + if (!right) + { + hex_error(ctx, "[symbol ^] Memory allocation failed"); + return 1; + } HEX_POP(ctx, right); - if (right.type == HEX_TYPE_INVALID) + ; + if (right->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, right); return 1; } + HEX_ALLOC(left); + if (!left) + { + hex_error(ctx, "[symbol ^] Memory allocation failed"); + HEX_FREE(ctx, right); + return 1; + } HEX_POP(ctx, left); - if (left.type == HEX_TYPE_INVALID) + ; + if (left->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, left); HEX_FREE(ctx, right); return 1; } - if (left.type == HEX_TYPE_INTEGER && right.type == HEX_TYPE_INTEGER) + if (left->type == HEX_TYPE_INTEGER && right->type == HEX_TYPE_INTEGER) { - return hex_push_integer(ctx, left.data.int_value ^ right.data.int_value); + int result = hex_push_integer(ctx, left->data.int_value ^ right->data.int_value); + HEX_FREE(ctx, left); + HEX_FREE(ctx, right); + return result; } hex_error(ctx, "[symbol ^] Two integers required"); HEX_FREE(ctx, left);@@ -522,23 +759,40 @@ }
int hex_symbol_shiftleft(hex_context_t *ctx) { - + HEX_ALLOC(right); + if (!right) + { + hex_error(ctx, "[symbol <<] Memory allocation failed"); + return 1; + } HEX_POP(ctx, right); - if (right.type == HEX_TYPE_INVALID) + ; + if (right->type == HEX_TYPE_INVALID) { + HEX_FREE(ctx, right); + return 1; + } + HEX_ALLOC(left); + if (!left) + { + hex_error(ctx, "[symbol <<] Memory allocation failed"); HEX_FREE(ctx, right); return 1; } HEX_POP(ctx, left); - if (left.type == HEX_TYPE_INVALID) + ; + if (left->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, left); HEX_FREE(ctx, right); return 1; } - if (left.type == HEX_TYPE_INTEGER && right.type == HEX_TYPE_INTEGER) + if (left->type == HEX_TYPE_INTEGER && right->type == HEX_TYPE_INTEGER) { - return hex_push_integer(ctx, left.data.int_value << right.data.int_value); + int result = hex_push_integer(ctx, left->data.int_value << right->data.int_value); + HEX_FREE(ctx, left); + HEX_FREE(ctx, right); + return result; } hex_error(ctx, "[symbol <<] Two integers required"); HEX_FREE(ctx, left);@@ -548,23 +802,40 @@ }
int hex_symbol_shiftright(hex_context_t *ctx) { - + HEX_ALLOC(right); + if (!right) + { + hex_error(ctx, "[symbol >>] Memory allocation failed"); + return 1; + } HEX_POP(ctx, right); - if (right.type == HEX_TYPE_INVALID) + ; + if (right->type == HEX_TYPE_INVALID) + { + HEX_FREE(ctx, right); + return 1; + } + HEX_ALLOC(left); + if (!left) { + hex_error(ctx, "[symbol >>] Memory allocation failed"); HEX_FREE(ctx, right); return 1; } HEX_POP(ctx, left); - if (left.type == HEX_TYPE_INVALID) + ; + if (left->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, left); HEX_FREE(ctx, right); return 1; } - if (left.type == HEX_TYPE_INTEGER && right.type == HEX_TYPE_INTEGER) + if (left->type == HEX_TYPE_INTEGER && right->type == HEX_TYPE_INTEGER) { - return hex_push_integer(ctx, left.data.int_value >> right.data.int_value); + int result = hex_push_integer(ctx, left->data.int_value >> right->data.int_value); + HEX_FREE(ctx, left); + HEX_FREE(ctx, right); + return result; } hex_error(ctx, "[symbol >>] Two integers required"); HEX_FREE(ctx, left);@@ -574,16 +845,24 @@ }
int hex_symbol_bitnot(hex_context_t *ctx) { - + HEX_ALLOC(item); + if (!item) + { + hex_error(ctx, "[symbol ~] Memory allocation failed"); + return 1; + } HEX_POP(ctx, item); - if (item.type == HEX_TYPE_INVALID) + ; + if (item->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, item); return 1; } - if (item.type == HEX_TYPE_INTEGER) + if (item->type == HEX_TYPE_INTEGER) { - return hex_push_integer(ctx, ~item.data.int_value); + int result = hex_push_integer(ctx, ~item->data.int_value); + HEX_FREE(ctx, item); + return result; } hex_error(ctx, "[symbol ~] Integer required"); HEX_FREE(ctx, item);@@ -594,16 +873,24 @@ // Conversion symbols
int hex_symbol_int(hex_context_t *ctx) { - + HEX_ALLOC(a); + if (!a) + { + hex_error(ctx, "[symbol int] Memory allocation failed"); + return 1; + } HEX_POP(ctx, a); - if (a.type == HEX_TYPE_INVALID) + ; + if (a->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, a); return 1; } - if (a.type == HEX_TYPE_STRING) + if (a->type == HEX_TYPE_STRING) { - return hex_push_integer(ctx, strtol(a.data.str_value, NULL, 16)); + int result = hex_push_integer(ctx, strtol(a->data.str_value, NULL, 16)); + HEX_FREE(ctx, a); + return result; } hex_error(ctx, "[symbol int] String representing a hexadecimal integer required"); HEX_FREE(ctx, a);@@ -612,16 +899,24 @@ }
int hex_symbol_str(hex_context_t *ctx) { - + HEX_ALLOC(a); + if (!a) + { + hex_error(ctx, "[symbol str] Memory allocation failed"); + return 1; + } HEX_POP(ctx, a); - if (a.type == HEX_TYPE_INVALID) + ; + if (a->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, a); return 1; } - if (a.type == HEX_TYPE_INTEGER) + if (a->type == HEX_TYPE_INTEGER) { - return hex_push_string(ctx, hex_itoa_hex(a.data.int_value)); + int result = hex_push_string(ctx, hex_itoa_hex(a->data.int_value)); + HEX_FREE(ctx, a); + return result; } hex_error(ctx, "[symbol str] Integer required"); HEX_FREE(ctx, a);@@ -630,17 +925,26 @@ }
int hex_symbol_dec(hex_context_t *ctx) { + HEX_ALLOC(a); + if (!a) + { + hex_error(ctx, "[symbol dec] Memory allocation failed"); + return 1; + } HEX_POP(ctx, a); - if (a.type == HEX_TYPE_INVALID) + ; + if (a->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, a); return 1; } - if (a.type == HEX_TYPE_INTEGER) + if (a->type == HEX_TYPE_INTEGER) { char buffer[32]; - snprintf(buffer, sizeof(buffer), "%d", a.data.int_value); - return hex_push_string(ctx, buffer); + snprintf(buffer, sizeof(buffer), "%d", a->data.int_value); + int result = hex_push_string(ctx, buffer); + HEX_FREE(ctx, a); + return result; } hex_error(ctx, "[symbol dec] Integer required"); HEX_FREE(ctx, a);@@ -649,16 +953,24 @@ }
int hex_symbol_hex(hex_context_t *ctx) { - + HEX_ALLOC(item); + if (!item) + { + hex_error(ctx, "[symbol hex] Memory allocation failed"); + return 1; + } HEX_POP(ctx, item); - if (item.type == HEX_TYPE_INVALID) + ; + if (item->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, item); return 1; } - if (item.type == HEX_TYPE_STRING) + if (item->type == HEX_TYPE_STRING) { - return hex_push_integer(ctx, strtol(item.data.str_value, NULL, 10)); + int result = hex_push_integer(ctx, strtol(item->data.str_value, NULL, 10)); + HEX_FREE(ctx, item); + return result; } hex_error(ctx, "[symbol hex] String representing a decimal integer required"); HEX_FREE(ctx, item);@@ -667,27 +979,39 @@ }
int hex_symbol_ord(hex_context_t *ctx) { + HEX_ALLOC(item); + if (!item) + { + hex_error(ctx, "[symbol ord] Memory allocation failed"); + return 1; + } HEX_POP(ctx, item); - if (item.type == HEX_TYPE_INVALID) + ; + if (item->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, item); return 1; } - if (item.type == HEX_TYPE_STRING) + if (item->type == HEX_TYPE_STRING) { - if (strlen(item.data.str_value) > 1) + if (strlen(item->data.str_value) > 1) { - return hex_push_integer(ctx, -1); + int result = hex_push_integer(ctx, -1); + HEX_FREE(ctx, item); + return result; } - unsigned char *str = (unsigned char *)item.data.str_value; + unsigned char *str = (unsigned char *)item->data.str_value; + int result; if (str[0] < 128) { - return hex_push_integer(ctx, str[0]); + result = hex_push_integer(ctx, str[0]); } else { - return hex_push_integer(ctx, -1); + result = hex_push_integer(ctx, -1); } + HEX_FREE(ctx, item); + return result; } hex_error(ctx, "[symbol ord] String required"); HEX_FREE(ctx, item);@@ -696,23 +1020,33 @@ }
int hex_symbol_chr(hex_context_t *ctx) { + HEX_ALLOC(item); + if (!item) + { + hex_error(ctx, "[symbol chr] Memory allocation failed"); + return 1; + } HEX_POP(ctx, item); - if (item.type == HEX_TYPE_INVALID) + ; + if (item->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, item); return 1; } - if (item.type == HEX_TYPE_INTEGER) + if (item->type == HEX_TYPE_INTEGER) { - if (item.data.int_value >= 0 && item.data.int_value < 128) + int result; + if (item->data.int_value >= 0 && item->data.int_value < 128) { - char str[2] = {(char)item.data.int_value, '\0'}; - return hex_push_string(ctx, str); + char str[2] = {(char)item->data.int_value, '\0'}; + result = hex_push_string(ctx, str); } else { - return hex_push_string(ctx, ""); + result = hex_push_string(ctx, ""); } + HEX_FREE(ctx, item); + return result; } hex_error(ctx, "[symbol chr] Integer required"); HEX_FREE(ctx, item);@@ -721,39 +1055,39 @@ }
// Comparison symbols -static int hex_equal(hex_item_t a, hex_item_t b) +static int hex_equal(hex_item_t *a, hex_item_t *b) { - if (a.type == HEX_TYPE_INVALID || b.type == HEX_TYPE_INVALID) + if (a->type == HEX_TYPE_INVALID || b->type == HEX_TYPE_INVALID) { return 0; } - if (a.type == HEX_TYPE_NATIVE_SYMBOL || a.type == HEX_TYPE_USER_SYMBOL) + if (a->type == HEX_TYPE_NATIVE_SYMBOL || a->type == HEX_TYPE_USER_SYMBOL) { - return (strcmp(a.token->value, b.token->value) == 0); + return (strcmp(a->token->value, b->token->value) == 0); } - if (a.type != b.type) + if (a->type != b->type) { return 0; } - if (a.type == HEX_TYPE_INTEGER) + if (a->type == HEX_TYPE_INTEGER) { - return a.data.int_value == b.data.int_value; + return a->data.int_value == b->data.int_value; } - if (a.type == HEX_TYPE_STRING) + if (a->type == HEX_TYPE_STRING) { - return (strcmp(a.data.str_value, b.data.str_value) == 0); + return (strcmp(a->data.str_value, b->data.str_value) == 0); } - if (a.type == HEX_TYPE_QUOTATION) + if (a->type == HEX_TYPE_QUOTATION) { - if (a.quotation_size != b.quotation_size) + if (a->quotation_size != b->quotation_size) { return 0; } else { - for (size_t i = 0; i < a.quotation_size; i++) + for (size_t i = 0; i < a->quotation_size; i++) { - if (!hex_equal(*a.data.quotation_value[i], *b.data.quotation_value[i])) + if (!hex_equal(a->data.quotation_value[i], b->data.quotation_value[i])) { return 0; }@@ -851,160 +1185,191 @@
int hex_symbol_equal(hex_context_t *ctx) { + HEX_ALLOC(b); HEX_POP(ctx, b); - if (b.type == HEX_TYPE_INVALID) + if (b->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, b); return 1; } + HEX_ALLOC(a) HEX_POP(ctx, a); - if (a.type == HEX_TYPE_INVALID) + if (a->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, a); HEX_FREE(ctx, b); return 1; } - if ((a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) || (a.type == HEX_TYPE_STRING && b.type == HEX_TYPE_STRING) || (a.type == HEX_TYPE_QUOTATION && b.type == HEX_TYPE_QUOTATION)) + if ((a->type == HEX_TYPE_INTEGER && b->type == HEX_TYPE_INTEGER) || (a->type == HEX_TYPE_STRING && b->type == HEX_TYPE_STRING) || (a->type == HEX_TYPE_QUOTATION && b->type == HEX_TYPE_QUOTATION)) { - return hex_push_integer(ctx, hex_equal(a, b)); + int result = hex_push_integer(ctx, hex_equal(a, b)); + HEX_FREE(ctx, a); + HEX_FREE(ctx, b); + return result; } // Different types => false - return hex_push_integer(ctx, 0); + int result = hex_push_integer(ctx, 0); + HEX_FREE(ctx, a); + HEX_FREE(ctx, b); + return result; } int hex_symbol_notequal(hex_context_t *ctx) { - + HEX_ALLOC(b); HEX_POP(ctx, b); - if (b.type == HEX_TYPE_INVALID) + if (b->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, b); return 1; } + HEX_ALLOC(a); HEX_POP(ctx, a); - if (a.type == HEX_TYPE_INVALID) + if (a->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, a); HEX_FREE(ctx, b); return 1; } - if ((a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) || (a.type == HEX_TYPE_STRING && b.type == HEX_TYPE_STRING) || (a.type == HEX_TYPE_QUOTATION && b.type == HEX_TYPE_QUOTATION)) + if ((a->type == HEX_TYPE_INTEGER && b->type == HEX_TYPE_INTEGER) || (a->type == HEX_TYPE_STRING && b->type == HEX_TYPE_STRING) || (a->type == HEX_TYPE_QUOTATION && b->type == HEX_TYPE_QUOTATION)) { - return hex_push_integer(ctx, !hex_equal(a, b)); + int result = hex_push_integer(ctx, !hex_equal(a, b)); + HEX_FREE(ctx, a); + HEX_FREE(ctx, b); + return result; } // Different types => true - return hex_push_integer(ctx, 1); + int result = hex_push_integer(ctx, 1); + HEX_FREE(ctx, a); + HEX_FREE(ctx, b); + return result; } int hex_symbol_greater(hex_context_t *ctx) { + HEX_ALLOC(b); HEX_POP(ctx, b); - if (b.type == HEX_TYPE_INVALID) + if (b->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, b); return 1; } - + HEX_ALLOC(a); HEX_POP(ctx, a); - if (a.type == HEX_TYPE_INVALID) + if (a->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, a); HEX_FREE(ctx, b); return 1; } - hex_item_t *pa = &a; - hex_item_t *pb = &b; - hex_push_integer(ctx, hex_greater(ctx, pa, pb, ">")); - return 0; + hex_item_t *pa = a; + hex_item_t *pb = b; + int result = hex_push_integer(ctx, hex_greater(ctx, pa, pb, ">")); + HEX_FREE(ctx, a); + HEX_FREE(ctx, b); + return result; } int hex_symbol_less(hex_context_t *ctx) { - + HEX_ALLOC(b); HEX_POP(ctx, b); - if (b.type == HEX_TYPE_INVALID) + if (b->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, b); return 1; } + HEX_ALLOC(a); HEX_POP(ctx, a); - if (a.type == HEX_TYPE_INVALID) + if (a->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, a); HEX_FREE(ctx, b); return 1; } - hex_item_t *pa = &a; - hex_item_t *pb = &b; - hex_push_integer(ctx, hex_greater(ctx, pb, pa, "<")); - return 0; + hex_item_t *pa = a; + hex_item_t *pb = b; + int result = hex_push_integer(ctx, hex_greater(ctx, pb, pa, "<")); + HEX_FREE(ctx, a); + HEX_FREE(ctx, b); + return result; } int hex_symbol_greaterequal(hex_context_t *ctx) { - + HEX_ALLOC(b); HEX_POP(ctx, b); - if (b.type == HEX_TYPE_INVALID) + if (b->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, b); return 1; } + HEX_ALLOC(a); HEX_POP(ctx, a); - if (a.type == HEX_TYPE_INVALID) + if (a->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, a); HEX_FREE(ctx, b); return 1; } - hex_item_t *pa = &a; - hex_item_t *pb = &b; - hex_push_integer(ctx, hex_greater(ctx, pa, pb, ">") || hex_equal(a, b)); - return 0; + hex_item_t *pa = a; + hex_item_t *pb = b; + int result = hex_push_integer(ctx, hex_greater(ctx, pa, pb, ">") || hex_equal(a, b)); + HEX_FREE(ctx, a); + HEX_FREE(ctx, b); + return result; } int hex_symbol_lessequal(hex_context_t *ctx) { - + HEX_ALLOC(b); HEX_POP(ctx, b); - if (b.type == HEX_TYPE_INVALID) + if (b->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, b); return 1; } + HEX_ALLOC(a); HEX_POP(ctx, a); - if (a.type == HEX_TYPE_INVALID) + if (a->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, a); HEX_FREE(ctx, b); return 1; } - hex_item_t *pa = &a; - hex_item_t *pb = &b; - hex_push_integer(ctx, hex_greater(ctx, pb, pa, "<") || hex_equal(a, b)); - return 0; + hex_item_t *pa = a; + hex_item_t *pb = b; + int result = hex_push_integer(ctx, hex_greater(ctx, pb, pa, "<") || hex_equal(a, b)); + HEX_FREE(ctx, a); + HEX_FREE(ctx, b); + return result; } // Boolean symbols int hex_symbol_and(hex_context_t *ctx) { - + HEX_ALLOC(b); HEX_POP(ctx, b); - if (b.type == HEX_TYPE_INVALID) + if (b->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, b); return 1; } + HEX_ALLOC(a); HEX_POP(ctx, a); - if (a.type == HEX_TYPE_INVALID) + if (a->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, a); HEX_FREE(ctx, b); return 1; } - if (a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) + if (a->type == HEX_TYPE_INTEGER && b->type == HEX_TYPE_INTEGER) { - return hex_push_integer(ctx, a.data.int_value && b.data.int_value); + int result = hex_push_integer(ctx, a->data.int_value && b->data.int_value); + HEX_FREE(ctx, a); + HEX_FREE(ctx, b); + return result; } hex_error(ctx, "[symbol and] Two integers required"); HEX_FREE(ctx, a);@@ -1014,23 +1379,27 @@ }
int hex_symbol_or(hex_context_t *ctx) { - + HEX_ALLOC(b); HEX_POP(ctx, b); - if (b.type == HEX_TYPE_INVALID) + if (b->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, b); return 1; } + HEX_ALLOC(a); HEX_POP(ctx, a); - if (a.type == HEX_TYPE_INVALID) + if (a->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, a); HEX_FREE(ctx, b); return 1; } - if (a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) + if (a->type == HEX_TYPE_INTEGER && b->type == HEX_TYPE_INTEGER) { - return hex_push_integer(ctx, a.data.int_value || b.data.int_value); + int result = hex_push_integer(ctx, a->data.int_value || b->data.int_value); + HEX_FREE(ctx, a); + HEX_FREE(ctx, b); + return result; } hex_error(ctx, "[symbol or] Two integers required"); HEX_FREE(ctx, a);@@ -1040,16 +1409,23 @@ }
int hex_symbol_not(hex_context_t *ctx) { - + HEX_ALLOC(a); + if (!a) + { + hex_error(ctx, "[symbol not] Memory allocation failed"); + return 1; + } HEX_POP(ctx, a); - if (a.type == HEX_TYPE_INVALID) + if (a->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, a); return 1; } - if (a.type == HEX_TYPE_INTEGER) + if (a->type == HEX_TYPE_INTEGER) { - return hex_push_integer(ctx, !a.data.int_value); + int result = hex_push_integer(ctx, !a->data.int_value); + HEX_FREE(ctx, a); + return result; } hex_error(ctx, "[symbol not] Integer required"); HEX_FREE(ctx, a);@@ -1058,23 +1434,40 @@ }
int hex_symbol_xor(hex_context_t *ctx) { - + HEX_ALLOC(b); + if (!b) + { + hex_error(ctx, "[symbol xor] Memory allocation failed"); + return 1; + } HEX_POP(ctx, b); - if (b.type == HEX_TYPE_INVALID) + ; + if (b->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, b); return 1; } + HEX_ALLOC(a); + if (!a) + { + hex_error(ctx, "[symbol xor] Memory allocation failed"); + HEX_FREE(ctx, b); + return 1; + } HEX_POP(ctx, a); - if (a.type == HEX_TYPE_INVALID) + ; + if (a->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, a); HEX_FREE(ctx, b); return 1; } - if (a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) + if (a->type == HEX_TYPE_INTEGER && b->type == HEX_TYPE_INTEGER) { - return hex_push_integer(ctx, a.data.int_value ^ b.data.int_value); + int result = hex_push_integer(ctx, a->data.int_value ^ b->data.int_value); + HEX_FREE(ctx, a); + HEX_FREE(ctx, b); + return result; } hex_error(ctx, "[symbol xor] Two integers required"); HEX_FREE(ctx, a);@@ -1083,19 +1476,31 @@ return 1;
} // Quotation and String (List) Symbols - int hex_symbol_cat(hex_context_t *ctx) { - + HEX_ALLOC(value); + if (!value) + { + hex_error(ctx, "[symbol cat] Memory allocation failed"); + return 1; + } HEX_POP(ctx, value); - if (value.type == HEX_TYPE_INVALID) + if (value->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, value); return 1; // Failed to pop value } + HEX_ALLOC(list); + if (!list) + { + hex_error(ctx, "[symbol cat] Memory allocation failed"); + HEX_FREE(ctx, value); + return 1; + } HEX_POP(ctx, list); - if (list.type == HEX_TYPE_INVALID) + ; + if (list->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, list); HEX_FREE(ctx, value);@@ -1104,12 +1509,12 @@ }
int result = 0; - if (list.type == HEX_TYPE_QUOTATION && value.type == HEX_TYPE_QUOTATION) + if (list->type == HEX_TYPE_QUOTATION && value->type == HEX_TYPE_QUOTATION) { // Concatenate two quotations - size_t newSize = list.quotation_size + value.quotation_size; + size_t newSize = list->quotation_size + value->quotation_size; hex_item_t **newQuotation = (hex_item_t **)realloc( - list.data.quotation_value, newSize * sizeof(hex_item_t *)); + list->data.quotation_value, newSize * sizeof(hex_item_t *)); if (!newQuotation) { hex_error(ctx, "[symbol cat] Memory allocation failed");@@ -1118,20 +1523,20 @@ }
else { // Append items from the second quotation - for (size_t i = 0; i < (size_t)value.quotation_size; i++) + for (size_t i = 0; i < (size_t)value->quotation_size; i++) { - newQuotation[list.quotation_size + i] = value.data.quotation_value[i]; + newQuotation[list->quotation_size + i] = value->data.quotation_value[i]; } - list.data.quotation_value = newQuotation; - list.quotation_size = newSize; - result = hex_push_quotation(ctx, list.data.quotation_value, newSize); + list->data.quotation_value = newQuotation; + list->quotation_size = newSize; + result = hex_push_quotation(ctx, list->data.quotation_value, newSize); } } - else if (list.type == HEX_TYPE_STRING && value.type == HEX_TYPE_STRING) + else if (list->type == HEX_TYPE_STRING && value->type == HEX_TYPE_STRING) { // Concatenate two strings - size_t newLength = strlen(list.data.str_value) + strlen(value.data.str_value) + 1; + size_t newLength = strlen(list->data.str_value) + strlen(value->data.str_value) + 1; char *newStr = (char *)malloc(newLength); if (!newStr) {@@ -1140,8 +1545,8 @@ result = 1;
} else { - strcpy(newStr, list.data.str_value); - strcat(newStr, value.data.str_value); + strcpy(newStr, list->data.str_value); + strcat(newStr, value->data.str_value); result = hex_push_string(ctx, newStr); } }@@ -1164,20 +1569,27 @@
int hex_symbol_len(hex_context_t *ctx) { + HEX_ALLOC(item); + if (!item) + { + hex_error(ctx, "[symbol len] Memory allocation failed"); + return 1; + } HEX_POP(ctx, item); - if (item.type == HEX_TYPE_INVALID) + ; + if (item->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, item); return 1; } int result = 0; - if (item.type == HEX_TYPE_QUOTATION) + if (item->type == HEX_TYPE_QUOTATION) { - result = hex_push_integer(ctx, item.quotation_size); + result = hex_push_integer(ctx, item->quotation_size); } - else if (item.type == HEX_TYPE_STRING) + else if (item->type == HEX_TYPE_STRING) { - result = hex_push_integer(ctx, strlen(item.data.str_value)); + result = hex_push_integer(ctx, strlen(item->data.str_value)); } else {@@ -1194,14 +1606,29 @@
int hex_symbol_get(hex_context_t *ctx) { + HEX_ALLOC(index); + if (!index) + { + hex_error(ctx, "[symbol get] Memory allocation failed"); + return 1; + } HEX_POP(ctx, index); - if (index.type == HEX_TYPE_INVALID) + ; + if (index->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, index); return 1; } + HEX_ALLOC(list); + if (!list) + { + hex_error(ctx, "[symbol get] Memory allocation failed"); + HEX_FREE(ctx, index); + return 1; + } HEX_POP(ctx, list); - if (list.type == HEX_TYPE_INVALID) + ; + if (list->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, list); HEX_FREE(ctx, index);@@ -1209,41 +1636,41 @@ return 1;
} int result = 0; hex_item_t copy; - if (list.type == HEX_TYPE_QUOTATION) + if (list->type == HEX_TYPE_QUOTATION) { - if (index.type != HEX_TYPE_INTEGER) + if (index->type != HEX_TYPE_INTEGER) { hex_error(ctx, "[symbol get] Index must be an integer"); result = 1; } - else if (index.data.int_value < 0 || (size_t)index.data.int_value >= list.quotation_size) + else if (index->data.int_value < 0 || (size_t)index->data.int_value >= list->quotation_size) { hex_error(ctx, "[symbol get] Index out of range"); result = 1; } else { - copy = hex_copy_item(list.data.quotation_value[index.data.int_value]); - result = hex_push(ctx, copy); + copy = *hex_copy_item(list->data.quotation_value[index->data.int_value]); + result = hex_push(ctx, ©); } } - else if (list.type == HEX_TYPE_STRING) + else if (list->type == HEX_TYPE_STRING) { - if (index.type != HEX_TYPE_INTEGER) + if (index->type != HEX_TYPE_INTEGER) { hex_error(ctx, "[symbol get] Index must be an integer"); result = 1; } - else if (index.data.int_value < 0 || index.data.int_value >= (int)strlen(list.data.str_value)) + else if (index->data.int_value < 0 || index->data.int_value >= (int)strlen(list->data.str_value)) { hex_error(ctx, "[symbol get] Index out of range"); result = 1; } else { - char str[2] = {list.data.str_value[index.data.int_value], '\0'}; - copy = hex_string_item(ctx, str); - result = hex_push(ctx, copy); + char str[2] = {list->data.str_value[index->data.int_value], '\0'}; + copy = *hex_string_item(ctx, str); + result = hex_push(ctx, ©); } } else@@ -1253,48 +1680,47 @@ result = 1;
} if (result != 0) { - HEX_FREE(ctx, list); HEX_FREE(ctx, index); - HEX_FREE(ctx, copy); } return result; } int hex_symbol_index(hex_context_t *ctx) { - + HEX_ALLOC(item); HEX_POP(ctx, item); - if (item.type == HEX_TYPE_INVALID) + if (item->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, item); return 1; } + HEX_ALLOC(list); HEX_POP(ctx, list); - if (list.type == HEX_TYPE_INVALID) + if (list->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, list); HEX_FREE(ctx, item); return 1; } int result = -1; - if (list.type == HEX_TYPE_QUOTATION) + if (list->type == HEX_TYPE_QUOTATION) { - for (size_t i = 0; i < list.quotation_size; i++) + for (size_t i = 0; i < list->quotation_size; i++) { - if (hex_equal(*list.data.quotation_value[i], item)) + if (hex_equal(list->data.quotation_value[i], item)) { result = i; break; } } } - else if (list.type == HEX_TYPE_STRING) + else if (list->type == HEX_TYPE_STRING) { - char *ptr = strstr(list.data.str_value, item.data.str_value); + char *ptr = strstr(list->data.str_value, item->data.str_value); if (ptr) { - result = ptr - list.data.str_value; + result = ptr - list->data.str_value; } } else@@ -1311,29 +1737,43 @@ // String symbols
int hex_symbol_join(hex_context_t *ctx) { - + HEX_ALLOC(separator); + if (!separator) + { + hex_error(ctx, "[symbol join] Memory allocation failed"); + return 1; + } HEX_POP(ctx, separator); - if (separator.type == HEX_TYPE_INVALID) + ; + if (separator->type == HEX_TYPE_INVALID) + { + HEX_FREE(ctx, separator); + return 1; + } + HEX_ALLOC(list); + if (!list) { + hex_error(ctx, "[symbol join] Memory allocation failed"); HEX_FREE(ctx, separator); return 1; } HEX_POP(ctx, list); - if (list.type == HEX_TYPE_INVALID) + ; + if (list->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, list); HEX_FREE(ctx, separator); return 1; } int result = 0; - if (list.type == HEX_TYPE_QUOTATION && separator.type == HEX_TYPE_STRING) + if (list->type == HEX_TYPE_QUOTATION && separator->type == HEX_TYPE_STRING) { int length = 0; - for (size_t i = 0; i < list.quotation_size; i++) + for (size_t i = 0; i < list->quotation_size; i++) { - if (list.data.quotation_value[i]->type == HEX_TYPE_STRING) + if (list->data.quotation_value[i]->type == HEX_TYPE_STRING) { - length += strlen(list.data.quotation_value[i]->data.str_value); + length += strlen(list->data.quotation_value[i]->data.str_value); } else {@@ -1345,7 +1785,7 @@ }
} if (result == 0) { - length += (list.quotation_size - 1) * strlen(separator.data.str_value); + length += (list->quotation_size - 1) * strlen(separator->data.str_value); char *newStr = (char *)malloc(length + 1); if (!newStr) {@@ -1355,12 +1795,12 @@ HEX_FREE(ctx, separator);
return 1; } newStr[0] = '\0'; - for (size_t i = 0; i < list.quotation_size; i++) + for (size_t i = 0; i < list->quotation_size; i++) { - strcat(newStr, list.data.quotation_value[i]->data.str_value); - if (i < list.quotation_size - 1) + strcat(newStr, list->data.quotation_value[i]->data.str_value); + if (i < list->quotation_size - 1) { - strcat(newStr, separator.data.str_value); + strcat(newStr, separator->data.str_value); } } result = hex_push_string(ctx, newStr);@@ -1381,26 +1821,41 @@ }
int hex_symbol_split(hex_context_t *ctx) { + HEX_ALLOC(separator); + if (!separator) + { + hex_error(ctx, "[symbol split] Memory allocation failed"); + return 1; + } HEX_POP(ctx, separator); - if (separator.type == HEX_TYPE_INVALID) + ; + if (separator->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, separator); return 1; } + HEX_ALLOC(str); + if (!str) + { + hex_error(ctx, "[symbol split] Memory allocation failed"); + HEX_FREE(ctx, separator); + return 1; + } HEX_POP(ctx, str); - if (str.type == HEX_TYPE_INVALID) + ; + if (str->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, str); HEX_FREE(ctx, separator); return 1; } int result = 0; - if (str.type == HEX_TYPE_STRING && separator.type == HEX_TYPE_STRING) + if (str->type == HEX_TYPE_STRING && separator->type == HEX_TYPE_STRING) { - if (strlen(separator.data.str_value) == 0) + if (strlen(separator->data.str_value) == 0) { // Separator is an empty string: split into individual characters - size_t size = strlen(str.data.str_value); + size_t size = strlen(str->data.str_value); hex_item_t **quotation = (hex_item_t **)malloc(size * sizeof(hex_item_t *)); if (!quotation) {@@ -1426,8 +1881,8 @@ hex_error(ctx, "[symbol split] Memory allocation failed");
result = 1; break; } - quotation[i]->data.str_value[0] = str.data.str_value[i]; // Copy the single character - quotation[i]->data.str_value[1] = '\0'; // Null-terminate the string + quotation[i]->data.str_value[0] = str->data.str_value[i]; // Copy the single character + quotation[i]->data.str_value[1] = '\0'; // Null-terminate the string } if (result == 0) {@@ -1438,7 +1893,7 @@ }
else { // Separator is not empty: split as usual - char *token = strtok(str.data.str_value, separator.data.str_value); + char *token = strtok(str->data.str_value, separator->data.str_value); size_t capacity = 2; size_t size = 0; hex_item_t **quotation = (hex_item_t **)malloc(capacity * sizeof(hex_item_t *));@@ -1466,7 +1921,7 @@ quotation[size] = (hex_item_t *)malloc(sizeof(hex_item_t));
quotation[size]->type = HEX_TYPE_STRING; quotation[size]->data.str_value = strdup(token); size++; - token = strtok(NULL, separator.data.str_value); + token = strtok(NULL, separator->data.str_value); } if (result == 0) {@@ -1490,22 +1945,24 @@ }
int hex_symbol_replace(hex_context_t *ctx) { - + HEX_ALLOC(replacement); HEX_POP(ctx, replacement); - if (replacement.type == HEX_TYPE_INVALID) + if (replacement->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, replacement); return 1; } + HEX_ALLOC(search); HEX_POP(ctx, search); - if (search.type == HEX_TYPE_INVALID) + if (search->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, search); HEX_FREE(ctx, replacement); return 1; } + HEX_ALLOC(list); HEX_POP(ctx, list); - if (list.type == HEX_TYPE_INVALID) + if (list->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, list); HEX_FREE(ctx, search);@@ -1513,11 +1970,11 @@ HEX_FREE(ctx, replacement);
return 1; } int result = 0; - if (list.type == HEX_TYPE_STRING && search.type == HEX_TYPE_STRING && replacement.type == HEX_TYPE_STRING) + if (list->type == HEX_TYPE_STRING && search->type == HEX_TYPE_STRING && replacement->type == HEX_TYPE_STRING) { - char *str = list.data.str_value; - char *find = search.data.str_value; - char *replace = replacement.data.str_value; + char *str = list->data.str_value; + char *find = search->data.str_value; + char *replace = replacement->data.str_value; char *ptr = strstr(str, find); if (ptr) {@@ -1561,19 +2018,26 @@ // File symbols
int hex_symbol_read(hex_context_t *ctx) { + HEX_ALLOC(filename); + if (!filename) + { + hex_error(ctx, "[symbol read] Memory allocation failed"); + return 1; + } HEX_POP(ctx, filename); - if (filename.type == HEX_TYPE_INVALID) + ; + if (filename->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, filename); return 1; } int result = 0; - if (filename.type == HEX_TYPE_STRING) + if (filename->type == HEX_TYPE_STRING) { - FILE *file = fopen(filename.data.str_value, "rb"); + FILE *file = fopen(filename->data.str_value, "rb"); if (!file) { - hex_error(ctx, "[symbol read] Could not open file for reading: %s", filename.data.str_value); + hex_error(ctx, "[symbol read] Could not open file for reading: %s", filename->data.str_value); result = 1; } else@@ -1620,11 +2084,12 @@ result = 1;
} else { - hex_item_t item = {.type = HEX_TYPE_STRING, .data.str_value = str}; + hex_item_t *item = (hex_item_t *)malloc(sizeof(hex_item_t)); + item->type = HEX_TYPE_STRING; + item->data.str_value = str; result = HEX_PUSH(ctx, item); } } - free(buffer); } fclose(file); }@@ -1643,52 +2108,53 @@ }
int hex_symbol_write(hex_context_t *ctx) { - + HEX_ALLOC(filename); HEX_POP(ctx, filename); - if (filename.type == HEX_TYPE_INVALID) + if (filename->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, filename); return 1; } + HEX_ALLOC(data); HEX_POP(ctx, data); - if (data.type == HEX_TYPE_INVALID) + if (data->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, data); HEX_FREE(ctx, filename); return 1; } int result = 0; - if (filename.type == HEX_TYPE_STRING) + if (filename->type == HEX_TYPE_STRING) { - if (data.type == HEX_TYPE_STRING) + if (data->type == HEX_TYPE_STRING) { - FILE *file = fopen(filename.data.str_value, "w"); + FILE *file = fopen(filename->data.str_value, "w"); if (file) { - fputs(data.data.str_value, file); + fputs(data->data.str_value, file); fclose(file); result = 0; } else { - hex_error(ctx, "[symbol write] Could not open file for writing: %s", filename.data.str_value); + hex_error(ctx, "[symbol write] Could not open file for writing: %s", filename->data.str_value); result = 1; } } - else if (data.type == HEX_TYPE_QUOTATION) + else if (data->type == HEX_TYPE_QUOTATION) { - FILE *file = fopen(filename.data.str_value, "wb"); + FILE *file = fopen(filename->data.str_value, "wb"); if (file) { - for (size_t i = 0; i < data.quotation_size; i++) + for (size_t i = 0; i < data->quotation_size; i++) { - if (data.data.quotation_value[i]->type != HEX_TYPE_INTEGER) + if (data->data.quotation_value[i]->type != HEX_TYPE_INTEGER) { hex_error(ctx, "[symbol write] Quotation must contain only integers"); result = 1; break; } - uint8_t byte = (uint8_t)data.data.quotation_value[i]->data.int_value; + uint8_t byte = (uint8_t)data->data.quotation_value[i]->data.int_value; fwrite(&byte, 1, 1, file); } fclose(file);@@ -1696,7 +2162,7 @@ result = 0;
} else { - hex_error(ctx, "[symbol write] Could not open file for writing: %s", filename.data.str_value); + hex_error(ctx, "[symbol write] Could not open file for writing: %s", filename->data.str_value); result = 1; } }@@ -1722,51 +2188,66 @@
int hex_symbol_append(hex_context_t *ctx) { + HEX_ALLOC(filename); + if (!filename) + { + hex_error(ctx, "[symbol append] Memory allocation failed"); + return 1; + } HEX_POP(ctx, filename); - if (filename.type == HEX_TYPE_INVALID) + ; + if (filename->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, filename); return 1; } + HEX_ALLOC(data); + if (!data) + { + hex_error(ctx, "[symbol append] Memory allocation failed"); + HEX_FREE(ctx, filename); + return 1; + } HEX_POP(ctx, data); - if (data.type == HEX_TYPE_INVALID) + ; + if (data->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, data); HEX_FREE(ctx, filename); return 1; } int result = 0; - if (filename.type == HEX_TYPE_STRING) + if (filename->type == HEX_TYPE_STRING) { - if (data.type == HEX_TYPE_STRING) + if (data->type == HEX_TYPE_STRING) { - FILE *file = fopen(filename.data.str_value, "a"); + FILE *file = fopen(filename->data.str_value, "a"); if (file) { - fputs(data.data.str_value, file); + fputs(data->data.str_value, file); fclose(file); result = 0; } else { - hex_error(ctx, "[symbol append] Could not open file for appending: %s", filename.data.str_value); + hex_error(ctx, "[symbol append] Could not open file for appending: %s", filename->data.str_value); result = 1; } } - else if (data.type == HEX_TYPE_QUOTATION) + else if (data->type == HEX_TYPE_QUOTATION) { - FILE *file = fopen(filename.data.str_value, "ab"); + FILE *file = fopen(filename->data.str_value, "ab"); if (file) { - for (size_t i = 0; i < data.quotation_size; i++) + for (size_t i = 0; i < data->quotation_size; i++) { - if (data.data.quotation_value[i]->type != HEX_TYPE_INTEGER) + if (data->data.quotation_value[i]->type != HEX_TYPE_INTEGER) { hex_error(ctx, "[symbol append] Quotation must contain only integers"); result = 1; break; } - uint8_t byte = (uint8_t)data.data.quotation_value[i]->data.int_value; + uint8_t byte = (uint8_t)data->data.quotation_value[i]->data.int_value; fwrite(&byte, 1, 1, file); } fclose(file);@@ -1774,7 +2255,7 @@ result = 0;
} else { - hex_error(ctx, "[symbol append] Could not open file for appending: %s", filename.data.str_value); + hex_error(ctx, "[symbol append] Could not open file for appending: %s", filename->data.str_value); result = 1; } }@@ -1827,37 +2308,50 @@ }
int hex_symbol_exit(hex_context_t *ctx) { - + HEX_ALLOC(item); + if (!item) + { + hex_error(ctx, "[symbol exit] Memory allocation failed"); + return 1; + } HEX_POP(ctx, item); - if (item.type == HEX_TYPE_INVALID) + ; + if (item->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, item); return 1; } - if (item.type != HEX_TYPE_INTEGER) + if (item->type != HEX_TYPE_INTEGER) { hex_error(ctx, "[symbol exit] Integer required"); HEX_FREE(ctx, item); return 1; } - int exit_status = item.data.int_value; + int exit_status = item->data.int_value; + HEX_FREE(ctx, item); exit(exit_status); return 0; // This line will never be reached, but it's here to satisfy the return type } int hex_symbol_exec(hex_context_t *ctx) { - + HEX_ALLOC(command); + if (!command) + { + hex_error(ctx, "[symbol exec] Memory allocation failed"); + return 1; + } HEX_POP(ctx, command); - if (command.type == HEX_TYPE_INVALID) + ; + if (command->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, command); return 1; } int result = 0; - if (command.type == HEX_TYPE_STRING) + if (command->type == HEX_TYPE_STRING) { - int status = system(command.data.str_value); + int status = system(command->data.str_value); result = hex_push_integer(ctx, status); } else@@ -1865,23 +2359,26 @@ {
hex_error(ctx, "[symbol exec] String required"); result = 1; } - if (result != 0) - { - HEX_FREE(ctx, command); - } + HEX_FREE(ctx, command); return result; } int hex_symbol_run(hex_context_t *ctx) { - + HEX_ALLOC(command); + if (!command) + { + hex_error(ctx, "[symbol run] Memory allocation failed"); + return 1; + } HEX_POP(ctx, command); - if (command.type == HEX_TYPE_INVALID) + ; + if (command->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, command); return 1; } - if (command.type != HEX_TYPE_STRING) + if (command->type != HEX_TYPE_STRING) { hex_error(ctx, "[symbol run] String required"); HEX_FREE(ctx, command);@@ -1921,7 +2418,7 @@ si.hStdError = hErrorWrite;
si.dwFlags |= STARTF_USESTDHANDLES; // Create the child process - if (!CreateProcess(NULL, command.data.str_value, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) + if (!CreateProcess(NULL, command->data.str_value, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) { hex_error(ctx, "[symbol run] Failed to create process"); HEX_FREE(ctx, command);@@ -1983,7 +2480,7 @@ close(stdout_pipe[0]);
close(stderr_pipe[0]); dup2(stdout_pipe[1], STDOUT_FILENO); dup2(stderr_pipe[1], STDERR_FILENO); - execl("/bin/sh", "sh", "-c", command.data.str_value, (char *)NULL); + execl("/bin/sh", "sh", "-c", command->data.str_value, (char *)NULL); exit(1); } else@@ -2030,6 +2527,8 @@ quotation[2] = (hex_item_t *)malloc(sizeof(hex_item_t));
quotation[2]->type = HEX_TYPE_STRING; quotation[2]->data.str_value = strdup(error); + HEX_FREE(ctx, command); + return hex_push_quotation(ctx, quotation, 3); }@@ -2037,38 +2536,67 @@ // Control flow symbols
int hex_symbol_if(hex_context_t *ctx) { - + HEX_ALLOC(elseBlock); + if (!elseBlock) + { + hex_error(ctx, "[symbol if] Memory allocation failed"); + return 1; + } HEX_POP(ctx, elseBlock); - if (elseBlock.type == HEX_TYPE_INVALID) + ; + if (elseBlock->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, elseBlock); return 1; } + + HEX_ALLOC(thenBlock); + if (!thenBlock) + { + hex_error(ctx, "[symbol if] Memory allocation failed"); + HEX_FREE(ctx, elseBlock); + return 1; + } HEX_POP(ctx, thenBlock); - if (thenBlock.type == HEX_TYPE_INVALID) + ; + if (thenBlock->type == HEX_TYPE_INVALID) + { + HEX_FREE(ctx, thenBlock); + HEX_FREE(ctx, elseBlock); + return 1; + } + + HEX_ALLOC(condition); + if (!condition) { + hex_error(ctx, "[symbol if] Memory allocation failed"); HEX_FREE(ctx, thenBlock); HEX_FREE(ctx, elseBlock); return 1; } HEX_POP(ctx, condition); - if (condition.type == HEX_TYPE_INVALID) + ; + if (condition->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, condition); HEX_FREE(ctx, thenBlock); HEX_FREE(ctx, elseBlock); return 1; } - if (condition.type != HEX_TYPE_QUOTATION || thenBlock.type != HEX_TYPE_QUOTATION || elseBlock.type != HEX_TYPE_QUOTATION) + + if (condition->type != HEX_TYPE_QUOTATION || thenBlock->type != HEX_TYPE_QUOTATION || elseBlock->type != HEX_TYPE_QUOTATION) { hex_error(ctx, "[symbol if] Three quotations required"); + HEX_FREE(ctx, condition); + HEX_FREE(ctx, thenBlock); + HEX_FREE(ctx, elseBlock); return 1; } else { - for (size_t i = 0; i < condition.quotation_size; i++) + for (size_t i = 0; i < condition->quotation_size; i++) { - if (hex_push(ctx, *condition.data.quotation_value[i]) != 0) + if (hex_push(ctx, condition->data.quotation_value[i]) != 0) { HEX_FREE(ctx, condition); HEX_FREE(ctx, thenBlock);@@ -2076,81 +2604,123 @@ HEX_FREE(ctx, elseBlock);
return 1; } } + HEX_ALLOC(evalResult); + if (!evalResult) + { + hex_error(ctx, "[symbol if] Memory allocation failed"); + HEX_FREE(ctx, condition); + HEX_FREE(ctx, thenBlock); + HEX_FREE(ctx, elseBlock); + return 1; + } HEX_POP(ctx, evalResult); - if (evalResult.type == HEX_TYPE_INTEGER && evalResult.data.int_value > 0) + ; + if (evalResult->type == HEX_TYPE_INTEGER && evalResult->data.int_value > 0) { - for (size_t i = 0; i < thenBlock.quotation_size; i++) + for (size_t i = 0; i < thenBlock->quotation_size; i++) { - if (hex_push(ctx, *thenBlock.data.quotation_value[i]) != 0) + if (hex_push(ctx, thenBlock->data.quotation_value[i]) != 0) { HEX_FREE(ctx, condition); HEX_FREE(ctx, thenBlock); HEX_FREE(ctx, elseBlock); + HEX_FREE(ctx, evalResult); return 1; } } } else { - for (size_t i = 0; i < elseBlock.quotation_size; i++) + for (size_t i = 0; i < elseBlock->quotation_size; i++) { - if (hex_push(ctx, *elseBlock.data.quotation_value[i]) != 0) + if (hex_push(ctx, elseBlock->data.quotation_value[i]) != 0) { HEX_FREE(ctx, condition); HEX_FREE(ctx, thenBlock); HEX_FREE(ctx, elseBlock); + HEX_FREE(ctx, evalResult); return 1; } } } + HEX_FREE(ctx, evalResult); } + HEX_FREE(ctx, condition); + HEX_FREE(ctx, thenBlock); + HEX_FREE(ctx, elseBlock); return 0; } int hex_symbol_when(hex_context_t *ctx) { - + HEX_ALLOC(action); + if (!action) + { + hex_error(ctx, "[symbol when] Memory allocation failed"); + return 1; + } HEX_POP(ctx, action); - if (action.type == HEX_TYPE_INVALID) + ; + if (action->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, action); return 1; } + + HEX_ALLOC(condition); + if (!condition) + { + hex_error(ctx, "[symbol when] Memory allocation failed"); + HEX_FREE(ctx, action); + return 1; + } HEX_POP(ctx, condition); - if (condition.type == HEX_TYPE_INVALID) + ; + if (condition->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, action); HEX_FREE(ctx, condition); return 1; } + int result = 0; - if (condition.type != HEX_TYPE_QUOTATION || action.type != HEX_TYPE_QUOTATION) + if (condition->type != HEX_TYPE_QUOTATION || action->type != HEX_TYPE_QUOTATION) { hex_error(ctx, "[symbol when] Two quotations required"); result = 1; } else { - for (size_t i = 0; i < condition.quotation_size; i++) + for (size_t i = 0; i < condition->quotation_size; i++) { - if (hex_push(ctx, *condition.data.quotation_value[i]) != 0) + if (hex_push(ctx, condition->data.quotation_value[i]) != 0) { result = 1; break; // Break if pushing the item failed } } + HEX_ALLOC(evalResult); + if (!evalResult) + { + hex_error(ctx, "[symbol when] Memory allocation failed"); + HEX_FREE(ctx, action); + HEX_FREE(ctx, condition); + return 1; + } HEX_POP(ctx, evalResult); - if (evalResult.type == HEX_TYPE_INTEGER && evalResult.data.int_value > 0) + ; + if (evalResult->type == HEX_TYPE_INTEGER && evalResult->data.int_value > 0) { - for (size_t i = 0; i < action.quotation_size; i++) + for (size_t i = 0; i < action->quotation_size; i++) { - if (hex_push(ctx, *action.data.quotation_value[i]) != 0) + if (hex_push(ctx, action->data.quotation_value[i]) != 0) { result = 1; break; } } } + HEX_FREE(ctx, evalResult); } if (result != 0) {@@ -2162,21 +2732,37 @@ }
int hex_symbol_while(hex_context_t *ctx) { - + HEX_ALLOC(action); + if (!action) + { + hex_error(ctx, "[symbol while] Memory allocation failed"); + return 1; + } HEX_POP(ctx, action); - if (action.type == HEX_TYPE_INVALID) + ; + if (action->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, action); return 1; } + + HEX_ALLOC(condition); + if (!condition) + { + hex_error(ctx, "[symbol while] Memory allocation failed"); + HEX_FREE(ctx, action); + return 1; + } HEX_POP(ctx, condition); - if (condition.type == HEX_TYPE_INVALID) + ; + if (condition->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, action); HEX_FREE(ctx, condition); return 1; } - if (condition.type != HEX_TYPE_QUOTATION || action.type != HEX_TYPE_QUOTATION) + + if (condition->type != HEX_TYPE_QUOTATION || action->type != HEX_TYPE_QUOTATION) { hex_error(ctx, "[symbol while] Two quotations required"); HEX_FREE(ctx, action);@@ -2187,23 +2773,36 @@ else
{ while (1) { - for (size_t i = 0; i < condition.quotation_size; i++) + for (size_t i = 0; i < condition->quotation_size; i++) { - if (hex_push(ctx, *condition.data.quotation_value[i]) != 0) + if (hex_push(ctx, condition->data.quotation_value[i]) != 0) { HEX_FREE(ctx, action); HEX_FREE(ctx, condition); return 1; } } + + HEX_ALLOC(evalResult); + if (!evalResult) + { + hex_error(ctx, "[symbol while] Memory allocation failed"); + HEX_FREE(ctx, action); + HEX_FREE(ctx, condition); + return 1; + } HEX_POP(ctx, evalResult); - if (evalResult.type == HEX_TYPE_INTEGER && evalResult.data.int_value == 0) + ; + if (evalResult->type == HEX_TYPE_INTEGER && evalResult->data.int_value == 0) { + HEX_FREE(ctx, evalResult); break; } - for (size_t i = 0; i < action.quotation_size; i++) + HEX_FREE(ctx, evalResult); + + for (size_t i = 0; i < action->quotation_size; i++) { - if (hex_push(ctx, *action.data.quotation_value[i]) != 0) + if (hex_push(ctx, action->data.quotation_value[i]) != 0) { HEX_FREE(ctx, action); HEX_FREE(ctx, condition);@@ -2212,6 +2811,9 @@ }
} } } + + HEX_FREE(ctx, action); + HEX_FREE(ctx, condition); return 0; }@@ -2225,25 +2827,47 @@ }
int hex_symbol_try(hex_context_t *ctx) { - + HEX_ALLOC(catch_block); + if (!catch_block) + { + hex_error(ctx, "[symbol try] Memory allocation failed"); + return 1; + } HEX_POP(ctx, catch_block); - if (catch_block.type == HEX_TYPE_INVALID) + ; + if (catch_block->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, catch_block); + free(catch_block); + return 1; + } + + HEX_ALLOC(try_block); + if (!try_block) + { + hex_error(ctx, "[symbol try] Memory allocation failed"); + HEX_FREE(ctx, catch_block); + free(catch_block); return 1; } HEX_POP(ctx, try_block); - if (try_block.type == HEX_TYPE_INVALID) + ; + if (try_block->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, catch_block); HEX_FREE(ctx, try_block); + free(catch_block); + free(try_block); return 1; } - if (try_block.type != HEX_TYPE_QUOTATION || catch_block.type != HEX_TYPE_QUOTATION) + + if (try_block->type != HEX_TYPE_QUOTATION || catch_block->type != HEX_TYPE_QUOTATION) { hex_error(ctx, "[symbol try] Two quotations required"); HEX_FREE(ctx, catch_block); HEX_FREE(ctx, try_block); + free(catch_block); + free(try_block); return 1; } else@@ -2253,23 +2877,25 @@ strncpy(prevError, ctx->error, sizeof(ctx->error));
ctx->error[0] = '\0'; ctx->settings.errors_enabled = 0; - for (size_t i = 0; i < try_block.quotation_size; i++) + for (size_t i = 0; i < try_block->quotation_size; i++) { - if (hex_push(ctx, *try_block.data.quotation_value[i]) != 0) + if (hex_push(ctx, try_block->data.quotation_value[i]) != 0) { ctx->settings.errors_enabled = 1; } } ctx->settings.errors_enabled = 1; - if (strcmp(ctx->error, "")) + if (strcmp(ctx->error, "") != 0) { - for (size_t i = 0; i < catch_block.quotation_size; i++) + for (size_t i = 0; i < catch_block->quotation_size; i++) { - if (hex_push(ctx, *catch_block.data.quotation_value[i]) != 0) + if (hex_push(ctx, catch_block->data.quotation_value[i]) != 0) { HEX_FREE(ctx, catch_block); HEX_FREE(ctx, try_block); + free(catch_block); + free(try_block); return 1; } }@@ -2277,24 +2903,37 @@ }
strncpy(ctx->error, prevError, sizeof(ctx->error)); } + + HEX_FREE(ctx, catch_block); + HEX_FREE(ctx, try_block); + free(catch_block); + free(try_block); return 0; } int hex_symbol_throw(hex_context_t *ctx) { + HEX_ALLOC(message); + if (!message) + { + hex_error(ctx, "[symbol throw] Memory allocation failed"); + return 1; + } HEX_POP(ctx, message); - if (message.type == HEX_TYPE_INVALID) + ; + if (message->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, message); return 1; } - if (message.type != HEX_TYPE_STRING) + if (message->type != HEX_TYPE_STRING) { hex_error(ctx, "[symbol throw] String required"); HEX_FREE(ctx, message); return 1; } - hex_error(ctx, message.data.str_value); + hex_error(ctx, message->data.str_value); + HEX_FREE(ctx, message); return 1; }@@ -2302,9 +2941,15 @@ // Quotation symbols
int hex_symbol_q(hex_context_t *ctx) { - + HEX_ALLOC(item); + if (!item) + { + hex_error(ctx, "[symbol '] Memory allocation failed"); + return 1; + } HEX_POP(ctx, item); - if (item.type == HEX_TYPE_INVALID) + ; + if (item->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, item); return 1;@@ -2318,27 +2963,32 @@ HEX_FREE(ctx, item);
return 1; } - *quotation = item; + *quotation = *item; - hex_item_t result; - result.type = HEX_TYPE_QUOTATION; - result.data.quotation_value = (hex_item_t **)malloc(sizeof(hex_item_t *)); - if (!result.data.quotation_value) + hex_item_t *result = (hex_item_t *)malloc(sizeof(hex_item_t)); + if (!result) { + hex_error(ctx, "[symbol '] Memory allocation failed"); HEX_FREE(ctx, item); - free(quotation); + return 1; + } + + result->type = HEX_TYPE_QUOTATION; + result->data.quotation_value = (hex_item_t **)malloc(sizeof(hex_item_t *)); + if (!result->data.quotation_value) + { hex_error(ctx, "[symbol '] Memory allocation failed"); + HEX_FREE(ctx, item); return 1; } - result.data.quotation_value[0] = quotation; - result.quotation_size = 1; + result->data.quotation_value[0] = quotation; + result->quotation_size = 1; if (HEX_PUSH(ctx, result) != 0) { HEX_FREE(ctx, item); - free(quotation); - free(result.data.quotation_value); + free(result->data.quotation_value); return 1; }@@ -2347,21 +2997,37 @@ }
int hex_symbol_map(hex_context_t *ctx) { - + HEX_ALLOC(action); + if (!action) + { + hex_error(ctx, "[symbol map] Memory allocation failed"); + return 1; + } HEX_POP(ctx, action); - if (action.type == HEX_TYPE_INVALID) + ; + if (action->type == HEX_TYPE_INVALID) { + HEX_FREE(ctx, action); + return 1; + } + + HEX_ALLOC(list); + if (!list) + { + hex_error(ctx, "[symbol map] Memory allocation failed"); HEX_FREE(ctx, action); return 1; } HEX_POP(ctx, list); - if (list.type == HEX_TYPE_INVALID) + ; + if (list->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, action); HEX_FREE(ctx, list); return 1; } - if (list.type != HEX_TYPE_QUOTATION || action.type != HEX_TYPE_QUOTATION) + + if (list->type != HEX_TYPE_QUOTATION || action->type != HEX_TYPE_QUOTATION) { hex_error(ctx, "[symbol map] Two quotations required"); HEX_FREE(ctx, action);@@ -2370,7 +3036,7 @@ return 1;
} else { - hex_item_t **quotation = (hex_item_t **)malloc(list.quotation_size * sizeof(hex_item_t *)); + hex_item_t **quotation = (hex_item_t **)malloc(list->quotation_size * sizeof(hex_item_t *)); if (!quotation) { hex_error(ctx, "[symbol map] Memory allocation failed");@@ -2378,18 +3044,18 @@ HEX_FREE(ctx, action);
HEX_FREE(ctx, list); return 1; } - for (size_t i = 0; i < list.quotation_size; i++) + for (size_t i = 0; i < list->quotation_size; i++) { - if (hex_push(ctx, *list.data.quotation_value[i]) != 0) + if (hex_push(ctx, list->data.quotation_value[i]) != 0) { HEX_FREE(ctx, action); HEX_FREE(ctx, list); hex_free_list(ctx, quotation, i); return 1; } - for (size_t j = 0; j < action.quotation_size; j++) + for (size_t j = 0; j < action->quotation_size; j++) { - if (hex_push(ctx, *action.data.quotation_value[j]) != 0) + if (hex_push(ctx, action->data.quotation_value[j]) != 0) { HEX_FREE(ctx, action); HEX_FREE(ctx, list);@@ -2398,31 +3064,56 @@ return 1;
} } quotation[i] = (hex_item_t *)malloc(sizeof(hex_item_t)); - *quotation[i] = hex_pop(ctx); + if (!quotation[i]) + { + hex_error(ctx, "[symbol map] Memory allocation failed"); + HEX_FREE(ctx, action); + HEX_FREE(ctx, list); + hex_free_list(ctx, quotation, i); + return 1; + } + *quotation[i] = *hex_copy_item(hex_pop(ctx)); } - if (hex_push_quotation(ctx, quotation, list.quotation_size) != 0) + if (hex_push_quotation(ctx, quotation, list->quotation_size) != 0) { HEX_FREE(ctx, action); HEX_FREE(ctx, list); - hex_free_list(ctx, quotation, list.quotation_size); + hex_free_list(ctx, quotation, list->quotation_size); return 1; } } + + HEX_FREE(ctx, action); + HEX_FREE(ctx, list); return 0; } // Stack manipulation symbols int hex_symbol_swap(hex_context_t *ctx) { - + HEX_ALLOC(a); + if (!a) + { + hex_error(ctx, "[symbol swap] Memory allocation failed"); + return 1; + } HEX_POP(ctx, a); - if (a.type == HEX_TYPE_INVALID) + ; + if (a->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, a); return 1; } + HEX_ALLOC(b); + if (!b) + { + hex_error(ctx, "[symbol swap] Memory allocation failed"); + HEX_FREE(ctx, a); + return 1; + } HEX_POP(ctx, b); - if (b.type == HEX_TYPE_INVALID) + ; + if (b->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, b); HEX_FREE(ctx, a);@@ -2445,14 +3136,26 @@ }
int hex_symbol_dup(hex_context_t *ctx) { - + HEX_ALLOC(item); + if (!item) + { + hex_error(ctx, "[symbol dup] Memory allocation failed"); + return 1; + } HEX_POP(ctx, item); - if (item.type == HEX_TYPE_INVALID) + ; + if (item->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, item); return 1; } - hex_item_t copy = hex_copy_item(&item); + hex_item_t *copy = hex_copy_item(item); + if (!copy) + { + hex_error(ctx, "[symbol dup] Memory allocation failed"); + HEX_FREE(ctx, item); + return 1; + } if (HEX_PUSH(ctx, copy) == 0 && HEX_PUSH(ctx, item) == 0) { HEX_FREE(ctx, item);@@ -2465,7 +3168,6 @@ }
int hex_symbol_stack(hex_context_t *ctx) { - hex_item_t **quotation = (hex_item_t **)malloc((ctx->stack.top + 1) * sizeof(hex_item_t *)); if (!quotation) {@@ -2473,16 +3175,15 @@ hex_error(ctx, "[symbol stack] Memory allocation failed");
return 1; } int count = 0; - for (size_t i = 0; i <= (size_t)ctx->stack.top + 1; i++) + for (size_t i = 0; i <= (size_t)ctx->stack.top; i++) { - quotation[i] = (hex_item_t *)malloc(sizeof(hex_item_t)); + quotation[i] = hex_copy_item(ctx->stack.entries[i]); if (!quotation[i]) { hex_error(ctx, "[symbol stack] Memory allocation failed"); hex_free_list(ctx, quotation, count); return 1; } - *quotation[i] = hex_copy_item(&ctx->stack.entries[i]); count++; }@@ -2497,7 +3198,12 @@ }
int hex_symbol_pop(hex_context_t *ctx) { - + HEX_ALLOC(item); + if (!item) + { + hex_error(ctx, "[symbol pop] Memory allocation failed"); + return 1; + } HEX_POP(ctx, item); HEX_FREE(ctx, item); return 0;
@@ -124,7 +124,7 @@ if (i > 0)
{ fprintf(stream, " "); } - hex_print_item(stream, *item.data.quotation_value[i]); + hex_print_item(stream, item.data.quotation_value[i]); } fprintf(stream, ")"); break;@@ -210,32 +210,32 @@ }
fprintf(stream, "\""); } -void hex_print_item(FILE *stream, hex_item_t item) +void hex_print_item(FILE *stream, hex_item_t *item) { - switch (item.type) + switch (item->type) { case HEX_TYPE_INTEGER: - fprintf(stream, "0x%x", item.data.int_value); + fprintf(stream, "0x%x", item->data.int_value); break; case HEX_TYPE_STRING: - hex_print_string(stream, item.data.str_value); + hex_print_string(stream, item->data.str_value); break; case HEX_TYPE_USER_SYMBOL: case HEX_TYPE_NATIVE_SYMBOL: - fprintf(stream, "%s", item.token->value); + fprintf(stream, "%s", item->token->value); break; case HEX_TYPE_QUOTATION: fprintf(stream, "("); - for (size_t i = 0; i < item.quotation_size; i++) + for (size_t i = 0; i < item->quotation_size; i++) { if (i > 0) { fprintf(stream, " "); } - hex_print_item(stream, *item.data.quotation_value[i]); + hex_print_item(stream, item->data.quotation_value[i]); } fprintf(stream, ")"); break;
@@ -337,8 +337,9 @@ *bytecode += length;
*size -= length; hex_debug(ctx, ">> PUSHIN[01]: 0x%x", value); - hex_item_t item = hex_integer_item(ctx, value); - *result = item; + HEX_ALLOC(item) + item = hex_integer_item(ctx, value); + *result = *item; return 0; }@@ -378,8 +379,9 @@ value[length] = '\0';
*bytecode += length; *size -= length; - hex_item_t item = hex_string_item(ctx, value); - *result = item; + HEX_ALLOC(item); + item = hex_string_item(ctx, value); + *result = *item; char *str = hex_process_string(value); if (!str) {@@ -400,27 +402,27 @@ hex_error(ctx, "[interpret bytecode native symbol] Invalid opcode for symbol");
return 1; } - hex_item_t item; - item.type = HEX_TYPE_NATIVE_SYMBOL; - hex_item_t value; + HEX_ALLOC(item); + item->type = HEX_TYPE_NATIVE_SYMBOL; + HEX_ALLOC(value); hex_token_t *token = (hex_token_t *)malloc(sizeof(hex_token_t)); token->value = (char *)symbol; - token->position.line = 0; - token->position.column = position; - if (hex_get_symbol(ctx, token->value, &value)) + token->position->line = 0; + token->position->column = position; + if (hex_get_symbol(ctx, token->value, value)) { - item.token = token; - item.type = HEX_TYPE_NATIVE_SYMBOL; - item.data.fn_value = value.data.fn_value; + item->token = token; + item->type = HEX_TYPE_NATIVE_SYMBOL; + item->data.fn_value = value->data.fn_value; } else { - hex_error(ctx, "(%d,%d) Unable to reference native symbol: %s (bytecode)", token->position.line, token->position.column, token->value); + hex_error(ctx, "(%d,%d) Unable to reference native symbol: %s (bytecode)", token->position->line, token->position->column, token->value); hex_free_token(token); return 1; } hex_debug(ctx, ">> NATSYM[X]: %s", opcode, symbol); - *result = item; + *result = *item; return 0; }@@ -457,8 +459,8 @@ hex_token_t *token = (hex_token_t *)malloc(sizeof(hex_token_t));
token->value = (char *)malloc(length + 1); strncpy(token->value, value, length + 1); - token->position.line = 0; - token->position.column = position; + token->position->line = 0; + token->position->column = position; hex_item_t item; item.type = HEX_TYPE_USER_SYMBOL;@@ -603,21 +605,21 @@ {
case HEX_OP_PUSHIN: if (hex_interpret_bytecode_integer(ctx, &bytecode, &size, item) != 0) { - HEX_FREE(ctx, *item); + HEX_FREE(ctx, item); return 1; } break; case HEX_OP_PUSHST: if (hex_interpret_bytecode_string(ctx, &bytecode, &size, item) != 0) { - HEX_FREE(ctx, *item); + HEX_FREE(ctx, item); return 1; } break; case HEX_OP_LOOKUP: if (hex_interpret_bytecode_user_symbol(ctx, &bytecode, &size, position, item) != 0) { - HEX_FREE(ctx, *item); + HEX_FREE(ctx, item); return 1; } break;@@ -625,21 +627,21 @@ case HEX_OP_PUSHQT:
if (hex_interpret_bytecode_quotation(ctx, &bytecode, &size, position, item) != 0) { - HEX_FREE(ctx, *item); + HEX_FREE(ctx, item); return 1; } break; default: if (hex_interpret_bytecode_native_symbol(ctx, opcode, position, item) != 0) { - HEX_FREE(ctx, *item); + HEX_FREE(ctx, item); return 1; } break; } - if (hex_push(ctx, *item) != 0) + if (hex_push(ctx, item) != 0) { - HEX_FREE(ctx, *item); + HEX_FREE(ctx, item); return 1; } }