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;
#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);
}
}
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, '}')) {
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;
}
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,
};
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;
}