-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.go
64 lines (54 loc) · 868 Bytes
/
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
62
63
64
package bcl
import "fmt"
type value any
func isInt(v value) bool {
_, ok := v.(int)
return ok
}
func isFloat(v value) bool {
_, ok := v.(float64)
return ok
}
func isNumber(v value) bool {
return isInt(v) || isFloat(v)
}
func isString(v value) bool {
_, ok := v.(string)
return ok
}
func isBool(v value) bool {
_, ok := v.(bool)
return ok
}
func isFalsey(v value) bool {
switch x := v.(type) {
case bool:
return !x
case int:
return x == 0
case float64:
return x == 0.0
case string:
return x == ""
default:
return x == nil
}
}
func isTruthy(v value) bool { return !isFalsey(v) }
func vtype(v value) string {
switch v.(type) {
case int:
return "int"
case float64:
return "float"
case string:
return "string"
case bool:
return "bool"
default:
if v == nil {
return "nil"
}
return fmt.Sprintf("unknown:%T", v)
}
}