all repos — hex @ bf5d004b45f7e64653707c2c08173dc627d2d7d9

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

Implemented symbol ::
h3rald h3rald@h3rald.com
Sun, 22 Dec 2024 17:28:37 +0100
commit

bf5d004b45f7e64653707c2c08173dc627d2d7d9

parent

9e198fc76c8ebef0246ee2e40abd3befee4f9565

8 files changed, 43 insertions(+), 27 deletions(-)

jump to
M scripts/test.hexscripts/test.hex

@@ -219,6 +219,8 @@ (("t" chr) (error) try "[symbol chr] Integer required" ==)

((() chr) (error) try "[symbol chr] Integer required" ==) ;120 + ((dup *) "square" :: 0x2 square 0x4 == "square" #) + ) "tests" :
M scripts/web.hexscripts/web.hex

@@ -174,7 +174,7 @@ d-contents "/" "changelog.html" cat cat read

"changelog-toc" changelog-toc process-tag . "releases" releases-content process-tag . "changelog-content" : -symbol-links (changelog-content swap process-symbol . "changelog-content" :) each +symbol-links (changelog-content swap process-symbol . "changelog-content" :) () map ; Write CHANGELOG.md changelog-content
M src/doc.csrc/doc.c

@@ -103,6 +103,7 @@ hex_set_doc(docs, "split", "s1 s2", "q", "Splits 's1' using separator 's2'.");

hex_set_doc(docs, "replace", "s1 s2 s3", "s", "Replaces 's2' with 's3' within 's1'."); // Quotation + hex_set_doc(docs, "each", "q1 q2", "*", "Executes 'q2' for each item of 'q1'."); hex_set_doc(docs, "map", "q1 q2", "q3", "Applies 'q2' to 'q1' items and returns results."); hex_set_doc(docs, "filter", "q1 q2", "q3", "Filters 'q2' by applying 'q1'.");
M src/hex.hsrc/hex.h

@@ -25,7 +25,7 @@ #define HEX_STDIN_BUFFER_SIZE 16384

#define HEX_REGISTRY_SIZE 1024 #define HEX_STACK_SIZE 256 #define HEX_STACK_TRACE_SIZE 16 -#define HEX_NATIVE_SYMBOLS 64 +#define HEX_NATIVE_SYMBOLS 65 #define HEX_MAX_SYMBOL_LENGTH 256 #define HEX_MAX_USER_SYMBOLS (HEX_REGISTRY_SIZE - HEX_NATIVE_SYMBOLS)

@@ -118,7 +118,7 @@ } hex_doc_entry_t;

typedef struct hex_doc_dictionary_t { - hex_doc_entry_t entries[64]; + hex_doc_entry_t entries[HEX_NATIVE_SYMBOLS]; size_t size; } hex_doc_dictionary_t;

@@ -160,7 +160,7 @@ HEX_OP_PUSHQT = 0x03,

// Native Symbols HEX_OP_STORE = 0x10, - HEX_OP_DEFINE = 0x42, + HEX_OP_DEFINE = 0x50, HEX_OP_FREE = 0x11, HEX_OP_IF = 0x12,

@@ -221,7 +221,7 @@

HEX_OP_SPLIT = 0x40, HEX_OP_REPLACE = 0x41, - //HEX_OP_EACH = 0x42, + HEX_OP_EACH = 0x42, HEX_OP_MAP = 0x43, HEX_OP_FILTER = 0x44,
M src/opcodes.csrc/opcodes.c

@@ -9,6 +9,10 @@ if (strcmp(symbol, ":") == 0)

{ return HEX_OP_STORE; } + else if (strcmp(symbol, "::") == 0) + { + return HEX_OP_DEFINE; + } else if (strcmp(symbol, "#") == 0) { return HEX_OP_FREE;

@@ -270,6 +274,8 @@ switch (opcode)

{ case HEX_OP_STORE: return ":"; + case HEX_OP_DEFINE: + return "::"; case HEX_OP_FREE: return "#"; case HEX_OP_IF:
M src/stack.csrc/stack.c

@@ -33,22 +33,22 @@ hex_item_t value;

if (hex_get_symbol(ctx, item.token->value, &value)) { add_to_stack_trace(ctx, item.token); - if (item.type == HEX_TYPE_QUOTATION && item.immediate) + if (value.type == HEX_TYPE_QUOTATION && value.immediate) { - for (size_t i=0; i<item.quotation_size; i++) + for (size_t i = 0; i < value.quotation_size; i++) { - if (hex_push(ctx, item.data.quotation_value[i]) != 0) + if (hex_push(ctx, *value.data.quotation_value[i]) != 0) { HEX_FREE(ctx, value); hex_debug_item(ctx, "FAIL", item); return 1; } } - } else + } + else { result = HEX_PUSH(ctx, value); } - } else {

@@ -59,9 +59,19 @@ }

} else if (item.type == HEX_TYPE_NATIVE_SYMBOL) { - add_to_stack_trace(ctx, item.token); - hex_debug_item(ctx, "CALL", item); - result = item.data.fn_value(ctx); + hex_item_t value; + 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); + } + else + { + hex_error(ctx, "[push] Undefined native symbol: %s", item.token->value); + HEX_FREE(ctx, value); + result = 1; + } } else {

@@ -104,6 +114,12 @@ hex_item_t item = {.type = HEX_TYPE_QUOTATION, .data.quotation_value = quotation, .quotation_size = size};

return item; } +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}; + return item; +} + int hex_push_string(hex_context_t *ctx, const char *value) { return HEX_PUSH(ctx, hex_string_item(ctx, value));

@@ -121,17 +137,7 @@ }

int hex_push_symbol(hex_context_t *ctx, hex_token_t *token) { - hex_item_t value; - if (hex_get_symbol(ctx, token->value, &value)) - { - value.token = token; - return HEX_PUSH(ctx, value); - } - else - { - hex_error(ctx, "[push] Undefined symbol: %s", token->value); - return 1; - } + return HEX_PUSH(ctx, hex_symbol_item(ctx, token)); } // Pop function
M src/symbols.csrc/symbols.c

@@ -2193,7 +2193,7 @@ }

return 0; } -/*int hex_symbol_each(hex_context_t *ctx) +int hex_symbol_each(hex_context_t *ctx) { HEX_POP(ctx, action);

@@ -2238,7 +2238,7 @@ }

} } return 0; -}*/ +} int hex_symbol_error(hex_context_t *ctx) {

@@ -2665,6 +2665,7 @@ hex_set_native_symbol(ctx, "while", hex_symbol_while);

hex_set_native_symbol(ctx, "error", hex_symbol_error); hex_set_native_symbol(ctx, "try", hex_symbol_try); hex_set_native_symbol(ctx, "'", hex_symbol_q); + hex_set_native_symbol(ctx, "each", hex_symbol_each); hex_set_native_symbol(ctx, "map", hex_symbol_map); hex_set_native_symbol(ctx, "filter", hex_symbol_filter); hex_set_native_symbol(ctx, "swap", hex_symbol_swap);
M src/vm.csrc/vm.c

@@ -396,7 +396,7 @@

const char *symbol = hex_opcode_to_symbol(opcode); if (!symbol) { - hex_error(ctx, "[interpret bytecode nattive symbol] Invalid opcode for symbol"); + hex_error(ctx, "[interpret bytecode native symbol] Invalid opcode for symbol"); return 1; }