From: Brendan Hansen Date: Mon, 21 Jun 2021 04:04:39 +0000 (-0500) Subject: fixed up the documentation generation X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=17f9bc01e86dcae7ea9cc1a3f674135bea2dfb4b;p=onyx.git fixed up the documentation generation --- diff --git a/CHANGELOG b/CHANGELOG index 2cdf3e7c..c8478d94 100644 --- 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. diff --git a/bin/onyx b/bin/onyx index 695c6b31..ef59bd00 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index bda14336..5a7bd3db 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -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; diff --git a/include/onyxdoc.h b/include/onyxdoc.h index 66bcde84..0e6e505c 100644 --- a/include/onyxdoc.h +++ b/include/onyxdoc.h @@ -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 diff --git a/src/onyx.c b/src/onyx.c index 07e8f2c3..f8fd400f 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -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 " "\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; } diff --git a/src/onyxdoc.c b/src/onyxdoc.c index b6015d41..ab1bc9dc 100644 --- a/src/onyxdoc.c +++ b/src/onyxdoc.c @@ -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); }