Browse Source

Make `switch in f {` valid

gingerBill 7 years ago
parent
commit
f96a897821
2 changed files with 10 additions and 6 deletions
  1. 1 1
      examples/demo.odin
  2. 9 5
      src/parser.cpp

+ 1 - 1
examples/demo.odin

@@ -752,7 +752,7 @@ complete_switch :: proc() {
 	{ // union
 	{ // union
 		Foo :: union {int, bool};
 		Foo :: union {int, bool};
 		f: Foo = 123;
 		f: Foo = 123;
-		#complete switch _ in f {
+		#complete switch in f {
 		case int:  fmt.println("int");
 		case int:  fmt.println("int");
 		case bool: fmt.println("bool");
 		case bool: fmt.println("bool");
 		case:
 		case:

+ 9 - 5
src/parser.cpp

@@ -3360,8 +3360,13 @@ AstNode *parse_switch_stmt(AstFile *f) {
 		defer (f->expr_level = prev_level);
 		defer (f->expr_level = prev_level);
 
 
 		if (allow_token(f, Token_in)) {
 		if (allow_token(f, Token_in)) {
-			Array<AstNode *> lhs = {};
+			Array<AstNode *> lhs = make_ast_node_array(f, 1);
 			Array<AstNode *> rhs = make_ast_node_array(f, 1);
 			Array<AstNode *> rhs = make_ast_node_array(f, 1);
+			Token blank_ident = token;
+			blank_ident.kind = Token_Ident;
+			blank_ident.string = str_lit("_");
+			AstNode *blank = ast_ident(f, blank_ident);
+			array_add(&lhs, blank);
 			array_add(&rhs, parse_expr(f, false));
 			array_add(&rhs, parse_expr(f, false));
 
 
 			tag = ast_assign_stmt(f, token, lhs, rhs);
 			tag = ast_assign_stmt(f, token, lhs, rhs);
@@ -3391,12 +3396,11 @@ AstNode *parse_switch_stmt(AstFile *f) {
 
 
 	body = ast_block_stmt(f, list, open, close);
 	body = ast_block_stmt(f, list, open, close);
 
 
-	if (!is_type_match) {
-		tag = convert_stmt_to_expr(f, tag, str_lit("switch expression"));
-		return ast_switch_stmt(f, token, init, tag, body);
-	} else {
+	if (is_type_match) {
 		return ast_type_switch_stmt(f, token, tag, body);
 		return ast_type_switch_stmt(f, token, tag, body);
 	}
 	}
+	tag = convert_stmt_to_expr(f, tag, str_lit("switch expression"));
+	return ast_switch_stmt(f, token, init, tag, body);
 }
 }
 
 
 AstNode *parse_defer_stmt(AstFile *f) {
 AstNode *parse_defer_stmt(AstFile *f) {