-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathtype.v
More file actions
69 lines (68 loc) · 1.48 KB
/
type.v
File metadata and controls
69 lines (68 loc) · 1.48 KB
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
// Copyright (c) 2024 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by a GPL license that can be found in the LICENSE file.
module main
fn (mut app App) gen_zero_value(t Type) {
match t {
MapType {
app.map_type(t)
app.gen('{}')
}
ArrayType {
app.force_upper = true
app.array_type(t)
// For function arrays, don't add {} as V doesn't parse []fn(){} correctly
// Instead generate []fn() which V understands as an empty array literal
if t.elt is FuncType || t.elt is ParenExpr {
// Skip {} for function arrays
} else {
app.gen('{}')
}
}
Ident {
// Check if it's a basic type
v_type := go2v_type(t.name)
match v_type {
'string' {
app.gen("''")
}
'bool' {
app.gen('false')
}
'isize' {
// isize is V's default int type, no cast needed
app.gen('0')
}
'i8', 'i16', 'i32', 'i64', 'usize', 'u8', 'u16', 'u32', 'u64', 'f32', 'f64',
'rune' {
// Non-default numeric types need explicit cast
app.gen('${v_type}(0)')
}
else {
// Custom type - generate Type{}
app.force_upper = true
app.gen(app.go2v_ident(t.name))
app.gen('{}')
}
}
}
StarExpr {
// Pointer type - nil
if app.in_unsafe_block {
app.gen('nil')
} else {
app.gen('unsafe { nil }')
}
}
FuncType {
// Function type - nil
if app.in_unsafe_block {
app.gen('nil')
} else {
app.gen('unsafe { nil }')
}
}
else {
app.gen('0')
}
}
}