-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.go
119 lines (100 loc) · 2.15 KB
/
generator.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
117
118
119
package goesprima
import (
"math/big"
"strings"
)
func NewGenerator() *Generator {
return new(Generator)
}
type Generator struct {
ModuleName string
Statements []StatementListItem
}
func (g *Generator) AddStatements(ss ...StatementListItem) *Generator {
g.Statements = append(g.Statements, ss...)
return g
}
func (g *Generator) AddStatement(s StatementListItem) *Generator {
g.Statements = append(g.Statements, s)
return g
}
func (g *Generator) String() string {
s := make([]string, len(g.Statements))
for i, st := range g.Statements {
s[i] = st.String()
}
return strings.Join(s, "\n")
}
// Helper Functions
func StringLiteral(s string) *LiteralValueString {
l := LiteralValueString(s)
return &l
}
func BoolLiteral(b bool) *LiteralValueBool {
l := LiteralValueBool(b)
return &l
}
func NumberLiteral(n interface{}) Literal {
switch t := n.(type) {
case int:
l := LiteralValueNumber(float64(t))
return &l
case *int:
l := LiteralValueNumber(float64(*t))
return &l
case *float64:
l := LiteralValueNumber(*t)
return &l
case float64:
l := LiteralValueNumber(t)
return &l
case big.Float:
l := LiteralValueBigFloat(t)
return &l
case *big.Float:
l := LiteralValueBigFloat(*t)
return &l
default:
panic("Invalid type passed to NumberLiteral")
}
}
func jsElementsToString[T JSElement](values []T) []string {
out := make([]string, len(values))
for i, v := range values {
out[i] = v.String()
}
return out
}
// Indentation
type Indentor interface {
Indent(string) string
IndentArray([]string) []string
}
type Spaces struct {
Spaces int
}
func (sp *Spaces) Indent(s string) string {
strs := sp.IndentArray(strings.Split(s, "\n"))
return strings.Join(strs, "\n")
}
func (sp *Spaces) IndentArray(strs []string) []string {
r := strings.Repeat(" ", sp.Spaces)
for i, str := range strs {
strs[i] = r + str
}
return strs
}
type Tabs struct {
Tabs int
}
func (t *Tabs) Indent(s string) string {
strs := t.IndentArray(strings.Split(s, "\n"))
return strings.Join(strs, "\n")
}
func (t *Tabs) IndentArray(strs []string) []string {
r := strings.Repeat("\t", t.Tabs)
for i, str := range strs {
strs[i] = r + str
}
return strs
}