b32 hit_unexpected_token : 1;
b32 parse_calls : 1;
+ b32 parse_quick_functions : 1;
// Currently, package expressions are only allowed in certain places.
b32 allow_package_expressions : 1;
ERROR(sc->token->pos, "Expected exactly one value in switch-case when using a capture.");
}
+ if (sc->capture && switchnode->switch_kind != Switch_Kind_Union) {
+ ERROR(sc->capture->token->pos, "Captures in switch cases are only allowed when switching over a union type.");
+ }
+
if (sc->flags & Ast_Flag_Has_Been_Checked) goto check_switch_case_block;
bh_arr_each(AstTyped *, value, sc->values) {
}
if (sl->type->kind == Type_Kind_Union) {
+ if ((sl->flags & Ast_Flag_Has_Been_Checked) != 0) return Check_Success;
+
if (bh_arr_length(sl->args.values) != 0 || bh_arr_length(sl->args.named_values) != 1) {
ERROR_(sl->token->pos, "Expected exactly one named member when constructing an instance of a union type, '%s'.", type_get_name(sl->type));
}
bh_arr_push(sl->args.values, (AstTyped *) tag_value);
bh_arr_push(sl->args.values, value->value);
+
+ sl->flags |= Ast_Flag_Has_Been_Checked;
return Check_Success;
}
break;
case Ast_Kind_Switch_Case: {
+ C(AstSwitchCase, capture);
C(AstSwitchCase, block);
AstSwitchCase *dw = (AstSwitchCase *) nn;
} else {
bh_arr_new(global_heap_allocator, sc_node->values, 1);
+ parser->parse_quick_functions = 0;
AstTyped* value = parse_expression(parser, 1);
bh_arr_push(sc_node->values, value);
while (consume_token_if_next(parser, ',')) {
value = parse_expression(parser, 1);
bh_arr_push(sc_node->values, value);
}
+
+ parser->parse_quick_functions = 1;
}
if (consume_token_if_next(parser, Token_Type_Fat_Right_Arrow)) {
} QuickParam;
static b32 parse_possible_quick_function_definition_no_consume(OnyxParser* parser) {
+ if (!parser->parse_quick_functions) return 0;
+
//
// x => x + 1 case.
if (next_tokens_are(parser, 2, Token_Type_Symbol, Token_Type_Fat_Right_Arrow)) {
parser.scope_flags = NULL;
parser.stored_tags = NULL;
parser.parse_calls = 1;
+ parser.parse_quick_functions = 1;
parser.tag_depth = 0;
parser.overload_count = 0;
parser.injection_point = NULL;
} else {
resolve_expression_type(value);
- if ((value->flags & Ast_Flag_Comptime) == 0) {
- if (err_msg) *err_msg = "Expected compile-time known argument here.";
- return;
- }
-
param_type = type_build_from_ast(context.ast_alloc, param_type_expr);
if (param_type == NULL) {
flag_to_yield = 1;
if (tm == TYPE_MATCH_YIELD) flag_to_yield = 1;
- *resolved = ((PolySolveResult) { PSK_Value, value });
+ if ((value_to_use->flags & Ast_Flag_Comptime) == 0) {
+ if (err_msg) *err_msg = "Expected compile-time known argument here.";
+ return;
+ }
+
+ *resolved = ((PolySolveResult) { PSK_Value, value_to_use });
}
if (orig_value->kind == Ast_Kind_Argument) {
}
contains :: #match #locked {
- macro (arr: [] $T, $cmp: Code) -> bool {
- for it: arr do if #unquote cmp do return true;
- return false;
- },
-
(arr: [] $T, x: T) -> bool {
for it: arr do if it == x do return true;
return false;
+ },
+
+ macro (arr: [] $T, $cmp: Code) -> bool {
+ for it: arr do if #unquote cmp do return true;
+ return false;
}
}
use core {*}
+extract_variant :: macro (u: $U, $variant: U.tag_enum) => {
+ switch u {
+ case variant => v {
+ return Optional.make(v);
+ }
+ }
+
+ return .{};
+}
+
SimpleUnion :: union {
a: i32;
b: struct {
println(cast(SimpleUnion.tag_enum) u);
call_test(u);
+
+ if cast(SimpleUnion.tag_enum, u) == .b {
+ println(extract_variant(u, .b)?);
+ }
}
main :: () {simple_test(); link_test();}