all repos — hex @ f27ecf68bbf8d888b463fe1a071e6f7e696a21ce

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

Fixes.
h3rald h3rald@h3rald.com
Mon, 23 Dec 2024 14:50:18 +0100
commit

f27ecf68bbf8d888b463fe1a071e6f7e696a21ce

parent

a0220a3a1d57e82b0876c17842aec8c8db3a80ce

4 files changed, 16 insertions(+), 163 deletions(-)

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

@@ -126,7 +126,7 @@

((args len 0x2 ==) ("two") ("no") if "two" ==) ((args len 0x3 !=) ("yes") when "yes" ==) (0x1 "tmp-a" : (tmp-a 0x3 <) (tmp-a 0x1 + "tmp-a" :) while tmp-a 0x3 ==) - (0x0 "tmp-b" : (0x1 0x2 0x3) (tmp-b + "tmp-b" :) each tmp-b 0x6 ==) + //(0x0 "tmp-b" : (0x1 0x2 0x3) (tmp-b + "tmp-b" :) each tmp-b 0x6 ==) ;60 ((0x2 0x0 /) (error "[symbol /] Division by zero" ==) try)

@@ -136,7 +136,7 @@ ((0x1 0x2 0x3) (dup dup * *) map (0x1 0x8 0x1b) ==)

;64 (0x2 dup stack (0x2 0x2) ==) - (clear stack () ==) + // (clear stack () ==) (0x1 0x2 swap pop stack (0x2) ==) ;68
M src/doc.csrc/doc.c

@@ -33,6 +33,7 @@ // Memory

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'."); + hex_set_doc(docs, "symbols", "", "q", "Pushes a quotation containing all registered symbols on the stack."); // Control flow hex_set_doc(docs, "if", "q q q", "*", "If 'q1' is not 0x0, executes 'q2', else 'q3'.");

@@ -40,13 +41,13 @@ hex_set_doc(docs, "when", "q1 q2", "*", "If 'q1' is not 0x0, executes 'q2'.");

hex_set_doc(docs, "while", "q1 q2", "*", "While 'q1' is not 0x0, executes 'q2'."); hex_set_doc(docs, "error", "", "s", "Returns the last error message."); hex_set_doc(docs, "try", "q1 q2", "*", "If 'q1' fails, executes 'q2'."); + hex_set_doc(docs, "throw", "s", "", "Throws error 's'."); // Stack hex_set_doc(docs, "dup", "a", "a a", "Duplicates 'a'."); hex_set_doc(docs, "pop", "a", "", "Removes the top item from the stack."); hex_set_doc(docs, "swap", "a1 a2", "a2 a1", "Swaps 'a2' with 'a1'."); hex_set_doc(docs, "stack", "", "q", "Returns the contents of the stack."); - hex_set_doc(docs, "clear", "", "", "Clears the stack."); // Evaluation hex_set_doc(docs, ".", "q", "*", "Pushes each item of 'q' on the stack.");

@@ -97,13 +98,8 @@ hex_set_doc(docs, "len", "(s|q)", "i ", "Returns the length of 's' or 'q'.");

hex_set_doc(docs, "get", "(s|q)", "a", "Gets the item at position 'i' in 's' or 'q'."); hex_set_doc(docs, "index", "(s a|q a)", "i", "Returns the index of 'a' within 's' or 'q'."); hex_set_doc(docs, "join", "q s", "s", "Joins the strings in 'q' using separator 's'."); - - // String 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."); // I/O
M src/hex.hsrc/hex.h

@@ -301,6 +301,7 @@

// Native symbols int hex_symbol_store(hex_context_t *ctx); int hex_symbol_free(hex_context_t *ctx); +int hex_symbol_symbols(hex_context_t *ctx); int hex_symbol_type(hex_context_t *ctx); int hex_symbol_i(hex_context_t *ctx); int hex_symbol_eval(hex_context_t *ctx);

@@ -352,16 +353,14 @@ int hex_symbol_run(hex_context_t *ctx);

int hex_symbol_if(hex_context_t *ctx); int hex_symbol_when(hex_context_t *ctx); int hex_symbol_while(hex_context_t *ctx); -int hex_symbol_each(hex_context_t *ctx); int hex_symbol_error(hex_context_t *ctx); int hex_symbol_try(hex_context_t *ctx); +int hex_symbol_throw(hex_context_t *ctx); int hex_symbol_q(hex_context_t *ctx); int hex_symbol_map(hex_context_t *ctx); -int hex_symbol_filter(hex_context_t *ctx); int hex_symbol_swap(hex_context_t *ctx); int hex_symbol_dup(hex_context_t *ctx); int hex_symbol_stack(hex_context_t *ctx); -int hex_symbol_clear(hex_context_t *ctx); int hex_symbol_pop(hex_context_t *ctx); // Opcodes
M src/symbols.csrc/symbols.c

@@ -66,7 +66,7 @@ return 1;

} if (value.type == HEX_TYPE_QUOTATION) { - value.operator = 1; + value.operator= 1; } if (hex_set_symbol(ctx, name.data.str_value, value, 0) != 0) {

@@ -119,20 +119,20 @@ return 0;

} int hex_symbol_symbols(hex_context_t *ctx) -{ +{ hex_item_t **quotation = (hex_item_t **)malloc(ctx->registry.size * sizeof(hex_item_t *)); - 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) * sizeof(char *)); strcpy(id, ctx->registry.entries[i].key); quotation[i] = (hex_item_t *)malloc(sizeof(hex_item_t)); - quotation[i] = hex_string_item(ctx, id); + *quotation[i] = hex_string_item(ctx, id); } if (hex_push_quotation(ctx, quotation, ctx->registry.size) != 0) - { - hex_free_list(ctx, quotation, list.quotation_size); - return 1; - } + { + hex_free_list(ctx, quotation, ctx->registry.size); + return 1; + } return 0; }

@@ -2211,53 +2211,6 @@ }

return 0; } -int hex_symbol_each(hex_context_t *ctx) -{ - - HEX_POP(ctx, action); - if (action.type == HEX_TYPE_INVALID) - { - HEX_FREE(ctx, action); - return 1; - } - HEX_POP(ctx, list); - if (list.type == HEX_TYPE_INVALID) - { - HEX_FREE(ctx, action); - HEX_FREE(ctx, list); - return 1; - } - if (list.type != HEX_TYPE_QUOTATION || action.type != HEX_TYPE_QUOTATION) - { - hex_error(ctx, "[symbol each] Two quotations required"); - HEX_FREE(ctx, action); - HEX_FREE(ctx, list); - return 1; - } - else - { - for (size_t i = 0; i < list.quotation_size; i++) - { - if (hex_push(ctx, *list.data.quotation_value[i]) != 0) - { - HEX_FREE(ctx, action); - HEX_FREE(ctx, list); - return 1; - } - for (size_t j = 0; j < action.quotation_size; j++) - { - if (hex_push(ctx, *action.data.quotation_value[j]) != 0) - { - HEX_FREE(ctx, action); - HEX_FREE(ctx, list); - return 1; - } - } - } - } - return 0; -} - int hex_symbol_error(hex_context_t *ctx) {

@@ -2454,92 +2407,7 @@ }

return 0; } -int hex_symbol_filter(hex_context_t *ctx) -{ - - HEX_POP(ctx, action); - if (action.type == HEX_TYPE_INVALID) - { - HEX_FREE(ctx, action); - return 1; - } - HEX_POP(ctx, list); - if (list.type == HEX_TYPE_INVALID) - { - HEX_FREE(ctx, action); - HEX_FREE(ctx, list); - return 1; - } - if (list.type != HEX_TYPE_QUOTATION || action.type != HEX_TYPE_QUOTATION) - { - hex_error(ctx, "[symbol filter] Two quotations required"); - HEX_FREE(ctx, action); - HEX_FREE(ctx, list); - return 1; - } - else - { - hex_item_t **quotation = (hex_item_t **)malloc(list.quotation_size * sizeof(hex_item_t *)); - if (!quotation) - { - hex_error(ctx, "[symbol filter] Memory allocation failed"); - HEX_FREE(ctx, action); - HEX_FREE(ctx, list); - return 1; - } - size_t count = 0; - for (size_t i = 0; i < list.quotation_size; i++) - { - if (hex_push(ctx, *list.data.quotation_value[i]) != 0) - { - HEX_FREE(ctx, action); - HEX_FREE(ctx, list); - hex_free_list(ctx, quotation, count); - return 1; - } - for (size_t j = 0; j < action.quotation_size; j++) - { - if (hex_push(ctx, *action.data.quotation_value[j]) != 0) - { - HEX_FREE(ctx, action); - HEX_FREE(ctx, list); - hex_free_list(ctx, quotation, count); - return 1; - } - } - HEX_POP(ctx, evalResult); - if (evalResult.type == HEX_TYPE_INTEGER && evalResult.data.int_value > 0) - { - quotation[count] = (hex_item_t *)malloc(sizeof(hex_item_t)); - if (!quotation[count]) - { - hex_error(ctx, "[symbol filter] Memory allocation failed"); - HEX_FREE(ctx, action); - HEX_FREE(ctx, list); - hex_free_list(ctx, quotation, count); - return 1; - } - *quotation[count] = *list.data.quotation_value[i]; - count++; - } - } - if (hex_push_quotation(ctx, quotation, count) != 0) - { - hex_error(ctx, "[symbol filter] An error occurred while pushing quotation"); - HEX_FREE(ctx, action); - HEX_FREE(ctx, list); - for (size_t i = 0; i < count; i++) - { - HEX_FREE(ctx, *quotation[i]); - } - return 1; - } - } - return 0; -} - // Stack manipulation symbols - int hex_symbol_swap(hex_context_t *ctx) {

@@ -2620,16 +2488,6 @@ }

return 0; } -int hex_symbol_clear(hex_context_t *ctx) -{ - for (size_t i = 0; i <= (size_t)ctx->stack.top; i++) - { - HEX_FREE(ctx, ctx->stack.entries[i]); - } - ctx->stack.top = -1; - return 0; -} - int hex_symbol_pop(hex_context_t *ctx) {

@@ -2647,6 +2505,7 @@ {

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, "symbols", hex_symbol_symbols); hex_set_native_symbol(ctx, "type", hex_symbol_type); hex_set_native_symbol(ctx, ".", hex_symbol_i); hex_set_native_symbol(ctx, "!", hex_symbol_eval);

@@ -2700,12 +2559,11 @@ hex_set_native_symbol(ctx, "when", hex_symbol_when);

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, "throw", hex_symbol_throw); 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, "swap", hex_symbol_swap); hex_set_native_symbol(ctx, "dup", hex_symbol_dup); hex_set_native_symbol(ctx, "stack", hex_symbol_stack); - hex_set_native_symbol(ctx, "clear", hex_symbol_clear); hex_set_native_symbol(ctx, "pop", hex_symbol_pop); }