vxx2 / vlib / v3 / types / universe.v
210 lines · 206 sloc · 3.84 KB · ebf2f0131f8dd63a65cbb7b25ceba589e07bd33c
Raw
1module types
2
3pub const bool_ = Primitive{
4 props: .boolean
5}
6pub const int_ = Primitive{
7 props: .integer
8}
9pub const i8_ = Primitive{
10 props: .integer
11 size: 8
12}
13pub const i16_ = Primitive{
14 props: .integer
15 size: 16
16}
17pub const i32_ = Primitive{
18 props: .integer
19 size: 32
20}
21pub const i64_ = Primitive{
22 props: .integer
23 size: 64
24}
25pub const u8_ = Primitive{
26 props: .integer | .unsigned
27 size: 8
28}
29pub const u16_ = Primitive{
30 props: .integer | .unsigned
31 size: 16
32}
33pub const u32_ = Primitive{
34 props: .integer | .unsigned
35 size: 32
36}
37pub const u64_ = Primitive{
38 props: .integer | .unsigned
39 size: 64
40}
41pub const f32_ = Primitive{
42 props: .float
43 size: 32
44}
45pub const f64_ = Primitive{
46 props: .float
47 size: 64
48}
49pub const string_ = String{}
50pub const char_ = Char{}
51pub const rune_ = Rune{}
52pub const isize_ = ISize{}
53pub const usize_ = USize{}
54pub const void_ = Void{}
55pub const nil_ = Nil{}
56pub const none_ = None{}
57pub const voidptr_ = Pointer{
58 base_type: Type(Void{})
59}
60pub const charptr_ = Pointer{
61 base_type: Type(Char{})
62}
63pub const byteptr_ = Pointer{
64 base_type: Type(Primitive{
65 props: .integer | .unsigned
66 size: 8
67 })
68}
69
70// is_builtin_type_name reports whether name is one of V's builtin type names.
71pub fn is_builtin_type_name(name string) bool {
72 return name == 'bool' || name == 'int' || name == 'i8' || name == 'i16' || name == 'i32'
73 || name == 'i64' || name == 'u8' || name == 'byte' || name == 'u16' || name == 'u32'
74 || name == 'u64' || name == 'f32' || name == 'f64' || name == 'string' || name == 'char'
75 || name == 'rune' || name == 'isize' || name == 'usize' || name == 'void'
76 || name == 'voidptr' || name == 'array' || name == 'charptr' || name == 'byteptr'
77 || name == 'nil' || name == 'none'
78}
79
80// builtin_type_value returns the Type for a known builtin type name.
81pub fn builtin_type_value(name string) Type {
82 if name == 'bool' {
83 return Type(Primitive{
84 props: .boolean
85 })
86 }
87 if name == 'int' {
88 return Type(Primitive{
89 props: .integer
90 })
91 }
92 if name == 'i8' {
93 return Type(Primitive{
94 props: .integer
95 size: 8
96 })
97 }
98 if name == 'i16' {
99 return Type(Primitive{
100 props: .integer
101 size: 16
102 })
103 }
104 if name == 'i32' {
105 return Type(Primitive{
106 props: .integer
107 size: 32
108 })
109 }
110 if name == 'i64' {
111 return Type(Primitive{
112 props: .integer
113 size: 64
114 })
115 }
116 if name == 'u8' || name == 'byte' {
117 return Type(Primitive{
118 props: .integer | .unsigned
119 size: 8
120 })
121 }
122 if name == 'u16' {
123 return Type(Primitive{
124 props: .integer | .unsigned
125 size: 16
126 })
127 }
128 if name == 'u32' {
129 return Type(Primitive{
130 props: .integer | .unsigned
131 size: 32
132 })
133 }
134 if name == 'u64' {
135 return Type(Primitive{
136 props: .integer | .unsigned
137 size: 64
138 })
139 }
140 if name == 'f32' {
141 return Type(Primitive{
142 props: .float
143 size: 32
144 })
145 }
146 if name == 'f64' {
147 return Type(Primitive{
148 props: .float
149 size: 64
150 })
151 }
152 if name == 'string' {
153 return Type(String{})
154 }
155 if name == 'char' {
156 return Type(Char{})
157 }
158 if name == 'rune' {
159 return Type(Rune{})
160 }
161 if name == 'isize' {
162 return Type(ISize{})
163 }
164 if name == 'usize' {
165 return Type(USize{})
166 }
167 if name == 'void' {
168 return Type(Void{})
169 }
170 if name == 'voidptr' {
171 return Type(Pointer{
172 base_type: Type(Void{})
173 })
174 }
175 if name == 'array' {
176 return Type(Array{
177 elem_type: Type(Void{})
178 })
179 }
180 if name == 'charptr' {
181 return Type(Pointer{
182 base_type: Type(Char{})
183 })
184 }
185 if name == 'byteptr' {
186 return Type(Pointer{
187 base_type: Type(Primitive{
188 props: .integer | .unsigned
189 size: 8
190 })
191 })
192 }
193 if name == 'nil' {
194 return Type(Nil{})
195 }
196 if name == 'none' {
197 return Type(None{})
198 }
199 return Type(Unknown{
200 reason: 'unknown builtin type'
201 })
202}
203
204// builtin_type returns the Type for a builtin type name, or none otherwise.
205pub fn builtin_type(name string) ?Type {
206 if is_builtin_type_name(name) {
207 return builtin_type_value(name)
208 }
209 return none
210}
211