-
Notifications
You must be signed in to change notification settings - Fork 0
/
constantRegistry.go
62 lines (50 loc) · 1.39 KB
/
constantRegistry.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
package gojacego
import (
"math"
"strings"
)
type constantRegistry struct {
caseSensitive bool
constants map[string]constantInfo
}
type constantInfo struct {
name string
value float64
isOverWritable bool
}
func newConstantRegistry(caseSensitive bool) *constantRegistry {
return &constantRegistry{
caseSensitive: caseSensitive,
constants: map[string]constantInfo{},
}
}
func (this *constantRegistry) get(name string) (float64, bool) {
if item, found := this.constants[this.convertConstantName(name)]; found {
return item.value, true
}
return 0, false
}
func (this *constantRegistry) registerConstant(name string, value float64, isOverWritable bool) {
handledConstantName := this.convertConstantName(name)
if item, found := this.constants[handledConstantName]; found {
if !item.isOverWritable {
panic("the constant '" + item.name + "' cannot be overwritten")
}
}
constantInfo := &constantInfo{
name: handledConstantName,
value: value,
isOverWritable: isOverWritable,
}
this.constants[handledConstantName] = *constantInfo
}
func (this *constantRegistry) convertConstantName(name string) string {
if this.caseSensitive {
return name
}
return strings.ToLower(name)
}
func registryDefaultConstants(registry *constantRegistry) {
registry.registerConstant("e", math.E, false)
registry.registerConstant("pi", math.Pi, false)
}