-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtype.v
86 lines (80 loc) · 1.61 KB
/
type.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
module reflect
pub struct Type {
pub:
kind Kind
// elem is the element type for arrays and the value type for maps.
elem &Type
// key is only used for describing the map key type.
key &Type
// name is used for structs
name string
}
pub fn (t Type) str() string {
match t.kind {
.is_array {
return '[]${*t.elem}'
}
.is_map {
return 'map[${*t.key}]${*t.elem}'
}
.is_struct {
return t.name
}
else {
return t.kind.str()
}
}
}
// none_type is a special constructor to create a none type used in situations
// where the type is not applicable.
pub fn none_type() &Type {
return &Type{
kind: Kind.is_none
elem: 0
key: 0
}
}
// parse_type returns the Type definition from the string representation.
pub fn parse_type(t string) ?Type {
if t.starts_with('[]') {
elem := parse_type(t[2..]) ?
return Type{
kind: Kind.is_array
elem: &elem
key: none_type()
}
}
if t.starts_with('map[') {
parts := t[4..].split(']')
key := parse_type(parts[0]) ?
elem := parse_type(parts[1]) ?
return Type{
kind: Kind.is_map
elem: &elem
key: &key
}
}
return Type{
kind: match t {
'bool' { Kind.is_bool }
'string' { Kind.is_string }
'i8' { Kind.is_i8 }
'i16' { Kind.is_i16 }
'int' { Kind.is_int }
'i64' { Kind.is_i64 }
'byte' { Kind.is_byte }
'u16' { Kind.is_u16 }
'u32' { Kind.is_u32 }
'u64' { Kind.is_u64 }
'rune' { Kind.is_rune }
'f32' { Kind.is_f32 }
'f64' { Kind.is_f64 }
// Another other name must be a struct name in the form of
// "mypkg.MyType".
else { Kind.is_struct }
}
elem: none_type()
key: none_type()
name: t
}
}