-
Notifications
You must be signed in to change notification settings - Fork 0
/
stmt_declare.go
70 lines (57 loc) · 1.35 KB
/
stmt_declare.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
package codegen
import "strings"
type declarationValues struct {
vars []string
declare bool
}
type declarationStmt struct {
varVal *declarationValues
values []Value
}
// Declare creates a new declaration statement without variable values (will not compile).
// In order to assign values call `Values()`
func Declare(vars ...string) *declarationValues {
if len(vars) == 0 {
panic("no variables are passed for declaration")
}
return &declarationValues{
vars: vars,
declare: true,
}
}
// Assign creates a new assignment statement without variable values (will not compile).
// In order to assign values call `Values()`
func Assign(vars ...string) *declarationValues {
if len(vars) == 0 {
panic("no variables are passed for assignment")
}
return &declarationValues{
vars: vars,
declare: false,
}
}
// Completes the declaration block with values
func (v *declarationValues) Values(values ...Value) *declarationStmt {
if len(values) == 0 {
panic("no values are provided for variable declaration")
}
return &declarationStmt{
varVal: v,
values: values,
}
}
func (d *declarationStmt) writeStmt(sb *strings.Builder) bool {
for i, v := range d.varVal.vars {
if i != 0 {
sb.WriteByte(',')
}
sb.WriteString(v)
}
if d.varVal.declare {
sb.WriteString(":=")
} else {
sb.WriteByte('=')
}
writeValues(sb, d.values)
return true
}