Skip to content

Commit

Permalink
support if..else if
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Sep 4, 2021
1 parent 8a0f6b0 commit b03535e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
7 changes: 6 additions & 1 deletion package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1405,8 +1405,11 @@ func TestIfElse(t *testing.T) {
/**/ If().DefineVarStart(0, "x").Val(3).EndInit(1).
/******/ Val(ctxRef(pkg, "x")).Val(1).BinaryOp(token.GTR).Then().
/******/ Val(fmt.Ref("Println")).Val("OK!").Call(1).EndStmt().
/**/ Else().
/**/ Else().If().Val(ctxRef(pkg, "x")).Val(0).BinaryOp(token.GTR).Then().
/******/ Val(fmt.Ref("Println")).Val("Hi").Call(1).EndStmt().
/****/ Else().
/******/ Val(fmt.Ref("Println")).Val("Error!").Call(1).EndStmt().
/****/ End().
/**/ End().
End()
domTest(t, pkg, `package main
Expand All @@ -1416,6 +1419,8 @@ import fmt "fmt"
func main() {
if x := 3; x > 1 {
fmt.Println("OK!")
} else if x > 0 {
fmt.Println("Hi")
} else {
fmt.Println("Error!")
}
Expand Down
5 changes: 5 additions & 0 deletions stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ func (p *ifStmt) End(cb *CodeBuilder) {
var el ast.Stmt
if p.body != nil { // if..else
el = blockStmt
if len(stmts) == 1 { // if..else if
if stmt, ok := stmts[0].(*ast.IfStmt); ok {
el = stmt
}
}
} else { // if without else
p.body = blockStmt
}
Expand Down

0 comments on commit b03535e

Please sign in to comment.