all repos — hex @ 9e198fc76c8ebef0246ee2e40abd3befee4f9565

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

experimenting with operator symbol
h3rald h3rald@h3rald.com
Sun, 22 Dec 2024 16:39:29 +0100
commit

9e198fc76c8ebef0246ee2e40abd3befee4f9565

parent

03ccdf7988588085c6dc4a0607da66393c1409b8

4 files changed, 61 insertions(+), 7 deletions(-)

jump to
M src/doc.csrc/doc.c

@@ -30,7 +30,8 @@

void hex_create_docs(hex_doc_dictionary_t *docs) { // Memory - hex_set_doc(docs, ":", "a s", "", "Stores 'a' as symbol 's'."); + hex_set_doc(docs, ":", "a s", "", "Stores 'a' as the literal symbol 's'."); + hex_set_doc(docs, "::", "a s", "", "Defines 'a' as the operator symbol 's'."); hex_set_doc(docs, "#", "s", "", "Deletes user symbol 's'."); // Control flow

@@ -102,7 +103,6 @@ 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

@@ -78,6 +78,7 @@ char *str_value;

int (*fn_value)(hex_context_t *); struct hex_item_t **quotation_value; } data; + int immediate; hex_token_t *token; // Token containing stack information (valid for HEX_TYPE_NATIVE_SYMBOL and HEX_TYPE_USER_SYMBOL) size_t quotation_size; // Size of the quotation (valid for HEX_TYPE_QUOTATION) } hex_item_t;

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

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

@@ -219,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/stack.csrc/stack.c

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

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

@@ -41,6 +41,43 @@ }

return 0; } +int hex_symbol_define(hex_context_t *ctx) +{ + + HEX_POP(ctx, name); + if (name.type == HEX_TYPE_INVALID) + { + HEX_FREE(ctx, name); + return 1; + } + HEX_POP(ctx, value); + if (value.type == HEX_TYPE_INVALID) + { + HEX_FREE(ctx, name); + HEX_FREE(ctx, value); + return 1; + } + 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) + { + value.immediate = 1; + } + 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_FREE(ctx, name); + HEX_FREE(ctx, value); + return 1; + } + return 0; +} + int hex_symbol_free(hex_context_t *ctx) {

@@ -2156,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);

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

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

@@ -2572,6 +2609,7 @@

void hex_register_symbols(hex_context_t *ctx) { hex_set_native_symbol(ctx, ":", hex_symbol_store); + hex_set_native_symbol(ctx, "::", hex_symbol_define); hex_set_native_symbol(ctx, "#", hex_symbol_free); hex_set_native_symbol(ctx, "type", hex_symbol_type); hex_set_native_symbol(ctx, ".", hex_symbol_i);

@@ -2624,7 +2662,6 @@ hex_set_native_symbol(ctx, "run", hex_symbol_run);

hex_set_native_symbol(ctx, "if", hex_symbol_if); hex_set_native_symbol(ctx, "when", hex_symbol_when); hex_set_native_symbol(ctx, "while", hex_symbol_while); - hex_set_native_symbol(ctx, "each", hex_symbol_each); 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);