-
Notifications
You must be signed in to change notification settings - Fork 9
/
value.go
61 lines (46 loc) · 1.1 KB
/
value.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 bytecode
import (
"strconv"
"evylang.dev/evy/pkg/parser"
)
type value interface {
Type() *parser.Type
Equals(value) bool
String() string
}
type numVal float64
func (n numVal) Type() *parser.Type { return parser.NUM_TYPE }
func (n numVal) String() string {
return strconv.FormatFloat(float64(n), 'f', -1, 64)
}
func (n numVal) Equals(v value) bool {
n2, ok := v.(numVal)
if !ok {
panic("internal error: Num.Equals called with non-Num value")
}
return n == n2
}
type boolVal bool
func (boolVal) Type() *parser.Type { return parser.BOOL_TYPE }
func (b boolVal) String() string {
return strconv.FormatBool(bool(b))
}
func (b boolVal) Equals(v value) bool {
b2, ok := v.(boolVal)
if !ok {
panic("internal error: Bool.Equals called with non-Bool value")
}
return b == b2
}
type stringVal string
func (s stringVal) Type() *parser.Type { return parser.STRING_TYPE }
func (s stringVal) String() string {
return string(s)
}
func (s stringVal) Equals(v value) bool {
s2, ok := v.(stringVal)
if !ok {
panic("internal error: String.Equals called with non-String value")
}
return s == s2
}