all repos — hex @ 7ea5324a704138c995dcdbec8d5dc38c939d9f48

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

Improved debug messages.
h3rald h3rald@h3rald.com
Thu, 19 Dec 2024 10:57:59 +0100
commit

7ea5324a704138c995dcdbec8d5dc38c939d9f48

parent

6b05f204e7b0a303b445886e037df7c34442623c

3 files changed, 100 insertions(+), 99 deletions(-)

jump to
M src/hex.hsrc/hex.h

@@ -148,6 +148,96 @@ int argc;

char **argv; } hex_context_t; +// Opcodes +typedef enum hex_opcode_t +{ + // Core Operations: <op> [prefix] <len> <data> + HEX_OP_LOOKUP = 0x00, + HEX_OP_PUSHIN = 0x01, + HEX_OP_PUSHST = 0x02, + HEX_OP_PUSHQT = 0x03, + + // Native Symbols + HEX_OP_STORE = 0x10, + HEX_OP_FREE = 0x11, + + HEX_OP_IF = 0x12, + HEX_OP_WHEN = 0x13, + HEX_OP_WHILE = 0x14, + HEX_OP_ERROR = 0x15, + HEX_OP_TRY = 0x16, + + HEX_OP_DUP = 0x17, + HEX_OP_STACK = 0x18, + HEX_OP_CLEAR = 0x19, + HEX_OP_POP = 0x1A, + HEX_OP_SWAP = 0x1B, + + HEX_OP_I = 0x1C, + HEX_OP_EVAL = 0x1D, + HEX_OP_QUOTE = 0x1E, + + HEX_OP_ADD = 0x1F, + HEX_OP_SUB = 0x20, + HEX_OP_MUL = 0x21, + HEX_OP_DIV = 0x22, + HEX_OP_MOD = 0x23, + + HEX_OP_BITAND = 0x24, + HEX_OP_BITOR = 0x25, + HEX_OP_BITXOR = 0x26, + HEX_OP_BITNOT = 0x27, + HEX_OP_SHL = 0x28, + HEX_OP_SHR = 0x29, + + HEX_OP_EQUAL = 0x2A, + HEX_OP_NOTEQUAL = 0x2B, + HEX_OP_GREATER = 0x2C, + HEX_OP_LESS = 0x2D, + HEX_OP_GREATEREQUAL = 0x2E, + HEX_OP_LESSEQUAL = 0x2F, + + HEX_OP_AND = 0x30, + HEX_OP_OR = 0x31, + HEX_OP_NOT = 0x32, + HEX_OP_XOR = 0x33, + + HEX_OP_INT = 0x34, + HEX_OP_STR = 0x35, + HEX_OP_DEC = 0x36, + HEX_OP_HEX = 0x37, + HEX_OP_ORD = 0x38, + HEX_OP_CHR = 0x39, + HEX_OP_TYPE = 0x3A, + + HEX_OP_CAT = 0x3B, + HEX_OP_LEN = 0x3C, + HEX_OP_GET = 0x3D, + HEX_OP_INDEX = 0x3E, + HEX_OP_JOIN = 0x3F, + + HEX_OP_SPLIT = 0x40, + HEX_OP_REPLACE = 0x41, + + HEX_OP_EACH = 0x42, + HEX_OP_MAP = 0x43, + HEX_OP_FILTER = 0x44, + + HEX_OP_PUTS = 0x45, + HEX_OP_WARN = 0x46, + HEX_OP_PRINT = 0x47, + HEX_OP_GETS = 0x48, + + HEX_OP_READ = 0x49, + HEX_OP_WRITE = 0x4A, + HEX_OP_APPEND = 0x4B, + HEX_OP_ARGS = 0x4C, + HEX_OP_EXIT = 0x4D, + HEX_OP_EXEC = 0x4E, + HEX_OP_RUN = 0x4F, + +} hex_opcode_t; + // Help System void hex_doc(hex_doc_dictionary_t *docs, const char *name, const char *description, const char *input, const char *output); int hex_get_doc(hex_doc_dictionary_t *docs, const char *key, hex_doc_entry_t *result);
M src/opcodes.csrc/opcodes.c

@@ -2,96 +2,6 @@ #ifndef HEX_H

#include "hex.h" #endif -// Opcodes -typedef enum hex_opcode_t -{ - // Core Operations: <op> [prefix] <len> <data> - HEX_OP_LOOKUP = 0x00, - HEX_OP_PUSHIN = 0x01, - HEX_OP_PUSHST = 0x02, - HEX_OP_PUSHQT = 0x03, - - // Native Symbols - HEX_OP_STORE = 0x10, - HEX_OP_FREE = 0x11, - - HEX_OP_IF = 0x12, - HEX_OP_WHEN = 0x13, - HEX_OP_WHILE = 0x14, - HEX_OP_ERROR = 0x15, - HEX_OP_TRY = 0x16, - - HEX_OP_DUP = 0x17, - HEX_OP_STACK = 0x18, - HEX_OP_CLEAR = 0x19, - HEX_OP_POP = 0x1A, - HEX_OP_SWAP = 0x1B, - - HEX_OP_I = 0x1C, - HEX_OP_EVAL = 0x1D, - HEX_OP_QUOTE = 0x1E, - - HEX_OP_ADD = 0x1F, - HEX_OP_SUB = 0x20, - HEX_OP_MUL = 0x21, - HEX_OP_DIV = 0x22, - HEX_OP_MOD = 0x23, - - HEX_OP_BITAND = 0x24, - HEX_OP_BITOR = 0x25, - HEX_OP_BITXOR = 0x26, - HEX_OP_BITNOT = 0x27, - HEX_OP_SHL = 0x28, - HEX_OP_SHR = 0x29, - - HEX_OP_EQUAL = 0x2A, - HEX_OP_NOTEQUAL = 0x2B, - HEX_OP_GREATER = 0x2C, - HEX_OP_LESS = 0x2D, - HEX_OP_GREATEREQUAL = 0x2E, - HEX_OP_LESSEQUAL = 0x2F, - - HEX_OP_AND = 0x30, - HEX_OP_OR = 0x31, - HEX_OP_NOT = 0x32, - HEX_OP_XOR = 0x33, - - HEX_OP_INT = 0x34, - HEX_OP_STR = 0x35, - HEX_OP_DEC = 0x36, - HEX_OP_HEX = 0x37, - HEX_OP_ORD = 0x38, - HEX_OP_CHR = 0x39, - HEX_OP_TYPE = 0x3A, - - HEX_OP_CAT = 0x3B, - HEX_OP_LEN = 0x3C, - HEX_OP_GET = 0x3D, - HEX_OP_INDEX = 0x3E, - HEX_OP_JOIN = 0x3F, - - HEX_OP_SPLIT = 0x40, - HEX_OP_REPLACE = 0x41, - - HEX_OP_EACH = 0x42, - HEX_OP_MAP = 0x43, - HEX_OP_FILTER = 0x44, - - HEX_OP_PUTS = 0x45, - HEX_OP_WARN = 0x46, - HEX_OP_PRINT = 0x47, - HEX_OP_GETS = 0x48, - - HEX_OP_READ = 0x49, - HEX_OP_WRITE = 0x4A, - HEX_OP_APPEND = 0x4B, - HEX_OP_ARGS = 0x4C, - HEX_OP_EXIT = 0x4D, - HEX_OP_EXEC = 0x4E, - HEX_OP_RUN = 0x4F, - -} hex_opcode_t; - uint8_t hex_symbol_to_opcode(const char *symbol) { // Native Symbols
M src/vm.csrc/vm.c

@@ -346,7 +346,7 @@

*bytecode += length; *size -= length; - hex_debug(ctx, "PUSHIN[%zu]: %d", length, value); + hex_debug(ctx, ">> PUSHIN: %d", value); hex_item_t item = hex_integer_item(ctx, value); *result = item; return 0;

@@ -390,7 +390,7 @@ *size -= length;

hex_item_t item = hex_string_item(ctx, value); *result = item; - hex_debug(ctx, "PUSHST[%zu]: %s", length, value); + hex_debug(ctx, ">> PUSHST: \"%s\"", hex_process_string(ctx, value)); return 0; }

@@ -423,7 +423,7 @@ 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[1]: %d (%s)", opcode, symbol); + hex_debug(ctx, ">> NATSYM: %02X (%s)", opcode, symbol); *result = item; return 0; }

@@ -468,7 +468,7 @@ hex_item_t item;

item.type = HEX_TYPE_USER_SYMBOL; item.token = token; - hex_debug(ctx, "LOOKUP[%zu]: %s", length, value); + hex_debug(ctx, ">> LOOKUP: #%zu -> %s", index, value); *result = item; return 0; }

@@ -492,7 +492,7 @@ (*bytecode)++;

(*size)--; } while (**bytecode & 0x80); - hex_debug(ctx, "PUSHQT[%zu]: <start>", n_items); + hex_debug(ctx, ">> PUSHQT: <start> (items: %zu)", n_items); hex_item_t **items = (hex_item_t **)malloc(n_items * sizeof(hex_item_t)); if (!items)

@@ -553,7 +553,7 @@ result->type = HEX_TYPE_QUOTATION;

result->data.quotation_value = items; result->quotation_size = n_items; - hex_debug(ctx, "PUSHQT[%zu]: <end>", n_items); + hex_debug(ctx, ">> PUSHQT: <end> (items: %zu)", n_items); return 0; }

@@ -587,16 +587,17 @@ return 1;

} } // Debug: Print all symbols in the symbol table - hex_debug(ctx, "Symbol Table:"); + hex_debug(ctx, "--- Symbol Table Start ---"); for (size_t i = 0; i < ctx->symbol_table.count; i++) { - hex_debug(ctx, "Symbol %zu: %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) { position = bytecode_size - size; uint8_t opcode = *bytecode; - hex_debug(ctx, "Bytecode Position: %zu - opcode: %02X", position, opcode); + hex_debug(ctx, "-- [%08d] OPCODE: %02X", position, opcode); bytecode++; size--;