Refactoring.
h3rald h3rald@h3rald.com
Thu, 19 Dec 2024 13:41:41 +0100
6 files changed,
28 insertions(+),
21 deletions(-)
M
Makefile
→
Makefile
@@ -7,7 +7,7 @@
hex: src/hex.c $(CC) $(CFLAGS) $(LDFLAGS) $< -o hex -src/hex.c: src/hex.h src/error.c src/help.c src/helpers.c src/interpreter.c src/main.c src/parser.c src/registry.c src/stack.c src/stacktrace.c src/symbols.c src/vm.c src/symboltable.c src/opcodes.c +src/hex.c: src/hex.h src/error.c src/doc.c src/utils.c src/interpreter.c src/main.c src/parser.c src/registry.c src/stack.c src/stacktrace.c src/symbols.c src/vm.c src/symboltable.c src/opcodes.c bash scripts/amalgamate.sh web/assets/hex.wasm: src/hex.c web/assets/hex-playground.js
M
scripts/amalgamate.sh
→
scripts/amalgamate.sh
@@ -6,14 +6,14 @@ source_files=(
"src/stack.c" "src/registry.c" "src/error.c" - "src/help.c" + "src/doc.c" "src/stacktrace.c" "src/parser.c" "src/symboltable.c" "src/opcodes.c" "src/vm.c" "src/interpreter.c" - "src/helpers.c" + "src/utils.c" "src/symbols.c" "src/main.c" )
M
src/helpers.c
→
src/utils.c
@@ -35,7 +35,7 @@ });
#endif -static void hex_rpad(const char *str, int total_length) +void hex_rpad(const char *str, int total_length) { int len = strlen(str); printf("%s", str);@@ -44,7 +44,7 @@ {
printf(" "); } } -static void hex_lpad(const char *str, int total_length) +void hex_lpad(const char *str, int total_length) { int len = strlen(str); for (int i = len; i < total_length; i++)@@ -218,3 +218,15 @@ fprintf(stream, "<unknown>");
break; } } + +void hex_encode_length(uint8_t **bytecode, size_t *size, size_t length) +{ + while (length >= 0x80) + { + (*bytecode)[*size] = (length & 0x7F) | 0x80; + length >>= 7; + (*size)++; + } + (*bytecode)[*size] = length & 0x7F; + (*size)++; +}
M
src/hex.h
→
src/hex.h
@@ -285,12 +285,15 @@ int32_t hex_parse_integer(const char *hex_str);
int hex_parse_quotation(hex_context_t *ctx, const char **input, hex_item_t *result, hex_file_position_t *position); int hex_interpret(hex_context_t *ctx, const char *code, const char *filename, int line, int column); -// Helpers +// Utils char *hex_itoa(int num, int base); char *hex_itoa_dec(int num); char *hex_itoa_hex(int num); void hex_raw_print_item(FILE *stream, hex_item_t item); char *hex_type(hex_item_type_t type); +void hex_rpad(const char *str, int total_length); +void hex_lpad(const char *str, int total_length); +void hex_encode_length(uint8_t **bytecode, size_t *size, size_t length); // Native symbols int hex_symbol_store(hex_context_t *ctx);@@ -357,6 +360,10 @@ 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 +uint8_t hex_symbol_to_opcode(const char *symbol); +const char *hex_opcode_to_symbol(uint8_t opcode); // VM int hex_bytecode(hex_context_t *ctx, const char *input, uint8_t **output, size_t *output_size, hex_file_position_t *position);
M
src/vm.c
→
src/vm.c
@@ -6,18 +6,6 @@ ////////////////////////////////////////
// Virtual Machine // //////////////////////////////////////// -static void encode_length(uint8_t **bytecode, size_t *size, size_t length) -{ - while (length >= 0x80) - { - (*bytecode)[*size] = (length & 0x7F) | 0x80; - length >>= 7; - (*size)++; - } - (*bytecode)[*size] = length & 0x7F; - (*size)++; -} - int hex_bytecode_integer(hex_context_t *ctx, uint8_t **bytecode, size_t *size, size_t *capacity, int32_t value) { hex_debug(ctx, "PUSHIN: %d", value);@@ -53,7 +41,7 @@ else
{ int_length = 4; } - encode_length(bytecode, size, int_length); + hex_encode_length(bytecode, size, int_length); // Encode the integer value in the minimum number of bytes, in little endian if (value >= -0x80 && value < 0x80) {@@ -102,7 +90,7 @@ *bytecode = new_bytecode;
} (*bytecode)[*size] = HEX_OP_PUSHST; *size += 1; // opcode - encode_length(bytecode, size, len); + hex_encode_length(bytecode, size, len); memcpy(&(*bytecode)[*size], value, len); *size += len; return 0;@@ -172,7 +160,7 @@ *bytecode = new_bytecode;
} (*bytecode)[*size] = HEX_OP_PUSHQT; *size += 1; // opcode - encode_length(bytecode, size, *n_items); + hex_encode_length(bytecode, size, *n_items); memcpy(&(*bytecode)[*size], *output, *output_size); *size += *output_size; hex_debug(ctx, "PUSHQT: <end> (items: %d)", *n_items);