/
for.go
59 lines (50 loc) · 1.4 KB
/
for.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
package generator
import "fmt"
// For represents a code generator for `for` block.
type For struct {
condition string
statements []Statement
}
// NewFor returns a new `For`.
func NewFor(condition string, statements ...Statement) *For {
return &For{
condition: condition,
statements: statements,
}
}
// AddStatements adds statements for `for` block to `For`. This does *not* set, just add.
// This method returns a *new* `For`; it means this method acts as immutable.
func (fg *For) AddStatements(statements ...Statement) *For {
return &For{
condition: fg.condition,
statements: append(fg.statements, statements...),
}
}
// Statements sets statements for `for` block to `For`. This does *not* add, just set.
// This method returns a *new* `For`; it means this method acts as immutable.
func (fg *For) Statements(statements ...Statement) *For {
return &For{
condition: fg.condition,
statements: statements,
}
}
// Generate generates a `for` block as golang code.
func (fg *For) Generate(indentLevel int) (string, error) {
indent := BuildIndent(indentLevel)
cond := fg.condition
stmt := fmt.Sprintf("%sfor %s", indent, cond)
if cond != "" {
stmt += " "
}
stmt += "{\n"
nextIndentLevel := indentLevel + 1
for _, c := range fg.statements {
gen, err := c.Generate(nextIndentLevel)
if err != nil {
return "", err
}
stmt += gen
}
stmt += fmt.Sprintf("%s}\n", indent)
return stmt, nil
}