Fixes.
h3rald h3rald@h3rald.com
Mon, 16 Dec 2024 14:36:05 +0100
4 files changed,
23 insertions(+),
11 deletions(-)
M
.gitignore
→
.gitignore
@@ -54,6 +54,7 @@ # Exe names
hex # Other +*.hexb src/hex.c example.hex web/out/
M
Makefile
→
Makefile
@@ -7,13 +7,13 @@
hex: src/hex.c $(CC) $(CFLAGS) $(LDFLAGS) $< -o hex -src/hex.c: +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 bash scripts/amalgamate.sh -web/assets/hex.wasm: src/hex.c +web/assets/hex.wasm: src/hex.c web/assets/hex-playground.js emcc -O2 -sASYNCIFY -DBROWSER -sEXPORTED_RUNTIME_METHODS=stringToUTF8 src/hex.c -o web/assets/hex.js --pre-js web/assets/hex-playground.js -hex.wasm: src/hex.c +hex.wasm: src/hex.c src/hex.node.js emcc -O2 -sASYNCIFY -sEXPORTED_RUNTIME_METHODS=stringToUTF8 src/hex.c -o hex.js --pre-js src/hex.node.js ape: src/hex.c
M
src/hex.h
→
src/hex.h
@@ -27,7 +27,7 @@ #define HEX_STACK_SIZE 128
#define HEX_STACK_TRACE_SIZE 16 #define HEX_NATIVE_SYMBOLS 64 -const uint8_t HEX_BYTECODE_HEADER[6] = {0x01, 0x48, 0x45, 0x78, 0x01, 0x02}; +const uint8_t HEX_BYTECODE_HEADER[6] = {0x01, 0x68, 0x65, 0x78, 0x01, 0x02}; // Type Definitions typedef enum hex_item_type_t@@ -360,6 +360,7 @@ hex_context_t hex_init();
void hex_repl(hex_context_t *ctx); void hex_process_stdin(hex_context_t *ctx); void hex_handle_sigint(int sig); +int hex_write_bytecode_file(hex_context_t *ctx, char *filename, uint8_t *bytecode, size_t size); char *hex_read_file(hex_context_t *ctx, const char *filename); // Common operations
M
src/main.c
→
src/main.c
@@ -222,6 +222,7 @@ "ARGUMENTS\n"
" file A .hex file to interpret\n" "\n" "OPTIONS\n" + " -b, --bytecode Generate bytecode file.\n" " -d, --debug Enable debug mode.\n" " -h, --help Display this help message.\n" " -m, --manual Display the manual.\n"@@ -271,6 +272,20 @@ hex_rpad(docs->entries[i].description, 47);
printf(" |\n"); } printf(" +---------+----------------------------+-------------------------------------------------+\n"); +} + +int hex_write_bytecode_file(hex_context_t *ctx, char *filename, uint8_t *bytecode, size_t size) +{ + FILE *file = fopen(filename, "wb"); + if (file == NULL) + { + hex_error(ctx, "Failed to write file: %s", filename); + return 1; + } + fwrite(HEX_BYTECODE_HEADER, 1, 6, file); + fwrite(bytecode, 1, size, file); + fclose(file); + return 0; } ////////////////////////////////////////@@ -343,15 +358,10 @@ {
hex_error(&ctx, "Failed to generate bytecode"); return 1; } - for (size_t i = 0; i < 6; i++) + if (hex_write_bytecode_file(&ctx, strcat(file, "b"), bytecode, bytecode_size) != 0) { - printf("%02x ", HEX_BYTECODE_HEADER[i]); + return 1; } - for (size_t i = 0; i < bytecode_size; i++) - { - printf("%02x ", bytecode[i]); - } - printf("\n"); } else {