nv50: emit predicate for interp
[mesa.git] / src / glsl / apps / tokenise.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <assert.h>
32 #include "../pp/sl_pp_public.h"
33
34
35 int
36 main(int argc,
37 char *argv[])
38 {
39 FILE *in;
40 long size;
41 char *inbuf;
42 struct sl_pp_purify_options options;
43 struct sl_pp_context *context;
44 struct sl_pp_token_info *tokens;
45 FILE *out;
46 unsigned int i;
47
48 if (argc != 3) {
49 printf("Usage: tokenize infile outfile\n");
50 return 1;
51 }
52
53 in = fopen(argv[1], "rb");
54 if (!in) {
55 return 1;
56 }
57
58 fseek(in, 0, SEEK_END);
59 size = ftell(in);
60 assert(size != -1);
61 if (size == -1) {
62 return 1;
63 }
64 fseek(in, 0, SEEK_SET);
65
66 out = fopen(argv[2], "wb");
67 if (!out) {
68 fclose(in);
69 return 1;
70 }
71
72 inbuf = malloc(size + 1);
73 if (!inbuf) {
74 fprintf(out, "$OOMERROR\n");
75
76 fclose(out);
77 fclose(in);
78 return 1;
79 }
80
81 if (fread(inbuf, 1, size, in) != size) {
82 fprintf(out, "$READERROR\n");
83
84 free(inbuf);
85 fclose(out);
86 fclose(in);
87 return 1;
88 }
89 inbuf[size] = '\0';
90
91 fclose(in);
92
93 memset(&options, 0, sizeof(options));
94
95 context = sl_pp_context_create(inbuf, &options);
96 if (!context) {
97 fprintf(out, "$CONTEXERROR\n");
98
99 free(inbuf);
100 fclose(out);
101 return 1;
102 }
103
104 if (sl_pp_tokenise(context, &tokens)) {
105 fprintf(out, "$ERROR: `%s'\n", sl_pp_context_error_message(context));
106
107 sl_pp_context_destroy(context);
108 free(inbuf);
109 fclose(out);
110 return 1;
111 }
112
113 free(inbuf);
114
115 for (i = 0; tokens[i].token != SL_PP_EOF; i++) {
116 switch (tokens[i].token) {
117 case SL_PP_WHITESPACE:
118 break;
119
120 case SL_PP_NEWLINE:
121 fprintf(out, "\n");
122 break;
123
124 case SL_PP_HASH:
125 fprintf(out, "# ");
126 break;
127
128 case SL_PP_COMMA:
129 fprintf(out, ", ");
130 break;
131
132 case SL_PP_SEMICOLON:
133 fprintf(out, "; ");
134 break;
135
136 case SL_PP_LBRACE:
137 fprintf(out, "{ ");
138 break;
139
140 case SL_PP_RBRACE:
141 fprintf(out, "} ");
142 break;
143
144 case SL_PP_LPAREN:
145 fprintf(out, "( ");
146 break;
147
148 case SL_PP_RPAREN:
149 fprintf(out, ") ");
150 break;
151
152 case SL_PP_LBRACKET:
153 fprintf(out, "[ ");
154 break;
155
156 case SL_PP_RBRACKET:
157 fprintf(out, "] ");
158 break;
159
160 case SL_PP_DOT:
161 fprintf(out, ". ");
162 break;
163
164 case SL_PP_INCREMENT:
165 fprintf(out, "++ ");
166 break;
167
168 case SL_PP_ADDASSIGN:
169 fprintf(out, "+= ");
170 break;
171
172 case SL_PP_PLUS:
173 fprintf(out, "+ ");
174 break;
175
176 case SL_PP_DECREMENT:
177 fprintf(out, "-- ");
178 break;
179
180 case SL_PP_SUBASSIGN:
181 fprintf(out, "-= ");
182 break;
183
184 case SL_PP_MINUS:
185 fprintf(out, "- ");
186 break;
187
188 case SL_PP_BITNOT:
189 fprintf(out, "~ ");
190 break;
191
192 case SL_PP_NOTEQUAL:
193 fprintf(out, "!= ");
194 break;
195
196 case SL_PP_NOT:
197 fprintf(out, "! ");
198 break;
199
200 case SL_PP_MULASSIGN:
201 fprintf(out, "*= ");
202 break;
203
204 case SL_PP_STAR:
205 fprintf(out, "* ");
206 break;
207
208 case SL_PP_DIVASSIGN:
209 fprintf(out, "/= ");
210 break;
211
212 case SL_PP_SLASH:
213 fprintf(out, "/ ");
214 break;
215
216 case SL_PP_MODASSIGN:
217 fprintf(out, "%%= ");
218 break;
219
220 case SL_PP_MODULO:
221 fprintf(out, "%% ");
222 break;
223
224 case SL_PP_LSHIFTASSIGN:
225 fprintf(out, "<<= ");
226 break;
227
228 case SL_PP_LSHIFT:
229 fprintf(out, "<< ");
230 break;
231
232 case SL_PP_LESSEQUAL:
233 fprintf(out, "<= ");
234 break;
235
236 case SL_PP_LESS:
237 fprintf(out, "< ");
238 break;
239
240 case SL_PP_RSHIFTASSIGN:
241 fprintf(out, ">>= ");
242 break;
243
244 case SL_PP_RSHIFT:
245 fprintf(out, ">> ");
246 break;
247
248 case SL_PP_GREATEREQUAL:
249 fprintf(out, ">= ");
250 break;
251
252 case SL_PP_GREATER:
253 fprintf(out, "> ");
254 break;
255
256 case SL_PP_EQUAL:
257 fprintf(out, "== ");
258 break;
259
260 case SL_PP_ASSIGN:
261 fprintf(out, "= ");
262 break;
263
264 case SL_PP_AND:
265 fprintf(out, "&& ");
266 break;
267
268 case SL_PP_BITANDASSIGN:
269 fprintf(out, "&= ");
270 break;
271
272 case SL_PP_BITAND:
273 fprintf(out, "& ");
274 break;
275
276 case SL_PP_XOR:
277 fprintf(out, "^^ ");
278 break;
279
280 case SL_PP_BITXORASSIGN:
281 fprintf(out, "^= ");
282 break;
283
284 case SL_PP_BITXOR:
285 fprintf(out, "^ ");
286 break;
287
288 case SL_PP_OR:
289 fprintf(out, "|| ");
290 break;
291
292 case SL_PP_BITORASSIGN:
293 fprintf(out, "|= ");
294 break;
295
296 case SL_PP_BITOR:
297 fprintf(out, "| ");
298 break;
299
300 case SL_PP_QUESTION:
301 fprintf(out, "? ");
302 break;
303
304 case SL_PP_COLON:
305 fprintf(out, ": ");
306 break;
307
308 case SL_PP_IDENTIFIER:
309 fprintf(out, "%s ", sl_pp_context_cstr(context, tokens[i].data.identifier));
310 break;
311
312 case SL_PP_UINT:
313 fprintf(out, "(%s) ", sl_pp_context_cstr(context, tokens[i].data._uint));
314 break;
315
316 case SL_PP_FLOAT:
317 fprintf(out, "(%s) ", sl_pp_context_cstr(context, tokens[i].data._float));
318 break;
319
320 case SL_PP_OTHER:
321 if (tokens[i].data.other == '\'') {
322 fprintf(out, "'\\'' ");
323 } else {
324 fprintf(out, "'%c' ", tokens[i].data.other);
325 }
326 break;
327
328 default:
329 assert(0);
330 }
331 }
332
333 sl_pp_context_destroy(context);
334 free(tokens);
335 fclose(out);
336
337 return 0;
338 }