all repos — hex @ 25096928db14b7cebb141a8c474d673bfbbbfa0b

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

Merge branch 'next' of github.com:h3rald/hex into next
h3rald h3rald@h3rald.com
Fri, 03 Jan 2025 20:23:50 +0000
commit

25096928db14b7cebb141a8c474d673bfbbbfa0b

parent

a7aa09cdd73eb13a8612a967d73b8606760d5fa5

6 files changed, 72 insertions(+), 76 deletions(-)

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

@@ -9,6 +9,17 @@ 0x0 "result" :

(dup puts) "_" :: +( + "_when_q" : + "_when_cond" : + (_when_cond .) + (_when_q .) + () + if + "_when_q" # + "_when_cond" # +) "when" :: + ( "current-test" :

@@ -125,7 +136,7 @@ (args 0x1 get "scripts/test.hbx" ==)

;56 ((args len 0x2 ==) ("two") ("no") if "two" ==) - ((args len 0x3 !=) ("yes") when "yes" ==) + ((dup *) "square" :: 0x2 square 0x4 == "square" #) (0x1 "tmp-a" : (tmp-a 0x3 <) (tmp-a 0x1 + "tmp-a" :) while tmp-a 0x3 ==) (symbols len 0x4c ==) ;60

@@ -220,7 +231,6 @@ (("t" chr) (error) try "[symbol chr] Integer required" ==)

((() chr) (error) try "[symbol chr] Integer required" ==) ;120 - ((dup *) "square" :: 0x2 square 0x4 == "square" #) ( "test\ntest\\ntest\t\"hello\"!" "_str" : _str "test1.txt" write
M scripts/web.hexscripts/web.hex

@@ -78,6 +78,18 @@

; Convenience symbol for debugging (dup puts) "_" :: +; when operator +( + "_when_q" : + "_when_cond" : + (_when_cond .) + (_when_q .) + () + if + "_when_q" # + "_when_cond" # +) "when" :: + ; Generate tag placeholder ( "{{" swap "}}" cat cat
M src/doc.csrc/doc.c

@@ -44,7 +44,6 @@ 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'."); - 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'.");

@@ -60,6 +59,7 @@ // Evaluation

hex_set_doc(docs, ".", "q", "*", "Pushes each item of 'q' on the stack."); hex_set_doc(docs, "!", "(s|q)", "*", "Evaluates 's' as a hex program or 'q' as hex bytecode."); hex_set_doc(docs, "'", "a", "q", "Wraps 'a' in a quotation."); + hex_set_doc(docs, "debug", "q", "*", "Enables debug mode and pushes each item of 'q' on the stack."); // Arithmetic hex_set_doc(docs, "+", "i1 12", "i", "Adds two integers.");
M src/hex.hsrc/hex.h

@@ -170,20 +170,20 @@ HEX_OP_FREE = 0x12,

HEX_OP_SYMBOLS = 0x13, HEX_OP_IF = 0x14, - HEX_OP_WHEN = 0x15, - HEX_OP_WHILE = 0x16, - HEX_OP_ERROR = 0x17, - HEX_OP_TRY = 0x18, - HEX_OP_THROW = 0x19, + HEX_OP_WHILE = 0x15, + HEX_OP_ERROR = 0x16, + HEX_OP_TRY = 0x17, + HEX_OP_THROW = 0x18, - HEX_OP_DUP = 0x1a, - HEX_OP_STACK = 0x1b, - HEX_OP_POP = 0x1c, - HEX_OP_SWAP = 0x1d, + HEX_OP_DUP = 0x19, + HEX_OP_STACK = 0x1a, + HEX_OP_POP = 0x1b, + HEX_OP_SWAP = 0x1c, - HEX_OP_I = 0x1e, - HEX_OP_EVAL = 0x1f, - HEX_OP_QUOTE = 0x20, + HEX_OP_I = 0x1d, + HEX_OP_EVAL = 0x1e, + HEX_OP_QUOTE = 0x1f, + HEX_OP_DEBUG = 0x20, HEX_OP_ADD = 0x21, HEX_OP_SUB = 0x22,
M src/opcodes.csrc/opcodes.c

@@ -25,10 +25,6 @@ else if (strcmp(symbol, "if") == 0)

{ return HEX_OP_IF; } - else if (strcmp(symbol, "when") == 0) - { - return HEX_OP_WHEN; - } else if (strcmp(symbol, "while") == 0) { return HEX_OP_WHILE;

@@ -72,6 +68,10 @@ }

else if (strcmp(symbol, "'") == 0) { return HEX_OP_QUOTE; + } + else if (strcmp(symbol, "debug") == 0) + { + return HEX_OP_DEBUG; } else if (strcmp(symbol, "+") == 0) {

@@ -278,8 +278,6 @@ case HEX_OP_SYMBOLS:

return "symbols"; case HEX_OP_IF: return "if"; - case HEX_OP_WHEN: - return "when"; case HEX_OP_WHILE: return "while"; case HEX_OP_ERROR:

@@ -302,6 +300,8 @@ case HEX_OP_EVAL:

return "!"; case HEX_OP_QUOTE: return "'"; + case HEX_OP_DEBUG: + return "debug"; case HEX_OP_ADD: return "+"; case HEX_OP_SUB:
M src/symbols.csrc/symbols.c

@@ -287,6 +287,34 @@ return 1;

} } +int hex_symbol_debug(hex_context_t *ctx) +{ + HEX_POP(ctx, item); + if (item->type == HEX_TYPE_INVALID) + { + HEX_FREE(ctx, item); + return 1; + } + if (item->type != HEX_TYPE_QUOTATION) + { + hex_error(ctx, "[symbol debug] Quotation required", hex_type(item->type)); + HEX_FREE(ctx, item); + return 1; + } + ctx->settings->debugging_enabled = 1; + for (size_t i = 0; i < item->quotation_size; i++) + { + if (hex_push(ctx, item->data.quotation_value[i]) != 0) + { + //HEX_FREE(ctx, item); + ctx->settings->debugging_enabled = 0; + return 1; + } + } + ctx->settings->debugging_enabled = 0; + return 0; +} + // IO Symbols int hex_symbol_puts(hex_context_t *ctx)

@@ -2277,60 +2305,6 @@ }

return 0; } -int hex_symbol_when(hex_context_t *ctx) -{ - HEX_POP(ctx, action); - if (action->type == HEX_TYPE_INVALID) - { - HEX_FREE(ctx, action); - return 1; - } - HEX_POP(ctx, condition); - ; - if (condition->type == HEX_TYPE_INVALID) - { - HEX_FREE(ctx, action); - HEX_FREE(ctx, condition); - return 1; - } - - int result = 0; - if (condition->type != HEX_TYPE_QUOTATION || action->type != HEX_TYPE_QUOTATION) - { - hex_error(ctx, "[symbol when] Two quotations required"); - result = 1; - } - else - { - for (size_t i = 0; i < condition->quotation_size; i++) - { - if (hex_push(ctx, condition->data.quotation_value[i]) != 0) - { - result = 1; - break; // Break if pushing the item failed - } - } - HEX_POP(ctx, evalResult); - if (evalResult->type == HEX_TYPE_INTEGER && evalResult->data.int_value > 0) - { - for (size_t i = 0; i < action->quotation_size; i++) - { - if (hex_push(ctx, action->data.quotation_value[i]) != 0) - { - result = 1; - break; - } - } - } - } - if (result != 0) - { - HEX_FREE(ctx, action); - HEX_FREE(ctx, condition); - } - return result; -} - int hex_symbol_while(hex_context_t *ctx) { HEX_POP(ctx, action);

@@ -2731,6 +2705,7 @@ 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); + hex_set_native_symbol(ctx, "debug", hex_symbol_debug); hex_set_native_symbol(ctx, "puts", hex_symbol_puts); hex_set_native_symbol(ctx, "warn", hex_symbol_warn); hex_set_native_symbol(ctx, "print", hex_symbol_print);

@@ -2777,7 +2752,6 @@ hex_set_native_symbol(ctx, "exit", hex_symbol_exit);

hex_set_native_symbol(ctx, "exec", hex_symbol_exec); 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, "error", hex_symbol_error); hex_set_native_symbol(ctx, "try", hex_symbol_try);