-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
interp: fix the behaviour of goto, continue and break (#1392)
The logic of goto was false due to mixing break label and goto label, despite being opposite. In case of break, jump to node (the exit point) instead of node.start. Also always define label symbols before their use is parsed. * Fix continue label, detect invalid labels for break and continue * fix multiple goto, break, continue to the same label Fixes #1354.
- Loading branch information
Showing
10 changed files
with
242 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
func main() { | ||
n := 2 | ||
m := 2 | ||
foo := true | ||
OuterLoop: | ||
println("Boo") | ||
for i := 0; i < n; i++ { | ||
println("I: ", i) | ||
for j := 0; j < m; j++ { | ||
switch foo { | ||
case true: | ||
println(foo) | ||
break OuterLoop | ||
case false: | ||
println(foo) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Error: | ||
// 15:5: invalid break label OuterLoop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
func main() { | ||
n := 2 | ||
m := 2 | ||
foo := true | ||
OuterLoop: | ||
for i := 0; i < n; i++ { | ||
println("I: ", i) | ||
for j := 0; j < m; j++ { | ||
switch foo { | ||
case true: | ||
println(foo) | ||
break OuterLoop | ||
case false: | ||
println(foo) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Output: | ||
// I: 0 | ||
// true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package main | ||
|
||
func main() { | ||
n := 2 | ||
m := 2 | ||
foo := true | ||
OuterLoop: | ||
for i := 0; i < n; i++ { | ||
println("I: ", i) | ||
for j := 0; j < m; j++ { | ||
switch foo { | ||
case true: | ||
println(foo) | ||
break OuterLoop | ||
case false: | ||
println(foo) | ||
continue OuterLoop | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Output: | ||
// I: 0 | ||
// true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
func main() { | ||
n := 2 | ||
m := 2 | ||
foo := true | ||
goto OuterLoop | ||
println("Boo") | ||
OuterLoop: | ||
for i := 0; i < n; i++ { | ||
println("I: ", i) | ||
for j := 0; j < m; j++ { | ||
switch foo { | ||
case true: | ||
println(foo) | ||
break OuterLoop | ||
case false: | ||
println(foo) | ||
goto OuterLoop | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Output: | ||
// I: 0 | ||
// true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package main | ||
|
||
func main() { | ||
n := 2 | ||
m := 2 | ||
foo := true | ||
OuterLoop: | ||
for i := 0; i < n; i++ { | ||
println("I: ", i) | ||
for j := 0; j < m; j++ { | ||
switch foo { | ||
case true: | ||
println(foo) | ||
continue OuterLoop | ||
case false: | ||
println(foo) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Output: | ||
// I: 0 | ||
// true | ||
// I: 1 | ||
// true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
func main() { | ||
n := 2 | ||
m := 2 | ||
foo := true | ||
OuterLoop: | ||
println("boo") | ||
for i := 0; i < n; i++ { | ||
println("I: ", i) | ||
for j := 0; j < m; j++ { | ||
switch foo { | ||
case true: | ||
println(foo) | ||
continue OuterLoop | ||
case false: | ||
println(foo) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Error: | ||
// 15:5: invalid continue label OuterLoop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
func main() { | ||
println(test()) // Go prints true, Yaegi false | ||
} | ||
|
||
func test() bool { | ||
if true { | ||
goto label | ||
} | ||
goto label | ||
label: | ||
println("Go continues here") | ||
return true | ||
println("Yaegi goes straight to this return (this line is never printed)") | ||
return false | ||
} | ||
|
||
// Output: | ||
// Go continues here | ||
// true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters