Skip to content

Commit

Permalink
Merge pull request #1834 from xushiwei/q
Browse files Browse the repository at this point in the history
RangeExpr
  • Loading branch information
xushiwei authored Apr 6, 2024
2 parents 601d8dc + 298b405 commit e1b4f80
Show file tree
Hide file tree
Showing 13 changed files with 354 additions and 249 deletions.
1 change: 1 addition & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ type (
Tok token.Token // ILLEGAL if Key == nil, ASSIGN, DEFINE
X Expr // value to range over
Body *BlockStmt
NoRangeOp bool
}
)

Expand Down
259 changes: 259 additions & 0 deletions cl/compile_gop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package cl_test

import (
"strings"
"testing"
)

Expand Down Expand Up @@ -1265,6 +1266,264 @@ func main() {
`)
}

func testRangeExpr(t *testing.T, codeTpl, expect string) {
for k, s := range []string{" <- ", " := range ", " = range "} {
if k == 2 {
codeTpl = "i:=0\n" + codeTpl
expect = strings.Replace(expect, "for i := ", "i := 0\n\tfor i = ", -1)
}
gopClTest(t, strings.Replace(codeTpl, "$", s, -1), expect)
}
}

func TestRangeExpr(t *testing.T) {
testRangeExpr(t, `
for i $ :10 {
println(i)
}`, `package main
import "fmt"
func main() {
for i := 0; i < 10; i += 1 {
fmt.Println(i)
}
}
`)
testRangeExpr(t, `
for i $ 1:10:3 {
println(i)
}`, `package main
import "fmt"
func main() {
for i := 1; i < 10; i += 3 {
fmt.Println(i)
}
}
`)
}

func TestRangeExpr2(t *testing.T) {
testRangeExpr(t, `
for i $ 1:10:2 {
println(i)
}`, `package main
import "fmt"
func main() {
for i := 1; i < 10; i += 2 {
fmt.Println(i)
}
}
`)
}

func TestRangeExpr3(t *testing.T) {
testRangeExpr(t, `
for i $ 1:10 {
println(i)
}`, `package main
import "fmt"
func main() {
for i := 1; i < 10; i += 1 {
fmt.Println(i)
}
}
`)
}

func TestRangeExpr4(t *testing.T) {
testRangeExpr(t, `
for i $ :10:2 {
println(i)
}`, `package main
import "fmt"
func main() {
for i := 0; i < 10; i += 2 {
fmt.Println(i)
}
}
`)
}

func TestRangeExpr5(t *testing.T) {
gopClTest(t, `
for range :10 {
println("Hi")
}`, `package main
import "fmt"
func main() {
for _gop_k := 0; _gop_k < 10; _gop_k += 1 {
fmt.Println("Hi")
}
}
`)
}

func TestRangeExpr6(t *testing.T) {
gopClTest(t, `
for _ <- :10 {
println("Hi")
}`, `package main
import "fmt"
func main() {
for _gop_k := 0; _gop_k < 10; _gop_k += 1 {
fmt.Println("Hi")
}
}
`)
}

func TestRangeExpr7(t *testing.T) {
gopClTest(t, `
println [x for x <- 0:3:1]
`, `package main
import (
"fmt"
"github.com/goplus/gop/builtin"
)
func main() {
fmt.Println(func() (_gop_ret []int) {
for _gop_it := builtin.NewRange__0(0, 3, 1).Gop_Enum(); ; {
var _gop_ok bool
x, _gop_ok := _gop_it.Next()
if !_gop_ok {
break
}
_gop_ret = append(_gop_ret, x)
}
return
}())
}
`)
}

func testRangeExpr8(t *testing.T, codeTpl, expect string) {
for _, s := range []string{" <- ", " := range "} {
gopClTest(t, strings.Replace(codeTpl, "$", s, -1), expect)
}
}

func TestRangeExpr8(t *testing.T) {
testRangeExpr8(t, `
type T struct{}
func (t T) start() int {
return 0
}
func (t T) end() int{
return 3
}
func (t T) step() int{
return 1
}
t:=T{}
for i <- t.start():t.end():t.step(){
println i
}
`, `package main
import "fmt"
type T struct {
}
func (t T) start() int {
return 0
}
func (t T) end() int {
return 3
}
func (t T) step() int {
return 1
}
func main() {
t := T{}
for i, _gop_end, _gop_step := t.start(), t.end(), t.step(); i < _gop_end; i += _gop_step {
fmt.Println(i)
}
}
`)
}

func TestRangeExpr9(t *testing.T) {
testRangeExpr8(t, `
type T struct{}
func (t T) start() int {
return 0
}
func (t T) end() int{
return 3
}
func (t T) step() int{
return 1
}
t:=T{}
i:=0
for i =range t.start():t.end():t.step(){
println i
}
`, `package main
import "fmt"
type T struct {
}
func (t T) start() int {
return 0
}
func (t T) end() int {
return 3
}
func (t T) step() int {
return 1
}
func main() {
t := T{}
i := 0
for _gop_k, _gop_end, _gop_step := t.start(), t.end(), t.step(); _gop_k < _gop_end; _gop_k += _gop_step {
i = _gop_k
fmt.Println(i)
}
}
`)
}

func TestRangeExpr10(t *testing.T) {
gopClTest(t, `
for :10 {
echo "Hi"
}
`, `package main
import "fmt"
func main() {
for _gop_k := 0; _gop_k < 10; _gop_k += 1 {
fmt.Println("Hi")
}
}
`)
}

func Test_RangeExpressionIf_Issue1243(t *testing.T) {
gopClTest(t, `
for i <- :10, i%3 == 0 {
Expand Down
Loading

0 comments on commit e1b4f80

Please sign in to comment.