all repos — hex @ 460edbbd4c0ed8c086ed7b1f771bed350156ca54

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

Investigating memory issues.
h3rald h3rald@h3rald.com
Fri, 22 Nov 2024 07:47:01 +0100
commit

460edbbd4c0ed8c086ed7b1f771bed350156ca54

parent

1fd820b8329e45800a9eee1ec0a39ec1e9d2956c

2 files changed, 60 insertions(+), 42 deletions(-)

jump to
M hex.chex.c

@@ -81,8 +81,8 @@ "args",

"exit", "exec", "run", + "if", "when", - "unless", "while", "each", "times",

@@ -271,10 +271,10 @@ result = hex_push(value);

} else { - hex_error("Undefined symbol: %s", element.symbolName); + hex_error("Undefined user symbol: %s", element.symbolName); result = 1; } - hex_free_element(value); + // hex_free_element(value); return result; } else if (element.type == HEX_TYPE_NATIVE_SYMBOL)

@@ -546,6 +546,7 @@ HEX_Token *token = (HEX_Token *)malloc(sizeof(HEX_Token));

token->value = NULL; token->line = *line; token->column = *column; + printf("Token: >%s<\n", ptr); if (*ptr == '"') {

@@ -645,6 +646,7 @@ size_t len = ptr - start;

token->value = (char *)malloc(len + 1); strncpy(token->value, start, len); token->value[len] = '\0'; + printf("Token value: >%s<\n", token->value); if (hex_valid_native_symbol(token->value) || hex_valid_user_symbol(token->value)) { token->type = HEX_TOKEN_SYMBOL;

@@ -701,7 +703,7 @@ {

if (token->type == HEX_TOKEN_QUOTATION_END) { balanced--; - hex_free_token(token); + // hex_free_token(token); break; }

@@ -775,7 +777,7 @@ }

quotation[size] = element; size++; - hex_free_token(token); + // hex_free_token(token); } if (balanced != 0)

@@ -1008,8 +1010,8 @@ {

hex_error("Failed to store variable"); result = 1; } - hex_free_element(name); - hex_free_element(value); + // hex_free_element(name); + // hex_free_element(value); return result; }

@@ -1084,7 +1086,7 @@ result = 1;

break; } } - hex_free_element(element); + // hex_free_element(element); return result; }

@@ -1134,7 +1136,6 @@ return 1;

} hex_print_element(stderr, element); printf("\n"); - hex_free_element(element); return 0; }

@@ -1147,7 +1148,6 @@ hex_free_element(element);

return 1; } hex_print_element(stdout, element); - hex_free_element(element); return 0; }

@@ -1667,8 +1667,8 @@ {

hex_error("'==' symbol requires two integers, two strings, or two quotations"); result = 1; } - hex_free_element(a); - hex_free_element(b); + // hex_free_element(a); + // hex_free_element(b); return result; }

@@ -2905,26 +2905,33 @@ ////////////////////////////////////////

// Control flow symbols // //////////////////////////////////////// -int hex_symbol_when() +int hex_symbol_if() { - - HEX_StackElement action = hex_pop(); - if (action.type == HEX_TYPE_INVALID) + HEX_StackElement elseBlock = hex_pop(); + if (elseBlock.type == HEX_TYPE_INVALID) { - hex_free_element(action); + hex_free_element(elseBlock); + return 1; + } + HEX_StackElement thenBlock = hex_pop(); + if (thenBlock.type == HEX_TYPE_INVALID) + { + hex_free_element(thenBlock); + hex_free_element(elseBlock); return 1; } HEX_StackElement condition = hex_pop(); if (condition.type == HEX_TYPE_INVALID) { - hex_free_element(action); hex_free_element(condition); + hex_free_element(thenBlock); + hex_free_element(elseBlock); return 1; } int result = 0; - if (condition.type != HEX_TYPE_QUOTATION || action.type != HEX_TYPE_QUOTATION) + if (condition.type != HEX_TYPE_QUOTATION || thenBlock.type != HEX_TYPE_QUOTATION || elseBlock.type != HEX_TYPE_QUOTATION) { - hex_error("'when' symbol requires two quotations"); + hex_error("'if' symbol requires three quotations"); result = 1; } else

@@ -2934,30 +2941,43 @@ {

if (hex_push(*condition.data.quotationValue[i]) != 0) { result = 1; - break; // Break if pushing the element failed + break; } } HEX_StackElement evalResult = hex_pop(); if (evalResult.type == HEX_TYPE_INTEGER && evalResult.data.intValue > 0) { - for (size_t i = 0; i < action.quotationSize; i++) + for (size_t i = 0; i < thenBlock.quotationSize; i++) { - if (hex_push(*action.data.quotationValue[i]) != 0) + if (hex_push(*thenBlock.data.quotationValue[i]) != 0) { result = 1; break; } } } - hex_free_element(evalResult); + else + { + for (size_t i = 0; i < elseBlock.quotationSize; i++) + { + if (hex_push(*elseBlock.data.quotationValue[i]) != 0) + { + result = 1; + break; + } + } + } + // hex_free_element(evalResult); } - hex_free_element(condition); - hex_free_element(action); + // hex_free_element(condition); + // hex_free_element(thenBlock); + // hex_free_element(elseBlock); return result; } -int hex_symbol_unless() +int hex_symbol_when() { + HEX_StackElement action = hex_pop(); if (action.type == HEX_TYPE_INVALID) {

@@ -2974,7 +2994,7 @@ }

int result = 0; if (condition.type != HEX_TYPE_QUOTATION || action.type != HEX_TYPE_QUOTATION) { - hex_error("'unless' symbol requires two quotations"); + hex_error("'when' symbol requires two quotations"); result = 1; } else

@@ -2988,7 +3008,7 @@ break; // Break if pushing the element failed

} } HEX_StackElement evalResult = hex_pop(); - if (evalResult.type == HEX_TYPE_INTEGER && evalResult.data.intValue == 0) + if (evalResult.type == HEX_TYPE_INTEGER && evalResult.data.intValue > 0) { for (size_t i = 0; i < action.quotationSize; i++) {

@@ -3506,8 +3526,8 @@ hex_set_native_symbol("args", hex_symbol_args);

hex_set_native_symbol("exit", hex_symbol_exit); hex_set_native_symbol("exec", hex_symbol_exec); hex_set_native_symbol("run", hex_symbol_run); + hex_set_native_symbol("if", hex_symbol_if); hex_set_native_symbol("when", hex_symbol_when); - hex_set_native_symbol("unless", hex_symbol_unless); hex_set_native_symbol("while", hex_symbol_while); hex_set_native_symbol("each", hex_symbol_each); hex_set_native_symbol("times", hex_symbol_times);

@@ -3550,8 +3570,8 @@ add_to_stack_trace(token);

} else if (token->type == HEX_TOKEN_QUOTATION_END) { - hex_error("Unexpected end of quotation"); - result = 1; + hex_error("Unexpected end of quotation"); + result = 1; } else if (token->type == HEX_TOKEN_QUOTATION_START) {
M tests.hextests.hex

@@ -1,12 +1,10 @@

( - "current-test" store - (concurrent-test i 0x0 >) - ("Test Failed: " print concurrent-test puts) - unless - (concurrent-test i 0x0 >) - ("." print) - when -) "test" store - + "value" store + (value 0x1 ==) + ("YES" puts) + ("NO" puts) + if +) "simple-test" store -(0x1 "a1" store a1 0x1 == "a1" free) test i +0x1 simple-test i +0x2 simple-test i