Added the possibility to load library files before loading the main file.
h3rald h3rald@h3rald.com
Mon, 19 May 2025 09:53:58 +0200
3 files changed,
158 insertions(+),
98 deletions(-)
M
src/hex.c
→
src/hex.c
@@ -412,6 +412,7 @@ 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); +int hex_interpret_file(hex_context_t *ctx, const char *file); char *hex_read_file(hex_context_t *ctx, const char *filename); // Common operations@@ -6626,6 +6627,9 @@ "OPTIONS\n"
" -b, --bytecode Generate a .hbx bytecode file.\n" " -d, --debug Enable debug mode.\n" " -h, --help Display this help message.\n" + " -l, --load Load a .hex or .hbx file before interpreting\n" + " the main file or starting the REPL\n" + " (can be specified multiple times).\n" " -m, --manual Display the manual.\n" " -v, --version Display hex version.\n\n"); }@@ -6698,6 +6702,44 @@ hex_debug(ctx, "Bytecode file written: %s", filename);
return 0; } +int hex_interpret_file(hex_context_t *ctx, const char *file) +{ + int result = 0; + if (strstr(file, ".hbx") != NULL) + { + FILE *bytecode_file = fopen(file, "rb"); + if (bytecode_file == NULL) + { + hex_error(ctx, "[open hbx file] Failed to open bytecode file: %s", file); + return 1; + } + fseek(bytecode_file, 0, SEEK_END); + size_t bytecode_size = ftell(bytecode_file); + fseek(bytecode_file, 0, SEEK_SET); + uint8_t *bytecode = (uint8_t *)malloc(bytecode_size); + if (bytecode == NULL) + { + hex_error(ctx, "[read hbx file] Memory allocation failed"); + fclose(bytecode_file); + return 1; + } + fread(bytecode, 1, bytecode_size, bytecode_file); + fclose(bytecode_file); + result = hex_interpret_bytecode(ctx, bytecode, bytecode_size, file); + free(bytecode); + } + else + { + char *fileContent = hex_read_file(ctx, file); + if (fileContent == NULL) + { + return 1; + } + result = hex_interpret(ctx, fileContent, file, 1 + ctx->hashbang, 1); + } + return result; +} + //////////////////////////////////////// // Main Program // ////////////////////////////////////////@@ -6747,6 +6789,20 @@ else if ((strcmp(arg, "-b") == 0 || strcmp(arg, "--bytecode") == 0))
{ generate_bytecode = 1; } + else if ((strcmp(arg, "-l") == 0 || strcmp(arg, "--load") == 0)) + { + // interpret a library file (e.g. hexlib.hex or hexlib.hbx) + // before executing the main file + i++; + if (i >= argc) + { + hex_error(ctx, "[load] No file specified"); + return 1; + } + char *libfile = strdup(argv[i]); + hex_interpret_file(ctx, libfile); + free(libfile); + } else { file = arg;@@ -6754,66 +6810,40 @@ }
} if (file) { - if (strstr(file, ".hbx") != NULL) + if (generate_bytecode) { - FILE *bytecode_file = fopen(file, "rb"); - if (bytecode_file == NULL) + uint8_t *bytecode; + size_t bytecode_size = 0; + hex_file_position_t position; + position.column = 1; + position.line = 1 + ctx->hashbang; + position.filename = file; + char *bytecode_file = strdup(file); + char *ext = strrchr(bytecode_file, '.'); + char *fileContent = hex_read_file(ctx, file); + if (ext != NULL) { - hex_error(ctx, "[open hbx file] Failed to open bytecode file: %s", file); + strcpy(ext, ".hbx"); + } + else + { + strcat(bytecode_file, ".hbx"); + } + if (hex_bytecode(ctx, fileContent, &bytecode, &bytecode_size, &position) != 0) + { + hex_error(ctx, "[generate bytecode] Failed to generate bytecode"); return 1; } - fseek(bytecode_file, 0, SEEK_END); - size_t bytecode_size = ftell(bytecode_file); - fseek(bytecode_file, 0, SEEK_SET); - uint8_t *bytecode = (uint8_t *)malloc(bytecode_size); - if (bytecode == NULL) + if (hex_write_bytecode_file(ctx, bytecode_file, bytecode, bytecode_size) != 0) { - hex_error(ctx, "[read hbx file] Memory allocation failed"); - fclose(bytecode_file); return 1; } - fread(bytecode, 1, bytecode_size, bytecode_file); - fclose(bytecode_file); - hex_interpret_bytecode(ctx, bytecode, bytecode_size, file); - free(bytecode); + return 0; } else { - char *fileContent = hex_read_file(ctx, file); - if (generate_bytecode) - { - uint8_t *bytecode; - size_t bytecode_size = 0; - hex_file_position_t position; - position.column = 1; - position.line = 1 + ctx->hashbang; - position.filename = file; - char *bytecode_file = strdup(file); - char *ext = strrchr(bytecode_file, '.'); - if (ext != NULL) - { - strcpy(ext, ".hbx"); - } - else - { - strcat(bytecode_file, ".hbx"); - } - if (hex_bytecode(ctx, fileContent, &bytecode, &bytecode_size, &position) != 0) - { - hex_error(ctx, "[generate bytecode] Failed to generate bytecode"); - return 1; - } - if (hex_write_bytecode_file(ctx, bytecode_file, bytecode, bytecode_size) != 0) - { - return 1; - } - } - else - { - hex_interpret(ctx, fileContent, file, 1 + ctx->hashbang, 1); - } + return hex_interpret_file(ctx, file); } - return 0; } } #if !(__EMSCRIPTEN__)
M
src/hex.h
→
src/hex.h
@@ -409,6 +409,7 @@ 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); +int hex_interpret_file(hex_context_t *ctx, const char *file); char *hex_read_file(hex_context_t *ctx, const char *filename); // Common operations
M
src/main.c
→
src/main.c
@@ -228,6 +228,9 @@ "OPTIONS\n"
" -b, --bytecode Generate a .hbx bytecode file.\n" " -d, --debug Enable debug mode.\n" " -h, --help Display this help message.\n" + " -l, --load Load a .hex or .hbx file before interpreting\n" + " the main file or starting the REPL\n" + " (can be specified multiple times).\n" " -m, --manual Display the manual.\n" " -v, --version Display hex version.\n\n"); }@@ -300,6 +303,44 @@ hex_debug(ctx, "Bytecode file written: %s", filename);
return 0; } +int hex_interpret_file(hex_context_t *ctx, const char *file) +{ + int result = 0; + if (strstr(file, ".hbx") != NULL) + { + FILE *bytecode_file = fopen(file, "rb"); + if (bytecode_file == NULL) + { + hex_error(ctx, "[open hbx file] Failed to open bytecode file: %s", file); + return 1; + } + fseek(bytecode_file, 0, SEEK_END); + size_t bytecode_size = ftell(bytecode_file); + fseek(bytecode_file, 0, SEEK_SET); + uint8_t *bytecode = (uint8_t *)malloc(bytecode_size); + if (bytecode == NULL) + { + hex_error(ctx, "[read hbx file] Memory allocation failed"); + fclose(bytecode_file); + return 1; + } + fread(bytecode, 1, bytecode_size, bytecode_file); + fclose(bytecode_file); + result = hex_interpret_bytecode(ctx, bytecode, bytecode_size, file); + free(bytecode); + } + else + { + char *fileContent = hex_read_file(ctx, file); + if (fileContent == NULL) + { + return 1; + } + result = hex_interpret(ctx, fileContent, file, 1 + ctx->hashbang, 1); + } + return result; +} + //////////////////////////////////////// // Main Program // ////////////////////////////////////////@@ -349,6 +390,20 @@ else if ((strcmp(arg, "-b") == 0 || strcmp(arg, "--bytecode") == 0))
{ generate_bytecode = 1; } + else if ((strcmp(arg, "-l") == 0 || strcmp(arg, "--load") == 0)) + { + // interpret a library file (e.g. hexlib.hex or hexlib.hbx) + // before executing the main file + i++; + if (i >= argc) + { + hex_error(ctx, "[load] No file specified"); + return 1; + } + char *libfile = strdup(argv[i]); + hex_interpret_file(ctx, libfile); + free(libfile); + } else { file = arg;@@ -356,66 +411,40 @@ }
} if (file) { - if (strstr(file, ".hbx") != NULL) + if (generate_bytecode) { - FILE *bytecode_file = fopen(file, "rb"); - if (bytecode_file == NULL) + uint8_t *bytecode; + size_t bytecode_size = 0; + hex_file_position_t position; + position.column = 1; + position.line = 1 + ctx->hashbang; + position.filename = file; + char *bytecode_file = strdup(file); + char *ext = strrchr(bytecode_file, '.'); + char *fileContent = hex_read_file(ctx, file); + if (ext != NULL) { - hex_error(ctx, "[open hbx file] Failed to open bytecode file: %s", file); + strcpy(ext, ".hbx"); + } + else + { + strcat(bytecode_file, ".hbx"); + } + if (hex_bytecode(ctx, fileContent, &bytecode, &bytecode_size, &position) != 0) + { + hex_error(ctx, "[generate bytecode] Failed to generate bytecode"); return 1; } - fseek(bytecode_file, 0, SEEK_END); - size_t bytecode_size = ftell(bytecode_file); - fseek(bytecode_file, 0, SEEK_SET); - uint8_t *bytecode = (uint8_t *)malloc(bytecode_size); - if (bytecode == NULL) + if (hex_write_bytecode_file(ctx, bytecode_file, bytecode, bytecode_size) != 0) { - hex_error(ctx, "[read hbx file] Memory allocation failed"); - fclose(bytecode_file); return 1; } - fread(bytecode, 1, bytecode_size, bytecode_file); - fclose(bytecode_file); - hex_interpret_bytecode(ctx, bytecode, bytecode_size, file); - free(bytecode); + return 0; } else { - char *fileContent = hex_read_file(ctx, file); - if (generate_bytecode) - { - uint8_t *bytecode; - size_t bytecode_size = 0; - hex_file_position_t position; - position.column = 1; - position.line = 1 + ctx->hashbang; - position.filename = file; - char *bytecode_file = strdup(file); - char *ext = strrchr(bytecode_file, '.'); - if (ext != NULL) - { - strcpy(ext, ".hbx"); - } - else - { - strcat(bytecode_file, ".hbx"); - } - if (hex_bytecode(ctx, fileContent, &bytecode, &bytecode_size, &position) != 0) - { - hex_error(ctx, "[generate bytecode] Failed to generate bytecode"); - return 1; - } - if (hex_write_bytecode_file(ctx, bytecode_file, bytecode, bytecode_size) != 0) - { - return 1; - } - } - else - { - hex_interpret(ctx, fileContent, file, 1 + ctx->hashbang, 1); - } + return hex_interpret_file(ctx, file); } - return 0; } } #if !(__EMSCRIPTEN__)