xgo/ast/if_stmt.go

38 lines
631 B
Go
Raw Normal View History

2019-01-09 10:17:42 +03:00
package ast
import "github.com/d5/tengo/source"
2019-01-09 10:17:42 +03:00
type IfStmt struct {
IfPos source.Pos
2019-01-09 10:17:42 +03:00
Init Stmt
Cond Expr
Body *BlockStmt
Else Stmt // else branch; or nil
}
func (s *IfStmt) stmtNode() {}
func (s *IfStmt) Pos() source.Pos {
2019-01-09 10:17:42 +03:00
return s.IfPos
}
func (s *IfStmt) End() source.Pos {
2019-01-09 10:17:42 +03:00
if s.Else != nil {
return s.Else.End()
}
return s.Body.End()
}
func (s *IfStmt) String() string {
var initStmt, elseStmt string
if s.Init != nil {
initStmt = s.Init.String() + "; "
}
if s.Else != nil {
elseStmt = " else " + s.Else.String()
}
return "if " + initStmt + s.Cond.String() + " " + s.Body.String() + elseStmt
}