all repos — hex @ 1c0f149e5dcac8b0d5aa023097df6531d86163e0

A tiny, minimalist, slightly-esoteric concatenative programming lannguage.

Still segfaults.
h3rald h3rald@h3rald.com
Thu, 26 Dec 2024 15:25:23 +0100
commit

1c0f149e5dcac8b0d5aa023097df6531d86163e0

parent

6eeaa286bfc5e8ba4e7ecd9a712614a7db9eea79

M src/error.csrc/error.c

@@ -11,7 +11,7 @@ {

va_list args; va_start(args, format); vsnprintf(ctx->error, sizeof(ctx->error), format, args); - if (ctx->settings.errors_enabled) /// FC + if (ctx->settings->errors_enabled) /// FC { fprintf(stderr, "ERROR: "); fprintf(stderr, "%s\n", ctx->error);

@@ -21,7 +21,7 @@ }

void hex_debug(hex_context_t *ctx, const char *format, ...) { - if (ctx->settings.debugging_enabled) + if (ctx->settings->debugging_enabled) { va_list args; va_start(args, format);

@@ -55,7 +55,7 @@ }

void hex_debug_item(hex_context_t *ctx, const char *message, hex_item_t *item) { - if (ctx->settings.debugging_enabled) + if (ctx->settings->debugging_enabled) { fprintf(stdout, "*** %s: ", message); hex_print_item(stdout, item);

@@ -70,35 +70,35 @@

// Add an entry to the circular stack trace void add_to_stack_trace(hex_context_t *ctx, hex_token_t *token) { - int index = (ctx->stack_trace.start + ctx->stack_trace.size) % HEX_STACK_TRACE_SIZE; + int index = (ctx->stack_trace->start + ctx->stack_trace->size) % HEX_STACK_TRACE_SIZE; - if (ctx->stack_trace.size < HEX_STACK_TRACE_SIZE) + if (ctx->stack_trace->size < HEX_STACK_TRACE_SIZE) { // Buffer is not full; add item - ctx->stack_trace.entries[index] = token; - ctx->stack_trace.size++; + 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.start = (ctx->stack_trace.start + 1) % HEX_STACK_TRACE_SIZE; + ctx->stack_trace->entries[index] = token; + ctx->stack_trace->start = (ctx->stack_trace->start + 1) % HEX_STACK_TRACE_SIZE; } } // Print the stack trace void print_stack_trace(hex_context_t *ctx) { - if (!ctx->settings.stack_trace_enabled || !ctx->settings.errors_enabled || ctx->stack_trace.size <= 0) + if (!ctx->settings->stack_trace_enabled || !ctx->settings->errors_enabled || ctx->stack_trace->size <= 0) { return; } fprintf(stderr, "[stack trace] (most recent symbol first):\n"); - for (size_t i = 0; i < ctx->stack_trace.size; i++) + 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]; + 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); } }
M src/hex.hsrc/hex.h

@@ -138,12 +138,12 @@ } hex_symbol_table_t;

typedef struct hex_context_t { - hex_stack_t stack; - hex_registry_t registry; - hex_stack_trace_t stack_trace; - hex_settings_t settings; - hex_doc_dictionary_t docs; - hex_symbol_table_t symbol_table; + hex_stack_t *stack; + hex_registry_t *registry; + hex_stack_trace_t *stack_trace; + hex_settings_t *settings; + hex_doc_dictionary_t *docs; + hex_symbol_table_t *symbol_table; int hashbang; char error[256]; int argc;

@@ -394,7 +394,7 @@ uint8_t *hex_encode_bytecode_symboltable(hex_context_t *ctx, size_t *out_size);

// REPL and initialization void hex_register_symbols(hex_context_t *ctx); -hex_context_t hex_init(); +hex_context_t *hex_init(); void hex_repl(hex_context_t *ctx); void hex_process_stdin(hex_context_t *ctx); void hex_handle_sigint(int sig);
M src/interpreter.csrc/interpreter.c

@@ -6,21 +6,25 @@ ////////////////////////////////////////

// Hex Interpreter Implementation // //////////////////////////////////////// -hex_context_t hex_init() +hex_context_t *hex_init() { - hex_context_t context; - context.argc = 0; - context.argv = NULL; - context.registry.size = 0; - context.docs.size = 0; - context.stack.top = -1; - context.stack_trace.start = 0; - context.stack_trace.size = 0; - context.settings.debugging_enabled = 0; - context.settings.errors_enabled = 1; - context.settings.stack_trace_enabled = 1; - context.symbol_table.count = 0; - context.symbol_table.symbols = malloc(HEX_MAX_USER_SYMBOLS * sizeof(char *)); + hex_context_t *context = malloc(sizeof(hex_context_t *)); + context->argc = 0; + context->argv = NULL; + context->registry = malloc(sizeof(hex_registry_t *)); + context->registry->size = 0; + context->docs = malloc(sizeof(hex_doc_dictionary_t *)); + context->docs->size = 0; + context->stack = malloc(sizeof(hex_stack_t *)); + context->stack->top = -1; + context->stack_trace->start = 0; + context->stack_trace->size = 0; + context->settings = malloc(sizeof(hex_settings_t *)); + context->settings->debugging_enabled = 0; + context->settings->errors_enabled = 1; + context->settings->stack_trace_enabled = 1; + context->symbol_table->count = 0; + context->symbol_table->symbols = malloc(HEX_MAX_USER_SYMBOLS * sizeof(char *)); return context; }
M src/main.csrc/main.c

@@ -149,7 +149,7 @@

// Tokenize and process the input hex_interpret(ctx, line, "<repl>", 1, 1); // Print the top item of the stack - hex_print_item(stdout, ctx->stack.entries[ctx->stack.top]); + hex_print_item(stdout, ctx->stack->entries[ctx->stack->top]); printf("\n"); return 0; }

@@ -307,12 +307,12 @@ // Register SIGINT (Ctrl+C) signal handler

signal(SIGINT, hex_handle_sigint); // Initialize the context - hex_context_t ctx = hex_init(); - ctx.argc = argc; - ctx.argv = argv; + hex_context_t *ctx = hex_init(); + ctx->argc = argc; + ctx->argv = argv; - hex_register_symbols(&ctx); - hex_create_docs(&ctx.docs); + hex_register_symbols(ctx); + hex_create_docs(ctx->docs); char *file; int generate_bytecode = 0;

@@ -334,12 +334,12 @@ return 0;

} else if ((strcmp(arg, "-m") == 0 || strcmp(arg, "--manual") == 0)) { - hex_print_docs(&ctx.docs); + hex_print_docs(ctx->docs); return 0; } else if ((strcmp(arg, "-d") == 0 || strcmp(arg, "--debug") == 0)) { - ctx.settings.debugging_enabled = 1; + ctx->settings->debugging_enabled = 1; printf("*** Debug mode enabled ***\n"); } else if ((strcmp(arg, "-b") == 0 || strcmp(arg, "--bytecode") == 0))

@@ -358,7 +358,7 @@ {

FILE *bytecode_file = fopen(file, "rb"); if (bytecode_file == NULL) { - hex_error(&ctx, "[open hbx file] Failed to open bytecode file: %s", file); + hex_error(ctx, "[open hbx file] Failed to open bytecode file: %s", file); return 1; } fseek(bytecode_file, 0, SEEK_END);

@@ -367,25 +367,25 @@ fseek(bytecode_file, 0, SEEK_SET);

uint8_t *bytecode = (uint8_t *)malloc(bytecode_size); if (bytecode == NULL) { - hex_error(&ctx, "[read hbx file] Memory allocation failed"); + hex_error(ctx, "[read hbx file] Memory allocation failed"); fclose(bytecode_file); return 1; } fread(bytecode, 1, bytecode_size, bytecode_file); fclose(bytecode_file); - hex_interpret_bytecode(&ctx, bytecode, bytecode_size); + hex_interpret_bytecode(ctx, bytecode, bytecode_size); free(bytecode); } else { - char *fileContent = hex_read_file(&ctx, file); + char *fileContent = hex_read_file(ctx, file); if (generate_bytecode) { uint8_t *bytecode; size_t bytecode_size = 0; hex_file_position_t position; position.column = 1; - position.line = 1 + ctx.hashbang; + position.line = 1 + ctx->hashbang; position.filename = file; char *bytecode_file = strdup(file); char *ext = strrchr(bytecode_file, '.');

@@ -397,19 +397,19 @@ else

{ strcat(bytecode_file, ".hbx"); } - if (hex_bytecode(&ctx, fileContent, &bytecode, &bytecode_size, &position) != 0) + if (hex_bytecode(ctx, fileContent, &bytecode, &bytecode_size, &position) != 0) { - hex_error(&ctx, "[generate bytecode] Failed to generate bytecode"); + hex_error(ctx, "[generate bytecode] Failed to generate bytecode"); return 1; } - if (hex_write_bytecode_file(&ctx, bytecode_file, bytecode, bytecode_size) != 0) + if (hex_write_bytecode_file(ctx, bytecode_file, bytecode, bytecode_size) != 0) { return 1; } } else { - hex_interpret(&ctx, fileContent, file, 1 + ctx.hashbang, 1); + hex_interpret(ctx, fileContent, file, 1 + ctx->hashbang, 1); } } return 0;

@@ -418,16 +418,16 @@ }

#if !(__EMSCRIPTEN__) if (!isatty(fileno(stdin))) { - ctx.settings.stack_trace_enabled = 0; + ctx->settings->stack_trace_enabled = 0; // Process piped input from stdin - hex_process_stdin(&ctx); + hex_process_stdin(ctx); } #endif else { - ctx.settings.stack_trace_enabled = 0; + ctx->settings->stack_trace_enabled = 0; // Start REPL - hex_repl(&ctx); + hex_repl(ctx); } return 0;
M src/parser.csrc/parser.c

@@ -224,10 +224,10 @@ }

int hex_valid_native_symbol(hex_context_t *ctx, const char *symbol) { - hex_doc_entry_t doc; + hex_doc_entry_t *doc = malloc(sizeof(hex_doc_entry_t *)); for (size_t i = 0; i < HEX_NATIVE_SYMBOLS; i++) { - if (hex_get_doc(&ctx->docs, symbol, &doc)) + if (hex_get_doc(ctx->docs, symbol, doc)) { return 1; }
M src/registry.csrc/registry.c

@@ -44,32 +44,32 @@ if (!native && hex_valid_user_symbol(ctx, key) == 0)

{ return 1; } - for (size_t i = 0; i < ctx->registry.size; i++) + 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; } } - if (ctx->registry.size >= HEX_REGISTRY_SIZE) + if (ctx->registry->size >= HEX_REGISTRY_SIZE) { hex_error(ctx, "Registry overflow"); 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.size++; + ctx->registry->entries[ctx->registry->size]->key = strdup(key); + ctx->registry->entries[ctx->registry->size]->value = value; + ctx->registry->size++; return 0; }

@@ -95,11 +95,11 @@

// 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++) + 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; } }
M src/stack.csrc/stack.c

@@ -19,7 +19,7 @@

// Push functions int hex_push(hex_context_t *ctx, hex_item_t *item) { - if (ctx->stack.top >= HEX_STACK_SIZE - 1) + if (ctx->stack->top >= HEX_STACK_SIZE - 1) { hex_error(ctx, "[push] Stack overflow"); return 1;

@@ -87,7 +87,7 @@ free(value);

} else { - ctx->stack.entries[++ctx->stack.top] = item; + ctx->stack->entries[++ctx->stack->top] = item; } if (result == 0) {

@@ -213,19 +213,19 @@

// Pop function hex_item_t *hex_pop(hex_context_t *ctx) { - if (ctx->stack.top < 0) + if (ctx->stack->top < 0) { hex_error(ctx, "[pop] Insufficient items on the stack"); return NULL; } - hex_debug_item(ctx, " POP", ctx->stack.entries[ctx->stack.top]); + hex_debug_item(ctx, " POP", ctx->stack->entries[ctx->stack->top]); hex_item_t *item = malloc(sizeof(hex_item_t)); if (item == NULL) { hex_error(ctx, "[pop] Failed to allocate memory for item"); return NULL; } - *item = *ctx->stack.entries[ctx->stack.top--]; + *item = *ctx->stack->entries[ctx->stack->top--]; return item; }
M src/symbols.csrc/symbols.c

@@ -141,17 +141,17 @@ 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++) + 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); - for (size_t j = i; j < ctx->registry.size - 1; j++) + 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]; + ctx->registry->entries[j] = ctx->registry->entries[j + 1]; } - ctx->registry.size--; + ctx->registry->size--; HEX_FREE(ctx, item); return 0; }

@@ -162,22 +162,22 @@ }

int hex_symbol_symbols(hex_context_t *ctx) { - hex_item_t **quotation = (hex_item_t **)malloc(ctx->registry.size * sizeof(hex_item_t *)); + 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++) + for (size_t i = 0; i < ctx->registry->size; i++) { - char *id = malloc(strlen(ctx->registry.entries[i]->key) + 1); + 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); + strcpy(id, ctx->registry->entries[i]->key); quotation[i] = (hex_item_t *)malloc(sizeof(hex_item_t)); if (!quotation[i]) {

@@ -187,9 +187,9 @@ return 1;

} *quotation[i] = *hex_string_item(ctx, id); } - if (hex_push_quotation(ctx, quotation, ctx->registry.size) != 0) + if (hex_push_quotation(ctx, quotation, ctx->registry->size) != 0) { - hex_free_list(ctx, quotation, ctx->registry.size); + hex_free_list(ctx, quotation, ctx->registry->size); return 1; } return 0;

@@ -2838,7 +2838,6 @@ ;

if (catch_block->type == HEX_TYPE_INVALID) { HEX_FREE(ctx, catch_block); - free(catch_block); return 1; }

@@ -2847,7 +2846,6 @@ 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);

@@ -2856,8 +2854,6 @@ 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; }

@@ -2866,8 +2862,6 @@ {

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

@@ -2876,15 +2870,15 @@ char prevError[256];

strncpy(prevError, ctx->error, sizeof(ctx->error)); ctx->error[0] = '\0'; - ctx->settings.errors_enabled = 0; + ctx->settings->errors_enabled = 0; for (size_t i = 0; i < try_block->quotation_size; i++) { if (hex_push(ctx, try_block->data.quotation_value[i]) != 0) { - ctx->settings.errors_enabled = 1; + ctx->settings->errors_enabled = 1; } } - ctx->settings.errors_enabled = 1; + ctx->settings->errors_enabled = 1; if (strcmp(ctx->error, "") != 0) {

@@ -2894,8 +2888,6 @@ 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; } }

@@ -2906,8 +2898,6 @@ }

HEX_FREE(ctx, catch_block); HEX_FREE(ctx, try_block); - free(catch_block); - free(try_block); return 0; }

@@ -2988,7 +2978,6 @@

if (HEX_PUSH(ctx, result) != 0) { HEX_FREE(ctx, item); - free(result->data.quotation_value); return 1; }

@@ -3168,16 +3157,16 @@ }

int hex_symbol_stack(hex_context_t *ctx) { - hex_item_t **quotation = (hex_item_t **)malloc((ctx->stack.top + 1) * sizeof(hex_item_t *)); + hex_item_t **quotation = (hex_item_t **)malloc((ctx->stack->top + 1) * sizeof(hex_item_t *)); if (!quotation) { hex_error(ctx, "[symbol stack] Memory allocation failed"); return 1; } int count = 0; - for (size_t i = 0; i <= (size_t)ctx->stack.top; i++) + for (size_t i = 0; i <= (size_t)ctx->stack->top; i++) { - quotation[i] = hex_copy_item(ctx->stack.entries[i]); + quotation[i] = hex_copy_item(ctx->stack->entries[i]); if (!quotation[i]) { hex_error(ctx, "[symbol stack] Memory allocation failed");

@@ -3187,7 +3176,7 @@ }

count++; } - if (hex_push_quotation(ctx, quotation, ctx->stack.top + 1) != 0) + if (hex_push_quotation(ctx, quotation, ctx->stack->top + 1) != 0) { hex_error(ctx, "[symbol stack] An error occurred while pushing quotation"); hex_free_list(ctx, quotation, count);
M src/symboltable.csrc/symboltable.c

@@ -4,7 +4,7 @@ #endif

int hex_symboltable_set(hex_context_t *ctx, const char *symbol) { - hex_symbol_table_t *table = &ctx->symbol_table; + hex_symbol_table_t *table = ctx->symbol_table; size_t len = strlen(symbol); if (len > HEX_MAX_SYMBOL_LENGTH)

@@ -32,7 +32,7 @@ }

int hex_symboltable_get_index(hex_context_t *ctx, const char *symbol) { - hex_symbol_table_t *table = &ctx->symbol_table; + hex_symbol_table_t *table = ctx->symbol_table; for (uint16_t i = 0; i < table->count; ++i) { if (strcmp(table->symbols[i], symbol) == 0)

@@ -45,16 +45,16 @@ }

char *hex_symboltable_get_value(hex_context_t *ctx, uint16_t index) { - if (index >= ctx->symbol_table.count) + if (index >= ctx->symbol_table->count) { return NULL; } - return ctx->symbol_table.symbols[index]; + return ctx->symbol_table->symbols[index]; } int hex_decode_bytecode_symboltable(hex_context_t *ctx, uint8_t **bytecode, size_t *size, size_t total) { - hex_symbol_table_t *table = &ctx->symbol_table; + hex_symbol_table_t *table = ctx->symbol_table; table->count = 0; for (size_t i = 0; i < total; i++)

@@ -82,7 +82,7 @@ }

uint8_t *hex_encode_bytecode_symboltable(hex_context_t *ctx, size_t *out_size) { - hex_symbol_table_t *table = &ctx->symbol_table; + hex_symbol_table_t *table = ctx->symbol_table; size_t total_size = 0; for (uint16_t i = 0; i < table->count; ++i)
M src/vm.csrc/vm.c

@@ -438,7 +438,7 @@ size_t index = **bytecode;

(*bytecode)++; (*size)--; - if (index >= ctx->symbol_table.count) + if (index >= ctx->symbol_table->count) { hex_error(ctx, "[interpret bytecode user symbol] Symbol index out of bounds"); return 1;

@@ -586,9 +586,9 @@ }

} // Debug: Print all symbols in the symbol table hex_debug(ctx, "--- Symbol Table Start ---"); - for (size_t i = 0; i < ctx->symbol_table.count; i++) + for (size_t i = 0; i < ctx->symbol_table->count; i++) { - hex_debug(ctx, "%03d: %s", i, ctx->symbol_table.symbols[i]); + hex_debug(ctx, "%03d: %s", i, ctx->symbol_table->symbols[i]); } hex_debug(ctx, "--- Symbol Table End ---"); while (size > 0)

@@ -655,7 +655,7 @@ header[1] = 'h';

header[2] = 'e'; header[3] = 'x'; header[4] = 0x01; // version - uint16_t symbol_table_size = (uint16_t)ctx->symbol_table.count; + uint16_t symbol_table_size = (uint16_t)ctx->symbol_table->count; header[5] = symbol_table_size & 0xFF; header[6] = (symbol_table_size >> 8) & 0xFF; header[7] = 0x02;