From 020960d66e3d839cdf9c04ec4fc5713215bcc83e Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Mon, 8 Feb 2021 08:50:34 -0600 Subject: [PATCH] code clean up; made 'use package' in procedures be an entity --- core/sys/wasi.onyx | 14 +++++++------- docs/compile_time_vars | 14 ++++++++++++++ src/onyxparser.c | 42 ++++++++++++++++++------------------------ src/onyxsymres.c | 3 ++- 4 files changed, 41 insertions(+), 32 deletions(-) create mode 100644 docs/compile_time_vars diff --git a/core/sys/wasi.onyx b/core/sys/wasi.onyx index c347c45a..15337577 100644 --- a/core/sys/wasi.onyx +++ b/core/sys/wasi.onyx @@ -39,13 +39,13 @@ proc () #export "_start" { context.temp_allocator = alloc.temp_allocator; context.assert_handler = assert_handler; - argc : Size; + args : [] cstr; argv_buf_size : Size; - args_sizes_get(^argc, ^argv_buf_size); + args_sizes_get(^args.count, ^argv_buf_size); - argv := cast(^cstr) calloc(sizeof cstr * argc); + args = memory.make_slice(cstr, args.count); argv_buf := cast(cstr) calloc(argv_buf_size); - args_get(argv, argv_buf); + args_get(args.data, argv_buf); // This post processing of the argv array needs to happen if the target is using @@ -55,17 +55,17 @@ proc () #export "_start" { // But for right now, WASI will give the argv array 32-bit pointers, instead of // 64-bit pointers. This loops expands the 32-bit pointers into 64-bit pointers // while not clobbering any of them. - while i := cast(i32) (argc - 1); i >= 0 { + while i := cast(i32) (args.count - 1); i >= 0 { defer i -= 1; - argv[i] = cast(cstr) (cast(^u32) argv)[i]; + args[i] = cast(cstr) (cast(^u32) args.data)[i]; } stdio_init(); - main.main(argv[0 .. argc]); + main.main(args); print_stream_flush(); } diff --git a/docs/compile_time_vars b/docs/compile_time_vars new file mode 100644 index 00000000..a81e31fb --- /dev/null +++ b/docs/compile_time_vars @@ -0,0 +1,14 @@ +With the recent addition of compile time if statements, there should be a set of variables +that give information about the current compilation. These would include things such as: + - Chosen backend (JS, WASI, others in the future) + - 32-Bit pointers are enabled + +The standard library will be rewritten to use these variables to conditionally include file +that are only supported on a particular system. + +For the moment, I think that the choice of backend will be given by compiler flags. It will +set the variables described below to their value. + +There should be an #error directive that when hit just produces a compile time error and prevents +compilation. It would be useful to ensure certain files that are only for a particular +backend will not compile with the wrong one, such as webgl.onyx. \ No newline at end of file diff --git a/src/onyxparser.c b/src/onyxparser.c index 7248fef3..f9a6dff6 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -5,7 +5,6 @@ // Things that need to be cleaned up in the parser: // - control block local variables should be more extensible and reuse more code -// - outermost loops that parse the top level elements // - package name parsing #include "onyxlex.h" @@ -874,6 +873,7 @@ static AstIfWhile* parse_if_stmt(OnyxParser* parser) { if (parser->hit_unexpected_token) return root_if; AstIfWhile* elseif_node = make_node(AstIfWhile, Ast_Kind_If); + elseif_node->token = parser->curr - 1; cond = parse_expression(parser, 1); true_stmt = parse_block(parser); @@ -1198,7 +1198,8 @@ static AstNode* parse_use_stmt(OnyxParser* parser) { expect_token(parser, ','); } } - + + ENTITY_SUBMIT(upack); return (AstNode *) upack; } else { @@ -1817,13 +1818,12 @@ static void parse_function_params(OnyxParser* parser, AstFunction* func) { else curr_param.vararg_kind = VA_Kind_Typed; } - i32 old_len = 0, new_len = 0; if (curr_param.vararg_kind != VA_Kind_Untyped) { // CLEANUP: This is mess and it is hard to follow what is going on here. // I think with recent rewrites, this should be easier to do. - old_len = bh_arr_length(*parser->polymorph_context.poly_params); + i32 old_len = bh_arr_length(*parser->polymorph_context.poly_params); curr_param.local->type_node = parse_type(parser); - new_len = bh_arr_length(*parser->polymorph_context.poly_params); + i32 new_len = bh_arr_length(*parser->polymorph_context.poly_params); if (curr_param.vararg_kind == VA_Kind_Typed) { AstVarArgType* va_type = make_node(AstVarArgType, Ast_Kind_VarArg_Type); @@ -1831,12 +1831,11 @@ static void parse_function_params(OnyxParser* parser, AstFunction* func) { va_type->token = curr_param.local->type_node->token; curr_param.local->type_node = (AstType *) va_type; } - } - i32 new_poly_params = new_len - old_len; - fori (i, 0, new_poly_params) { - (*parser->polymorph_context.poly_params)[old_len + i].type_expr = curr_param.local->type_node; - (*parser->polymorph_context.poly_params)[old_len + i].idx = param_idx; + fori (i, 0, new_len - old_len) { + (*parser->polymorph_context.poly_params)[old_len + i].type_expr = curr_param.local->type_node; + (*parser->polymorph_context.poly_params)[old_len + i].idx = param_idx; + } } } @@ -2137,23 +2136,18 @@ static AstTyped* parse_top_level_expression(OnyxParser* parser) { return (AstTyped *) func_node; } - else if (parser->curr->type == Token_Type_Keyword_Global) { - return parse_global_declaration(parser); - } - else if (parser->curr->type == Token_Type_Keyword_Struct) { - return (AstTyped *) parse_struct(parser); - } - else if (parse_possible_directive(parser, "type")) { + + if (parser->curr->type == Token_Type_Keyword_Global) return parse_global_declaration(parser); + if (parser->curr->type == Token_Type_Keyword_Struct) return (AstTyped *) parse_struct(parser); + if (parser->curr->type == Token_Type_Keyword_Enum) return (AstTyped *) parse_enum_declaration(parser); + + if (parse_possible_directive(parser, "type")) { AstTypeAlias* alias = make_node(AstTypeAlias, Ast_Kind_Type_Alias); alias->to = parse_type(parser); return (AstTyped *) alias; } - else if (parser->curr->type == Token_Type_Keyword_Enum) { - return (AstTyped *) parse_enum_declaration(parser); - } - else { - return parse_expression(parser, 1); - } + + return parse_expression(parser, 1); } static AstBinding* parse_top_level_binding(OnyxParser* parser, OnyxToken* symbol) { @@ -2428,7 +2422,7 @@ void onyx_parse(OnyxParser *parser) { AstUsePackage* implicit_use_builtin = make_node(AstUsePackage, Ast_Kind_Use_Package); implicit_use_builtin->package_name = &builtin_package_token; ENTITY_SUBMIT(implicit_use_builtin); - + while (parser->curr->type != Token_Type_End_Stream) { if (parser->hit_unexpected_token) break; if (onyx_has_errors()) break; diff --git a/src/onyxsymres.c b/src/onyxsymres.c index e02cd155..0778a940 100644 --- a/src/onyxsymres.c +++ b/src/onyxsymres.c @@ -676,7 +676,8 @@ static SymresStatus symres_statement(AstNode** stmt, b32 *remove) { case Ast_Kind_Use_Package: if (remove) *remove = 1; - SYMRES(use_package, (AstUsePackage *) *stmt); + // This is handled by an entity now. + // SYMRES(use_package, (AstUsePackage *) *stmt); break; default: SYMRES(expression, (AstTyped **) stmt); break; -- 2.25.1