From: Brendan Hansen Date: Sun, 5 Mar 2023 04:16:51 +0000 (-0600) Subject: bugfix: exporting aliased symbol; added: better cli help X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=b41504ca6e05fdc0e208ef34f3f2267b89c19a2b;p=onyx.git bugfix: exporting aliased symbol; added: better cli help --- diff --git a/compiler/include/astnodes.h b/compiler/include/astnodes.h index 09d3dc2d..f078fbc6 100644 --- a/compiler/include/astnodes.h +++ b/compiler/include/astnodes.h @@ -1606,6 +1606,7 @@ struct CompileOptions { const char* target_file; const char* documentation_file; const char* symbol_info_file; + const char* help_subcommand; b32 debug_enabled; diff --git a/compiler/src/onyx.c b/compiler/src/onyx.c index 8ee38dc3..13f2ab5c 100644 --- a/compiler/src/onyx.c +++ b/compiler/src/onyx.c @@ -16,41 +16,57 @@ Context context; - -static const char* docstring = "Onyx toolchain version " VERSION "\n" - "\n" - "The toolchain for the Onyx programming language, created by Brendan Hansen.\n" +#define DOCSTRING_HEADER "Onyx toolchain version " VERSION "\n" \ + "\n" \ + "The toolchain for the Onyx programming language, created by Brendan Hansen.\n" \ "\n" + + + +static const char* top_level_docstring = DOCSTRING_HEADER "Usage:\n" - "\tonyx compile [-o ] [--verbose] \n" - "\tonyx check \n" + "\tonyx \n" + "\n" + "Subcommands:\n" + "\thelp Shows this help message. Use \"onyx help \".\n" + "\tbuild Compiles an Onyx program into an executable.\n" #ifdef ENABLE_RUN_WITH_WASMER - "\tonyx run -- \n" + "\trun Compiles and runs an Onyx program, all at once.\n" #endif - "\tonyx pkg\n" - // "\tonyx doc \n" - "\tonyx help\n" + "\tcheck Checks syntax and types of an Onyx program.\n" + "\tpackage Package manager\n"; + // "\tdoc \n" + +static const char *build_docstring = DOCSTRING_HEADER + "Usage:\n" + "\tonyx %s [-o target_file] OPTIONS\n" + "\n" + "Required:\n" + "\t One or more Onyx files to include in the program.\n" "\n" - "Compile Flags:\n" - "\t List of initial files\n" + "Options:\n" "\t-o Specify the target file (default: out.wasm).\n" "\t --output \n" + "\t-I Include a directory in the search path.\n" "\t--runtime, -r Specifies the runtime. Can be: onyx, wasi, js, custom.\n" + "\t (default: onyx)\n" "\t--verbose, -V Verbose output.\n" "\t -VV Very verbose output.\n" "\t -VVV Very very verbose output (to be used by compiler developers).\n" + "\t--no-std Disable automatically including \"core/std\".\n" "\t--wasm-mvp Use only WebAssembly MVP features.\n" "\t--multi-threaded Enables multi-threading for this compilation.\n" + "\t Automatically enabled for \"onyx\" runtime.\n" "\t--tag Generates a C-Tag file.\n" "\t--syminfo Generates a symbol resolution information file. Used by onyx-lsp.\n" + "\t--generate-foreign-info Generates information for #foreign blocks.\n" // "\t--doc \n" - "\t--generate-foreign-info\n" "\n" - "Developer flags:\n" - "\t--print-function-mappings Prints a mapping from WASM function index to source location.\n" - "\t--print-static-if-results Prints the conditional result of each #if statement. Useful for debugging.\n" + "Developer options:\n" "\t--no-colors Disables colors in the error message.\n" "\t--no-file-contents Disables '#file_contents' for security.\n" + "\t--print-function-mappings Prints a mapping from WASM function index to source location.\n" + "\t--print-static-if-results Prints the conditional result of each #if statement. Useful for debugging.\n" "\n"; @@ -74,6 +90,8 @@ static CompileOptions compile_opts_parse(bh_allocator alloc, int argc, char *arg .target_file = "out.wasm", .documentation_file = NULL, + .symbol_info_file = NULL, + .help_subcommand = NULL, .debug_enabled = 0, @@ -103,16 +121,19 @@ static CompileOptions compile_opts_parse(bh_allocator alloc, int argc, char *arg if (argc == 1) return options; i32 arg_parse_start = 1; - if (!strcmp(argv[1], "help")) options.action = ONYX_COMPILE_ACTION_PRINT_HELP; - if (!strcmp(argv[1], "compile") || !strcmp(argv[1], "build")) { + if (!strcmp(argv[1], "help")) { + options.action = ONYX_COMPILE_ACTION_PRINT_HELP; + options.help_subcommand = argc > 2 ? argv[2] : NULL; + } + else if (!strcmp(argv[1], "compile") || !strcmp(argv[1], "build")) { options.action = ONYX_COMPILE_ACTION_COMPILE; arg_parse_start = 2; } - if (!strcmp(argv[1], "check")) { + else if (!strcmp(argv[1], "check")) { options.action = ONYX_COMPILE_ACTION_CHECK; arg_parse_start = 2; } - if (!strcmp(argv[1], "pkg") || !strcmp(argv[1], "package")) { + else if (!strcmp(argv[1], "pkg") || !strcmp(argv[1], "package")) { options.action = ONYX_COMPILE_ACTION_RUN; options.passthrough_argument_count = argc - 2; options.passthrough_argument_data = &argv[2]; @@ -126,6 +147,11 @@ static CompileOptions compile_opts_parse(bh_allocator alloc, int argc, char *arg arg_parse_start = 2; } #endif + else { + bh_printf("Unknown subcommand: '%s'\n", argv[1]); + bh_printf("Run \"onyx help\" for valid subcommands.\n"); + exit(1); + } if (options.action != ONYX_COMPILE_ACTION_PRINT_HELP) { fori(i, arg_parse_start, argc) { @@ -176,7 +202,7 @@ static CompileOptions compile_opts_parse(bh_allocator alloc, int argc, char *arg else if (!strcmp(argv[i], "custom")) options.runtime = Runtime_Custom; else { bh_printf("WARNING: '%s' is not a valid runtime. Defaulting to 'onyx'.\n", argv[i]); - options.runtime = Runtime_Wasi; + options.runtime = Runtime_Onyx; } } // else if (!strcmp(argv[i], "--doc")) { @@ -239,7 +265,19 @@ static void compile_opts_free(CompileOptions* opts) { bh_arr_free(opts->included_folders); } +static void print_subcommand_help(const char *subcommand) { + if (!strcmp(subcommand, "build") + || !strcmp(subcommand, "run") + || !strcmp(subcommand, "check")) { + bh_printf(build_docstring, subcommand); + } + else { + bh_printf("Unknown subcommand: '%s'\n", subcommand); + bh_printf("Run \"onyx help\" for valid subcommands.\n"); + exit(1); + } +} @@ -860,7 +898,14 @@ int main(int argc, char *argv[]) { CompilerProgress compiler_progress = ONYX_COMPILER_PROGRESS_ERROR; switch (compile_opts.action) { - case ONYX_COMPILE_ACTION_PRINT_HELP: bh_printf(docstring); return 1; + case ONYX_COMPILE_ACTION_PRINT_HELP: { + if (compile_opts.help_subcommand) { + print_subcommand_help(compile_opts.help_subcommand); + } else { + bh_printf(top_level_docstring); + } + return 1; + } case ONYX_COMPILE_ACTION_CHECK: compiler_progress = onyx_compile(); diff --git a/compiler/src/wasm_emit.c b/compiler/src/wasm_emit.c index 2f77783d..88ca1d35 100644 --- a/compiler/src/wasm_emit.c +++ b/compiler/src/wasm_emit.c @@ -4141,12 +4141,15 @@ static void emit_export_directive(OnyxWasmModule* mod, AstDirectiveExport* expor token_toggle_end(export->export_name); - i64 idx = bh_imap_get(&mod->index_map, (u64) export->export); + AstTyped *the_export = (AstTyped *) strip_aliases((AstNode *) export->export); + assert(the_export); + + i64 idx = bh_imap_get(&mod->index_map, (u64) the_export); WasmExport wasm_export; wasm_export.idx = (i32) idx; - switch (export->export->kind) { + switch (the_export->kind) { case Ast_Kind_Function: wasm_export.kind = WASM_FOREIGN_FUNCTION; break; diff --git a/core/encoding/base64.onyx b/core/encoding/base64.onyx index f1936f66..e1153c39 100644 --- a/core/encoding/base64.onyx +++ b/core/encoding/base64.onyx @@ -1,5 +1,7 @@ package core.encoding.base64 +use core {array} + // // A simple Base64 encoding and decoding library. Currently // only supports base64 with + and / characters. A simple @@ -12,7 +14,7 @@ package core.encoding.base64 // from the allocator provided. It is the callers responsibilty // to free this memory. encode :: (data: [] u8, allocator := context.allocator) -> [] u8 { - out := make([..] u8, allocator=allocator); + out := array.make(u8, allocator=allocator); for i: range.{0, data.count - 2, 3} { c1 := data[i + 0]; @@ -50,7 +52,7 @@ encode :: (data: [] u8, allocator := context.allocator) -> [] u8 { decode :: (data: [] u8, allocator := context.allocator) -> [] u8 { if data.count % 4 != 0 do return null_str; - out := make([..] u8, allocator=allocator); + out := array.make(u8, allocator=allocator); for i: range.{0, data.count, 4} { c1 := data[i + 0];