glsl/apps: Adapt to pp interface change.
[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 <assert.h>
31 #include "../pp/sl_pp_context.h"
32 #include "../pp/sl_pp_purify.h"
33 #include "../pp/sl_pp_token.h"
34
35
36 int
37 main(int argc,
38 char *argv[])
39 {
40 FILE *in;
41 long size;
42 char *inbuf;
43 struct sl_pp_purify_options options;
44 char *outbuf;
45 struct sl_pp_context context;
46 struct sl_pp_token_info *tokens;
47 FILE *out;
48 unsigned int i;
49
50 if (argc != 3) {
51 return 1;
52 }
53
54 in = fopen(argv[1], "rb");
55 if (!in) {
56 return 1;
57 }
58
59 fseek(in, 0, SEEK_END);
60 size = ftell(in);
61 fseek(in, 0, SEEK_SET);
62
63 inbuf = malloc(size + 1);
64 if (!inbuf) {
65 fclose(in);
66 return 1;
67 }
68
69 if (fread(inbuf, 1, size, in) != size) {
70 free(inbuf);
71 fclose(in);
72 return 1;
73 }
74 inbuf[size] = '\0';
75
76 fclose(in);
77
78 memset(&options, 0, sizeof(options));
79
80 if (sl_pp_purify(inbuf, &options, &outbuf)) {
81 free(inbuf);
82 return 1;
83 }
84
85 free(inbuf);
86
87 if (sl_pp_context_init(&context)) {
88 free(outbuf);
89 return 1;
90 }
91
92 if (sl_pp_tokenise(&context, outbuf, &tokens)) {
93 sl_pp_context_destroy(&context);
94 free(outbuf);
95 return 1;
96 }
97
98 free(outbuf);
99
100 out = fopen(argv[2], "wb");
101 if (!out) {
102 sl_pp_context_destroy(&context);
103 free(tokens);
104 return 1;
105 }
106
107 for (i = 0; tokens[i].token != SL_PP_EOF; i++) {
108 switch (tokens[i].token) {
109 case SL_PP_WHITESPACE:
110 break;
111
112 case SL_PP_NEWLINE:
113 fprintf(out, "\n");
114 break;
115
116 case SL_PP_HASH:
117 fprintf(out, "# ");
118 break;
119
120 case SL_PP_COMMA:
121 fprintf(out, ", ");
122 break;
123
124 case SL_PP_SEMICOLON:
125 fprintf(out, "; ");
126 break;
127
128 case SL_PP_LBRACE:
129 fprintf(out, "{ ");
130 break;
131
132 case SL_PP_RBRACE:
133 fprintf(out, "} ");
134 break;
135
136 case SL_PP_LPAREN:
137 fprintf(out, "( ");
138 break;
139
140 case SL_PP_RPAREN:
141 fprintf(out, ") ");
142 break;
143
144 case SL_PP_LBRACKET:
145 fprintf(out, "[ ");
146 break;
147
148 case SL_PP_RBRACKET:
149 fprintf(out, "] ");
150 break;
151
152 case SL_PP_DOT:
153 fprintf(out, ". ");
154 break;
155
156 case SL_PP_INCREMENT:
157 fprintf(out, "++ ");
158 break;
159
160 case SL_PP_ADDASSIGN:
161 fprintf(out, "+= ");
162 break;
163
164 case SL_PP_PLUS:
165 fprintf(out, "+ ");
166 break;
167
168 case SL_PP_DECREMENT:
169 fprintf(out, "-- ");
170 break;
171
172 case SL_PP_SUBASSIGN:
173 fprintf(out, "-= ");
174 break;
175
176 case SL_PP_MINUS:
177 fprintf(out, "- ");
178 break;
179
180 case SL_PP_BITNOT:
181 fprintf(out, "~ ");
182 break;
183
184 case SL_PP_NOTEQUAL:
185 fprintf(out, "!= ");
186 break;
187
188 case SL_PP_NOT:
189 fprintf(out, "! ");
190 break;
191
192 case SL_PP_MULASSIGN:
193 fprintf(out, "*= ");
194 break;
195
196 case SL_PP_STAR:
197 fprintf(out, "* ");
198 break;
199
200 case SL_PP_DIVASSIGN:
201 fprintf(out, "/= ");
202 break;
203
204 case SL_PP_SLASH:
205 fprintf(out, "/ ");
206 break;
207
208 case SL_PP_MODASSIGN:
209 fprintf(out, "%= ");
210 break;
211
212 case SL_PP_MODULO:
213 fprintf(out, "% ");
214 break;
215
216 case SL_PP_LSHIFTASSIGN:
217 fprintf(out, "<<= ");
218 break;
219
220 case SL_PP_LSHIFT:
221 fprintf(out, "<< ");
222 break;
223
224 case SL_PP_LESSEQUAL:
225 fprintf(out, "<= ");
226 break;
227
228 case SL_PP_LESS:
229 fprintf(out, "< ");
230 break;
231
232 case SL_PP_RSHIFTASSIGN:
233 fprintf(out, ">>= ");
234 break;
235
236 case SL_PP_RSHIFT:
237 fprintf(out, ">> ");
238 break;
239
240 case SL_PP_GREATEREQUAL:
241 fprintf(out, ">= ");
242 break;
243
244 case SL_PP_GREATER:
245 fprintf(out, "> ");
246 break;
247
248 case SL_PP_EQUAL:
249 fprintf(out, "== ");
250 break;
251
252 case SL_PP_ASSIGN:
253 fprintf(out, "= ");
254 break;
255
256 case SL_PP_AND:
257 fprintf(out, "&& ");
258 break;
259
260 case SL_PP_BITANDASSIGN:
261 fprintf(out, "&= ");
262 break;
263
264 case SL_PP_BITAND:
265 fprintf(out, "& ");
266 break;
267
268 case SL_PP_XOR:
269 fprintf(out, "^^ ");
270 break;
271
272 case SL_PP_BITXORASSIGN:
273 fprintf(out, "^= ");
274 break;
275
276 case SL_PP_BITXOR:
277 fprintf(out, "^ ");
278 break;
279
280 case SL_PP_OR:
281 fprintf(out, "|| ");
282 break;
283
284 case SL_PP_BITORASSIGN:
285 fprintf(out, "|= ");
286 break;
287
288 case SL_PP_BITOR:
289 fprintf(out, "| ");
290 break;
291
292 case SL_PP_QUESTION:
293 fprintf(out, "? ");
294 break;
295
296 case SL_PP_COLON:
297 fprintf(out, ": ");
298 break;
299
300 case SL_PP_IDENTIFIER:
301 fprintf(out, "%s ", sl_pp_context_cstr(&context, tokens[i].data.identifier));
302 break;
303
304 case SL_PP_NUMBER:
305 fprintf(out, "(%s) ", sl_pp_context_cstr(&context, tokens[i].data.number));
306 break;
307
308 case SL_PP_OTHER:
309 if (tokens[i].data.other == '\'') {
310 fprintf(out, "'\\'' ");
311 } else {
312 fprintf(out, "'%c' ", tokens[i].data.other);
313 }
314 break;
315
316 default:
317 assert(0);
318 }
319 }
320
321 sl_pp_context_destroy(&context);
322 free(tokens);
323 fclose(out);
324
325 return 0;
326 }