From: Brendan Hansen Date: Sat, 12 Dec 2020 04:45:48 +0000 (-0600) Subject: cleaning up code for CLI X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=30c0f71c59e22df9f602b667174cd9d61c0b3260;p=onyx.git cleaning up code for CLI --- diff --git a/docs/todo b/docs/todo index 80390be1..2d1a728e 100644 --- a/docs/todo +++ b/docs/todo @@ -9,7 +9,7 @@ Command Line Interface: - Abstract syntax tree printing - Running the binary right away using Wasmtime, Wasmer or NodeJS - [ ] Remove old code from CLI logic + [X] Remove old code from CLI logic [ ] Fix documentation generation (broken since compiler architecture change) [ ] Add statistic printing [ ] Fix AST printing (broken for a long time) diff --git a/include/onyxparser.h b/include/onyxparser.h index 18b38ebf..789119c2 100644 --- a/include/onyxparser.h +++ b/include/onyxparser.h @@ -17,8 +17,6 @@ typedef struct ParseResults { // NOTE: The allocator used to make the arrays below bh_allocator allocator; - bh_arr(AstInclude *) includes; - // NOTE: Contains all the nodes that will need some processing (symbol resolution, type checking) bh_arr(NodeToProcess) nodes_to_process; } ParseResults; diff --git a/onyx b/onyx index 25b0aaf1..1bc237a9 100755 Binary files a/onyx and b/onyx differ diff --git a/src/onyx.c b/src/onyx.c index 0229d810..812452f8 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -10,7 +10,7 @@ #include "onyxwasm.h" #include "onyxdoc.h" -#define VERSION "0.1" +#define VERSION "v0.0.5" #ifndef CORE_INSTALLATION @@ -22,49 +22,20 @@ -#ifdef REPORT_TIMES -// CLEANUP: Move this to another file -typedef struct TimerStackElem { - char* label; - u64 time; -} TimerStackElem; - -static bh_arr(TimerStackElem) global_timer_stack; - -void timer_stack_init() { - global_timer_stack = NULL; - bh_arr_new(global_heap_allocator, global_timer_stack, 4); -} - -void timer_stack_push(char* label) { - TimerStackElem elem; - elem.label = label; - elem.time = bh_time_curr(); - bh_arr_push(global_timer_stack, elem); -} - -void timer_stack_pop() { - TimerStackElem elem = bh_arr_pop(global_timer_stack); - u64 duration = bh_time_duration(elem.time); - bh_printf("[Time] %s took %lms.\n", elem.label, duration); -} -#endif - - static const char* docstring = "Onyx compiler version " VERSION "\n" "\n" - "The compiler for the Onyx programming language.\n" + "The compiler for the Onyx programming language, created by Brendan Hansen.\n" "\n" "Usage:\n" - "\tonyx [-o ] [-verbose] \n" + "\tonyx [-o ] [--verbose] \n" "\tonyx doc \n" - "\tonyx -help\n" - "\nFlags:\n" + "\tonyx help\n" + "\n" + "Flags:\n" + "\t List of initial files\n" "\t-o Specify the target file (default: out.wasm)\n" - "\t-ast Print the abstract syntax tree after parsing\n" - "\t-verbose Verbose output\n" - "\t-help Print this help message\n"; + "\t--verbose Verbose output\n"; typedef enum CompileAction { ONYX_COMPILE_ACTION_COMPILE, @@ -101,28 +72,32 @@ static OnyxCompileOptions compile_opts_parse(bh_allocator alloc, int argc, char bh_arr_push(options.included_folders, CORE_INSTALLATION); bh_arr_push(options.included_folders, "."); - fori(i, 1, argc) { - if (!strcmp(argv[i], "doc") && i == 1) { - options.action = ONYX_COMPILE_ACTION_DOCUMENT; - } - else if (!strcmp(argv[i], "-help")) { - options.action = ONYX_COMPILE_ACTION_PRINT_HELP; - break; - } - else if (!strcmp(argv[i], "-o")) { - options.target_file = argv[++i]; - } - else if (!strcmp(argv[i], "-verbose")) { - options.verbose_output = 1; - } - else if (!strcmp(argv[i], "-I")) { - bh_arr_push(options.included_folders, argv[++i]); - } - else { - if (options.action == ONYX_COMPILE_ACTION_PRINT_HELP) - options.action = ONYX_COMPILE_ACTION_COMPILE; + if (argc == 1) return options; + + if (!strcmp(argv[1], "doc")) { + options.action = ONYX_COMPILE_ACTION_DOCUMENT; + } + else if (!strcmp(argv[1], "help")) { + options.action = ONYX_COMPILE_ACTION_PRINT_HELP; + } + else { + options.action = ONYX_COMPILE_ACTION_COMPILE; + } - bh_arr_push(options.files, argv[i]); + if (options.action == ONYX_COMPILE_ACTION_COMPILE) { + fori(i, 1, argc) { + if (!strcmp(argv[i], "-o")) { + options.target_file = argv[++i]; + } + else if (!strcmp(argv[i], "-verbose")) { + options.verbose_output = 1; + } + else if (!strcmp(argv[i], "-I")) { + bh_arr_push(options.included_folders, argv[++i]); + } + else { + bh_arr_push(options.files, argv[i]); + } } } @@ -257,19 +232,6 @@ static ParseResults parse_source_file(CompilerState* compiler_state, bh_file_con } static void merge_parse_results(CompilerState* compiler_state, ParseResults* results) { - bh_arr_each(AstInclude *, include, results->includes) { - EntityType et = Entity_Type_Include_File; - - if ((*include)->kind == Ast_Kind_Include_Folder) et = Entity_Type_Include_Folder; - - entity_heap_insert(&compiler_state->prog_info.entities, (Entity) { - .state = Entity_State_Parse, - .type = et, - .scope = NULL, - .include = *include, - }); - } - Entity ent; bh_arr_each(NodeToProcess, n, results->nodes_to_process) { AstNode* node = n->node; @@ -280,6 +242,22 @@ static void merge_parse_results(CompilerState* compiler_state, ParseResults* res ent.scope = n->scope; switch (nkind) { + case Ast_Kind_Include_File: { + ent.state = Entity_State_Parse; + ent.type = Entity_Type_Include_File; + ent.include = (AstInclude *) node; + entity_heap_insert(&compiler_state->prog_info.entities, ent); + break; + } + + case Ast_Kind_Include_Folder: { + ent.state = Entity_State_Parse; + ent.type = Entity_Type_Include_Folder; + ent.include = (AstInclude *) node; + entity_heap_insert(&compiler_state->prog_info.entities, ent); + break; + } + case Ast_Kind_Function: { if ((node->flags & Ast_Flag_Foreign) != 0) { ent.type = Entity_Type_Foreign_Function_Header; @@ -451,10 +429,6 @@ static b32 process_include_entity(CompilerState* compiler_state, Entity* ent) { } static b32 process_entity(CompilerState* compiler_state, Entity* ent) { - // bh_printf("Processing entity: %s %s\n", - // entity_state_strings[ent->state], - // entity_type_strings[ent->type]); - i32 changed = 1; switch (ent->state) { @@ -549,10 +523,14 @@ int main(int argc, char *argv[]) { return 1; case ONYX_COMPILE_ACTION_COMPILE: - case ONYX_COMPILE_ACTION_DOCUMENT: compiler_progress = onyx_compile(&compile_state); break; + case ONYX_COMPILE_ACTION_DOCUMENT: + bh_printf("Documentation has not been fully implemented yet.\n"); + exit(EXIT_FAILURE); + break; + default: break; } @@ -571,7 +549,8 @@ int main(int argc, char *argv[]) { break; case ONYX_COMPILER_PROGRESS_SUCCESS: - bh_printf("Successfully compiled to '%s'\n", compile_opts.target_file); + if (compile_opts.verbose_output) + bh_printf("Successfully compiled to '%s'\n", compile_opts.target_file); break; } diff --git a/src/onyxparser.c b/src/onyxparser.c index 67e4c8a2..4c6783b6 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -2132,7 +2132,6 @@ OnyxParser onyx_parser_create(bh_allocator alloc, OnyxTokenizer *tokenizer, Prog parser.results = (ParseResults) { .allocator = global_heap_allocator, - .includes = NULL, .nodes_to_process = NULL, }; @@ -2142,7 +2141,6 @@ OnyxParser onyx_parser_create(bh_allocator alloc, OnyxTokenizer *tokenizer, Prog .poly_params = NULL, }; - bh_arr_new(parser.results.allocator, parser.results.includes, 4); bh_arr_new(parser.results.allocator, parser.results.nodes_to_process, 4); return parser; @@ -2195,7 +2193,7 @@ ParseResults onyx_parse(OnyxParser *parser) { switch (curr_stmt->kind) { case Ast_Kind_Include_File: case Ast_Kind_Include_Folder: - bh_arr_push(parser->results.includes, (AstInclude *) curr_stmt); + add_node_to_process(parser, curr_stmt); break; case Ast_Kind_Binding: {