1 |
/* ASCEND modelling environment |
2 |
Copyright (C) 2006 Carnegie Mellon University |
3 |
Copyright (C) 1990, 1993, 1994 Thomas Guthrie Epperly |
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 |
*//** |
20 |
@file |
21 |
Expression Module |
22 |
|
23 |
Requires: |
24 |
#include "utilities/ascConfig.h" |
25 |
#include "fractions.h" |
26 |
#include "compiler.h" |
27 |
#include "dimen.h" |
28 |
#include "expr_types.h" |
29 |
*//* |
30 |
by Tom Epperly |
31 |
Last in CVS: $Revision: 1.11 $ $Date: 1998/02/05 16:36:00 $ $Author: ballan $ |
32 |
*/ |
33 |
|
34 |
#ifndef ASC_EXPRS_H |
35 |
#define ASC_EXPRS_H |
36 |
|
37 |
/** addtogroup compiler Compiler |
38 |
@{ |
39 |
*/ |
40 |
|
41 |
extern struct Expr *CreateVarExpr(struct Name *n); |
42 |
/**< |
43 |
* Create a name type expr node. |
44 |
*/ |
45 |
|
46 |
extern struct Expr *CreateDiffExpr(struct Name *n); |
47 |
/**< |
48 |
Create a time-derivative-of-a-single-variable node. Currently just returns |
49 |
the same as 0.0 (a real-valued expression with wildcard dimensions) |
50 |
*/ |
51 |
|
52 |
extern void InitVarExpr(struct Expr *e, CONST struct Name *n); |
53 |
/**< |
54 |
* Given an empty expr node, initialize it to contain the name. |
55 |
* Generally this is only used to init a temporary expr node |
56 |
* that you want to be able to destroy later (or forget later) |
57 |
* without destroying the contents -- in this case name. |
58 |
* How you create and destroy e is your business. using stack space |
59 |
* is suggested.<br><br> |
60 |
* |
61 |
* The problem with creating a varexpr with a name you want to keep |
62 |
* after the node dies is that the name is destroyed when the node is. |
63 |
*/ |
64 |
|
65 |
extern struct Expr *CreateOpExpr(enum Expr_enum t); |
66 |
/**< |
67 |
* Create an operator node. |
68 |
*/ |
69 |
|
70 |
extern struct Expr *CreateSatisfiedExpr(struct Name *n, |
71 |
double tol, |
72 |
CONST dim_type *dims); |
73 |
/**< |
74 |
* Create an satisfied operator node. |
75 |
*/ |
76 |
|
77 |
extern struct Expr *CreateFuncExpr(CONST struct Func *f); |
78 |
/**< |
79 |
* Create a function node. |
80 |
*/ |
81 |
|
82 |
extern struct Expr *CreateIntExpr(long i); |
83 |
/**< |
84 |
* Create an integer node. |
85 |
*/ |
86 |
|
87 |
extern struct Expr *CreateRealExpr(double r, CONST dim_type *dims); |
88 |
/**< |
89 |
* Create a real node with value r and dimensions "dims". |
90 |
*/ |
91 |
|
92 |
extern struct Expr *CreateTrueExpr(void); |
93 |
/**< |
94 |
* Create a boolean node with value TRUE. |
95 |
*/ |
96 |
|
97 |
extern struct Expr *CreateFalseExpr(void); |
98 |
/**< |
99 |
* Create a boolean node with value FALSE. |
100 |
*/ |
101 |
|
102 |
extern struct Expr *CreateAnyExpr(void); |
103 |
/**< |
104 |
* Create a boolean node with undefined value. b_value will be 2. |
105 |
*/ |
106 |
|
107 |
extern struct Expr *CreateSetExpr(struct Set *set); |
108 |
/**< |
109 |
* Create a set node. |
110 |
*/ |
111 |
|
112 |
extern struct Expr *CreateSymbolExpr(symchar *sym); |
113 |
/**< |
114 |
* Create a symbol node. |
115 |
*/ |
116 |
|
117 |
extern struct Expr *CreateQStringExpr(CONST char *qstring); |
118 |
/**< |
119 |
* Create a string node. The difference is that string may contain |
120 |
* anything and are quoted as "qstring is string", whereas symbols |
121 |
* are of the form 'symbol' and may have content restrictions. |
122 |
*/ |
123 |
|
124 |
extern struct Expr *CreateBuiltin(enum Expr_enum t, struct Set *set); |
125 |
/**< |
126 |
* Create a node for SUM, PROD, UNION, etc.... |
127 |
*/ |
128 |
|
129 |
extern void LinkExprs(struct Expr *cur, struct Expr *next); |
130 |
/**< |
131 |
* Link cur to next. |
132 |
*/ |
133 |
|
134 |
extern unsigned long ExprListLength(CONST struct Expr *e); |
135 |
/**< |
136 |
* Traverse list to the end to find the length. |
137 |
* Sometimes one would like to know the length a priori. |
138 |
*/ |
139 |
|
140 |
#ifdef NDEBUG |
141 |
#define NextExpr(e) ((e)->next) |
142 |
#else |
143 |
#define NextExpr(e) NextExprF(e) |
144 |
#endif |
145 |
/**< |
146 |
* Return the expr node linked to e. |
147 |
* @param e <code>CONST struct Expr*</code>, the expr to query. |
148 |
* @return Returns the linked node as type <code>struct Expr*</code>. |
149 |
* @see NextExprF() |
150 |
*/ |
151 |
extern struct Expr *NextExprF(CONST struct Expr *e); |
152 |
/**< |
153 |
* Return the expr node linked to e. |
154 |
* Implementation function for NextExpr(). Do not use this function |
155 |
* directly - use NextExpr() instead. |
156 |
*/ |
157 |
|
158 |
#ifdef NDEBUG |
159 |
#define ExprType(e) ((e)->t) |
160 |
#else |
161 |
#define ExprType(e) ExprTypeF(e) |
162 |
#endif |
163 |
/**< |
164 |
* Return the type of e. |
165 |
* @param e <code>CONST struct Expr*</code>, the expr to query. |
166 |
* @return Returns the type as a <code>enum Expr_enum</code>. |
167 |
* @see ExprTypeF() |
168 |
*/ |
169 |
extern enum Expr_enum ExprTypeF(CONST struct Expr *e); |
170 |
/**< |
171 |
* Return the type of e. |
172 |
* Implementation function for ExprType(). Do not use this function |
173 |
* directly - use ExprType() instead. |
174 |
*/ |
175 |
|
176 |
#ifdef NDEBUG |
177 |
#define ExprName(e) ((e)->v.nptr) |
178 |
#else |
179 |
#define ExprName(e) ExprNameF(e) |
180 |
#endif |
181 |
/**< |
182 |
* Return the name field of a var type expr node. |
183 |
* @param e <code>CONST struct Expr*</code>, the expr to query. |
184 |
* @return Returns the name as a <code>CONST struct Name*</code>. |
185 |
* @see ExprNameF() |
186 |
*/ |
187 |
extern CONST struct Name *ExprNameF(CONST struct Expr *e); |
188 |
/**< |
189 |
* Return the name field of a var type expr node. |
190 |
* Implementation function for ExprName(). Do not use this function |
191 |
* directly - use ExprName() instead. |
192 |
*/ |
193 |
|
194 |
#ifdef NDEBUG |
195 |
#define ExprFunc(e) ((e)->v.fptr) |
196 |
#else |
197 |
#define ExprFunc(e) ExprFuncF(e) |
198 |
#endif |
199 |
/**< |
200 |
* Return the func field of a function type expr node. |
201 |
* @param e <code>CONST struct Expr*</code>, the expr to query. |
202 |
* @return Returns the func as a <code>CONST struct Func*</code>. |
203 |
* @see ExprFuncF() |
204 |
*/ |
205 |
extern CONST struct Func *ExprFuncF(CONST struct Expr *e); |
206 |
/**< |
207 |
* Return the func field of a function type expr node. |
208 |
* Implementation function for ExprFunc(). Do not use this function |
209 |
* directly - use ExprFunc() instead. |
210 |
*/ |
211 |
|
212 |
#ifdef NDEBUG |
213 |
#define ExprIValue(e) ((e)->v.ivalue) |
214 |
#else |
215 |
#define ExprIValue(e) ExprIValueF(e) |
216 |
#endif |
217 |
/**< |
218 |
* Return the integer value of a integer type expr node. |
219 |
* @param e <code>CONST struct Expr*</code>, the expr to query. |
220 |
* @return Returns the value as a <code>long</code>. |
221 |
* @see ExprIValueF() |
222 |
*/ |
223 |
extern long ExprIValueF(CONST struct Expr *e); |
224 |
/**< |
225 |
* Return the integer value of a integer type expr node. |
226 |
* Implementation function for ExprIValue(). Do not use this function |
227 |
* directly - use ExprIValue() instead. |
228 |
*/ |
229 |
|
230 |
#ifdef NDEBUG |
231 |
#define ExprRValue(e) ((e)->v.r.rvalue) |
232 |
#else |
233 |
#define ExprRValue(e) ExprRValueF(e) |
234 |
#endif |
235 |
/**< |
236 |
* Return the real value of a real type expr node. |
237 |
* @param e <code>CONST struct Expr*</code>, the expr to query. |
238 |
* @return Returns the value as a <code>double</code>. |
239 |
* @see ExprRValueF() |
240 |
*/ |
241 |
extern double ExprRValueF(CONST struct Expr *e); |
242 |
/**< |
243 |
* Return the real value of a real type expr node. |
244 |
* Implementation function for ExprRValue(). Do not use this function |
245 |
* directly - use ExprRValue() instead. |
246 |
*/ |
247 |
|
248 |
#ifdef NDEBUG |
249 |
#define ExprRDimensions(e) ((e)->v.r.dimensions) |
250 |
#else |
251 |
#define ExprRDimensions(e) ExprRDimensionsF(e) |
252 |
#endif |
253 |
/**< |
254 |
* Return the dimensions of a real type expr node. |
255 |
* @param e <code>CONST struct Expr*</code>, the expr to query. |
256 |
* @return Returns the value as a <code>CONST dim_type*</code>. |
257 |
* @see ExprRDimensionsF() |
258 |
*/ |
259 |
extern CONST dim_type *ExprRDimensionsF(CONST struct Expr *e); |
260 |
/**< |
261 |
* Return the dimensions of a real type expr node. |
262 |
* Implementation function for ExprRDimensions(). Do not use this function |
263 |
* directly - use ExprRDimensions() instead. |
264 |
*/ |
265 |
|
266 |
#ifdef NDEBUG |
267 |
#define SatisfiedExprName(e) ((e)->v.se.sen) |
268 |
#else |
269 |
#define SatisfiedExprName(e) SatisfiedExprNameF(e) |
270 |
#endif |
271 |
/**< |
272 |
* Return the name field of a var type satisfied expr node. |
273 |
* @param e <code>CONST struct Expr*</code>, the expr to query. |
274 |
* @return Returns the name as a <code>CONST struct Name*</code>. |
275 |
* @see SatisfiedExprNameF() |
276 |
*/ |
277 |
extern CONST struct Name *SatisfiedExprNameF(CONST struct Expr *e); |
278 |
/**< |
279 |
* Return the name field of a var type satisfied expr node. |
280 |
* Implementation function for SatisfiedExprName(). Do not use this function |
281 |
* directly - use SatisfiedExprName() instead. |
282 |
*/ |
283 |
|
284 |
#ifdef NDEBUG |
285 |
#define SatisfiedExprRValue(e) ((e)->v.se.ser.rvalue) |
286 |
#else |
287 |
#define SatisfiedExprRValue(e) SatisfiedExprRValueF(e) |
288 |
#endif |
289 |
/**< |
290 |
* Return the real value of a real type satisfied expr node. |
291 |
* @param e <code>CONST struct Expr*</code>, the expr to query. |
292 |
* @return Returns the value as a <code>double</code>. |
293 |
* @see SatisfiedExprRValueF() |
294 |
*/ |
295 |
extern double SatisfiedExprRValueF(CONST struct Expr *e); |
296 |
/**< |
297 |
* Return the real value of a real type satisfied expr node. |
298 |
* Implementation function for SatisfiedExprRValue(). Do not use this function |
299 |
* directly - use SatisfiedExprRValue() instead. |
300 |
*/ |
301 |
|
302 |
#ifdef NDEBUG |
303 |
#define SatisfiedExprRDimensions(e) ((e)->v.se.ser.dimensions) |
304 |
#else |
305 |
#define SatisfiedExprRDimensions(e) SatisfiedExprRDimensionsF(e) |
306 |
#endif |
307 |
/**< |
308 |
* Return the dimensions of a real type satisfied expr node. |
309 |
* @param e <code>CONST struct Expr*</code>, the expr to query. |
310 |
* @return Returns the dimension as a <code>CONST dim_type*</code>. |
311 |
* @see SatisfiedExprRDimensionsF() |
312 |
*/ |
313 |
extern CONST dim_type *SatisfiedExprRDimensionsF(CONST struct Expr *e); |
314 |
/**< |
315 |
* Return the dimensions of a real type satisfied expr node. |
316 |
* Implementation function for SatisfiedExprRDimensions(). Do not use this function |
317 |
* directly - use SatisfiedExprRDimensions() instead. |
318 |
*/ |
319 |
|
320 |
#ifdef NDEBUG |
321 |
#define ExprBValue(e) ((e)->v.bvalue) |
322 |
#else |
323 |
#define ExprBValue(e) ExprBValueF(e) |
324 |
#endif |
325 |
/**< |
326 |
* Return the boolean value of a boolean type satisfied expr node. |
327 |
* Returns 1 if e is TRUE, 0 if e is FALSE, 2 if e is ANY. |
328 |
* @param e <code>CONST struct Expr*</code>, the expr to query. |
329 |
* @return Returns the value as an <code>int</code>. |
330 |
* @see ExprBValueF() |
331 |
*/ |
332 |
extern int ExprBValueF(CONST struct Expr *e); |
333 |
/**< |
334 |
* Return 1 if e is TRUE, 0 if e is FALSE, 2 if e is ANY. |
335 |
* Implementation function for ExprBValue(). Do not use this function |
336 |
* directly - use ExprBValue() instead. |
337 |
*/ |
338 |
|
339 |
#ifdef NDEBUG |
340 |
#define ExprSValue(e) ((e)->v.s) |
341 |
#else |
342 |
#define ExprSValue(e) ExprSValueF(e) |
343 |
#endif |
344 |
/**< |
345 |
* Return the set value of a set node type. |
346 |
* @param e <code>CONST struct Expr*</code>, the expr to query. |
347 |
* @return Returns the value as an <code>struct Set*</code>. |
348 |
* @see ExprSValueF() |
349 |
*/ |
350 |
extern struct Set *ExprSValueF(CONST struct Expr *e); |
351 |
/**< |
352 |
* Return the set value of a set node type. |
353 |
* Implementation function for ExprSValue(). Do not use this function |
354 |
* directly - use ExprSValue() instead. |
355 |
*/ |
356 |
|
357 |
#ifdef NDEBUG |
358 |
#define ExprSymValue(e) ((e)->v.sym_ptr) |
359 |
#else |
360 |
#define ExprSymValue(e) ExprSymValueF(e) |
361 |
#endif |
362 |
/**< |
363 |
* Return the symbol pointer value from a symbol node type. |
364 |
* @param e <code>CONST struct Expr*</code>, the expr to query. |
365 |
* @return Returns the value as an <code>symchar*</code>. |
366 |
* @see ExprSymValueF() |
367 |
*/ |
368 |
extern symchar *ExprSymValueF(CONST struct Expr *e); |
369 |
/**< |
370 |
* Return the symbol pointer value from a symbol node type. |
371 |
* Implementation function for ExprSymValue(). Do not use this function |
372 |
* directly - use ExprSymValue() instead. |
373 |
*/ |
374 |
|
375 |
#ifdef NDEBUG |
376 |
#define ExprQStrValue(e) ((e)->v.sym_ptr) |
377 |
#else |
378 |
#define ExprQStrValue(e) ExprQStrValueF(e) |
379 |
#endif |
380 |
/**< |
381 |
* Return the string pointer value from a string node type. |
382 |
* The difference between a string and a symbol, is that the former |
383 |
* may contain whitespace. The type is called e_qstring |
384 |
* @param e <code>CONST struct Expr*</code>, the expr to query. |
385 |
* @return Returns the value as an <code>CONST char*</code>. |
386 |
* @see ExprQStrValueF() |
387 |
*/ |
388 |
extern CONST char *ExprQStrValueF(CONST struct Expr *e); |
389 |
/**< |
390 |
* Return the string pointer value from a string node type. |
391 |
* The difference between a string and a symbol, is that the former |
392 |
* may contain whitespace. The type is called e_qstring |
393 |
* Implementation function for ExprQStrValue(). Do not use this function |
394 |
* directly - use ExprQStrValue() instead. |
395 |
*/ |
396 |
|
397 |
#ifdef NDEBUG |
398 |
#define ExprBuiltinSet(e) ((e)->v.s) |
399 |
#else |
400 |
#define ExprBuiltinSet(e) ExprBuiltinSetF(e) |
401 |
#endif |
402 |
/**< |
403 |
* Return the set argument for one of the builtin operations. |
404 |
* SUM, PROD, CARD, etc.. |
405 |
* @param e <code>CONST struct Expr*</code>, the expr to query. |
406 |
* @return Returns the set as an <code>CONST struct Set*</code>. |
407 |
* @see ExprBuiltinSetF() |
408 |
*/ |
409 |
extern CONST struct Set *ExprBuiltinSetF(CONST struct Expr *e); |
410 |
/**< |
411 |
* Return the set argument for one of the builtin operations. |
412 |
* SUM, PROD, CARD, etc. |
413 |
* Implementation function for ExprBuiltinSet(). Do not use this function |
414 |
* directly - use ExprBuiltinSet() instead. |
415 |
*/ |
416 |
|
417 |
extern struct Expr *CopyExprList(CONST struct Expr *e); |
418 |
/**< |
419 |
* Make and return a copy of e. |
420 |
*/ |
421 |
|
422 |
extern void DestroyExprList(struct Expr *e); |
423 |
/**< |
424 |
* Deallocate all the memory associated with e. |
425 |
* Handles NULL input gracefully. |
426 |
*/ |
427 |
|
428 |
extern struct Expr *JoinExprLists(struct Expr *e1, struct Expr *e2); |
429 |
/**< |
430 |
* Append list e2 to the end of e1. This returns e1, unless e1 |
431 |
* is NULL in which case it returns e2. |
432 |
*/ |
433 |
|
434 |
extern int ExprsEqual(CONST struct Expr *e1, CONST struct Expr *e2); |
435 |
/**< |
436 |
* Return TRUE if and only if e1 and e2 are structurally equivalent. |
437 |
*/ |
438 |
|
439 |
extern int CompareExprs(CONST struct Expr *e1, CONST struct Expr *e2); |
440 |
/**< |
441 |
* Compares2 expressions. |
442 |
* Return -1, 0, 1 as e1 is < == > e2. |
443 |
* Expressions being complicated things, this is not easily |
444 |
* explained. The expressions are being compared in the |
445 |
* absence of an instance context, so we're looking for |
446 |
* structural differences. |
447 |
* The NULL Expr > all Expr. |
448 |
*/ |
449 |
|
450 |
extern void exprs_init_pool(void); |
451 |
/**< |
452 |
* Starts memory recycle. do not call twice before stopping recycle. |
453 |
*/ |
454 |
|
455 |
extern void exprs_destroy_pool(void); |
456 |
/**< |
457 |
* Stops memory recycle. do not call while ANY Expr are outstanding. |
458 |
*/ |
459 |
|
460 |
extern void exprs_report_pool(void); |
461 |
/**< |
462 |
* Write the pool report to ASCERR for the exprs pool. |
463 |
*/ |
464 |
|
465 |
/* @} */ |
466 |
|
467 |
#endif /* ASC_EXPRS_H */ |
468 |
|