-
Notifications
You must be signed in to change notification settings - Fork 0
/
constant_container.go
61 lines (52 loc) · 1.43 KB
/
constant_container.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
package value
import "fmt"
// Struct for embedding, contains fields
// shared by Module, Mixin, Class, Struct
type ConstantContainer struct {
Name string
Constants SymbolMap
}
// Return a human readable name.
func (m *ConstantContainer) PrintableName() string {
if m.Name == "" {
return "<anonymous>"
}
return m.Name
}
// Set the constant with the specified name
// to the given value.
func (m *ConstantContainer) AddConstantString(name string, val Value) {
switch v := val.(type) {
case *Module:
m.setObjectName(&v.ConstantContainer, name)
case *Class:
m.setObjectName(&v.ConstantContainer, name)
case *Mixin:
m.setObjectName(&v.ConstantContainer, name)
}
m.Constants.SetString(name, val)
}
// Set the constant with the specified name
// to the given value.
func (m *ConstantContainer) AddConstant(name Symbol, val Value) {
switch v := val.(type) {
case *Module:
m.setObjectName(&v.ConstantContainer, string(name.ToString()))
case *Class:
m.setObjectName(&v.ConstantContainer, string(name.ToString()))
case *Mixin:
m.setObjectName(&v.ConstantContainer, string(name.ToString()))
}
m.Constants.Set(name, val)
}
// Set the name of the value when it's assigned to a constant.
func (m *ConstantContainer) setObjectName(obj *ConstantContainer, name string) {
if obj.Name != "" || m.Name == "" {
return
}
if m == &RootModule.ConstantContainer {
obj.Name = name
return
}
obj.Name = fmt.Sprintf("%s::%s", m.Name, name)
}