-
Notifications
You must be signed in to change notification settings - Fork 266
/
branch_kind.go
98 lines (84 loc) 路 2.07 KB
/
branch_kind.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package ifelse
// BranchKind is a classifier for if-else branches. It says whether the branch is empty,
// and whether the branch ends with a statement that deviates control flow.
type BranchKind int
const (
// Empty branches do nothing
Empty BranchKind = iota
// Return branches return from the current function
Return
// Continue branches continue a surrounding "for" loop
Continue
// Break branches break a surrounding "for" loop
Break
// Goto branches conclude with a "goto" statement
Goto
// Panic branches panic the current function
Panic
// Exit branches end the program
Exit
// Regular branches do not fit any category above
Regular
)
// IsEmpty tests if the branch is empty
func (k BranchKind) IsEmpty() bool { return k == Empty }
// Returns tests if the branch returns from the current function
func (k BranchKind) Returns() bool { return k == Return }
// Deviates tests if the control does not flow to the first
// statement following the if-else chain.
func (k BranchKind) Deviates() bool {
switch k {
case Empty, Regular:
return false
case Return, Continue, Break, Goto, Panic, Exit:
return true
default:
panic("invalid kind")
}
}
// String returns a brief string representation
func (k BranchKind) String() string {
switch k {
case Empty:
return ""
case Regular:
return "..."
case Return:
return "... return"
case Continue:
return "... continue"
case Break:
return "... break"
case Goto:
return "... goto"
case Panic:
return "... panic()"
case Exit:
return "... os.Exit()"
default:
panic("invalid kind")
}
}
// LongString returns a longer form string representation
func (k BranchKind) LongString() string {
switch k {
case Empty:
return "an empty block"
case Regular:
return "a regular statement"
case Return:
return "a return statement"
case Continue:
return "a continue statement"
case Break:
return "a break statement"
case Goto:
return "a goto statement"
case Panic:
return "a function call that panics"
case Exit:
return "a function call that exits the program"
default:
panic("invalid kind")
}
}