Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RangeExpr #1834

Merged
merged 5 commits into from
Apr 6, 2024
Merged
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
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
Loading