Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions demo1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"fmt"
)

func main() {
for i := 8; i <= 20; i += 4 {
if !(i%8 == 0) {
fmt.Println(i, "is not divisible by 8")
continue
}
fmt.Println(i, "is divisible by 8")
}
}
16 changes: 16 additions & 0 deletions demo2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"fmt"
)

func main() {
four i := 8; i <= 20; i++ {
unless i%8 == 0 {
fmt.Println(i, "is not divisible by 8")
continue
}
fmt.Println(i, "is divisible by 8")
}
}

23 changes: 23 additions & 0 deletions demo3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"fmt"
)

func f() {
fmt.Println("number is divisible by 8")
}

func g() {
fmt.Println("number is not divisible by 8")
}

func main() {
four i := 8; i <= 20; i++ {
unless i%8 == 0 {
g()
continue
}
f()
}
}
13 changes: 13 additions & 0 deletions src/cmd/compile/internal/escape/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ func (e *escape) stmt(n ir.Node) {
e.block(n.Body)
e.loopDepth--

case ir.OFOUR:
n := n.(*ir.FourStmt)
e.loopDepth++
e.discard(n.Cond)
e.stmt(n.Post)
e.block(n.Body)
e.loopDepth--

case ir.OUNLESS:
n := n.(*ir.UnlessStmt)
e.discard(n.Cond)
e.block(n.Body)

case ir.ORANGE:
// for Key, Value = range X { Body }
n := n.(*ir.RangeStmt)
Expand Down
40 changes: 39 additions & 1 deletion src/cmd/compile/internal/ir/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ var OpNames = []string{
OEQ: "==",
OFALL: "fallthrough",
OFOR: "for",
OFOUR: "four",
OUNLESS: "unless",
OGE: ">=",
OGOTO: "goto",
OGT: ">",
Expand Down Expand Up @@ -279,6 +281,8 @@ var OpPrec = []int{
ODEFER: -1,
OFALL: -1,
OFOR: -1,
OFOUR: -1,
OUNLESS: -1,
OGOTO: -1,
OIF: -1,
OLABEL: -1,
Expand All @@ -294,7 +298,7 @@ var OpPrec = []int{
// StmtWithInit reports whether op is a statement with an explicit init list.
func StmtWithInit(op Op) bool {
switch op {
case OIF, OFOR, OSWITCH:
case OIF, OFOR, OSWITCH, OFOUR, OUNLESS:
return true
}
return false
Expand Down Expand Up @@ -434,6 +438,40 @@ func stmtFmt(n Node, s fmt.State) {

fmt.Fprintf(s, " { %v }", n.Body)

case OUNLESS:
n := n.(*UnlessStmt)
if simpleinit {
fmt.Fprintf(s, "unless %v; %v { %v }", n.Init()[0], n.Cond, n.Body)
} else {
fmt.Fprintf(s, "unless %v { %v }", n.Cond, n.Body)
}

case OFOUR:
n := n.(*FourStmt)
if !exportFormat { // TODO maybe only if FmtShort, same below
fmt.Fprintf(s, "four loop")
break
}

fmt.Fprint(s, "four")
if simpleinit {
fmt.Fprintf(s, " %v;", n.Init()[0])
} else if n.Post != nil {
fmt.Fprint(s, " ;")
}

if n.Cond != nil {
fmt.Fprintf(s, " %v", n.Cond)
}

if n.Post != nil {
fmt.Fprintf(s, "; %v", n.Post)
} else if simpleinit {
fmt.Fprint(s, ";")
}

fmt.Fprintf(s, " { %v }", n.Body)

case ORANGE:
n := n.(*RangeStmt)
if !exportFormat {
Expand Down
2 changes: 2 additions & 0 deletions src/cmd/compile/internal/ir/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ const (
ODEFER // defer Call
OFALL // fallthrough
OFOR // for Init; Cond; Post { Body }
OFOUR // four Init; Cond; Post { Body }
OUNLESS // unless Init; Cond { Body }
OGOTO // goto Label
OIF // if Init; Cond { Then } else { Else }
OLABEL // Label:
Expand Down
104 changes: 104 additions & 0 deletions src/cmd/compile/internal/ir/node_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading