-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvalue.v
509 lines (440 loc) · 9.4 KB
/
value.v
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
module reflect
union Val {
bool bool
string string
i8 i8
i16 i16
int int
i64 i64
byte byte
u16 u16
u32 u32
u64 u64
rune rune
f32 f32
f64 f64
}
pub struct Value {
mut:
value Val
array_or_map_len int
array_cap int
array_elem_size int
map_keys []Value
map_values []Value
// structs
f map[string]voidptr
ft map[string]Type
// pointer to the array, map or struct
obj voidptr
pub:
typ Type
}
// value_of is used to create a Value
pub fn value_of<T>(x T) Value {
$if T is bool {
return Value{
typ: Type{Kind.is_bool, none_type(), none_type(), ''}
value: Val{
bool: x
}
}
}
$if T is string {
return Value{
typ: Type{Kind.is_string, none_type(), none_type(), ''}
value: Val{
string: x
}
}
}
$if T is i8 {
return Value{
typ: Type{Kind.is_i8, none_type(), none_type(), ''}
value: Val{
i8: x
}
}
}
$if T is i16 {
return Value{
typ: Type{Kind.is_i16, none_type(), none_type(), ''}
value: Val{
i16: x
}
}
}
$if T is int {
return Value{
typ: Type{Kind.is_int, none_type(), none_type(), ''}
value: Val{
int: x
}
}
}
$if T is i64 {
return Value{
typ: Type{Kind.is_i64, none_type(), none_type(), ''}
value: Val{
i64: x
}
}
}
$if T is byte {
return Value{
typ: Type{Kind.is_byte, none_type(), none_type(), ''}
value: Val{
byte: x
}
}
}
$if T is u16 {
return Value{
typ: Type{Kind.is_u16, none_type(), none_type(), ''}
value: Val{
u16: x
}
}
}
$if T is u32 {
return Value{
typ: Type{Kind.is_u32, none_type(), none_type(), ''}
value: Val{
u32: x
}
}
}
$if T is u64 {
return Value{
typ: Type{Kind.is_u64, none_type(), none_type(), ''}
value: Val{
u64: x
}
}
}
$if T is rune {
return Value{
typ: Type{Kind.is_rune, none_type(), none_type(), ''}
value: Val{
rune: x
}
}
}
$if T is f32 {
return Value{
typ: Type{Kind.is_f32, none_type(), none_type(), ''}
value: Val{
f32: x
}
}
}
$if T is f64 {
return Value{
typ: Type{Kind.is_f64, none_type(), none_type(), ''}
value: Val{
f64: x
}
}
}
panic('unsupported value $x')
}
// array_of creates a Value from an array.
pub fn array_of<T>(x []T) Value {
return Value{
typ: parse_type(typeof(x).name) or { panic(err) }
array_or_map_len: x.len
array_cap: x.cap
array_elem_size: x.element_size
obj: x.data
}
}
// map_of creates a Value from a map.
pub fn map_of<K, V>(x map[K]V) Value {
mut keys := []Value{cap: x.len}
mut values := []Value{cap: x.len}
for k, v in x {
keys << value_of(k)
values << value_of(v)
}
return Value{
typ: parse_type(typeof(x).name) or { panic(err) }
array_or_map_len: x.len
map_keys: keys
map_values: values
}
}
pub fn struct_of<T>(x &T) Value {
mut f := map[string]voidptr{}
mut ft := map[string]Type{}
$for field in T.fields {
f[field.name] = &x.$(field.name)
ft[field.name] = parse_type(typeof(field).name) or { panic(err) }
}
return Value{
obj: voidptr(x)
f: f
ft: ft
typ: Type{Kind.is_struct, none_type(), none_type(), ''}
}
}
pub fn (v Value) len() int {
v.must_be2(Kind.is_array, Kind.is_map)
return v.array_or_map_len
}
pub fn (v Value) cap() int {
v.must_be(Kind.is_array)
return v.array_cap
}
pub fn (v Value) get_index(index int) Value {
v.must_be(Kind.is_array)
if v.array_or_map_len < 0 || v.array_or_map_len <= index {
panic('array index $index is out of bounds (len = $v.array_or_map_len)')
}
v2 := Value{
typ: *v.typ.elem
}
unsafe {
dest := match v2.typ.kind {
.is_bool, .is_string, .is_i8, .is_i16, .is_int, .is_i64, .is_byte, .is_u16, .is_u32,
.is_u64, .is_rune, .is_f32, .is_f64 {
voidptr(&v2.value)
}
// TODO(elliotchance): Doesn't support multidimensional arrays.
else {
voidptr(0)
}
}
C.memcpy(dest, voidptr(u64(v.obj) + u64(index * v.array_elem_size)), v.array_elem_size)
}
return v2
}
fn (v Value) must_be(k Kind) {
if v.typ.kind != k {
panic('value must be $k but is $v.typ.kind')
}
}
fn (v Value) must_be2(k1 Kind, k2 Kind) {
if v.typ.kind != k1 && v.typ.kind != k2 {
panic('value must be $k1 or $k2 but is $v.typ.kind')
}
}
pub fn (v Value) get_bool() bool {
v.must_be(Kind.is_bool)
unsafe {
return v.value.bool
}
}
pub fn (v Value) get_string() string {
v.must_be(Kind.is_string)
unsafe {
return v.value.string
}
}
pub fn (v Value) get_i8() i8 {
v.must_be(Kind.is_i8)
unsafe {
return v.value.i8
}
}
pub fn (v Value) get_i16() i16 {
v.must_be(Kind.is_i16)
unsafe {
return v.value.i16
}
}
pub fn (v Value) get_int() int {
v.must_be(Kind.is_int)
unsafe {
return v.value.int
}
}
pub fn (v Value) get_i64() i64 {
v.must_be(Kind.is_i64)
unsafe {
return v.value.i64
}
}
pub fn (v Value) get_byte() byte {
v.must_be(Kind.is_byte)
unsafe {
return v.value.byte
}
}
pub fn (v Value) get_u16() u16 {
v.must_be(Kind.is_u16)
unsafe {
return v.value.u16
}
}
pub fn (v Value) get_u32() u32 {
v.must_be(Kind.is_u32)
unsafe {
return v.value.u32
}
}
pub fn (v Value) get_u64() u64 {
v.must_be(Kind.is_u64)
unsafe {
return v.value.u64
}
}
pub fn (v Value) get_rune() rune {
v.must_be(Kind.is_rune)
unsafe {
return v.value.rune
}
}
pub fn (v Value) get_f32() f32 {
v.must_be(Kind.is_f32)
unsafe {
return v.value.f32
}
}
pub fn (v Value) get_f64() f64 {
v.must_be(Kind.is_f64)
unsafe {
return v.value.f64
}
}
pub fn (v Value) get_key(key Value) Value {
v.must_be(Kind.is_map)
key.must_be(v.typ.key.kind)
for i, k in v.map_keys {
if k.eq(key) {
return Value{
typ: *v.typ.elem
value: v.map_values[i].value
}
}
}
panic('key not found: $key')
}
fn (v Value) eq(v2 Value) bool {
if v.typ.kind != v2.typ.kind {
return false
}
unsafe {
return match v.typ.kind {
.is_bool, .is_string, .is_i8, .is_i16, .is_int, .is_i64, .is_byte, .is_u16, .is_u32,
.is_u64, .is_rune, .is_f32, .is_f64 {
// Biggest by memory, 16 bytes
v.value.string == v2.value.string
}
else {
panic('cannot compare $v.str() and $v2.str()')
false
}
}
}
}
fn (v Value) str() string {
unsafe {
return match v.typ.kind {
.is_bool { '$v.value.bool' }
.is_string { '$v.value.string' }
.is_i8 { '$v.value.i8' }
.is_i16 { '$v.value.i16' }
.is_int { '$v.value.int' }
.is_i64 { '$v.value.i64' }
.is_byte { '$v.value.byte' }
.is_u16 { '$v.value.u16' }
.is_u32 { '$v.value.u32' }
.is_u64 { '$v.value.u64' }
.is_rune { '$v.value.rune' }
.is_f32 { '$v.value.f32' }
.is_f64 { '$v.value.f64' }
// TODO(elliotchance): We should print arrays and maps
else { '<Value:$v.typ.kind>' }
}
}
}
pub fn (v Value) keys() []Value {
v.must_be(Kind.is_map)
return v.map_keys
}
pub fn (v Value) fields() []string {
v.must_be(Kind.is_struct)
mut fields := []string{cap: v.f.len}
for field, _ in v.f {
fields << field
}
return fields
}
pub fn (v Value) field(name string) Value {
v2 := Value{
typ: v.ft[name]
obj: v.f[name]
}
unsafe {
match v2.typ.kind {
.is_none {}
.is_bool { C.memcpy(voidptr(&v2.value.bool), v.f[name], sizeof(bool)) }
.is_string { C.memcpy(voidptr(&v2.value.string), v.f[name], sizeof(string)) }
.is_i8 { C.memcpy(voidptr(&v2.value.i8), v.f[name], sizeof(i8)) }
.is_i16 { C.memcpy(voidptr(&v2.value.i16), v.f[name], sizeof(i16)) }
.is_int { C.memcpy(voidptr(&v2.value.int), v.f[name], sizeof(int)) }
.is_i64 { C.memcpy(voidptr(&v2.value.i64), v.f[name], sizeof(i64)) }
.is_byte { C.memcpy(voidptr(&v2.value.byte), v.f[name], sizeof(byte)) }
.is_u16 { C.memcpy(voidptr(&v2.value.u16), v.f[name], sizeof(u16)) }
.is_u32 { C.memcpy(voidptr(&v2.value.u32), v.f[name], sizeof(u32)) }
.is_u64 { C.memcpy(voidptr(&v2.value.u64), v.f[name], sizeof(u64)) }
.is_rune { C.memcpy(voidptr(&v2.value.rune), v.f[name], sizeof(rune)) }
.is_f32 { C.memcpy(voidptr(&v2.value.f32), v.f[name], sizeof(f32)) }
.is_f64 { C.memcpy(voidptr(&v2.value.f64), v.f[name], sizeof(f64)) }
else { panic('bad type $v2.typ for field $name') }
}
}
return v2
}
pub fn (v Value) set_bool(x bool) {
v.must_be(Kind.is_bool)
unsafe { C.memcpy(voidptr(v.obj), &x, sizeof(x)) }
}
pub fn (v Value) set_string(x string) {
v.must_be(Kind.is_string)
unsafe { C.memcpy(voidptr(v.obj), &x, sizeof(x)) }
}
pub fn (v Value) set_i8(x i8) {
v.must_be(Kind.is_i8)
unsafe { C.memcpy(voidptr(v.obj), &x, sizeof(x)) }
}
pub fn (v Value) set_i16(x i16) {
v.must_be(Kind.is_i16)
unsafe { C.memcpy(voidptr(v.obj), &x, sizeof(x)) }
}
pub fn (v Value) set_int(x int) {
v.must_be(Kind.is_int)
unsafe { C.memcpy(voidptr(v.obj), &x, sizeof(x)) }
}
pub fn (v Value) set_i64(x i64) {
v.must_be(Kind.is_i64)
unsafe { C.memcpy(voidptr(v.obj), &x, sizeof(x)) }
}
pub fn (v Value) set_byte(x byte) {
v.must_be(Kind.is_byte)
unsafe { C.memcpy(voidptr(v.obj), &x, sizeof(x)) }
}
pub fn (v Value) set_u16(x u16) {
v.must_be(Kind.is_u16)
unsafe { C.memcpy(voidptr(v.obj), &x, sizeof(x)) }
}
pub fn (v Value) set_u32(x u32) {
v.must_be(Kind.is_u32)
unsafe { C.memcpy(voidptr(v.obj), &x, sizeof(x)) }
}
pub fn (v Value) set_u64(x u64) {
v.must_be(Kind.is_u64)
unsafe { C.memcpy(voidptr(v.obj), &x, sizeof(x)) }
}
pub fn (v Value) set_rune(x rune) {
v.must_be(Kind.is_rune)
unsafe { C.memcpy(voidptr(v.obj), &x, sizeof(x)) }
}
pub fn (v Value) set_f32(x f32) {
v.must_be(Kind.is_f32)
unsafe { C.memcpy(voidptr(v.obj), &x, sizeof(x)) }
}
pub fn (v Value) set_f64(x f64) {
v.must_be(Kind.is_f64)
unsafe { C.memcpy(voidptr(v.obj), &x, sizeof(x)) }
}