added 'else' clause to static if statements
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 8 Feb 2021 15:07:55 +0000 (09:07 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 8 Feb 2021 15:07:55 +0000 (09:07 -0600)
bin/onyx
include/onyxastnodes.h
include/onyxparser.h
onyx.exe
src/onyx.c
src/onyxchecker.c
src/onyxparser.c

index 680972bd76d755268bd55c3e10828561f5025f59..d9c520d451ace33fe6c14d93a1a30ef192d313a9 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index 274b97c0fea5f922866f8159c52204db720f813f..33b47dd4fd634fe41349fd5343441629ab144523 100644 (file)
@@ -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;
 
index 1f5112ef190a95fd7448efd445d9f1fc7bbe6cea..11d024c27c9aa36e838e60a0912745408510c63e 100644 (file)
@@ -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;
index 262152ee431c2ba9cf3db3eb35f915108a46ddc2..aa1006f0ef2f547be4e747de0a2e0ce98709a0f9 100644 (file)
Binary files a/onyx.exe and b/onyx.exe differ
index 6019e3b59d915f0b24323b4959eed0d4ca361a40..1e2f58d9b6d04a9e8d45f0718505a726a0bd0003 100644 (file)
@@ -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;
             }
index bcd96fc6784de83fb4a8ea147d5f83ee60f8acdc..9f3d7c0c1baae9388d03e528a2328ec6feed2423 100644 (file)
@@ -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;
index f9a6dff6b66ef09f00bdcfd6ebf846039e31acea..7f41b1400f9ebd94bc38d9cec791f4dcf11ed912 100644 (file)
@@ -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;
 }