/[ascend]/trunk/base/generic/compiler/exprio.c
ViewVC logotype

Contents of /trunk/base/generic/compiler/exprio.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 978 - (show annotations) (download) (as text)
Wed Dec 20 10:26:04 2006 UTC (17 years, 6 months ago) by johnpye
File MIME type: text/x-csrc
File size: 9514 byte(s)
Failed 'ASSERT' statements in a METHOD now cause the statement to be output (as well as its location).
Improved code docs/GPL header for a few files.
Added support for 'ASSERT' in WriteStatement.
Added new WriteStatementError that included var-arg support.
Fixed ASC_ASSERT_RANGE in AscPanic.
1 /* ASCEND modelling environment
2 Copyright (C) 1990, 1993, 1994 Thomas Guthrie Epperly
3 Copyright (C) 2006 Carnegie Mellon University
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19 *//* @file
20 Expression Output Routines
21 *//*
22 by Tom Epperly
23 Last in CVS: $Revision: 1.14 $ $Date: 1998/04/10 23:25:42 $ $Author: ballan $
24 */
25
26 #include <utilities/ascConfig.h>
27 #include <utilities/ascPanic.h>
28 #include <utilities/ascMalloc.h>
29 #include <general/dstring.h>
30 #include "compiler.h"
31 #include "fractions.h"
32 #include "dimen.h"
33 #include "functype.h"
34 #include "expr_types.h"
35 #include "func.h"
36 #include "instance_enum.h"
37 #include <general/list.h>
38 #include "extfunc.h"
39 #include "exprs.h"
40 #include "exprio.h"
41 #include "nameio.h"
42 #include "setio.h"
43
44 CONST char *ExprEnumName(CONST enum Expr_enum t)
45 {
46 switch (t) {
47 case e_nop:
48 return "e_nop";
49 case e_undefined:
50 return "e_undefined";
51 case e_glassbox:
52 return "e_glassbox";
53 case e_blackbox:
54 return "e_blackbox";
55 case e_opcode:
56 return "e_opcode";
57 case e_token:
58 return "e_token";
59 case e_zero:
60 return "e_zero";
61 case e_real:
62 return "e_real";
63 case e_int:
64 return "e_int";
65 case e_var:
66 return "e_var";
67 case e_uminus:
68 return "e_uminus";
69 case e_func:
70 return "e_func";
71 case e_plus:
72 return "e_plus";
73 case e_minus:
74 return "e_minus";
75 case e_times:
76 return "e_times";
77 case e_divide:
78 return "e_divide";
79 case e_power:
80 return "e_power";
81 case e_ipower:
82 return "e_ipower";
83 case e_notequal:
84 return "e_notequal";
85 case e_equal:
86 return "e_equal";
87 case e_less:
88 return "e_less";
89 case e_greater:
90 return "e_greater";
91 case e_lesseq:
92 return "e_lesseq";
93 case e_greatereq:
94 return "e_greatereq";
95 case e_maximize:
96 return "e_maximize";
97 case e_minimize:
98 return "e_minimize";
99 case e_boolean:
100 return "e_boolean";
101 case e_or:
102 return "e_or";
103 case e_and:
104 return "e_and";
105 case e_not:
106 return "e_not";
107 case e_subexpr:
108 return "e_subexpr";
109 case e_const:
110 return "e_const";
111 case e_par:
112 return "e_par";
113 case e_sum:
114 return "e_sum";
115 case e_prod:
116 return "e_prod";
117 case e_symbol:
118 return "e_symbol";
119 case e_set:
120 return "e_set";
121 case e_in:
122 return "e_in";
123 case e_st:
124 return "e_st";
125 case e_card:
126 return "e_card";
127 case e_choice:
128 return "e_choice";
129 case e_union:
130 return "e_union";
131 case e_inter:
132 return "e_inter";
133 case e_qstring:
134 return "e_qstring";
135 default:
136 return "UNKNOWN_TYPE";
137 }
138 }
139
140 void WriteExprNode(FILE *f, CONST struct Expr *e)
141 {
142 AssertMemory(e);
143 switch(ExprType(e)) {
144 case e_uminus:
145 PUTC('-',f);
146 break;
147 case e_plus:
148 PUTC('+',f);
149 break;
150 case e_minus:
151 PUTC('-',f);
152 break;
153 case e_times:
154 PUTC('*',f);
155 break;
156 case e_divide:
157 PUTC('/',f);
158 break;
159 case e_power:
160 PUTC('^',f);
161 break;
162 case e_boolean:
163 switch (ExprBValue(e)) {
164 case 0:
165 FPRINTF(f,"FALSE");
166 break;
167 case 2:
168 FPRINTF(f,"ANY");
169 break;
170 default:
171 FPRINTF(f,"TRUE");
172 break;
173 }
174 break;
175 case e_and:
176 FPRINTF(f,"AND");
177 break;
178 case e_or:
179 FPRINTF(f,"OR");
180 break;
181 case e_not:
182 FPRINTF(f,"NOT");
183 break;
184 case e_equal:
185 PUTC('=',f);
186 break;
187 case e_notequal:
188 FPRINTF(f,"<>");
189 break;
190 case e_boolean_eq:
191 FPRINTF(f,"==");
192 break;
193 case e_boolean_neq:
194 FPRINTF(f,"!=");
195 break;
196 case e_less:
197 PUTC('<',f);
198 break;
199 case e_greater:
200 PUTC('>',f);
201 break;
202 case e_lesseq:
203 FPRINTF(f,"<=");
204 break;
205 case e_greatereq:
206 FPRINTF(f,">=");
207 break;
208 case e_st:
209 PUTC('|',f);
210 break;
211 case e_in:
212 FPRINTF(f,"IN");
213 break;
214 case e_var:
215 WriteName(f,ExprName(e));
216 break;
217 case e_int:
218 FPRINTF(f,"%ld",ExprIValue(e));
219 break;
220 case e_zero:
221 FPRINTF(f,"0");
222 break;
223 case e_real:
224 FPRINTF(f,"%g",ExprRValue(e));
225 break;
226 case e_set:
227 PUTC('[',f);
228 WriteSet(f,ExprSValue(e));
229 PUTC(']',f);
230 break;
231 case e_symbol:
232 FPRINTF(f,"'%s'",SCP(ExprSymValue(e)));
233 break;
234 case e_qstring:
235 FPRINTF(f,"\"%s\"",ExprQStrValue(e));
236 break;
237 case e_card:
238 FPRINTF(f,"CARD[");
239 WriteSet(f,ExprBuiltinSet(e));
240 PUTC(']',f);
241 break;
242 case e_choice:
243 FPRINTF(f,"CHOICE[");
244 WriteSet(f,ExprBuiltinSet(e));
245 PUTC(']',f);
246 break;
247 case e_sum:
248 FPRINTF(f,"SUM[");
249 WriteSet(f,ExprBuiltinSet(e));
250 PUTC(']',f);
251 break;
252 case e_prod:
253 FPRINTF(f,"PROD[");
254 WriteSet(f,ExprBuiltinSet(e));
255 PUTC(']',f);
256 break;
257 case e_union:
258 FPRINTF(f,"UNION[");
259 WriteSet(f,ExprBuiltinSet(e));
260 PUTC(']',f);
261 break;
262 case e_inter:
263 FPRINTF(f,"INTERSECTION[");
264 WriteSet(f,ExprBuiltinSet(e));
265 PUTC(']',f);
266 break;
267 case e_minimize:
268 FPRINTF(f,"MINIMIZE");
269 break;
270 case e_maximize:
271 FPRINTF(f,"MAXIMIZE");
272 break;
273 case e_func:
274 FPRINTF(f,FuncName(ExprFunc(e)));
275 break;
276 case e_satisfied:
277 FPRINTF(f,"SATISFIED(");
278 WriteName(f,SatisfiedExprName(e));
279 if (SatisfiedExprRValue(e)!=DBL_MAX){
280 FPRINTF(f,",");
281 FPRINTF(f,"%g",SatisfiedExprRValue(e));
282 }
283 PUTC(')',f);
284 break;
285 default:
286 FPRINTF(f,"<term>");
287 break;
288 }
289 }
290
291
292 void WriteExpr(FILE *f, CONST struct Expr *e)
293 {
294 while (e!=NULL) {
295 AssertMemory(e);
296 WriteExprNode(f,e);
297 e = NextExpr(e);
298 if (e!=NULL) {
299 PUTC(' ',f);
300 }
301 }
302 return;
303 }
304
305 /*
306 * A ridiculous piece of code to convert an expr back into
307 * a string. This version is in postfix, as it was read in.
308 * The temporary buffer is used to collect known small strings
309 * which then get appended to the dynamic string. These cases
310 * break rather than return. The longer strings get appended
311 * immeadiately, and return.
312 */
313 void WriteExprNode2Str(Asc_DString *dstring, CONST struct Expr *e)
314 {
315 char tmp[64];
316
317 AssertMemory(e);
318 switch(ExprType(e)) {
319 case e_uminus:
320 strcpy(tmp,"-");
321 break;
322 case e_plus:
323 strcpy(tmp,"+");
324 break;
325 case e_minus:
326 strcpy(tmp,"-");
327 break;
328 case e_times:
329 strcpy(tmp,"*");
330 break;
331 case e_divide:
332 strcpy(tmp,"/");
333 break;
334 case e_power:
335 strcpy(tmp,"^");
336 break;
337 case e_ipower:
338 strcpy(tmp,"^");
339 break;
340 case e_boolean:
341 ExprBValue(e) ? strcpy(tmp,"TRUE") : strcpy(tmp,"FALSE");
342 break;
343 case e_and:
344 strcpy(tmp,"AND");
345 break;
346 case e_or:
347 strcpy(tmp,"OR");
348 break;
349 case e_not:
350 strcpy(tmp,"NOT");
351 break;
352 case e_equal:
353 strcpy(tmp,"=");
354 break;
355 case e_notequal:
356 strcpy(tmp,"<>");
357 break;
358 case e_less:
359 strcpy(tmp,"<");
360 break;
361 case e_greater:
362 strcpy(tmp,">");
363 break;
364 case e_lesseq:
365 strcpy(tmp,"<=");
366 break;
367 case e_greatereq:
368 strcpy(tmp,">=");
369 break;
370 case e_st:
371 strcpy(tmp,"/");
372 break;
373 case e_in:
374 strcpy(tmp,"IN");
375 break;
376 case e_var:
377 WriteName2Str(dstring,ExprName(e));
378 return;
379 case e_int:
380 sprintf(tmp,"%ld",ExprIValue(e));
381 break;
382 case e_zero:
383 sprintf(tmp,"0");
384 break;
385 case e_real:
386 sprintf(tmp,"%g",ExprRValue(e));
387 break;
388 case e_set:
389 Asc_DStringAppend(dstring,"[",-1);
390 WriteSet2Str(dstring,ExprSValue(e));
391 Asc_DStringAppend(dstring,"]",-1);
392 return;
393 case e_symbol:
394 sprintf(tmp,"'%s'",SCP(ExprSymValue(e)));
395 break;
396 case e_qstring:
397 sprintf(tmp,"\"%s\"",ExprQStrValue(e));
398 break;
399 case e_card:
400 Asc_DStringAppend(dstring,"CARD[",-1);
401 WriteSet2Str(dstring,ExprBuiltinSet(e));
402 Asc_DStringAppend(dstring,"]",-1);
403 return;
404 case e_choice:
405 Asc_DStringAppend(dstring,"CHOICE[",-1);
406 WriteSet2Str(dstring,ExprBuiltinSet(e));
407 Asc_DStringAppend(dstring,"]",-1);
408 return;
409 case e_sum:
410 Asc_DStringAppend(dstring,"SUM[",-1);
411 WriteSet2Str(dstring,ExprBuiltinSet(e));
412 Asc_DStringAppend(dstring,"]",-1);
413 return;
414 case e_prod:
415 Asc_DStringAppend(dstring,"PROD[",-1);
416 WriteSet2Str(dstring,ExprBuiltinSet(e));
417 Asc_DStringAppend(dstring,"]",-1);
418 return;
419 case e_union:
420 Asc_DStringAppend(dstring,"UNION[",-1);
421 WriteSet2Str(dstring,ExprBuiltinSet(e));
422 Asc_DStringAppend(dstring,"]",-1);
423 return;
424 case e_inter:
425 Asc_DStringAppend(dstring,"INTERSECTION[",-1);
426 WriteSet2Str(dstring,ExprBuiltinSet(e));
427 Asc_DStringAppend(dstring,"]",-1);
428 return;
429 case e_minimize:
430 strcpy(tmp,"MINIMIZE");
431 break;
432 case e_maximize:
433 strcpy(tmp,"MAXIMIZE");
434 break;
435 case e_func:
436 strcpy(tmp,FuncName(ExprFunc(e)));
437 break;
438 default:
439 strcpy(tmp,"<term>");
440 break;
441 }
442 Asc_DStringAppend(dstring,tmp,-1);
443 }
444
445 void WriteExpr2Str(Asc_DString *dstring, CONST struct Expr *e)
446 {
447 while (e!=NULL) {
448 AssertMemory(e);
449 WriteExprNode2Str(dstring,e);
450 e = NextExpr(e);
451 if (e!=NULL) {
452 Asc_DStringAppend(dstring," ",-1);
453 }
454 }
455 return;
456 }
457
458
459
460
461
462
463
464
465

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22