Skip to content

Commit

Permalink
Merge pull request #1244 from xushiwei/1243
Browse files Browse the repository at this point in the history
for rangeExpr support if condition (fix #1243)
  • Loading branch information
xushiwei committed Jun 6, 2022
2 parents 38292c9 + 4ab0438 commit 2e490be
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
20 changes: 19 additions & 1 deletion cl/compile_test.go
Expand Up @@ -149,7 +149,25 @@ var x string = c.Str()
`)
}

func Test_Issue1240(t *testing.T) {
func Test_RangeExpressionIf_Issue1243(t *testing.T) {
gopClTest(t, `
for i <- :10, i%3 == 0 {
println i
}`, `package main
import fmt "fmt"
func main() {
for i := 0; i < 10; i += 1 {
if i%3 == 0 {
fmt.Println(i)
}
}
}
`)
}

func Test_CastSlice_Issue1240(t *testing.T) {
gopClTest(t, `
type fvec []float64
type foo float64
Expand Down
23 changes: 17 additions & 6 deletions cl/stmt.go
Expand Up @@ -258,7 +258,7 @@ func compileRangeStmt(ctx *blockCtx, v *ast.RangeStmt) {
if v.Tok == token.ASSIGN {
tok = v.Tok
}
compileForStmt(ctx, toForStmt(v.For, v.Key, v.Body, re, tok))
compileForStmt(ctx, toForStmt(v.For, v.Key, v.Body, re, tok, nil))
return
}
cb := ctx.cb
Expand Down Expand Up @@ -306,7 +306,7 @@ func compileRangeStmt(ctx *blockCtx, v *ast.RangeStmt) {

func compileForPhraseStmt(ctx *blockCtx, v *ast.ForPhraseStmt) {
if re, ok := v.X.(*ast.RangeExpr); ok {
compileForStmt(ctx, toForStmt(v.For, v.Value, v.Body, re, token.DEFINE))
compileForStmt(ctx, toForStmt(v.For, v.Value, v.Body, re, token.DEFINE, v.ForPhrase))
return
}
cb := ctx.cb
Expand Down Expand Up @@ -338,7 +338,7 @@ func compileForPhraseStmt(ctx *blockCtx, v *ast.ForPhraseStmt) {
cb.End()
}

func toForStmt(forPos token.Pos, value ast.Expr, body *ast.BlockStmt, re *ast.RangeExpr, tok token.Token) *ast.ForStmt {
func toForStmt(forPos token.Pos, value ast.Expr, body *ast.BlockStmt, re *ast.RangeExpr, tok token.Token, fp *ast.ForPhrase) *ast.ForStmt {
nilIdent := value == nil
if !nilIdent {
if v, ok := value.(*ast.Ident); ok && v.Name == "_" {
Expand All @@ -348,11 +348,12 @@ func toForStmt(forPos token.Pos, value ast.Expr, body *ast.BlockStmt, re *ast.Ra
if nilIdent {
value = &ast.Ident{NamePos: forPos, Name: "_gop_k"}
}
if re.First == nil {
re.First = &ast.BasicLit{ValuePos: forPos, Kind: token.INT, Value: "0"}
first := re.First
if first == nil {
first = &ast.BasicLit{ValuePos: forPos, Kind: token.INT, Value: "0"}
}
initLhs := []ast.Expr{value}
initRhs := []ast.Expr{re.First}
initRhs := []ast.Expr{first}
replaceValue := false
var cond ast.Expr
var post ast.Expr
Expand Down Expand Up @@ -390,6 +391,16 @@ func toForStmt(forPos token.Pos, value ast.Expr, body *ast.BlockStmt, re *ast.Ra
}}, body.List...)
tok = token.DEFINE
}
if fp != nil && fp.Cond != nil {
condStmt := &ast.IfStmt{
Init: fp.Init,
Cond: fp.Cond,
Body: body,
}
body = &ast.BlockStmt{
List: []ast.Stmt{condStmt},
}
}
return &ast.ForStmt{
For: forPos,
Init: &ast.AssignStmt{
Expand Down

0 comments on commit 2e490be

Please sign in to comment.