From f96a8978212dabaf4f6a5267b8f098d356d1ffe2 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 25 Feb 2018 10:55:18 +0000 Subject: [PATCH] Make `switch in f {` valid --- examples/demo.odin | 2 +- src/parser.cpp | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/examples/demo.odin b/examples/demo.odin index 3450e8078..466eb625e 100644 --- a/examples/demo.odin +++ b/examples/demo.odin @@ -752,7 +752,7 @@ complete_switch :: proc() { { // union Foo :: union {int, bool}; f: Foo = 123; - #complete switch _ in f { + #complete switch in f { case int: fmt.println("int"); case bool: fmt.println("bool"); case: diff --git a/src/parser.cpp b/src/parser.cpp index 182576913..7b3bee25d 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -3360,8 +3360,13 @@ AstNode *parse_switch_stmt(AstFile *f) { defer (f->expr_level = prev_level); if (allow_token(f, Token_in)) { - Array lhs = {}; + Array lhs = make_ast_node_array(f, 1); Array 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)); 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); - 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); } + 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) {