Line | Branch | Exec | Source |
---|---|---|---|
1 | // Copyright Contributors to the OpenVDB Project | ||
2 | // SPDX-License-Identifier: MPL-2.0 | ||
3 | |||
4 | /// @file ast/Tokens.h | ||
5 | /// | ||
6 | /// @authors Nick Avramoussis | ||
7 | /// | ||
8 | /// @brief Various function and operator tokens used throughout the | ||
9 | /// AST and code generation | ||
10 | /// | ||
11 | |||
12 | #ifndef OPENVDB_AX_AST_TOKENS_HAS_BEEN_INCLUDED | ||
13 | #define OPENVDB_AX_AST_TOKENS_HAS_BEEN_INCLUDED | ||
14 | |||
15 | #include "../Exceptions.h" | ||
16 | |||
17 | #include <openvdb/version.h> | ||
18 | #include <openvdb/Types.h> | ||
19 | |||
20 | #include <stdexcept> | ||
21 | |||
22 | namespace openvdb { | ||
23 | OPENVDB_USE_VERSION_NAMESPACE | ||
24 | namespace OPENVDB_VERSION_NAME { | ||
25 | |||
26 | namespace ax { | ||
27 | namespace ast { | ||
28 | |||
29 | namespace tokens { | ||
30 | |||
31 | enum CoreType | ||
32 | { | ||
33 | BOOL = 0, | ||
34 | CHAR, | ||
35 | INT16, | ||
36 | INT32, | ||
37 | INT64, | ||
38 | FLOAT, | ||
39 | DOUBLE, | ||
40 | // | ||
41 | VEC2I, | ||
42 | VEC2F, | ||
43 | VEC2D, | ||
44 | // | ||
45 | VEC3I, | ||
46 | VEC3F, | ||
47 | VEC3D, | ||
48 | // | ||
49 | VEC4I, | ||
50 | VEC4F, | ||
51 | VEC4D, | ||
52 | // | ||
53 | MAT3F, | ||
54 | MAT3D, | ||
55 | // | ||
56 | MAT4F, | ||
57 | MAT4D, | ||
58 | // | ||
59 | QUATF, | ||
60 | QUATD, | ||
61 | // | ||
62 | STRING, | ||
63 | UNKNOWN | ||
64 | }; | ||
65 | |||
66 |
2/2✓ Branch 0 taken 14704 times.
✓ Branch 1 taken 40539 times.
|
55243 | inline CoreType tokenFromTypeString(const std::string& type) |
67 | { | ||
68 |
2/2✓ Branch 0 taken 14704 times.
✓ Branch 1 taken 40539 times.
|
55243 | if (type[0] == 'v') { |
69 |
2/2✓ Branch 0 taken 13630 times.
✓ Branch 1 taken 1074 times.
|
14704 | if (type == "vec2i") return VEC2I; |
70 |
2/2✓ Branch 0 taken 13038 times.
✓ Branch 1 taken 592 times.
|
13630 | if (type == "vec2f") return VEC2F; |
71 |
2/2✓ Branch 0 taken 11964 times.
✓ Branch 1 taken 1074 times.
|
13038 | if (type == "vec2d") return VEC2D; |
72 |
2/2✓ Branch 0 taken 10338 times.
✓ Branch 1 taken 1626 times.
|
11964 | if (type == "vec3i") return VEC3I; |
73 |
2/2✓ Branch 0 taken 8880 times.
✓ Branch 1 taken 1458 times.
|
10338 | if (type == "vec3f") return VEC3F; |
74 |
2/2✓ Branch 0 taken 6030 times.
✓ Branch 1 taken 2850 times.
|
8880 | if (type == "vec3d") return VEC3D; |
75 |
2/2✓ Branch 0 taken 4604 times.
✓ Branch 1 taken 1426 times.
|
6030 | if (type == "vec4i") return VEC4I; |
76 |
2/2✓ Branch 0 taken 3808 times.
✓ Branch 1 taken 796 times.
|
4604 | if (type == "vec4f") return VEC4F; |
77 |
2/2✓ Branch 0 taken 2294 times.
✓ Branch 1 taken 1514 times.
|
3808 | if (type == "vec4d") return VEC4D; |
78 | } | ||
79 |
2/2✓ Branch 0 taken 5776 times.
✓ Branch 1 taken 34763 times.
|
40539 | else if (type[0] == 'm') { |
80 |
2/2✓ Branch 0 taken 5028 times.
✓ Branch 1 taken 748 times.
|
5776 | if (type == "mat3f") return MAT3F; |
81 |
2/2✓ Branch 0 taken 3522 times.
✓ Branch 1 taken 1506 times.
|
5028 | if (type == "mat3d") return MAT3D; |
82 |
2/2✓ Branch 0 taken 2750 times.
✓ Branch 1 taken 772 times.
|
3522 | if (type == "mat4f") return MAT4F; |
83 |
2/2✓ Branch 0 taken 1244 times.
✓ Branch 1 taken 1506 times.
|
2750 | if (type == "mat4d") return MAT4D; |
84 | } | ||
85 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 34763 times.
|
34763 | else if (type[0] == 'q') { |
86 | ✗ | if (type == "quatf") return QUATF; | |
87 | ✗ | if (type == "quatd") return QUATD; | |
88 | } | ||
89 |
2/2✓ Branch 0 taken 9149 times.
✓ Branch 1 taken 25614 times.
|
34763 | else if (type[0] == 'i') { |
90 |
2/2✓ Branch 0 taken 8407 times.
✓ Branch 1 taken 742 times.
|
9149 | if (type == "int16") return INT16; |
91 |
1/2✓ Branch 0 taken 8407 times.
✗ Branch 1 not taken.
|
8407 | if (type == "int") return INT32; |
92 |
2/2✓ Branch 0 taken 2164 times.
✓ Branch 1 taken 6243 times.
|
8407 | if (type == "int32") return INT32; |
93 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2164 times.
|
2164 | if (type == "int64") return INT64; |
94 | } | ||
95 |
2/2✓ Branch 0 taken 15751 times.
✓ Branch 1 taken 9863 times.
|
25614 | else if (type == "bool") return BOOL; |
96 |
1/2✓ Branch 0 taken 15751 times.
✗ Branch 1 not taken.
|
15751 | else if (type == "char") return CHAR; |
97 |
2/2✓ Branch 0 taken 9525 times.
✓ Branch 1 taken 6226 times.
|
15751 | else if (type == "float") return FLOAT; |
98 |
2/2✓ Branch 0 taken 787 times.
✓ Branch 1 taken 8738 times.
|
9525 | else if (type == "double") return DOUBLE; |
99 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 723 times.
|
787 | else if (type == "string") return STRING; |
100 | |||
101 | // also handle vdb types that have different type strings to our tokens | ||
102 | // @todo These should probably be separated out. The executables currently | ||
103 | // use this function to guarantee conversion | ||
104 |
2/2✓ Branch 0 taken 2294 times.
✓ Branch 1 taken 1308 times.
|
3602 | if (type[0] == 'v') { |
105 |
2/2✓ Branch 0 taken 1812 times.
✓ Branch 1 taken 482 times.
|
2294 | if (type == "vec2s") return VEC2F; |
106 |
2/2✓ Branch 0 taken 652 times.
✓ Branch 1 taken 1160 times.
|
1812 | if (type == "vec3s") return VEC3F; |
107 |
1/2✓ Branch 0 taken 652 times.
✗ Branch 1 not taken.
|
652 | if (type == "vec4s") return VEC4F; |
108 | } | ||
109 |
2/2✓ Branch 0 taken 1244 times.
✓ Branch 1 taken 64 times.
|
1308 | else if (type[0] == 'm') { |
110 |
2/2✓ Branch 0 taken 632 times.
✓ Branch 1 taken 612 times.
|
1244 | if (type == "mat3s") return MAT3F; |
111 |
1/2✓ Branch 0 taken 632 times.
✗ Branch 1 not taken.
|
632 | if (type == "mat4s") return MAT4F; |
112 | } | ||
113 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
|
64 | else if (type == "quats") return QUATF; |
114 | |||
115 | return UNKNOWN; | ||
116 | } | ||
117 | |||
118 | 443919 | inline std::string typeStringFromToken(const CoreType type) | |
119 | { | ||
120 |
20/24✓ Branch 0 taken 80517 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5146 times.
✓ Branch 3 taken 34768 times.
✓ Branch 4 taken 12444 times.
✓ Branch 5 taken 35896 times.
✓ Branch 6 taken 46950 times.
✓ Branch 7 taken 7581 times.
✓ Branch 8 taken 7601 times.
✓ Branch 9 taken 7621 times.
✓ Branch 10 taken 11036 times.
✓ Branch 11 taken 15957 times.
✓ Branch 12 taken 15205 times.
✓ Branch 13 taken 10949 times.
✓ Branch 14 taken 11061 times.
✓ Branch 15 taken 11325 times.
✓ Branch 16 taken 23993 times.
✓ Branch 17 taken 24741 times.
✓ Branch 18 taken 37789 times.
✓ Branch 19 taken 38569 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✓ Branch 22 taken 4770 times.
✗ Branch 23 not taken.
|
443919 | switch (type) { |
121 | 80517 | case BOOL : return "bool"; | |
122 | ✗ | case CHAR : return "char"; | |
123 | 5146 | case INT16 : return "int16"; | |
124 | 34768 | case INT32 : return "int32"; | |
125 | 12444 | case INT64 : return "int64"; | |
126 | 35896 | case FLOAT : return "float"; | |
127 | 46950 | case DOUBLE : return "double"; | |
128 | 7581 | case VEC2I : return "vec2i"; | |
129 | 7601 | case VEC2F : return "vec2f"; | |
130 | 7621 | case VEC2D : return "vec2d"; | |
131 | 11036 | case VEC3I : return "vec3i"; | |
132 | 15957 | case VEC3F : return "vec3f"; | |
133 | 15205 | case VEC3D : return "vec3d"; | |
134 | 10949 | case VEC4I : return "vec4i"; | |
135 | 11061 | case VEC4F : return "vec4f"; | |
136 | 11325 | case VEC4D : return "vec4d"; | |
137 | 23993 | case MAT3F : return "mat3f"; | |
138 | 24741 | case MAT3D : return "mat3d"; | |
139 | 37789 | case MAT4F : return "mat4f"; | |
140 | 38569 | case MAT4D : return "mat4d"; | |
141 | ✗ | case QUATF : return "quatf"; | |
142 | ✗ | case QUATD : return "quatd"; | |
143 | 4770 | case STRING : return "string"; | |
144 | case UNKNOWN : | ||
145 | default : | ||
146 | ✗ | return "unknown"; | |
147 | } | ||
148 | } | ||
149 | |||
150 | enum OperatorToken | ||
151 | { | ||
152 | //////////////////////////////////////////////////////////////// | ||
153 | /// ARITHMETIC | ||
154 | //////////////////////////////////////////////////////////////// | ||
155 | PLUS = 0, | ||
156 | MINUS, | ||
157 | MULTIPLY, | ||
158 | DIVIDE, | ||
159 | MODULO, | ||
160 | //////////////////////////////////////////////////////////////// | ||
161 | /// LOGICAL | ||
162 | //////////////////////////////////////////////////////////////// | ||
163 | AND, | ||
164 | OR, | ||
165 | NOT, | ||
166 | //////////////////////////////////////////////////////////////// | ||
167 | /// RELATIONAL | ||
168 | //////////////////////////////////////////////////////////////// | ||
169 | EQUALSEQUALS, | ||
170 | NOTEQUALS, | ||
171 | MORETHAN, | ||
172 | LESSTHAN, | ||
173 | MORETHANOREQUAL, | ||
174 | LESSTHANOREQUAL, | ||
175 | //////////////////////////////////////////////////////////////// | ||
176 | /// BITWISE | ||
177 | //////////////////////////////////////////////////////////////// | ||
178 | SHIFTLEFT, | ||
179 | SHIFTRIGHT, | ||
180 | BITAND, | ||
181 | BITOR, | ||
182 | BITXOR, | ||
183 | BITNOT, | ||
184 | //////////////////////////////////////////////////////////////// | ||
185 | /// ASSIGNMENT | ||
186 | //////////////////////////////////////////////////////////////// | ||
187 | EQUALS, | ||
188 | PLUSEQUALS, | ||
189 | MINUSEQUALS, | ||
190 | MULTIPLYEQUALS, | ||
191 | DIVIDEEQUALS, | ||
192 | MODULOEQUALS, | ||
193 | SHIFTLEFTEQUALS, | ||
194 | SHIFTRIGHTEQUALS, | ||
195 | BITANDEQUALS, | ||
196 | BITXOREQUALS, | ||
197 | BITOREQUALS | ||
198 | }; | ||
199 | |||
200 | enum OperatorType | ||
201 | { | ||
202 | ARITHMETIC = 0, | ||
203 | LOGICAL, | ||
204 | RELATIONAL, | ||
205 | BITWISE, | ||
206 | ASSIGNMENT, | ||
207 | UNKNOWN_OPERATOR | ||
208 | }; | ||
209 | |||
210 | inline OperatorType operatorType(const OperatorToken token) | ||
211 | { | ||
212 | 54106 | const size_t idx = static_cast<size_t>(token); | |
213 |
6/8✓ Branch 0 taken 20833 times.
✓ Branch 1 taken 17162 times.
✓ Branch 2 taken 4158 times.
✓ Branch 3 taken 11953 times.
✓ Branch 4 taken 4158 times.
✓ Branch 5 taken 11953 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
58264 | if (idx <= static_cast<size_t>(MODULO)) return ARITHMETIC; |
214 |
4/8✓ Branch 0 taken 6765 times.
✓ Branch 1 taken 14068 times.
✓ Branch 2 taken 4158 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4158 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
29149 | if (idx <= static_cast<size_t>(NOT)) return LOGICAL; |
215 |
2/4✓ Branch 0 taken 304 times.
✓ Branch 1 taken 6816 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
7120 | if (idx <= static_cast<size_t>(LESSTHANOREQUAL)) return RELATIONAL; |
216 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 304 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
304 | if (idx <= static_cast<size_t>(BITNOT)) return BITWISE; |
217 | ✗ | if (idx <= static_cast<size_t>(BITOREQUALS)) return ASSIGNMENT; | |
218 | return UNKNOWN_OPERATOR; | ||
219 | } | ||
220 | |||
221 | inline OperatorToken operatorTokenFromName(const std::string& name) | ||
222 | { | ||
223 | if (name == "+") return PLUS; | ||
224 | if (name == "-") return MINUS; | ||
225 | if (name == "*") return MULTIPLY; | ||
226 | if (name == "/") return DIVIDE; | ||
227 | if (name == "%") return MODULO; | ||
228 | if (name == "&&") return AND; | ||
229 | if (name == "||") return OR; | ||
230 | if (name == "!") return NOT; | ||
231 | if (name == "==") return EQUALSEQUALS; | ||
232 | if (name == "!=") return NOTEQUALS; | ||
233 | if (name == ">") return MORETHAN; | ||
234 | if (name == "<") return LESSTHAN; | ||
235 | if (name == ">=") return MORETHANOREQUAL; | ||
236 | if (name == "<=") return LESSTHANOREQUAL; | ||
237 | if (name == "<<") return SHIFTLEFT; | ||
238 | if (name == ">>") return SHIFTRIGHT; | ||
239 | if (name == "&") return BITAND; | ||
240 | if (name == "|") return BITOR; | ||
241 | if (name == "^") return BITXOR; | ||
242 | if (name == "~") return BITNOT; | ||
243 | if (name == "=") return EQUALS; | ||
244 | if (name == "+=") return PLUSEQUALS; | ||
245 | if (name == "-=") return MINUSEQUALS; | ||
246 | if (name == "*=") return MULTIPLYEQUALS; | ||
247 | if (name == "/=") return DIVIDEEQUALS; | ||
248 | if (name == "%=") return MODULOEQUALS; | ||
249 | if (name == "<<=") return SHIFTLEFTEQUALS; | ||
250 | if (name == ">>=") return SHIFTRIGHTEQUALS; | ||
251 | if (name == "&=") return BITANDEQUALS; | ||
252 | if (name == "^=") return BITXOREQUALS; | ||
253 | if (name == "|=") return BITOREQUALS; | ||
254 | OPENVDB_THROW(AXTokenError, "Unsupported op \"" + name + "\""); | ||
255 | } | ||
256 | |||
257 | 146 | inline std::string operatorNameFromToken(const OperatorToken token) | |
258 | { | ||
259 |
21/32✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 10 times.
✓ Branch 8 taken 1 times.
✓ Branch 9 taken 1 times.
✓ Branch 10 taken 1 times.
✓ Branch 11 taken 3 times.
✓ Branch 12 taken 1 times.
✓ Branch 13 taken 1 times.
✓ Branch 14 taken 18 times.
✓ Branch 15 taken 18 times.
✓ Branch 16 taken 18 times.
✓ Branch 17 taken 14 times.
✓ Branch 18 taken 19 times.
✓ Branch 19 taken 11 times.
✓ Branch 20 taken 6 times.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
|
146 | switch (token) { |
260 | 3 | case PLUS : return "+"; | |
261 | 3 | case MINUS : return "-"; | |
262 | 4 | case MULTIPLY : return "*"; | |
263 | 4 | case DIVIDE : return "/"; | |
264 | 6 | case MODULO : return "%"; | |
265 | 2 | case AND : return "&&"; | |
266 | 2 | case OR : return "||"; | |
267 | 10 | case NOT : return "!"; | |
268 | 1 | case EQUALSEQUALS : return "=="; | |
269 | 1 | case NOTEQUALS : return "!="; | |
270 | 1 | case MORETHAN : return ">"; | |
271 | 3 | case LESSTHAN : return "<"; | |
272 | 1 | case MORETHANOREQUAL : return ">="; | |
273 | 1 | case LESSTHANOREQUAL : return "<="; | |
274 | 18 | case SHIFTLEFT : return "<<"; | |
275 | 18 | case SHIFTRIGHT : return ">>"; | |
276 | 18 | case BITAND : return "&"; | |
277 | 14 | case BITOR : return "|"; | |
278 | 19 | case BITXOR : return "^"; | |
279 | 11 | case BITNOT : return "~"; | |
280 | 6 | case EQUALS : return "="; | |
281 | ✗ | case PLUSEQUALS : return "+="; | |
282 | ✗ | case MINUSEQUALS : return "-="; | |
283 | ✗ | case MULTIPLYEQUALS : return "*="; | |
284 | ✗ | case DIVIDEEQUALS : return "/="; | |
285 | ✗ | case MODULOEQUALS : return "%="; | |
286 | ✗ | case SHIFTLEFTEQUALS : return "<<="; | |
287 | ✗ | case SHIFTRIGHTEQUALS : return ">>="; | |
288 | ✗ | case BITANDEQUALS : return "&="; | |
289 | ✗ | case BITXOREQUALS : return "^="; | |
290 | ✗ | case BITOREQUALS : return "|="; | |
291 | default : | ||
292 | ✗ | OPENVDB_THROW(AXTokenError, "Unsupported op"); | |
293 | } | ||
294 | } | ||
295 | |||
296 | enum LoopToken | ||
297 | { | ||
298 | FOR = 0, | ||
299 | DO, | ||
300 | WHILE | ||
301 | }; | ||
302 | |||
303 | ✗ | inline std::string loopNameFromToken(const LoopToken loop) | |
304 | { | ||
305 | ✗ | switch (loop) { | |
306 | ✗ | case FOR : return "for"; | |
307 | ✗ | case DO : return "do"; | |
308 | ✗ | case WHILE : return "while"; | |
309 | default : | ||
310 | ✗ | OPENVDB_THROW(AXTokenError, "Unsupported loop"); | |
311 | } | ||
312 | } | ||
313 | |||
314 | enum KeywordToken | ||
315 | { | ||
316 | RETURN = 0, | ||
317 | BREAK, | ||
318 | CONTINUE | ||
319 | }; | ||
320 | |||
321 | 4 | inline std::string keywordNameFromToken(const KeywordToken keyw) | |
322 | { | ||
323 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
4 | switch (keyw) { |
324 | 1 | case RETURN : return "return"; | |
325 | 1 | case BREAK : return "break"; | |
326 | 2 | case CONTINUE : return "continue"; | |
327 | default : | ||
328 | ✗ | OPENVDB_THROW(AXTokenError, "Unsupported keyword"); | |
329 | } | ||
330 | } | ||
331 | |||
332 | |||
333 | } // namespace tokens | ||
334 | |||
335 | } // namespace ast | ||
336 | } // namespace ax | ||
337 | } // namespace OPENVDB_VERSION_NAME | ||
338 | } // namespace openvdb | ||
339 | |||
340 | #endif // OPENVDB_AX_AST_TOKENS_HAS_BEEN_INCLUDED | ||
341 | |||
342 |