fixed up the documentation generation
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 21 Jun 2021 04:04:39 +0000 (23:04 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 21 Jun 2021 04:04:39 +0000 (23:04 -0500)
CHANGELOG
bin/onyx
include/onyxastnodes.h
include/onyxdoc.h
src/onyx.c
src/onyxdoc.c

index 2cdf3e7c78a9479adbcd07e4708c475e93b195a3..c8478d94c9c6f9d11c5d79384fc509f1a04192cc 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -31,8 +31,8 @@ Removals:
 
 Changes:
 * the "proc" keyword is now optional in a lot of cases. There are inevitably some bugs with this change,
-    but you can always add it in when it may be necessary. Note, that for overloaded procedures, "proc"
-    is still required.
+    but you can always add it in when it may be necessary. Note, that for overloaded procedures, "#match"
+    is now used.
 * operator overloading is now done as a top level declaration handled through the entity
     system, instead of relying on creating a procedure. This lets you use an existing
     procedure as an operator overload. Take a look at '#operator ==' in string.onyx.
index 695c6b31fd1ea80cbc63d2f70337d6aab1d2ffdf..ef59bd00a7c084852243138d452bbe7a756801fe 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index bda1433696a8d281a24786d60a0b6d57a045ec99..5a7bd3dbf2866af068e8aac35f156a6df241fb3f 100644 (file)
@@ -1137,6 +1137,7 @@ struct CompileOptions {
     bh_arr(const char *) included_folders;
     bh_arr(const char *) files;
     const char* target_file;
+    const char* documentation_file;
 };
 
 typedef struct Context Context;
index 66bcde84a714cb6fb2171902a7d83106c2e2b2ad..0e6e505c6e57a7d39a597a604ca03b3d0c54bd14 100644 (file)
@@ -33,6 +33,6 @@ typedef struct OnyxDocumentation {
 } OnyxDocumentation;
 
 OnyxDocumentation onyx_docs_generate();
-void onyx_docs_emit(OnyxDocumentation* doc);
+void onyx_docs_emit(OnyxDocumentation* doc, const char* filename);
 
 #endif
index 07e8f2c3ca30e1e947c246c616b5866716eda179..f8fd400f1f772e85ab087352b0e9fac46fbf3bae 100644 (file)
@@ -43,6 +43,7 @@ static const char* docstring = "Onyx compiler version " VERSION "\n"
     "\t           -VV          Very verbose output\n"
     "\t           -VVV         Very very verbose output (to be used by compiler developers)\n"
     "\t--use-post-mvp-features Enables post MVP WASM features such as memory.copy and memory.fill\n"
+    "\t--doc <doc_file>"
     "\n"
     "Developer flags:\n"
     "\t--print-function-mappings Prints a mapping from WASM function index to source location.\n"
@@ -66,6 +67,8 @@ static CompileOptions compile_opts_parse(bh_allocator alloc, int argc, char *arg
 
         .files = NULL,
         .target_file = "out.wasm",
+
+        .documentation_file = NULL,
     };
 
     bh_arr_new(alloc, options.files, 2);
@@ -77,13 +80,10 @@ static CompileOptions compile_opts_parse(bh_allocator alloc, int argc, char *arg
 
     if (argc == 1) return options;
 
-    if (!strcmp(argv[1], "help")) options.action = ONYX_COMPILE_ACTION_PRINT_HELP;
-    // else if (!strcmp(argv[1], "doc")) {
-    //     options.action = ONYX_COMPILE_ACTION_DOCUMENT;
-    // }
+    if (!strcmp(argv[1], "help"))     options.action = ONYX_COMPILE_ACTION_PRINT_HELP;
     else options.action = ONYX_COMPILE_ACTION_COMPILE;
 
-    if (options.action == ONYX_COMPILE_ACTION_COMPILE) {
+    if (options.action != ONYX_COMPILE_ACTION_PRINT_HELP) {
         fori(i, 1, argc) {
             if (!strcmp(argv[i], "-o")) {
                 options.target_file = argv[++i];
@@ -125,6 +125,9 @@ static CompileOptions compile_opts_parse(bh_allocator alloc, int argc, char *arg
                     options.runtime = Runtime_Wasi;
                 }
             }
+            else if (!strcmp(argv[i], "--doc")) {
+                options.documentation_file = argv[++i];
+            }
 #if defined(_BH_LINUX)
             // NOTE: Fun output is only enabled for Linux because Windows command line
             // is not ANSI compatible and for a silly feature, I don't want to learn
@@ -458,6 +461,12 @@ static i32 onyx_compile() {
         printf("\n");
     }
 
+    if (context.options->documentation_file != NULL) {
+        OnyxDocumentation docs = onyx_docs_generate();
+        docs.format = Doc_Format_Human;
+        onyx_docs_emit(&docs, context.options->documentation_file);
+    }
+
     return ONYX_COMPILER_PROGRESS_SUCCESS;
 }
 
index b6015d4181fb1625a2d91ab74002318802618d7e..ab1bc9dc896393f7bfb8aad3067fc3147ed7b02e 100644 (file)
@@ -45,12 +45,8 @@ static char* node_to_doc_def(const char* sym, AstNode *node, bh_allocator a) {
                 }
             }
 
-            strncat(buf, ")", 1023);
-
-            if (func->type->Function.return_type != &basic_types[Basic_Kind_Void]) {
-                strncat(buf, " -> ", 1023);
-                strncat(buf, type_get_name(func->type->Function.return_type), 1023);
-            }
+            strncat(buf, ") -> ", 1023);
+            strncat(buf, type_get_name(func->type->Function.return_type), 1023);
 
             break;
         }
@@ -104,7 +100,7 @@ static DocPackage doc_package_create(Package* p, bh_allocator a) {
 
     bh_table_each_start(AstNode *, p->scope->symbols)
         DocEntry de;
-        de.pos = value->token->pos;
+        if (value->token) de.pos = value->token->pos;
         de.def = node_to_doc_def(key, value, a);
         de.sym = (char *) key;
         de.additional = NULL;
@@ -114,7 +110,7 @@ static DocPackage doc_package_create(Package* p, bh_allocator a) {
 
     bh_table_each_start(AstNode *, p->private_scope->symbols)
         DocEntry de;
-        de.pos = value->token->pos;
+        if (value->token) de.pos = value->token->pos;
         de.def = node_to_doc_def(key, value, a);
         de.sym = (char *) key;
         de.additional = NULL;
@@ -147,7 +143,7 @@ OnyxDocumentation onyx_docs_generate() {
     return doc;
 }
 
-static void onyx_docs_emit_human(OnyxDocumentation* doc) {
+static void onyx_docs_emit_human(OnyxDocumentation* doc, bh_file* file) {
     // NOTE: Disabling fancy line printing until I can make it better
     #if 0
     bh_arr_each(DocPackage, dp, doc->package_docs) {
@@ -184,35 +180,35 @@ static void onyx_docs_emit_human(OnyxDocumentation* doc) {
     }
     #else
     bh_arr_each(DocPackage, dp, doc->package_docs) {
-        bh_printf("Package '%s'\n", dp->name);
+        bh_fprintf(file, "Package '%s'\n", dp->name);
 
         if (bh_arr_length(dp->public_entries) > 0) {
-            bh_printf("   Public symbols\n");
+            bh_fprintf(file, "   Public symbols\n");
             bh_arr_each(DocEntry, de, dp->public_entries) {
-                bh_printf("     %s\n", de->def);
+                bh_fprintf(file, "     %s\n", de->def);
 
                 if (de->pos.filename != NULL)
-                    bh_printf("        at %s:%d,%d\n", de->pos.filename, de->pos.line, de->pos.column);
+                    bh_fprintf(file, "        at %s:%d,%d\n", de->pos.filename, de->pos.line, de->pos.column);
                 else
-                    bh_printf("        compiler built-in\n");
+                    bh_fprintf(file, "        compiler built-in\n");
 
-                bh_printf("    \n");
+                bh_fprintf(file, "    \n");
             }
         }
 
         if (bh_arr_length(dp->private_entries) > 0) {
-            bh_printf("   Private symbols\n");
+            bh_fprintf(file, "   Private symbols\n");
             bh_arr_each(DocEntry, de, dp->private_entries) {
-                bh_printf("      %s\n", de->def);
+                bh_fprintf(file, "      %s\n", de->def);
                 if (de->pos.filename != NULL)
-                    bh_printf("        at %s:%d,%d\n", de->pos.filename, de->pos.line, de->pos.column);
+                    bh_fprintf(file, "        at %s:%d,%d\n", de->pos.filename, de->pos.line, de->pos.column);
                 else
-                    bh_printf("        compiler built-in\n");
+                    bh_fprintf(file, "        compiler built-in\n");
 
-                bh_printf("    \n");
+                bh_fprintf(file, "    \n");
             }
 
-            bh_printf("\n");
+            bh_fprintf(file, "\n");
         }
     }
     #endif
@@ -267,16 +263,24 @@ static void onyx_docs_emit_tags(OnyxDocumentation* doc) {
     bh_file_close(&tags_file);
 }
 
-static void onyx_docs_emit_html(OnyxDocumentation* doc) {
-    bh_printf("HTML documentation output not supported yet.\n");
+static void onyx_docs_emit_html(OnyxDocumentation* doc, bh_file* file) {
+    bh_fprintf(file, "HTML documentation output not supported yet.\n");
     return;
 }
 
-void onyx_docs_emit(OnyxDocumentation* doc) {
+void onyx_docs_emit(OnyxDocumentation* doc, const char* filename) {
+    bh_file file;
+    if (bh_file_create(&file, filename) != BH_FILE_ERROR_NONE) {
+        bh_printf("ERROR: Failed to open file '%s' for writing documentation.\n", filename);
+        return;
+    }
+
     switch (doc->format) {
-        case Doc_Format_Human: onyx_docs_emit_human(doc); break;
+        case Doc_Format_Human: onyx_docs_emit_human(doc, &file); break;
+        case Doc_Format_Html: onyx_docs_emit_html(doc, &file); break;
         case Doc_Format_Tags: onyx_docs_emit_tags(doc); break;
-        case Doc_Format_Html: onyx_docs_emit_html(doc); break;
     }
+
+    bh_file_close(&file);
 }