1 %{
2 /* Force the definition for Linux */
3 /* Possible bug in older Linux yacc */
4 
5 #ifndef NOBISON
6 extern int yylex();
7 extern "C" {
8 	void yyerror( const char *);
9 	int  yyparse( void );
10 }
11 #endif
12 #include <XdmfExpr.h>
13 #include <XdmfArray.h>
14 #include <XdmfHDF.h>
15 #include <math.h>
16 
17 
18 static xdmf2::XdmfArray *XdmfExprReturnValue;
19 XdmfExprSymbol *XdmfExprItemsTable = NULL;
20 
21 namespace xdmf2
22 {
23 
24 class XdmfInt64Array : public XdmfArray {
25 public :
XdmfInt64Array(XdmfInt64 Length)26 	XdmfInt64Array( XdmfInt64 Length ) {
27 		this->SetNumberType( XDMF_INT64_TYPE );
28 		this->SetNumberOfElements( Length );
29 		}
XdmfInt64Array()30 	XdmfInt64Array() {
31 		this->SetNumberType( XDMF_INT64_TYPE );
32 		this->SetNumberOfElements( 10 );
33 		};
34 };
35 
36 }
37 
38 #define ADD_XDMF_tokARRAY_TO_SYMBOL( a ) \
39 	{ \
40 	char	name[80]; \
41 	XdmfExprSymbol *sp; \
42 	sprintf( name, "XdmfArray_%X", ( XdmfLength)(a) ); \
43 	sp = XdmfExprSymbolLookup( name ); \
44 	sp->ClientData = (a); \
45 	}
46 
47 %}
48 
49 %union {
50 	double		DoubleValue;
51 	long		IntegerValue;
52 	void		*ArrayPointer;
53 	XdmfExprSymbol	*Symbol;
54 }
55 
56 %token <DoubleValue>	lFLOAT
57 %token <IntegerValue>	tokINTEGER
58 %token <ArrayPointer>	tokARRAY
59 %token <Symbol>		NAME
60 
61 %token SIN COS TAN ACOS ASIN ATAN LOG EXP ABS_TOKEN SQRT WHERE INDEX
62 %token EQEQ LT LE GT GE NE LTLT GTGT JOIN
63 
64 %left	'-' '+'
65 %left	'*' '/'
66 %left	',' ';'
67 
68 %type <DoubleValue> ScalarExpression
69 %type <ArrayPointer> ArrayExpression
70 %%
71 
72 /* Comments */
73 statemant_list : statement {
74 		/*
75 		printf("Complete\n");
76 		printf("XdmfExprReturnValue Nelms = %d\n", XdmfExprReturnValue->GetNumberOfElements());
77 		*/
78 		}
79 	;
80 
81 statement: tokARRAY '=' ArrayExpression	{
82 		xdmf2::XdmfArray *TempArray = ( xdmf2::XdmfArray *)$3;
83 
84 		/* printf("Setting %s from ArrayExpression\n", $1); */
85 		XdmfExprReturnValue = (xdmf2::XdmfArray *)$1;
86 		*XdmfExprReturnValue = *TempArray;
87 		delete TempArray;
88 		}
89 	|  tokARRAY '=' ScalarExpression {
90 		/* printf("Setting %s from ScalarExpression\n", $1); */
91 		XdmfExprReturnValue = (xdmf2::XdmfArray *)$1;
92 		*XdmfExprReturnValue = $3;
93 		}
94 	|	tokARRAY '[' ArrayExpression ']'  '=' ScalarExpression {
95 			xdmf2::XdmfArray	*Array1 = ( xdmf2::XdmfArray *)$3;
96 			xdmf2::XdmfArray	*Result = ( xdmf2::XdmfArray *)$1;
97 			XdmfLength	i, index, Length = Array1->GetNumberOfElements();
98 
99 			for( i = 0 ; i < Length ; i++ ){
100 				index = (XdmfLength)Array1->GetValueAsFloat64( i );
101 				Result->SetValueFromFloat64( index, $6 );
102 				}
103 			delete Array1;
104 			XdmfExprReturnValue = Result;
105 		}
106 	|	tokARRAY '[' ArrayExpression ']'  '=' ArrayExpression {
107 			xdmf2::XdmfArray	*Array1 = ( xdmf2::XdmfArray *)$3;
108 			xdmf2::XdmfArray	*Array2 = ( xdmf2::XdmfArray *)$6;
109 			xdmf2::XdmfArray	*Result = ( xdmf2::XdmfArray *)$1;
110 			XdmfFloat64	Value;
111 			XdmfLength	i, index, Length = Array1->GetNumberOfElements();
112 
113 			for( i = 0 ; i < Length ; i++ ){
114 				index = (XdmfLength)Array1->GetValueAsFloat64( i );
115 				Value = Array2->GetValueAsFloat64( i );
116 				Result->SetValueFromFloat64( index, Value );
117 				}
118 			delete Array1;
119 			delete Array2;
120 			XdmfExprReturnValue = Result;
121 		}
122 	|	tokARRAY '[' tokINTEGER ':' tokINTEGER ']'  '=' ScalarExpression {
123 			xdmf2::XdmfArray *Range;
124 
125 			/* printf("Array Range %d:%d = ScalarExpression \n", $3, $5);	 */
126 			Range = (xdmf2::XdmfArray *)$1;
127 			XdmfExprReturnValue = Range->Reference( $3, $5 ); /* This is a Reference */
128 			*XdmfExprReturnValue = $8;
129 
130 			/* Now Point to the Entire Array */
131 			XdmfExprReturnValue = (xdmf2::XdmfArray *)$1;
132 			}
133 	|	tokARRAY '[' tokINTEGER ':' tokINTEGER ']'  '=' ArrayExpression {
134 			xdmf2::XdmfArray *TempArray = ( xdmf2::XdmfArray *)$8;
135 			xdmf2::XdmfArray *Range;
136 
137 			/* printf("Array Range %d:%d = ArrayExpression \n", $3, $5); */
138 			Range = (xdmf2::XdmfArray *)$1;
139 			XdmfExprReturnValue = Range->Reference( $3, $5 ); /* This is a Reference */
140 			*XdmfExprReturnValue = *TempArray;
141 
142 			/* Now Point to the Entire Array */
143 			XdmfExprReturnValue = (xdmf2::XdmfArray *)$1;
144 			delete TempArray;
145 			}
146 	|  ArrayExpression {
147 		xdmf2::XdmfArray *TempArray = ( xdmf2::XdmfArray *)$1;
148 
149 		/* printf("Clone from ArrayExpression\n"); */
150 		XdmfExprReturnValue = TempArray;
151 		/* printf("XdmfExprReturnValue Nelms = %d\n", XdmfExprReturnValue->GetNumberOfElements()); */
152 		}
153 	|  ScalarExpression	{
154 		printf("Pointless !! Scalar = %g\n", $1);
155 		}
156 	;
157 
158 ArrayExpression: ArrayExpression '+' ArrayExpression {
159 			xdmf2::XdmfArray *Array1 = ( xdmf2::XdmfArray *)$1;
160 			xdmf2::XdmfArray *Array2 = ( xdmf2::XdmfArray *)$3;
161 
162 			/* printf("Array 0x%X + 0x%X\n", Array1, Array2); */
163 			*Array1 += *Array2;
164 			$$ = Array1;
165 			delete Array2;
166 			}
167 	|	ArrayExpression ',' ArrayExpression {
168 			/* Interlace */
169 			xdmf2::XdmfArray *Array1 = ( xdmf2::XdmfArray *)$1;
170 			xdmf2::XdmfArray *Array2 = ( xdmf2::XdmfArray *)$3;
171 			xdmf2::XdmfArray *NewArray = new xdmf2::XdmfArray();
172 			XdmfInt32 i, Rank1, Rank2;
173 			XdmfInt64 NewLength, Length1, Length2, IFactor, Lcd;
174 			XdmfInt64 Dimension1[ XDMF_MAX_DIMENSION ];
175 			XdmfInt64 Dimension2[ XDMF_MAX_DIMENSION ];
176 			XdmfInt64 Start[ XDMF_MAX_DIMENSION ];
177 			XdmfInt64 Stride[ XDMF_MAX_DIMENSION ];
178 			XdmfInt64 Count[ XDMF_MAX_DIMENSION ];
179 			XdmfInt64 NewDimension[ XDMF_MAX_DIMENSION ];
180 
181 			/* printf("Array 0x%X , 0x%X\n", Array1, Array2); */
182 
183 			Rank1 = Array1->GetShape( Dimension1 );
184 			Rank2 = Array2->GetShape( Dimension2 );
185 			if( Rank1 != Rank2 ){
186 				printf(" Interlace : Rank Mismatch !!\n");
187 				}
188 			NewArray->CopyType( Array1 );
189 
190 			Length1 = Array1->GetNumberOfElements();
191 			Length2 = Array2->GetNumberOfElements();
192 			NewLength = Length1 + Length2;
193 			IFactor = Length1 / Length2;
194 			Lcd = Length1;
195 			if( Length2 < Length1 ){
196 				Lcd = Length2;
197 				}
198 			NewDimension[0] = Lcd;
199 			NewDimension[1] = NewLength / Lcd;
200 			NewArray->SetShape( 2, NewDimension );
201 			/*
202 			printf("Rank1 = %d Rank2 = %d\n", Rank1, Rank2 );
203 			printf("Array1 Size = %d\n", Array1->GetNumberOfElements() );
204 			printf("Array2 Size = %d\n", Array2->GetNumberOfElements() );
205 			printf("NewLength = %d\n", NewLength );
206 			printf("Lcd = %d\n", Lcd );
207 			printf("IFactor = %d\n", IFactor );
208 			printf("New Dims = %s\n", NewArray->GetShapeAsString() );
209 			*/
210 			/* NewArray->Generate( -55.0,  -55.0 ); */
211 			/* Copy in Array 1 */
212 			Start[0] = 0; Start[1] = 0;
213 			Stride[0] = 1; Stride[1] = 1;
214 			Count[0] = Lcd; Count[1] = Length1 / Lcd;
215 			NewArray->SelectHyperSlab( Start, Stride, Count );
216 			Array1->SelectAll();
217 			/*
218 			printf("Copy in Array1 = %s\n", NewArray->GetHyperSlabAsString() );
219 			*/
220 			CopyArray( Array1, NewArray );
221 			/* Copy in Array 2 */
222 			Start[0] = 0; Start[1] = Length1 / Lcd;
223 			Stride[0] = 1; Stride[1] = 1;
224 			Count[0] = Lcd; Count[1] = Length2 / Lcd;
225 			NewArray->SelectHyperSlab( Start, Stride, Count );
226 			Array2->SelectAll();
227 			/*
228 			printf("Copy in Array2 = %s\n", NewArray->GetHyperSlabAsString() );
229 			*/
230 			CopyArray( Array2, NewArray );
231 			NewDimension[0] = Dimension1[0] + Dimension2[0];
232 			for( i = 1 ; i < Rank1 ; i++ ){
233 				NewDimension[i] = Dimension1[i];
234 				}
235 			NewArray->Reform( Rank1, NewDimension );
236 			/*
237 			printf("Result(%s) = %s\n", NewArray->GetShapeAsString(), NewArray->GetValues() );
238 			*/
239 			$$ = NewArray;
240 			delete Array1;
241 			delete Array2;
242 			}
243 
244 	|	ArrayExpression ';' ArrayExpression {
245 			/* Interlace */
246 			xdmf2::XdmfArray *Array1 = ( xdmf2::XdmfArray *)$1;
247 			xdmf2::XdmfArray *Array2 = ( xdmf2::XdmfArray *)$3;
248 			xdmf2::XdmfArray *NewArray = new xdmf2::XdmfArray();
249 			XdmfInt32 i, Rank1, Rank2;
250 			XdmfInt64 Dimension1[ XDMF_MAX_DIMENSION ];
251 			XdmfInt64 Dimension2[ XDMF_MAX_DIMENSION ];
252 			XdmfInt64 Start[ XDMF_MAX_DIMENSION ];
253 			XdmfInt64 Stride[ XDMF_MAX_DIMENSION ];
254 			XdmfInt64 Count[ XDMF_MAX_DIMENSION ];
255 			XdmfInt64 NewDimension[ XDMF_MAX_DIMENSION ];
256 
257 		 	/* printf("Array 0x%X  << 0x%X\n", Array1, Array2); */
258 
259 			Rank1 = Array1->GetShape( Dimension1 );
260 			Rank2 = Array2->GetShape( Dimension2 );
261 			if( Rank1 != Rank2 ){
262 				printf(" Cat : Rank Mismatch !!\n");
263 				}
264 			NewDimension[0] = Dimension1[0] + Dimension2[0];
265 			for( i = 1 ; i < Rank1 ; i++ ){
266 				NewDimension[i] = Dimension1[i];
267 				}
268 			NewArray->CopyType( Array1 );
269 			NewArray->SetShape( Rank1, NewDimension );
270 
271 			/*
272 			NewArray->Generate( -55.0,  -55.0 );
273 			*/
274 			/* Copy in Array 1 */
275 			for( i = 0 ; i < Rank1 ; i++ ){
276 				Start[i] = 0;
277 				Stride[i] = 1;
278 				Count[i] = Dimension1[i];
279 				}
280 			NewArray->SelectHyperSlab( Start, Stride, Count );
281 			Array1->SelectAll();
282 			/*
283 			printf("Copy in Array1 = %s\n", NewArray->GetHyperSlabAsString() );
284 			*/
285 			CopyArray( Array1, NewArray );
286 			/* Copy in Array 2 */
287 			Start[0] = Dimension1[0];
288 			Stride[0] = 1;
289 			Count[0] = Dimension2[0];
290 			for( i = 1 ; i < Rank1 ; i++ ){
291 				Start[i] = 0;
292 				Stride[i] = 1;
293 				Count[i] = Dimension1[i];
294 				}
295 			NewArray->SelectHyperSlab( Start, Stride, Count );
296 			Array2->SelectAll();
297 			/*
298 			printf("Copy in Array2 = %s\n", NewArray->GetHyperSlabAsString() );
299 			*/
300 			CopyArray( Array2, NewArray );
301 			/*
302 			printf("Result(%s) = %s\n", NewArray->GetShapeAsString(), NewArray->GetValues() );
303 			*/
304 			$$ = NewArray;
305 			delete Array1;
306 			delete Array2;
307 			}
308 	|	ArrayExpression '-' ArrayExpression {
309 			xdmf2::XdmfArray *Array1 = ( xdmf2::XdmfArray *)$1;
310 			xdmf2::XdmfArray *Array2 = ( xdmf2::XdmfArray *)$3;
311 
312 			/* printf("Array 0x%X + 0x%X\n", Array1, Array2); */
313 			*Array1 -= *Array2;
314 			$$ = Array1;
315 			delete Array2;
316 			}
317 	|	ArrayExpression '*' ArrayExpression {
318 			xdmf2::XdmfArray *Array1 = ( xdmf2::XdmfArray *)$1;
319 			xdmf2::XdmfArray *Array2 = ( xdmf2::XdmfArray *)$3;
320 
321 			/* printf("Array 0x%X * 0x%X\n", Array1, Array2); */
322 			*Array1 *= *Array2;
323 			$$ = Array1;
324 			delete Array2;
325 			/* printf("Array1 Nelms = %d\n", Array1->GetNumberOfElements()); */
326 			}
327 	|	ArrayExpression '/' ArrayExpression {
328 			xdmf2::XdmfArray *Array1 = ( xdmf2::XdmfArray *)$1;
329 			xdmf2::XdmfArray *Array2 = ( xdmf2::XdmfArray *)$3;
330 
331 			/* printf("Array 0x%X + 0x%X\n", Array1, Array2); */
332 			*Array1 /= *Array2;
333 			$$ = Array1;
334 			delete Array2;
335 			}
336 	|	ArrayExpression '+' ScalarExpression {
337 			xdmf2::XdmfArray *Array1 = ( xdmf2::XdmfArray *)$1;
338 			xdmf2::XdmfArray *Result;
339 
340 			/* printf("Array + %g\n", $3); */
341 			Result  = Array1;
342 			*Result += $3;
343 			$$ = Result;
344 			}
345 	|	ArrayExpression '-' ScalarExpression {
346 			xdmf2::XdmfArray *Array1 = ( xdmf2::XdmfArray *)$1;
347 			xdmf2::XdmfArray *Result;
348 
349 			/* printf("Array - %g\n", $3); */
350 			Result  = Array1;
351 			*Result -= $3;
352 			$$ = Result;
353 			}
354 	|	ArrayExpression '*' ScalarExpression {
355 			xdmf2::XdmfArray *Array1 = ( xdmf2::XdmfArray *)$1;
356 			xdmf2::XdmfArray *Result;
357 
358 			/* printf("Array * %g\n", $3); */
359 			Result  = Array1;
360 			*Result *= $3;
361 			$$ = Result;
362 			}
363 	|	ArrayExpression '/' ScalarExpression {
364 			xdmf2::XdmfArray *Array1 = ( xdmf2::XdmfArray *)$1;
365 			xdmf2::XdmfArray *Result;
366 
367 			/* printf("Array / %g\n", $3); */
368 			Result  = Array1;
369 			*Result /= $3;
370 			$$ = Result;
371 			}
372 	|	ScalarExpression '+' ArrayExpression {
373 			xdmf2::XdmfArray *Array1 = ( xdmf2::XdmfArray *)$3;
374 			xdmf2::XdmfArray *Result;
375 
376 			/* printf("Array + %g\n", $1); */
377 			Result  = Array1;
378 			*Result += $1;
379 			$$ = Result;
380 			}
381 	|	ScalarExpression '-' ArrayExpression {
382 			xdmf2::XdmfArray *Array1 = ( xdmf2::XdmfArray *)$3;
383 			xdmf2::XdmfArray *Result;
384 
385 			/* printf("Array - %g\n", $1); */
386 			Result  = Array1;
387 			*Result -= $1;
388 			$$ = Result;
389 			}
390 	|	ScalarExpression '*' ArrayExpression {
391 			xdmf2::XdmfArray *Array1 = ( xdmf2::XdmfArray *)$3;
392 			xdmf2::XdmfArray *Result;
393 
394 			/* printf("Array * %g\n", $1); */
395 			Result  = Array1;
396 			*Result *= $1;
397 			$$ = Result;
398 			}
399 	|	ScalarExpression '/' ArrayExpression {
400 			xdmf2::XdmfArray *Array1 = ( xdmf2::XdmfArray *)$3;
401 			xdmf2::XdmfArray *Result;
402 
403 			/* printf("Array / %g\n", $1); */
404 			Result  = Array1;
405 			*Result /= $1;
406 			$$ = Result;
407 			}
408 	|	tokARRAY '[' ArrayExpression ']' {
409 			xdmf2::XdmfArray	*Array1 = ( xdmf2::XdmfArray *)$1;
410 			xdmf2::XdmfArray	*Array2 = ( xdmf2::XdmfArray *)$3;
411 			xdmf2::XdmfArray	*Result;
412 
413 			/* printf("ArrayExpression From Indexes\n"); */
414 			Result = Array1->Clone( Array2 );
415 			delete Array2;
416 			$$ = Result;
417 		}
418 	|	tokARRAY '[' tokINTEGER ':' tokINTEGER ']' {
419 			xdmf2::XdmfArray *Array1 = ( xdmf2::XdmfArray *)$1;
420 			xdmf2::XdmfArray *Range, *Result;
421 
422 			/* printf("ArrayExpression From Array Range %d:%d\n", $3, $5); */
423 			Range = Array1->Reference( $3, $5 ); /* This not a copy  */
424 
425 			Result  = Range->Clone(); /* So Copy It */
426             /* This is a passed in array ... don't delete
427 			delete Array1; */
428 			$$ = Result;
429 			}
430 	|	INDEX'(' ArrayExpression EQEQ ArrayExpression ')' {
431 			xdmf2::XdmfArray	*Array1 = ( xdmf2::XdmfArray *)$3;
432 			xdmf2::XdmfArray	*Array2 = ( xdmf2::XdmfArray *)$5;
433 			XdmfLength	i, howmany = 0, cntr = 0;
434 			XdmfLength	Length1 = Array1->GetNumberOfElements(), Length2;
435 			XdmfInt64Array	*Index = new XdmfInt64Array( Length1 );
436 			XdmfInt64	A1Value, A2Value;
437 			XdmfInt64	*A1Values, *A2Values;
438 			float		Percent;
439 
440 			if(Array1->GetNumberType() != XDMF_INT64_TYPE){
441 				yyerror("INDEX operator only uses XdmfInt64 Arrays");
442 				return( 0 );
443 				}
444 			if(Array2->GetNumberType() != XDMF_INT64_TYPE){
445 				yyerror("INDEX operator only uses XdmfInt64 Arrays");
446 				return( 0 );
447 				}
448 			Length2 = Array2->GetNumberOfElements();
449 			A1Values = (XdmfInt64 *)Array1->GetDataPointer();
450 			A2Values = (XdmfInt64 *)Array2->GetDataPointer();
451 			for( i = 0 ; i < Length1 ; i++ ){
452 				/* A1Value = Array1->GetValueAsFloat64( i ); */
453 				A1Value = *A1Values++;
454 				cntr = 0;
455 				A2Value = A1Value + 1;
456 				while((cntr < Length2) && (A2Value != A1Value)) {
457 					/* A2Value = Array2->GetValueAsFloat64(cntr); */
458 					A2Value = A2Values[cntr];
459 					cntr++;
460 					}
461 				howmany++;
462 				if(howmany > 5000){
463 					Percent = 100.0 * i / Length1;
464 					printf("%5.2f %% Done\n", Percent);
465 					howmany = 0;
466 					}
467 				if( A1Value == A2Value ) {
468 					Index->SetValue( i, cntr - 1 );
469 				}else{
470 					Index->SetValue( i, -1);
471 					}
472 				}
473 			$$ = ( xdmf2::XdmfArray *)Index;
474 			}
475 	|	WHERE '(' ArrayExpression EQEQ ArrayExpression ')' {
476 			xdmf2::XdmfArray	*Array1 = ( xdmf2::XdmfArray *)$3;
477 			/* XdmfLength	howmany = 0; */
478 			XdmfLength	i, cntr = 0;
479 			XdmfLength	Length1 = Array1->GetNumberOfElements(), Length2;
480 			XdmfInt64Array	*Index = new XdmfInt64Array( Length1 );
481 			xdmf2::XdmfArray	*Array2 = ( xdmf2::XdmfArray *)$5;
482 			XdmfFloat64	A1Value, A2Value;
483 
484 			Length2 = Array2->GetNumberOfElements();
485 			for( i = 0 ; i < Length1 ; i++ ){
486 				A1Value = Array1->GetValueAsFloat64( i );
487 				cntr = 0;
488 				A2Value = A1Value + 1;
489 				while((cntr < Length2) && (A2Value != A1Value)) {
490 					A2Value = Array2->GetValueAsFloat64(cntr);
491 					cntr++;
492 					}
493 /*
494 				howmany++;
495 				if(howmany > 1000){
496 					cout << "Checked " << i << " of " << Length1 << endl;
497 					howmany = 0;
498 					}
499 */
500 				if( A1Value == A2Value ) {
501 					Index->SetValue( i, cntr - 1 );
502 				}else{
503 					Index->SetValue( i, -1);
504 					}
505 				}
506 			$$ = ( xdmf2::XdmfArray *)Index;
507 			}
508 	|	WHERE '(' ArrayExpression EQEQ ScalarExpression ')' {
509 			xdmf2::XdmfArray	*Array1 = ( xdmf2::XdmfArray *)$3;
510 			XdmfLength	i, cntr = 0;
511 			XdmfLength	Length = Array1->GetNumberOfElements();
512 			XdmfInt64Array	*Index = new XdmfInt64Array( Length );
513 			XdmfFloat64	Value, SValue = $5;
514 
515 			for( i = 0 ; i < Length ; i++ ){
516 				Value = Array1->GetValueAsFloat64( i );
517 				if( Value == SValue ) {
518 					Index->SetValue( cntr++, i );
519 					}
520 				}
521 			/* printf("Found %d Wheres\n", cntr ); */
522 			if( cntr == 0 ){
523 				yyerror("WHERE Function Length == 0");
524 				return( 0 );
525 				}
526 			Index->SetNumberOfElements( cntr );
527 			$$ = ( xdmf2::XdmfArray *)Index;
528 			}
529 	|	WHERE '(' ArrayExpression LT ScalarExpression ')' {
530 			xdmf2::XdmfArray	*Array1 = ( xdmf2::XdmfArray *)$3;
531 			XdmfLength	i, cntr = 0;
532 			XdmfLength	Length = Array1->GetNumberOfElements();
533 			XdmfInt64Array	*Index = new XdmfInt64Array( Length );
534 			XdmfFloat64	Value, SValue = $5;
535 
536 			for( i = 0 ; i < Length ; i++ ){
537 				Value = Array1->GetValueAsFloat64( i );
538 				if( Value < SValue ) {
539 					Index->SetValue( cntr++, i );
540 					}
541 				}
542 			/* printf("Found %d Wheres\n", cntr ); */
543 			if( cntr == 0 ){
544 				yyerror("WHERE Function Length == 0");
545 				return( 0 );
546 				}
547 			Index->SetNumberOfElements( cntr );
548 			$$ = ( xdmf2::XdmfArray *)Index;
549 			}
550 	|	WHERE '(' ArrayExpression LE ScalarExpression ')' {
551 			xdmf2::XdmfArray	*Array1 = ( xdmf2::XdmfArray *)$3;
552 			XdmfLength	i, cntr = 0;
553 			XdmfLength	Length = Array1->GetNumberOfElements();
554 			XdmfInt64Array	*Index = new XdmfInt64Array( Length );
555 			XdmfFloat64	Value, SValue = $5;
556 
557 			for( i = 0 ; i < Length ; i++ ){
558 				Value = Array1->GetValueAsFloat64( i );
559 				if( Value <= SValue ) {
560 					Index->SetValue( cntr++, i );
561 					}
562 				}
563 			/* printf("Found %d Wheres\n", cntr ); */
564 			if( cntr == 0 ){
565 				yyerror("WHERE Function Length == 0");
566 				return( 0 );
567 				}
568 			Index->SetNumberOfElements( cntr );
569 			$$ = ( xdmf2::XdmfArray *)Index;
570 			}
571 	|	WHERE '(' ArrayExpression GT ScalarExpression ')' {
572 			xdmf2::XdmfArray	*Array1 = ( xdmf2::XdmfArray *)$3;
573 			XdmfLength	i, cntr = 0;
574 			XdmfLength	Length = Array1->GetNumberOfElements();
575 			XdmfInt64Array	*Index = new XdmfInt64Array( Length );
576 			XdmfFloat64	Value, SValue = $5;
577 
578 			for( i = 0 ; i < Length ; i++ ){
579 				Value = Array1->GetValueAsFloat64( i );
580 				if( Value > SValue ) {
581 					Index->SetValue( cntr++, i );
582 					}
583 				}
584 			/* printf("Found %d Wheres\n", cntr ); */
585 			if( cntr == 0 ){
586 				yyerror("WHERE Function Length == 0");
587 				return( 0 );
588 				}
589 			Index->SetNumberOfElements( cntr );
590 			$$ = ( xdmf2::XdmfArray *)Index;
591 			}
592 	|	WHERE '(' ArrayExpression GE ScalarExpression ')' {
593 			xdmf2::XdmfArray	*Array1 = ( xdmf2::XdmfArray *)$3;
594 			XdmfLength	i, cntr = 0;
595 			XdmfLength	Length = Array1->GetNumberOfElements();
596 			XdmfInt64Array	*Index = new XdmfInt64Array( Length );
597 			XdmfFloat64	Value, SValue = $5;
598 
599 			for( i = 0 ; i < Length ; i++ ){
600 				Value = Array1->GetValueAsFloat64( i );
601 				if( Value >= SValue ) {
602 					Index->SetValue( cntr++, i );
603 					}
604 				}
605 			/* printf("Found %d Wheres\n", cntr ); */
606 			if( cntr == 0 ){
607 				yyerror("WHERE Function Length == 0");
608 				return( 0 );
609 				}
610 			Index->SetNumberOfElements( cntr );
611 			$$ = ( xdmf2::XdmfArray *)Index;
612 			}
613 	|	WHERE '(' ArrayExpression NE ScalarExpression ')' {
614 			xdmf2::XdmfArray	*Array1 = ( xdmf2::XdmfArray *)$3;
615 			XdmfLength	i, cntr = 0;
616 			XdmfLength	Length = Array1->GetNumberOfElements();
617 			XdmfInt64Array	*Index = new XdmfInt64Array( Length );
618 			XdmfFloat64	Value, SValue = $5;
619 
620 			for( i = 0 ; i < Length ; i++ ){
621 				Value = Array1->GetValueAsFloat64( i );
622 				if( Value != SValue ) {
623 					Index->SetValue( cntr++, i );
624 					}
625 				}
626 			/* printf("Found %d Wheres\n", cntr ); */
627 			if( cntr == 0 ){
628 				yyerror("WHERE Function Length == 0");
629 				return( 0 );
630 				}
631 			Index->SetNumberOfElements( cntr );
632 			$$ = ( xdmf2::XdmfArray *)Index;
633 			}
634 	|	NAME '(' ArrayExpression ')' {
635 
636 			if( $1->DoubleFunctionPtr == NULL ){
637 				/* printf("Bad Function Ptr for %s\n", $1->Name ); */
638 				$$ = $3;
639 			} else {
640 				xdmf2::XdmfArray *Array1 = ( xdmf2::XdmfArray *)$3;
641 				XdmfFloat64	Value;
642 				XdmfLength	i, Length = Array1->GetNumberOfElements();
643 
644 				/* printf("Function Call %s\n", $1->Name ); */
645 				for( i = 0 ; i < Length ; i++ ){
646 					Value = Array1->GetValueAsFloat64( i );
647 					Array1->SetValueFromFloat64( i, ($1->DoubleFunctionPtr)( Value ) );
648 					}
649 				$$ = Array1;
650 			}
651 			}
652 	|	'(' ArrayExpression ')'	{
653 			/* printf("( ArrayExpression )\n"); */
654 			$$ = $2;
655 			}
656 	|	JOIN '(' ArrayExpression ')'	{
657 			/* printf("( ArrayExpression )\n"); */
658 			$$ = $3;
659 			}
660 	|	tokARRAY {
661 			xdmf2::XdmfArray *Array1 = ( xdmf2::XdmfArray *)$1;
662 			xdmf2::XdmfArray *Result;
663 
664 			/* printf("ArrayExpression From Array\n"); */
665 
666 			if ( Array1 == NULL ){
667 				/* Bomb */
668 				yyerror("NULL Array Pointer");
669 				return( 0 );
670 			} else {
671 				Result  = Array1->Clone();
672 				$$ = Result;
673 				}
674 			}
675 	;
676 
677 ScalarExpression: 	ScalarExpression '+' ScalarExpression {
678 			/* printf("Scalar +\n"); */
679 			$$ = $1 + $3;
680 			}
681 	|	ScalarExpression '-' ScalarExpression {
682 			/* printf("Scalar -\n"); */
683 			$$ = $1 - $3;
684 			}
685 	|	ScalarExpression '*' ScalarExpression {
686 			/* printf("Scalar *\n"); */
687 			$$ = $1 * $3;
688 			}
689 	|	ScalarExpression '/' ScalarExpression {
690 			/* printf("Scalar /\n"); */
691 			$$ = $1 / $3;
692 			}
693 	|	NAME '(' ScalarExpression ')' {
694 			if( $1->DoubleFunctionPtr == NULL ){
695 				/* printf("Bad Function Ptr for %s\n", $1->Name ); */
696 				$$ = 0.0;
697 			} else {
698 				$$ = ($1->DoubleFunctionPtr)( $3 );
699 			}
700 			}
701 	|	'(' ScalarExpression ')' {
702 			/* printf ("( ScalarExpression )\n"); */
703 			$$ = $2;
704 			}
705 	|	tokINTEGER {
706 			/* printf ("ScalarExpression from tokINTEGER\n"); */
707 			$$ = $1;
708 			}
709 	|	lFLOAT {
710 			/* printf ("ScalarExpression from FLOAT\n"); */
711 			$$ = $1;
712 			}
713 	;
714 
715 
716 %%
717 
718 /* extern	FILE	*yyin, *yyout; */
719 
720 #ifdef __cplusplus
721 /**/
722 extern "C" {
723 /**/
724 #endif
725 
726 char	InputBuffer[ 512 ];
727 size_t InputBufferPtr = 0, InputBufferEnd = 0;
728 char	OutputBuffer[ 512 ];
729 size_t OutputBufferPtr = 0;
730 /* static int OutputBufferEnd = 511; */
731 
732 int
dice_yywrap(void)733 dice_yywrap( void ) {
734 
735 return 1;
736 }
737 
738 void
dice_yyerror(const char * string)739 dice_yyerror( const char *string ) {
740 fprintf(stderr, "XdmfExpr : %s \n", string);
741 }
742 
743 int
XdmfExprFlexInput(char * buf,int maxlen)744 XdmfExprFlexInput( char *buf, int maxlen ) {
745 (void)maxlen;
746 if ( InputBufferPtr < InputBufferEnd ){
747 	buf[0] = InputBuffer[ InputBufferPtr++ ];
748 	return(1);
749 } else {
750 	buf[0] = '\n';
751 	return( 0 );
752 	}
753 }
754 
755 int
XdmfExprInput(void)756 XdmfExprInput( void ){
757 
758 if ( InputBufferPtr < InputBufferEnd ){
759 	return( InputBuffer[ InputBufferPtr++ ] );
760 } else {
761 	return '\n';
762 	}
763 }
764 
765 void
XdmfExprUnput(int c)766 XdmfExprUnput( int c ) {
767 if( InputBufferPtr > 0 ){
768 	InputBufferPtr--;
769 	InputBuffer[ InputBufferPtr ] = c;
770 	}
771 }
772 
773 void
XdmfExprOutput(int c)774 XdmfExprOutput( int c ) {
775 	/* printf("XdmfExprOutput Called\n"); */
776 	OutputBuffer[ OutputBufferPtr++ ] = c;
777 	OutputBuffer[ OutputBufferPtr ] = '\0';
778 	}
779 
780 XdmfExprSymbol
XdmfExprSymbolLookup(const char * Name)781 *XdmfExprSymbolLookup( const char *Name ){
782 
783 XdmfExprSymbol	*Last = NULL, *Item = XdmfExprItemsTable;
784 
785 if( Name == NULL ) {
786 	/* Table Check  */
787 	return( XdmfExprItemsTable );
788 	}
789 
790 while( Item != NULL ) {
791 	if( strcmp( Item->Name, Name ) == 0 ) {
792 		/* printf("Found Symbol %s\n", Name ); */
793 		return( Item );
794 		}
795 	Last = Item;
796 	Item = Item->Next;
797 }
798 /* Not Found : Create New One */
799 Item = ( XdmfExprSymbol *)calloc( 1, sizeof( XdmfExprSymbol ));
800 Item->Next = NULL;
801 Item->Name = strdup( Name );
802 Item->ClientData = NULL;
803 Item->DoubleValue = 0;
804 Item->DoubleFunctionPtr = NULL;
805 if( XdmfExprItemsTable == NULL ) {
806 	XdmfExprItemsTable = Item;
807 	}
808 if( Last != NULL ){
809 	Last->Next = Item;
810 	}
811 /* printf("New Symbol for %s\n", Name ); */
812 return( Item );
813 }
814 
815 #ifdef __cplusplus
816 /**/
817 }
818 /**/
819 #endif
820 
821 xdmf2::XdmfArray *
XdmfExprParse(char * string)822 XdmfExprParse( char *string ){
823 
824 XdmfExprSymbol	*Item;
825 XdmfLength	CurrentTime;
826 XdmfLength	TimeOfCreation;
827 xdmf2::XdmfArray	*ap;
828 
829 /* Build the Symbol Table if Necessary */
830 Item = XdmfExprSymbolLookup( NULL );
831 if( Item == NULL ){
832 	/* printf("Creating Symbol Table\n"); */
833 	Item = XdmfExprSymbolLookup( "cos" );
834 	Item->DoubleFunctionPtr = cos;
835 	Item = XdmfExprSymbolLookup( "sin" );
836 	Item->DoubleFunctionPtr = sin;
837 	Item = XdmfExprSymbolLookup( "exp" );
838 	Item->DoubleFunctionPtr = exp;
839 	Item = XdmfExprSymbolLookup( "tan" );
840 	Item->DoubleFunctionPtr = tan;
841 	Item = XdmfExprSymbolLookup( "acos" );
842 	Item->DoubleFunctionPtr = acos;
843 	Item = XdmfExprSymbolLookup( "asin" );
844 	Item->DoubleFunctionPtr = asin;
845 	Item = XdmfExprSymbolLookup( "atan" );
846 	Item->DoubleFunctionPtr = atan;
847 	Item = XdmfExprSymbolLookup( "log" );
848 	Item->DoubleFunctionPtr = log;
849 	Item = XdmfExprSymbolLookup( "sqrt" );
850 	Item->DoubleFunctionPtr = sqrt;
851 	}
852 /* Print Symbol Table */
853 Item = XdmfExprSymbolLookup( NULL );
854 while( Item != NULL ) {
855 	if( Item->ClientData != NULL ){
856 		/* printf("Found Symbol %s\n", Item->Name ); */
857 		}
858 	Item = Item->Next;
859 	}
860 strcpy( InputBuffer, string );
861 InputBufferEnd = strlen( InputBuffer );
862 InputBufferPtr = OutputBufferPtr = 0;
863 XdmfExprReturnValue = NULL;
864 /* printf("XdmfExprParse Scanning <%s>\n", InputBuffer); */
865 CurrentTime = GetCurrentArrayTime();
866 if ( yyparse() != 0 ){
867 	/* Error */
868 	XdmfExprReturnValue = NULL;
869 	}
870 Item = XdmfExprSymbolLookup( NULL );
871 while( Item != NULL ) {
872   XdmfExprSymbol *next = Item->Next;
873   if ( Item->Name )
874     {
875     free(Item->Name);
876     }
877   free(Item);
878 	Item = next;
879 	}
880 
881 XdmfExprItemsTable = NULL;
882 
883 /* Remove All Arrays Older than when we started */
884 /* printf("Cleaning up Temparary Arrays\n"); */
885 while( ( ap = GetNextOlderArray( CurrentTime, &TimeOfCreation ) ) != NULL ){
886 	/* Don't remove the return value */
887 	if( ap != XdmfExprReturnValue ){
888 		/* printf("Removing Temporary Array\n"); */
889 		delete ap;
890 		}
891 	CurrentTime = TimeOfCreation;
892 	}
893 return( XdmfExprReturnValue );
894 }
895 
896