Refactoring.
@@ -11,8 +11,11 @@ char HEX_ERROR[256] = "";
char **HEX_ARGV; int HEX_ARGC = 0; int HEX_ERRORS = 1; -int HEX_REPL = 0; -int HEX_STDIN = 0; +int HEX_STACK_TRACE = 0; +hex_item_t HEX_STACK[HEX_STACK_SIZE]; +int HEX_TOP = -1; +hex_registry_entry_t HEX_REGISTRY[HEX_REGISTRY_SIZE]; +int HEX_REGISTRY_COUNT = 0; char *HEX_NATIVE_SYMBOLS[] = { "store",@@ -80,25 +83,9 @@ "stack",
"clear", "pop"}; -void hex_error(const char *format, ...) -{ - va_list args; - va_start(args, format); - vsnprintf(HEX_ERROR, sizeof(HEX_ERROR), format, args); - if (HEX_ERRORS) - { - fprintf(stderr, "[error] "); - fprintf(stderr, "%s\n", HEX_ERROR); - } - va_end(args); -} - //////////////////////////////////////// // Registry Implementation // //////////////////////////////////////// - -hex_registry_entry_t HEX_REGISTRY[HEX_REGISTRY_SIZE]; -int HEX_REGISTRY_COUNT = 0; void hex_free_element(hex_item_t element); void hex_free_token(hex_token_t *token);@@ -195,9 +182,6 @@
//////////////////////////////////////// // Stack Implementation // //////////////////////////////////////// - -hex_item_t HEX_STACK[HEX_STACK_SIZE]; -int HEX_TOP = -1; // Push functions int hex_push(hex_item_t element)@@ -393,9 +377,22 @@ }
} //////////////////////////////////////// -// Debugging // +// Error & Debugging // //////////////////////////////////////// +void hex_error(const char *format, ...) +{ + va_list args; + va_start(args, format); + vsnprintf(HEX_ERROR, sizeof(HEX_ERROR), format, args); + if (HEX_ERRORS) + { + fprintf(stderr, "[error] "); + fprintf(stderr, "%s\n", HEX_ERROR); + } + va_end(args); +} + void hex_debug(const char *format, ...) { if (HEX_DEBUG)@@ -765,13 +762,13 @@
if (stackTrace.size < HEX_STACK_TRACE_SIZE) { // Buffer is not full; add element - stackTrace.entries[index].token = *token; + stackTrace.entries[index] = *token; stackTrace.size++; } else { // Buffer is full; overwrite the oldest element - stackTrace.entries[index].token = *token; + stackTrace.entries[index] = *token; stackTrace.start = (stackTrace.start + 1) % HEX_STACK_TRACE_SIZE; } }@@ -779,7 +776,7 @@
// Print the stack trace void print_stack_trace() { - if (HEX_STDIN || HEX_REPL || !HEX_ERRORS) + if (HEX_STACK_TRACE && HEX_ERRORS) { return; }@@ -788,7 +785,7 @@
for (int i = 0; i < stackTrace.size; i++) { int index = (stackTrace.start + stackTrace.size - 1 - i) % HEX_STACK_TRACE_SIZE; - hex_token_t token = stackTrace.entries[index].token; + hex_token_t token = stackTrace.entries[index]; fprintf(stderr, " %s (%s:%d:%d)\n", token.value, token.filename, token.line, token.column); } }@@ -2647,12 +2644,11 @@ // Shell symbols
int hex_symbol_args() { - int result = 0; hex_item_t **quotation = (hex_item_t **)malloc(HEX_ARGC * sizeof(hex_item_t *)); if (!quotation) { hex_error("Memory allocation failed"); - result = 1; + return 1; } else {@@ -2662,9 +2658,13 @@ quotation[i] = (hex_item_t *)malloc(sizeof(hex_item_t));
quotation[i]->type = HEX_TYPE_STRING; quotation[i]->data.strValue = HEX_ARGV[i]; } - result = hex_push_quotation(quotation, HEX_ARGC); + if (hex_push_quotation(quotation, HEX_ARGC) != 0) + { + hex_free_list(quotation, HEX_ARGC); + return 1; + } } - return result; + return 0; } int hex_symbol_exit()@@ -3248,6 +3248,7 @@ if (hex_push(*list.data.quotationValue[i]) != 0)
{ FREE(action); FREE(list); + hex_free_list(quotation, i); return 1; } for (int j = 0; j < action.quotationSize; j++)@@ -3256,6 +3257,7 @@ if (hex_push(*action.data.quotationValue[j]) != 0)
{ FREE(action); FREE(list); + hex_free_list(quotation, i); return 1; } }@@ -3266,6 +3268,7 @@ if (hex_push_quotation(quotation, list.quotationSize) != 0)
{ FREE(action); FREE(list); + hex_free_list(quotation, list.quotationSize); return 1; } }@@ -3311,6 +3314,7 @@ if (hex_push(*list.data.quotationValue[i]) != 0)
{ FREE(action); FREE(list); + hex_free_list(quotation, count); return 1; } for (int j = 0; j < action.quotationSize; j++)@@ -3319,6 +3323,7 @@ if (hex_push(*action.data.quotationValue[j]) != 0)
{ FREE(action); FREE(list); + hex_free_list(quotation, count); return 1; } }@@ -3331,6 +3336,7 @@ {
hex_error("Memory allocation failed"); FREE(action); FREE(list); + hex_free_list(quotation, count); return 1; } *quotation[count] = *list.data.quotationValue[i];@@ -3416,6 +3422,7 @@ quotation[i] = (hex_item_t *)malloc(sizeof(hex_item_t));
if (!quotation[i]) { hex_error("Memory allocation failed"); + hex_free_list(quotation, count); return 1; } *quotation[i] = HEX_STACK[i];@@ -3754,13 +3761,13 @@ }
} if (!isatty(fileno(stdin))) { - HEX_STDIN = 1; + HEX_STACK_TRACE = 0; // Process piped input from stdin hex_process_stdin(); } else { - HEX_REPL = 1; + HEX_STACK_TRACE = 0; // Start REPL hex_repl(); }
@@ -1,3 +1,4 @@
+ #ifndef HEX_H #define HEX_H@@ -24,7 +25,7 @@ #define HEX_REGISTRY_SIZE 1024
#define HEX_STACK_SIZE 128 #define HEX_STACK_TRACE_SIZE 16 -// Enum to represent the type of stack elements +// Type Definitions typedef enum hex_item_type_t { HEX_TYPE_INTEGER,@@ -35,7 +36,6 @@ HEX_TYPE_USER_SYMBOL,
HEX_TYPE_INVALID } hex_item_type_t; -// Token Types typedef enum hex_token_type_t { HEX_TOKEN_NUMBER,@@ -56,7 +56,6 @@ int line;
int column; } hex_token_t; -// Unified Stack Element typedef struct hex_item_t { hex_item_type_t type;@@ -71,27 +70,55 @@ hex_token_t *token; // Token containing stack information (valid for HEX_TYPE_NATIVE_SYMBOL and HEX_TYPE_USER_SYMBOL)
int quotationSize; // Size of the quotation (valid for HEX_TYPE_QUOTATION) } hex_item_t; -// Registry Entry typedef struct hex_registry_entry { char *key; hex_item_t value; } hex_registry_entry_t; -// Stack trace entry with token information -typedef struct -{ - hex_token_t token; -} hex_stack_trace_entry_t; - -// Circular buffer structure typedef struct hex_stack_trace_t { - hex_stack_trace_entry_t entries[HEX_STACK_TRACE_SIZE]; + hex_token_t entries[HEX_STACK_TRACE_SIZE]; int start; // Index of the oldest element int size; // Current number of elements in the buffer } hex_stack_trace_t; +// TODO: refactor and use this +typedef struct hex_stack_t +{ + hex_item_t item[HEX_STACK_SIZE]; + int top; +} hex_stack_t; + +// TODO: refactor and use this +typedef struct hex_registry_t +{ + hex_registry_entry_t entries[HEX_REGISTRY_SIZE]; + int size; +} hex_registry_t; + +// TODO: refactor and use this +typedef struct hext_settings_t +{ + int debug_enabled; + int errors_enabled; + int stack_trace_enabled; +} hex_settings_t; + +// TODO: refactor and use this +typedef struct hex_context_t +{ + hex_stack_t stack; + hex_registry_t registry; + hex_stack_trace_t stack_trace; + hex_settings_t settings; + char *error; + int argc; + char **argv; +} hex_context_t; + +// Functions + void hex_free_element(hex_item_t element); void hex_free_token(hex_token_t *token); void hex_free_list(hex_item_t **quotation, int size);@@ -112,6 +139,8 @@
int hex_push(hex_item_t element); int hex_push_int(int value); int hex_push_string(const char *value); +int hex_push_quotation(hex_item_t **quotation, int size); +int hex_push_symbol(hex_token_t *token); hex_item_t hex_pop(); char *hex_process_string(const char *value);@@ -130,7 +159,13 @@ int hex_is_symbol(hex_token_t *token, char *value);
int hex_symbol_store(); int hex_symbol_free(); +int hex_symbol_type(); int hex_symbol_i(); +int hex_symbol_eval(); +int hex_symbol_puts(); +int hex_symbol_warn(); +int hex_symbol_print(); +int hex_symbol_gets(); int hex_symbol_add(); int hex_symbol_subtract(); int hex_symbol_multiply();@@ -142,6 +177,10 @@ int hex_symbol_bitxor();
int hex_symbol_bitnot(); int hex_symbol_shiftleft(); int hex_symbol_shiftright(); +int hex_symbol_int(); +int hex_symbol_str(); +int hex_symbol_dec(); +int hex_symbol_hex(); int hex_symbol_equal(); int hex_symbol_notequal(); int hex_symbol_greater();@@ -183,15 +222,14 @@ int hex_symbol_stack();
int hex_symbol_clear(); int hex_symbol_pop(); -int hex_parse_quotation(const char **input, hex_item_t *result, const char *filename, int *line, int *column); -int hex_push_quotation(hex_item_t **quotation, int size); -int hex_push_symbol(hex_token_t *token); -void hex_free_list(hex_item_t **quotation, int size); void hex_register_symbols(); + void hex_repl(); void hex_process_stdin(); void hex_handle_sigint(int sig); char *hex_read_file(const char *filename); + +int hex_parse_quotation(const char **input, hex_item_t *result, const char *filename, int *line, int *column); int hex_interpret(const char *code, const char *filename, int line, int column); #endif // HEX_H