all repos — hex @ 4d681b98f8bd7dc2fcb67a87bb81d4b773156446

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

Implemented basic bytecode interpreter.
h3rald h3rald@h3rald.com
Wed, 18 Dec 2024 11:37:38 +0100
commit

4d681b98f8bd7dc2fcb67a87bb81d4b773156446

parent

e8b05dafba19cae2250aaa19bfc032dc23997442

2 files changed, 29 insertions(+), 24 deletions(-)

jump to
M src/hex.hsrc/hex.h

@@ -361,7 +361,8 @@ int hex_bytecode_symbol(hex_context_t *ctx, uint8_t **bytecode, size_t *size, size_t *capacity, const char *value);

int hex_interpret_bytecode_integer(hex_context_t *ctx, uint8_t **bytecode, size_t *size, hex_item_t *result); int hex_interpret_bytecode_string(hex_context_t *ctx, uint8_t **bytecode, size_t *size, hex_item_t *result); int hex_interpret_bytecode_native_symbol(hex_context_t *ctx, uint8_t opcode, size_t position, hex_item_t *result); -int hex_interpret_bytecode_user_symbol(hex_context_t *ctx, uint8_t **bytecode, size_t *size, hex_item_t *result); +int hex_interpret_bytecode_user_symbol(hex_context_t *ctx, uint8_t **bytecode, size_t *size, size_t position, hex_item_t *result); +int hex_interpret_bytecode_quotation(hex_context_t *ctx, uint8_t **bytecode, size_t *size, size_t position, hex_item_t *result); int hex_interpret_bytecode(hex_context_t *ctx, uint8_t *bytecode, size_t size); // REPL and initialization
M src/vm.csrc/vm.c

@@ -698,9 +698,9 @@ value[length] = '\0';

*bytecode += length; *size -= length; - hex_debug(ctx, "PUSHST[%d]: %s", length, value); hex_item_t item = hex_string_item(ctx, value); *result = item; + hex_debug(ctx, "PUSHST[%zu]: %s", length, value); free(value); return 0; }

@@ -739,7 +739,7 @@ *result = item;

return 0; } -int hex_interpret_bytecode_user_symbol(hex_context_t *ctx, uint8_t **bytecode, size_t *size, hex_item_t *result) +int hex_interpret_bytecode_user_symbol(hex_context_t *ctx, uint8_t **bytecode, size_t *size, size_t position, hex_item_t *result) { if (*size < 4) {

@@ -767,9 +767,14 @@ value[length] = '\0';

*bytecode += length; *size -= length; + hex_token_t *token = (hex_token_t *)malloc(sizeof(hex_token_t)); + token->value = (char *)value; + token->position.line = 0; + token->position.column = position; + hex_item_t item; item.type = HEX_TYPE_USER_SYMBOL; - item.data.str_value = value; + item.token = token; hex_debug(ctx, "LOOKUP[%d]: %s", length, value); free(value);

@@ -777,7 +782,7 @@ *result = item;

return 0; } -int hex_interpret_bytecode_quotation(hex_context_t *ctx, uint8_t **bytecode, size_t *size, hex_item_t *result) +int hex_interpret_bytecode_quotation(hex_context_t *ctx, uint8_t **bytecode, size_t *size, size_t position, hex_item_t *result) { if (*size < 4) {

@@ -789,8 +794,8 @@ *bytecode += 4;

*size -= 4; hex_debug(ctx, "PUSHQT[%zu]: <start>", n_items); - hex_item_t **items = NULL; - items = (hex_item_t **)malloc(n_items * sizeof(hex_item_t)); + hex_item_t **items = (hex_item_t **)malloc(n_items * sizeof(hex_item_t)); + if (!items) { hex_error(ctx, "Memory allocation failed");

@@ -803,41 +808,41 @@ uint8_t opcode = **bytecode;

(*bytecode)++; (*size)--; - hex_item_t *item = malloc(sizeof(hex_item_t)); + hex_item_t *item = (hex_item_t *)malloc(sizeof(hex_item_t)); switch (opcode) { case HEX_OP_PUSHIN: if (hex_interpret_bytecode_integer(ctx, bytecode, size, item) != 0) { - free(items); + hex_free_list(ctx, items, n_items); return 1; } break; case HEX_OP_PUSHST: if (hex_interpret_bytecode_string(ctx, bytecode, size, item) != 0) { - free(items); + hex_free_list(ctx, items, n_items); return 1; } break; case HEX_OP_LOOKUP: - if (hex_interpret_bytecode_user_symbol(ctx, bytecode, size, item) != 0) + if (hex_interpret_bytecode_user_symbol(ctx, bytecode, size, position, item) != 0) { - free(items); + hex_free_list(ctx, items, n_items); return 1; } break; case HEX_OP_PUSHQT: - if (hex_interpret_bytecode_quotation(ctx, bytecode, size, item) != 0) + if (hex_interpret_bytecode_quotation(ctx, bytecode, size, position, item) != 0) { - free(items); + hex_free_list(ctx, items, n_items); return 1; } break; default: if (hex_interpret_bytecode_native_symbol(ctx, opcode, *size, item) != 0) { - free(items); + hex_free_list(ctx, items, n_items); return 1; } break;

@@ -845,12 +850,10 @@ }

items[i] = item; } - hex_item_t quotation; - quotation.type = HEX_TYPE_QUOTATION; - quotation.data.quotation_value = items; - quotation.quotation_size = n_items; + result->type = HEX_TYPE_QUOTATION; + result->data.quotation_value = items; + result->quotation_size = n_items; - *result = quotation; hex_debug(ctx, "PUSHQT[%zu]: <end>", n_items); return 0; }

@@ -870,11 +873,11 @@ while (size > 0)

{ position = bytecode_size - size; uint8_t opcode = *bytecode; - hex_debug(ctx, "Processing bytecode at position: %zu, opcode: %u", position, opcode); + hex_debug(ctx, "Bytecode Position: %zu - opcode: %u", position, opcode); bytecode++; size--; - hex_item_t *item = malloc(sizeof(hex_item_t)); + hex_item_t *item = (hex_item_t *)malloc(sizeof(hex_item_t)); switch (opcode) { case HEX_OP_PUSHIN:

@@ -890,13 +893,14 @@ return 1;

} break; case HEX_OP_LOOKUP: - if (hex_interpret_bytecode_user_symbol(ctx, &bytecode, &size, item) != 0) + if (hex_interpret_bytecode_user_symbol(ctx, &bytecode, &size, position, item) != 0) { return 1; } break; case HEX_OP_PUSHQT: - if (hex_interpret_bytecode_quotation(ctx, &bytecode, &size, item) != 0) + + if (hex_interpret_bytecode_quotation(ctx, &bytecode, &size, position, item) != 0) { return 1; }