-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalues.go
271 lines (204 loc) · 3.68 KB
/
values.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package wren
import (
"fmt"
)
type ObjType uint
const (
OBJ_STRING ObjType = iota
OBJ_FIBER
OBJ_CLOSURE
OBJ_MODULE
OBJ_RANGE
OBJ_CLASS
OBJ_INSTANCE
)
type Object struct {
classObj *ObjClass
}
type Obj interface {
ObjType() ObjType
PrintObject() string
}
type ValueType uint
const (
VAL_FALSE ValueType = iota
VAL_NUM
VAL_TRUE
VAL_NULL
VAL_STRING
VAL_OBJ
)
type Value interface {
ValueType() ValueType
Print() string
}
type BoolValue struct {
Type ValueType
value bool
}
func (bl BoolValue) ValueType() ValueType {
return bl.Type
}
func (bl BoolValue) Print() string {
return fmt.Sprintf("%t", bl.value)
}
type NullValue struct {
Type ValueType
}
func (nl NullValue) ValueType() ValueType {
return nl.Type
}
func (nl NullValue) Print() string {
return fmt.Sprintf("%s", "null")
}
type NumValue struct {
Type ValueType
Number float64
}
func (nl NumValue) ValueType() ValueType {
return VAL_NUM
}
func (nl NumValue) Print() string {
return fmt.Sprintf("%f", nl.Number)
}
type StringValue struct {
Type ValueType
Value string
}
func (sv StringValue) ValueType() ValueType {
return VAL_STRING
}
func (sv StringValue) Print() string {
return sv.Value
}
type ObjectValue struct {
Type ValueType
Obj Obj
Object
}
func (sv ObjectValue) ValueType() ValueType {
return VAL_OBJ
}
func (sv ObjectValue) Print() string {
return sv.Obj.PrintObject()
}
type StringObject struct {
Type ObjType
Value string
}
func (so StringObject) ObjType() ObjType {
return so.Type
}
func (so StringObject) PrintObject() string {
return so.Value
}
type ObjFn struct {
numUpvalues int
Type ObjType
constants []Value
instructions Instructions
numUpValues int
module *ObjModule
}
func (of *ObjFn) ObjType() ObjType {
return of.Type
}
func (of *ObjFn) PrintObject() string {
return fmt.Sprintf("Fn: [%p]", of)
}
type ObjUpvalue struct {
value *Value
closed Value
}
type ObjClosure struct {
Type ObjType
fn *ObjFn
upvalues []*ObjUpvalue
}
func (oc *ObjClosure) ObjType() ObjType {
return oc.Type
}
func (oc *ObjClosure) PrintObject() string {
return fmt.Sprintf("Closure: [%p]", oc)
}
type CallFrame struct {
ip int
closure *ObjClosure
stackStart int
stack []Value
}
type ObjFiber struct {
Type ObjType
stack []Value
stackTop int
frames []*CallFrame
numFrames int
}
type MethodType uint
const (
METHOD_PRIMITIVE MethodType = iota
METHOD_BLOCK
METHOD_NONE
)
type Primitive func(*WrenVM, []Value) bool
type Method struct {
Type MethodType
primitive Primitive
Closure *ObjClosure
}
type ObjClass struct {
Type ObjType
name string
methods map[string]*Method
superclass *ObjClass
}
func (oc *ObjClass) ObjType() ObjType {
return oc.Type
}
func (oc *ObjClass) PrintObject() string {
return fmt.Sprintf("Class: %s", oc.name)
}
type Demo struct {
value Value
index int
}
type ObjModule struct {
Type ObjType
VariableNames map[string]Demo
//VariableNames map[string]map[Value]int
//VariableNames map[string]Value
variables []Value
Name Value
}
func (om *ObjModule) ObjType() ObjType {
return om.Type
}
func (om *ObjModule) PrintObject() string {
return fmt.Sprintf("Module: [%p]", om)
}
type ObjRange struct {
Type ObjType
from float64
to float64
isInclusive bool
}
func (or *ObjRange) ObjType() ObjType {
return or.Type
}
func (or *ObjRange) PrintObject() string {
sep := "..."
if or.isInclusive {
sep = ".."
}
return fmt.Sprintf("%.f%s%.f", or.from, sep, or.to)
}
type ObjInstance struct {
Type ObjType
Object Object
}
func (or *ObjInstance) ObjType() ObjType {
return or.Type
}
func (oi *ObjInstance) PrintObject() string {
return fmt.Sprintf("Instance[%p]", oi)
}