-
Notifications
You must be signed in to change notification settings - Fork 15
/
logic.go
116 lines (100 loc) · 1.87 KB
/
logic.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package gg
import "io"
type iif struct {
judge Node
body *group
}
func If(judge interface{}) *iif {
return &iif{
judge: parseNode(judge),
body: newGroup("{\n", "\n}", "\n"),
}
}
func (i *iif) render(w io.Writer) {
writeString(w, "if ")
i.judge.render(w)
i.body.render(w)
}
func (i *iif) AddBody(node ...interface{}) *iif {
i.body.append(node...)
return i
}
type ifor struct {
judge Node
body *group
}
func (i *ifor) render(w io.Writer) {
writeString(w, "for ")
i.judge.render(w)
i.body.render(w)
}
func For(judge interface{}) *ifor {
return &ifor{
judge: parseNode(judge),
body: newGroup("{\n", "\n}", "\n"),
}
}
func (i *ifor) AddBody(node ...interface{}) *ifor {
i.body.append(node...)
return i
}
type icase struct {
judge Node // judge == nil means it's a default case.
body *group
}
func (i *icase) render(w io.Writer) {
if i.judge == nil {
writeString(w, "default:")
} else {
writeString(w, "case ")
i.judge.render(w)
writeString(w, ":")
}
i.body.render(w)
}
func (i *icase) AddBody(node ...interface{}) *icase {
i.body.append(node...)
return i
}
type iswitch struct {
judge Node
cases []*icase
defaultCase *icase
}
func (i *iswitch) render(w io.Writer) {
writeString(w, "switch ")
i.judge.render(w)
writeString(w, "{\n")
for _, c := range i.cases {
c.render(w)
writeString(w, "\n")
}
if i.defaultCase != nil {
i.defaultCase.render(w)
writeString(w, "\n")
}
writeString(w, "}")
}
func Switch(judge interface{}) *iswitch {
return &iswitch{
judge: parseNode(judge),
}
}
func (i *iswitch) NewCase(judge Node) *icase {
ic := &icase{
judge: judge,
body: newGroup("\n", "", "\n"),
}
i.cases = append(i.cases, ic)
return ic
}
func (i *iswitch) NewDefault() *icase {
ic := &icase{
body: newGroup("\n", "", "\n"),
}
i.cases = append(i.cases, ic)
return ic
}
func Continue() Node {
return String("continue")
}