bugfix: exporting aliased symbol; added: better cli help
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 5 Mar 2023 04:16:51 +0000 (22:16 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 5 Mar 2023 04:16:51 +0000 (22:16 -0600)
compiler/include/astnodes.h
compiler/src/onyx.c
compiler/src/wasm_emit.c
core/encoding/base64.onyx

index 09d3dc2dd89bf223fbdf7cea5ba41c5413dce10d..f078fbc657fef01b60cb756109991ab070570532 100644 (file)
@@ -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;
 
index 8ee38dc31fde8d7d93b193cc0c179b912269f7f6..13f2ab5c361fc388b187ddd1fc4f95501e85af7d 100644 (file)
 
 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 <target file>] [--verbose] <input files>\n"
-    "\tonyx check <input files>\n"
+    "\tonyx <subcommand>\n"
+    "\n"
+    "Subcommands:\n"
+    "\thelp      Shows this help message. Use \"onyx help <subcommand>\".\n"
+    "\tbuild     Compiles an Onyx program into an executable.\n"
 #ifdef ENABLE_RUN_WITH_WASMER
-    "\tonyx run <input files> -- <args>\n"
+    "\trun       Compiles and runs an Onyx program, all at once.\n"
 #endif
-    "\tonyx pkg\n"
-    // "\tonyx doc <input files>\n"
-    "\tonyx help\n"
+    "\tcheck     Checks syntax and types of an Onyx program.\n"
+    "\tpackage   Package manager\n";
+    // "\tdoc <input files>\n"
+
+static const char *build_docstring = DOCSTRING_HEADER
+    "Usage:\n"
+    "\tonyx %s <input files> [-o target_file] OPTIONS\n"
+    "\n"
+    "Required:\n"
+    "\t<input files>           One or more Onyx files to include in the program.\n"
     "\n"
-    "Compile Flags:\n"
-    "\t<input files>           List of initial files\n"
+    "Options:\n"
     "\t-o <target_file>        Specify the target file (default: out.wasm).\n"
     "\t   --output <target_file>\n"
+    "\t-I <dir>                Include a directory in the search path.\n"
     "\t--runtime, -r <runtime> 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 <target_file> Generates a symbol resolution information file. Used by onyx-lsp.\n"
+    "\t--generate-foreign-info Generates information for #foreign blocks.\n"
     // "\t--doc <doc_file>\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();
index 2f77783dfa471f080d79edaae10188b6f2994add..88ca1d351807c4617a81bac9c744dff419eba379 100644 (file)
@@ -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;
 
index f1936f666bd013a63e953e60160d240c00473cc1..e1153c39d35b1a8be6c2041e57dbff7ea506c1c7 100644 (file)
@@ -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];