From: Brendan Hansen Date: Mon, 8 Feb 2021 15:07:55 +0000 (-0600) Subject: added 'else' clause to static if statements X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=99e641f6751abb6a1f20a9c16a309a950113388e;p=onyx.git added 'else' clause to static if statements --- diff --git a/bin/onyx b/bin/onyx index 680972bd..d9c520d4 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index 274b97c0..33b47dd4 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -1001,9 +1001,10 @@ struct CompileOptions { bh_allocator allocator; CompileAction action; - u32 verbose_output : 29; + u32 verbose_output : 28; b32 fun_output : 1; b32 print_function_mappings : 1; + b32 print_static_if_results : 1; b32 use_post_mvp_features : 1; diff --git a/include/onyxparser.h b/include/onyxparser.h index 1f5112ef..11d024c2 100644 --- a/include/onyxparser.h +++ b/include/onyxparser.h @@ -26,7 +26,7 @@ typedef struct OnyxParser { PolymorphicContext polymorph_context; bh_arr(Scope *) scope_stack; - bh_arr(AstStaticIf *) static_if_stack; + bh_arr(bh_arr(Entity *) *) alternate_entity_placement_stack; b32 hit_unexpected_token : 1; } OnyxParser; diff --git a/onyx.exe b/onyx.exe index 262152ee..aa1006f0 100644 Binary files a/onyx.exe and b/onyx.exe differ diff --git a/src/onyx.c b/src/onyx.c index 6019e3b5..1e2f58d9 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -86,6 +86,9 @@ static CompileOptions compile_opts_parse(bh_allocator alloc, int argc, char *arg else if (!strcmp(argv[i], "--print-function-mappings")) { options.print_function_mappings = 1; } + else if (!strcmp(argv[i], "--print-static-if-results")) { + options.print_static_if_results = 1; + } else if (!strcmp(argv[i], "--use-post-mvp-features")) { options.use_post_mvp_features = 1; } diff --git a/src/onyxchecker.c b/src/onyxchecker.c index bcd96fc6..9f3d7c0c 100644 --- a/src/onyxchecker.c +++ b/src/onyxchecker.c @@ -1817,10 +1817,22 @@ CheckStatus check_static_if(AstStaticIf* static_if) { AstNumLit* condition_value = (AstNumLit *) static_if->cond; assert(condition_value->kind == Ast_Kind_NumLit); // This should be right, right? + if (context.options->print_static_if_results) + bh_printf("Static if statement at %s:%d:%d resulted in %s\n", + static_if->token->pos.filename, + static_if->token->pos.line, + static_if->token->pos.column, + condition_value->value.i ? "true" : "false"); + if (condition_value->value.i) { bh_arr_each(Entity *, ent, static_if->true_entities) { entity_heap_insert_existing(&context.entities, *ent); } + + } else { + bh_arr_each(Entity *, ent, static_if->false_entities) { + entity_heap_insert_existing(&context.entities, *ent); + } } return Check_Complete; diff --git a/src/onyxparser.c b/src/onyxparser.c index f9a6dff6..7f41b140 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -21,14 +21,14 @@ static AstNode error_node = { Ast_Kind_Error, 0, NULL, NULL }; #define ENTITY_SUBMIT_IN_SCOPE(node, scope) (submit_entity_in_scope(parser, (AstNode *) (node), scope, parser->package)) void submit_entity_in_scope(OnyxParser* parser, AstNode* node, Scope* scope, Package* package) { - if (bh_arr_length(parser->static_if_stack) == 0) { + if (bh_arr_length(parser->alternate_entity_placement_stack) == 0) { add_entities_for_node(NULL, node, scope, package); } else { - AstStaticIf* static_if = bh_arr_last(parser->static_if_stack); + bh_arr(Entity *) *entity_array = bh_arr_last(parser->alternate_entity_placement_stack); // nocheckin This should also be able to place them in the false entities - add_entities_for_node(&static_if->true_entities, node, scope, package); + add_entities_for_node(entity_array, node, scope, package); } } @@ -2114,8 +2114,8 @@ static AstStaticIf* parse_static_if_stmt(OnyxParser* parser) { static_if_node->cond = parse_expression(parser, 0); // TODO: Add else statements to static ifs - bh_arr_new(global_heap_allocator, static_if_node->true_entities, 4); - bh_arr_push(parser->static_if_stack, static_if_node); + bh_arr_new(global_heap_allocator, static_if_node->true_entities, 2); + bh_arr_push(parser->alternate_entity_placement_stack, &static_if_node->true_entities); expect_token(parser, '{'); while (!consume_token_if_next(parser, '}')) { @@ -2124,7 +2124,21 @@ static AstStaticIf* parse_static_if_stmt(OnyxParser* parser) { parse_top_level_statement(parser); } - bh_arr_pop(parser->static_if_stack); + bh_arr_pop(parser->alternate_entity_placement_stack); + + if (consume_token_if_next(parser, Token_Type_Keyword_Else)) { + bh_arr_new(global_heap_allocator, static_if_node->false_entities, 2); + bh_arr_push(parser->alternate_entity_placement_stack, &static_if_node->false_entities); + + expect_token(parser, '{'); + while (!consume_token_if_next(parser, '}')) { + if (parser->hit_unexpected_token) return static_if_node; + + parse_top_level_statement(parser); + } + + bh_arr_pop(parser->alternate_entity_placement_stack); + } return static_if_node; } @@ -2394,7 +2408,7 @@ OnyxParser onyx_parser_create(bh_allocator alloc, OnyxTokenizer *tokenizer) { parser.prev = NULL; parser.hit_unexpected_token = 0; parser.scope_stack = NULL; - parser.static_if_stack = NULL; + parser.alternate_entity_placement_stack = NULL; parser.polymorph_context = (PolymorphicContext) { .root_node = NULL, @@ -2402,7 +2416,7 @@ OnyxParser onyx_parser_create(bh_allocator alloc, OnyxTokenizer *tokenizer) { }; bh_arr_new(global_heap_allocator, parser.scope_stack, 4); - bh_arr_new(global_heap_allocator, parser.static_if_stack, 4); + bh_arr_new(global_heap_allocator, parser.alternate_entity_placement_stack, 4); return parser; }