-
Notifications
You must be signed in to change notification settings - Fork 0
/
if_stmt.go
58 lines (47 loc) · 1.28 KB
/
if_stmt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package goastutil
import (
"fmt"
"go/ast"
)
type IfStmt struct {
parent Stmt
*ast.IfStmt
}
func NewIfStmt(parent Stmt, stmt *ast.IfStmt) *IfStmt {
return &IfStmt{IfStmt: stmt, parent: parent}
}
func (s *IfStmt) PrependStmt(st string) error {
return prependStmt(st, s.parent, s.IfStmt)
}
func (s *IfStmt) AppendStmt(st string) error {
return appendStmt(st, s.parent, s.IfStmt)
}
func (ifs *IfStmt) StmtType() StmtType {
return IfStmtType
}
func (ifs *IfStmt) Init() Stmt {
return NewStmt(ifs, ifs.IfStmt.Init)
}
func (ifs *IfStmt) Cond() Expr {
return NewExpr(ifs.IfStmt.Cond)
}
func (ifs *IfStmt) Body() *BlockStmt {
return NewBlockStmt(ifs, ifs.IfStmt.Body)
}
func (ifs *IfStmt) Else() Stmt {
return NewStmt(ifs, ifs.IfStmt.Else)
}
// String returns a string representation of the IfStmt.
//
// It formats the IfStmt as "if {condition} {body} {else}", where
// {condition} is the string representation of the condition expression,
// {body} is the string representation of the if block body, and {else}
// is the string representation of the else block.
//
// The return type is a string.
func (ifs *IfStmt) String() string {
if ifs.Else() == nil {
return fmt.Sprintf("if %s %s", ifs.Cond(), ifs.Body())
}
return fmt.Sprintf("if %s %s else %s", ifs.Cond(), ifs.Body(), ifs.Else())
}