/
val.go
90 lines (81 loc) · 1.25 KB
/
val.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package clac
import (
"github.com/kpmy/ypk/halt"
"reflect"
)
func (v Value) ToBool() (ret bool) {
switch v.T {
case BOOLEAN:
ret = v.V.(bool)
default:
halt.As(100, v.T)
}
return
}
func (v Value) ToInt() (ret int64) {
switch v.T {
case INTEGER:
ret = v.V.(int64)
default:
halt.As(100, v.T)
}
return
}
func (v Value) ToFlo() (ret float64) {
switch v.T {
case INTEGER:
ret = float64(v.V.(int64))
case FLOAT:
ret = v.V.(float64)
default:
halt.As(100, v.T)
}
return
}
func (v Value) ToCmp() (ret complex128) {
switch v.T {
case COMPLEX:
ret = v.V.(complex128)
default:
halt.As(100, v.T)
}
return
}
func This(typ Type, _x interface{}) (ret Value) {
switch typ {
case INTEGER:
switch x := _x.(type) {
case int64:
ret.V = x
case int:
ret.V = int64(x)
default:
halt.As(101, reflect.TypeOf(x))
}
case FLOAT:
switch x := _x.(type) {
case float64:
ret.V = x
default:
halt.As(102, reflect.TypeOf(x))
}
case COMPLEX:
switch x := _x.(type) {
case complex128:
ret.V = x
}
default:
halt.As(100, typ)
}
ret.T = typ
return
}
func thisVal(x interface{}) (ret interface{}) {
switch x.(type) {
case int64, complex128, float64:
ret = x
default:
halt.As(100, reflect.TypeOf(x))
}
return
}