-
Notifications
You must be signed in to change notification settings - Fork 0
/
value_goFunc.go
76 lines (61 loc) · 2.12 KB
/
value_goFunc.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
package codegen
import "strings"
type goFuncValue struct {
name string
args []Value
}
// Len creates a new function call of the Go built-in function `len()`
func Len(val Value) *goFuncValue {
return newGoFunc("len", val)
}
// MakeSlice creates a new function call of the Go built-in function `make()` for an empty slice
func MakeSlice(sliceType *TypeDecl) *goFuncValue {
return MakeSliceWithCount(sliceType, 0)
}
// MakeSliceWithCount creates a new function call of the Go built-in function `make()` for a slice with count
func MakeSliceWithCount(sliceType *TypeDecl, count int) *goFuncValue {
sliceType.Array()
typeString := Identifier(sliceType.name.String())
return newGoFunc("make", typeString, Int(count))
}
// MakeMap creates a new function call of the Go built-in function `make()` for an empty map
func MakeMap(mapType *MapTypeDecl) *goFuncValue {
typeString := Identifier(mapType.String())
return newGoFunc("make", typeString)
}
// MakeMapWithCount creates a new function call of the Go built-in function `make()` for a map with count
func MakeMapWithCount(mapType *MapTypeDecl, count int) *goFuncValue {
typeString := Identifier(mapType.String())
return newGoFunc("make", typeString, Int(count))
}
// Append creates a new function call of the built-in function `append`
func Append(sliceValue Value, elementValues ...Value) *goFuncValue {
vals := make([]Value, len(elementValues)+1)
vals[0] = sliceValue
for i, v := range elementValues {
vals[i+1] = v
}
return newGoFunc("append", vals...)
}
// Equals compares a value of the go function for equality
func (g *goFuncValue) Equals(val Value) *comparisonValue {
return newEquals(g, val, cmpType_Equals)
}
// Equals compares a value of the go function for not being equal
func (g *goFuncValue) NotEquals(val Value) *comparisonValue {
return newEquals(g, val, cmpType_NotEquals)
}
func newGoFunc(name string, args ...Value) *goFuncValue {
return &goFuncValue{
name: name,
args: args,
}
}
func (g *goFuncValue) writeValue(sb *strings.Builder) {
writeF(sb, "%s(", g.name)
writeValues(sb, g.args)
sb.WriteByte(')')
}
func (g *goFuncValue) isPointer() bool {
return false
}