-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.go
229 lines (190 loc) · 3.09 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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package trompe
import (
"fmt"
"strconv"
)
const (
ValueTypeUnit = iota
ValueTypeBool
ValueTypeInt
ValueTypeString
ValueTypeList
ValueTypeTuple
ValueTypeClos
ValueTypeOption
ValueTypeRange
ValueTypeIter
ValueTypePattern
ValueTypeRef
)
type Value interface {
Type() int
Desc() string
}
type Unit struct{}
type Bool struct {
Value bool
}
type Int struct {
Value int
}
type String struct {
Value string
}
type Tuple struct {
Values []Value
}
var SharedUnit = &Unit{}
var SharedTrue = &Bool{true}
var SharedFalse = &Bool{false}
var SharedNone = &Option{nil}
func NewBool(b bool) *Bool {
if b {
return SharedTrue
} else {
return SharedFalse
}
}
func ValueToBool(v Value) (*Bool, bool) {
switch v := v.(type) {
case *Bool:
return v, true
default:
return nil, false
}
}
func ValueToInt(v Value) (*Int, bool) {
switch v := v.(type) {
case *Int:
return v, true
default:
return nil, false
}
}
func ValueToString(v Value) (*String, bool) {
switch v := v.(type) {
case *String:
return v, true
default:
return nil, false
}
}
func ValueToList(v Value) (*List, bool) {
switch v := v.(type) {
case *List:
return v, true
default:
return nil, false
}
}
func ValueToTuple(v Value) (*Tuple, bool) {
switch v := v.(type) {
case *Tuple:
return v, true
default:
return nil, false
}
}
func ValueToClos(v Value) (Closure, bool) {
switch v := v.(type) {
case *CompiledCode:
return v, true
case *Primitive:
return v, true
default:
return nil, false
}
}
func ValueToOption(v Value) (*Option, bool) {
switch v := v.(type) {
case *Option:
return v, true
default:
return nil, false
}
}
func ValueToIter(v Value) (Iter, bool) {
switch v := v.(type) {
case Iter:
return v, true
default:
return nil, false
}
}
func ValueToRef(v Value) (*Ref, bool) {
switch v := v.(type) {
case *Ref:
return v, true
default:
return nil, false
}
}
func (u *Unit) Type() int {
return ValueTypeUnit
}
func (u *Unit) Desc() string {
return "()"
}
func (b *Bool) Type() int {
return ValueTypeBool
}
func (b *Bool) Desc() string {
if b.Value {
return "true"
} else {
return "false"
}
}
func NewInt(i int) *Int {
return &Int{i}
}
func (i *Int) Type() int {
return ValueTypeInt
}
func (i *Int) Desc() string {
return fmt.Sprintf("%d", i.Value)
}
func NewString(s string) *String {
return &String{s}
}
func (s *String) Type() int {
return ValueTypeString
}
func (s *String) Desc() string {
return s.Value
}
func NewTuple(v ...Value) *Tuple {
return &Tuple{v}
}
func (t *Tuple) Type() int {
return ValueTypeTuple
}
func (t *Tuple) Desc() string {
// TODO
return fmt.Sprintf("tuple")
}
func (t *Tuple) Len() int {
return len(t.Values)
}
type Ref struct {
Path string
module *Module
}
func NewRef(path string, mod *Module) *Ref {
return &Ref{Path: path, module: mod}
}
func (r *Ref) Type() int {
return ValueTypeRef
}
func (r *Ref) Desc() string {
return fmt.Sprintf("{%s}", r.Path)
}
func (r *Ref) Module() *Module {
if r.module == nil {
r.module = GetModule(r.Path)
}
return r.module
}
func StrToInt(s string) (int, error) {
return strconv.Atoi(s)
}