From 354645fbe3a7946e1ffac12d79034817108ebd70 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 24 Aug 2020 18:55:30 +0300 Subject: [PATCH] compiler: allow to use `switch` without tag --- pkg/compiler/codegen.go | 10 +++++++--- pkg/compiler/switch_test.go | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index a12a753bd..0d4445b3f 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -578,9 +578,13 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { return nil case *ast.SwitchStmt: - ast.Walk(c, n.Tag) - - eqOpcode, _ := convertToken(token.EQL, c.typeOf(n.Tag)) + eqOpcode := opcode.EQUAL + if n.Tag != nil { + ast.Walk(c, n.Tag) + eqOpcode, _ = convertToken(token.EQL, c.typeOf(n.Tag)) + } else { + emit.Bool(c.prog.BinWriter, true) + } switchEnd, label := c.generateLabel(labelEnd) lastSwitch := c.currentSwitch diff --git a/pkg/compiler/switch_test.go b/pkg/compiler/switch_test.go index 7a34e3174..ce90ee0c2 100644 --- a/pkg/compiler/switch_test.go +++ b/pkg/compiler/switch_test.go @@ -18,6 +18,21 @@ var switchTestCases = []testCase{ }`, big.NewInt(2), }, + { + "switch with no tag", + `package main + func f() bool { return false } + func Main() int { + switch { + case f(): + return 1 + case true: + return 2 + } + return 3 + }`, + big.NewInt(2), + }, { "simple switch fail", `package main