summaryrefslogtreecommitdiff
path: root/src/piraha/Call.cc
blob: 78dbec32f125f2103024eabec6ca1410b9d26822 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
#include <iostream>
#include <iomanip>
#include <string.h>
#include "Piraha.hpp"
#include <stdlib.h>
#include <sstream>
#include "cctk_core.h"
#include "cctk_Parameter.h"
#include <map>
#include <math.h>
#include <algorithm>
#include <limits>

namespace piraha {

#define VAR(X) " " #X "=" << X

extern "C" int CCTK_ParameterFilename(int len, char *filename);

/**
 * This holds the structure required for parsing
 * a Cactus par file.
 */
static smart_ptr<Grammar> par_file_grammar = new Grammar();

static std::string mklower(std::string& in) {
	std::string s = in;
	std::transform(s.begin(), s.end(), s.begin(), ::tolower);
	return s;
}

enum ValueType { PIR_STRING,PIR_INT,PIR_REAL,PIR_BOOL,PIR_VOID };

std::string get_parfile() {
    char path[500];
    CCTK_ParameterFilename(500, path);
    char *value = strrchr (path, '/');
    if (value == NULL) {
        value = path;
    } else {
        value++;
    }
    int n = strlen(value);
    if(n > 4 && strcmp(value+n-4,".par")==0) {
    	value[n-4] = '\0';
    }
    std::string s = value;
    return s;
}

std::ostream& operator<<(std::ostream& o,const ValueType& vt) {
	if(vt == PIR_STRING)
		o << "STRING";
	else if(vt == PIR_INT)
		o << "INT";
	else if(vt == PIR_REAL)
		o << "REAL";
	else if(vt == PIR_BOOL)
		o << "BOOL";
	else if(vt == PIR_VOID)
		o << "VOID";
	else
		o << "UNDEF";
	return o;
}

std::string current_thorn;

/**
 * The Value class is something like a union. It holds
 * integer, real, and string data and has a type field
 * that identifies which field is currently valid.
 */
struct Value {
	/** This field holds the parse tree element associated with this Value. */
	smart_ptr<Group> hold;
	double ddata;
	int idata;
	std::string sdata;
	ValueType type;
	Value(smart_ptr<Group> g) : hold(g), ddata(0), idata(0), sdata(), type(PIR_VOID) {}
	~Value() {}
	/**
	 * Create a string representation of the Value.
	 */
	std::string copy() {
		assert(type != PIR_VOID);
		if(type == PIR_STRING) {
			return sdata;
		} else if(type == PIR_BOOL) {
			std::string s = idata ? "yes" : "no";
			return s;
		} else {
			std::ostringstream o;
			if(type == PIR_REAL) {
				o << std::setprecision(15) << ddata;
			} else {
				o << idata;
			}
			return o.str();
		}
	}
	/**
	 * Check to see if something is a bool and throw an error
	 * if it's not.
	 */
	void checkBool() {
		if(type != PIR_BOOL) {
			std::ostringstream msg;
			msg << "Does not evaluate to a boolean: " << hold->substring();
			std::string par = get_parfile();
			CCTK_Error(hold->line(),par.c_str(),current_thorn.c_str(),msg.str().c_str());
		}
	}
	/**
	 * Compares with another Value, and throws a CCTK_Error
	 * if the comparison doesn't make sense.
	 */
	bool equals(smart_ptr<Value> v) {
		if(type == PIR_BOOL && v->type == PIR_BOOL) {
			return idata == v->idata;
		} else if(type == PIR_INT && v->type == PIR_INT) {
			return idata == v->idata;
		} else if(type == PIR_INT && v->type == PIR_REAL) {
			return idata == v->ddata;
		} else if(type == PIR_REAL && v->type == PIR_INT) {
			return ddata == v->idata;
		} else if(type == PIR_REAL && v->type == PIR_REAL) {
			return ddata == v->ddata;
		} else if(type == PIR_STRING && v->type == PIR_STRING) {
			return sdata == v->sdata;
		}
		std::ostringstream msg;
		msg << "Cannot compare " << type << " and " << v->type << std::endl;
		std::string par = get_parfile();
		CCTK_Error(hold->line(),par.c_str(),current_thorn.c_str(),msg.str().c_str());
	}
	/**
	 * Return a double value, whether the underlying
	 * quantity is integer or real.
	 */
	double doubleValue() {
		if(type == PIR_REAL)
			return ddata;
		else if(type == PIR_INT)
			return idata;
		std::ostringstream msg;
		msg << "Cannot convert " << type << " to double." << std::endl;
		std::string par = get_parfile();
		CCTK_Error(hold->line(),par.c_str(),current_thorn.c_str(),msg.str().c_str());
		return 0;
	}
	bool intOrDouble() {
		return type == PIR_INT || type == PIR_REAL;
	}
	/**
	 * This function converts a double to a real, but
	 * only if this can be done without loss of precision.
	 */
	void integerize() {
		if(type == PIR_REAL) {
			idata = ddata;
			if(idata == ddata) {
				type = PIR_INT;
			}
		}
	}
	void booleanize(smart_ptr<Group> gr) {
		if(type == PIR_STRING) {
			std::string s = mklower(sdata);
			if(s == "yes" || s == "true") {
				idata = 1;
				type = PIR_BOOL;
			} else if(s == "no" || s == "false") {
				idata = 0;
				type = PIR_BOOL;
			}
		} else if(type == PIR_INT) {
			/// Steven R. Brandt would like to remove this
			/// particular auto-conversion
			if(idata == 1 || idata == 0) {
				type = PIR_BOOL;
				std::ostringstream msg;
				msg << "Boolean variable is set with integer: " << gr->substring() << std::endl;
				std::string par = get_parfile();
				CCTK_Warn(1,gr->line(),par.c_str(),current_thorn.c_str(),msg.str().c_str());
			}
		}
	}
};

std::ostream& operator<<(std::ostream& o,const smart_ptr<Value>& val) {
	if(!val.valid()) {
		return o << "NULL";
	}
	o << "(" << val->type << ")";
	if(val->type == PIR_REAL) {
		o << val->ddata;
	} else if(val->type == PIR_INT) {
		o << val->idata;
	} else if(val->type == PIR_BOOL) {
		o << (val->idata ? "true" : "false");
	} else if(val->type == PIR_STRING) {
		o << "\"" << val->sdata << "\"";
	} else {
		o << val->hold->substring();
	}
	return o;
}

std::map<std::string,std::map<std::string,smart_ptr<Value> > > values;
typedef std::map<std::string,std::map<std::string,smart_ptr<Value> > >::iterator th_iter;
typedef std::map<std::string,smart_ptr<Value> >::iterator nm_iter;

smart_ptr<Value> eval_expr(std::string inp);

// If the value was already defined in this parameter file, look
// it up in the map. Otherwise, get it from Cactus.
smart_ptr<Value> find_val(std::string thorn,std::string name) {
	smart_ptr<Value> ret;
	th_iter th = values.find(thorn);
	if(th != values.end()) {
		nm_iter nm = th->second.find(name);
		if(nm != th->second.end()) {
			ret = nm->second;
			return ret;
		}
	}
	const cParamData *data = CCTK_ParameterData(name.c_str(),thorn.c_str());
	if(data != NULL) {
		std::string expr = data->defval;
		ret = eval_expr(expr);
	}
	return ret;
}

smart_ptr<Value> lookup_var(smart_ptr<Group> gr) {
	smart_ptr<Value> ret;
	if(gr->group(0)->getPatternName() == "env") {
		const char *env = getenv(gr->group(0)->group(0)->substring().c_str());
		if(env != NULL) {
			ret = new Value(gr);
			ret->type = PIR_STRING;
			ret->sdata = env;
		}
	} else if(gr->group(0)->substring() == "parfile") {
		ret = new Value(gr);
		ret->type = PIR_STRING;
		ret->sdata = get_parfile();
	} else if(gr->group(0)->substring() == "pi") {
		ret = new Value(gr);
		ret->type = PIR_REAL;
		ret->ddata = 4.0*atan2(1.0,1.0);
	}
	if(!ret.valid()) {
		std::ostringstream msg;
		msg << "Unknown variable: " << gr->substring() << std::endl;
		std::string par = get_parfile();
		CCTK_Error(gr->line(),par.c_str(),current_thorn.c_str(),msg.str().c_str());
	}
	return ret;
}


std::string string_reparser(std::string s) {
	smart_ptr<Matcher> m = new Matcher(par_file_grammar,"stringparser",s.c_str(),s.length());
	if(m->matches()) {
		std::string out = "";
		for(int i=0;i < m->groupCount(); i++) {
			std::string pn = m->group(i)->getPatternName();
			if(pn == "any" || pn == "name") {
				out += m->group(i)->substring();
			} else if(pn == "stringcomment") {
				;
			} else {
				smart_ptr<Value> val = lookup_var(m->group(i));
				out += val->copy();
			}
		}
		return out;
	} else {
		return s;
	}
}

/**
 * The meval() function takes any node within
 * the parse tree and creates a Value object
 * from it. It is, therefore, designed to be
 * used recursively.
 **/
smart_ptr<Value> meval(smart_ptr<Group> gr) {
	assert(gr.valid());
	std::string pn = gr->getPatternName();
	smart_ptr<Value> ret = new Value(gr);
	if(pn == "num") {
		std::string s = gr->substring();
		s = mklower(s);
		if(s == "nan") {
			ret->ddata = NAN;
			ret->type = PIR_REAL;
		} else if(s == "inf") {
			ret->ddata = INFINITY;
			ret->type = PIR_REAL;
		} else {
			ret->ddata = atof(s.c_str());
			ret->idata = ret->ddata;
			if(ret->idata == ret->ddata && (s.find('.') == std::string::npos))
				ret->type = PIR_INT;
			else
				ret->type = PIR_REAL;
		}
	} else if(pn == "paren" || pn == "parindex") {
		return meval(gr->group(0));
	} else if(pn == "func") {
		std::string fn = gr->group(0)->substring();
		fn = mklower(fn);
		smart_ptr<Value> val = meval(gr->group(1));
		if(val->type == PIR_REAL || val->type == PIR_INT) {
			if(fn == "trunc") {
				val->ddata = trunc(val->doubleValue());
				val->type = PIR_REAL;
				return val;
			} else if(fn == "floor") {
				val->ddata = floor(val->doubleValue());
				val->type = PIR_REAL;
				return val;
			} else if(fn == "ceil") {
				val->ddata = ceil(val->doubleValue());
				val->type = PIR_REAL;
				return val;
			} else if(fn == "sqrt") {
				val->ddata = sqrt(val->doubleValue());
				val->type = PIR_REAL;
				return val;
			} else if(fn == "atan") {
				val->ddata = atan(val->doubleValue());
				val->type = PIR_REAL;
				return val;
			} else if(fn == "sin") {
				val->ddata = sin(val->doubleValue());
				val->type = PIR_REAL;
				return val;
			} else if(fn == "cos") {
				val->ddata = cos(val->doubleValue());
				val->type = PIR_REAL;
				return val;
			} else if(fn == "tan") {
				val->ddata = tan(val->doubleValue());
				val->type = PIR_REAL;
				return val;
			} else if(fn == "exp") {
				val->ddata = exp(val->doubleValue());
				val->type = PIR_REAL;
				return val;
			} else if(fn == "log") {
				val->ddata = log(val->doubleValue());
				val->type = PIR_REAL;
				return val;
			} else if(fn == "abs") {
				val->ddata = fabs(val->doubleValue());
				val->type = PIR_REAL;
				return val;
			} else if(fn == "acos") {
				val->ddata = acos(val->doubleValue());
				val->type = PIR_REAL;
				return val;
			} else if(fn == "asin") {
				val->ddata = asin(val->doubleValue());
				val->type = PIR_REAL;
				return val;
			} else if(fn == "bool") {
				if(val->type == PIR_REAL) {
					val->idata = val->ddata;
				}
				val->type = PIR_BOOL;
				return val;
			} else if(fn == "int") {
				if(val->type == PIR_REAL) {
					val->idata = val->ddata;
					val->type = PIR_INT;
				}
				return val;
			} else if(fn == "real") {
				if(val->type == PIR_INT) {
					val->ddata = val->idata;
					val->type = PIR_REAL;
				}
				return val;
			}
		} else if(val->type == PIR_BOOL) {
			if(fn == "int") {
				val->type = PIR_INT;
				return val;
			} else if(fn == "real") {
				val->type = PIR_REAL;
				val->ddata = val->idata;
				return val;
			}
		}
		std::ostringstream msg;
		msg << "Unknown func: " << fn << "(" << val->type << ")" << std::endl;
		std::string par = get_parfile();
		CCTK_Error(gr->line(),par.c_str(),current_thorn.c_str(),msg.str().c_str());
	} else if(pn == "name"||pn == "dname") {
		std::string s = gr->substring();
		s = mklower(s);
		if(s == "no" || s == "false") {
			ret->type = PIR_BOOL;
			ret->idata = 0;
		} else if(s == "yes" || s == "true") {
			ret->type = PIR_BOOL;
			ret->idata = 1;
		} else {
			ret->type = PIR_STRING;
			ret->sdata = gr->substring();
		}
		return ret;
	} else if(pn == "par") {
		std::string thorn = gr->group(0)->substring();
		std::string name = gr->group(1)->substring();
		if(gr->groupCount() == 3) {
			std::ostringstream vn;
			smart_ptr<Value> index = meval(gr->group(2));
			if(index->type == PIR_INT) {
				std::stringstream o;
				o << name << "[" << index->idata << "]";
				o << std::flush;
				std::string keyi = o.str();
				ret = find_val(thorn,keyi);
				return ret;
			}
		} else {
			ret = find_val(thorn,name);
			return ret;
		}
		std::ostringstream msg;
		msg << "Unknown par: " << thorn << "::" << name << std::endl;
		std::string par = get_parfile();
		CCTK_Error(gr->line(),par.c_str(),current_thorn.c_str(),msg.str().c_str());
	} else if(pn == "var") {
		ret = lookup_var(gr);
	} else if(pn == "value") {
		if(gr->groupCount()==2) {
			std::string unop = gr->group(0)->substring();
			ret = meval(gr->group(1));
			if(unop == "-") {
				if(ret->type == PIR_INT) {
					ret->idata = -ret->idata;
				} else if(ret->type == PIR_REAL) {
					ret->ddata = -ret->ddata;
				} else {
					std::ostringstream msg;
					msg << "Unknown operation: " << unop << ret->type << std::endl;
					std::string par = get_parfile();
					CCTK_Error(gr->line(),par.c_str(),current_thorn.c_str(),msg.str().c_str());
				}
			} else if(unop == "!") {
				if(ret->type == PIR_BOOL) {
					ret->idata = !ret->idata;
				} else {
					std::ostringstream msg;
					msg << "Unknown operation: " << unop << ret->type << std::endl;
					std::string par = get_parfile();
					CCTK_Error(gr->line(),par.c_str(),current_thorn.c_str(),msg.str().c_str());
				}
			} else {
				std::ostringstream msg;
				msg << "Unknown operation: " << unop << ret->type << std::endl;
				std::string par = get_parfile();
				CCTK_Error(gr->line(),par.c_str(),current_thorn.c_str(),msg.str().c_str());
			}
		} else {
			return meval(gr->group(0));
		}
	} else if(pn == "quot") {
		ret->type = PIR_STRING;
		ret->sdata = string_reparser(gr->group(0)->substring());
	} else if(pn == "inquot") {
		ret->type = PIR_STRING;
		ret->sdata = gr->substring();
	} else if(pn == "expr") {
		if(gr->groupCount()==1)
			return meval(gr->group(0));
		smart_ptr<Value> v1 = meval(gr->group(0));
		smart_ptr<Value> v2 = meval(gr->group(1));
		v1->checkBool();
		v2->checkBool();
		ret->type = PIR_BOOL;
		ret->idata = v1->idata || v2->idata;
	} else if(pn == "powexpr") {
		if(gr->groupCount()==1)
			return meval(gr->group(0));
		smart_ptr<Value> v1 = meval(gr->group(0));
		smart_ptr<Value> v2 = meval(gr->group(1));
		ret->type = PIR_REAL;
		ret->ddata = pow(v1->doubleValue(),v2->doubleValue());
	} else if(pn == "andexpr") {
		if(gr->groupCount()==1)
			return meval(gr->group(0));
		smart_ptr<Value> v1 = meval(gr->group(0));
		smart_ptr<Value> v2 = meval(gr->group(1));
		v1->checkBool();
		v2->checkBool();
		ret->type = PIR_BOOL;
		ret->idata = v1->idata && v2->idata;
	} else if(pn == "eqexpr") {
		if(gr->groupCount()==1)
			return meval(gr->group(0));
		smart_ptr<Value> v1 = meval(gr->group(0));
		std::string eqop = gr->group(1)->substring();
		smart_ptr<Value> v2 = meval(gr->group(2));
		ret->type = PIR_BOOL;
		if(eqop == "==") {
			ret->idata = v1->equals(v2);
		} else if(eqop == "!=") {
			ret->idata = !v1->equals(v2);
		} else {
			std::ostringstream msg;
			msg << "Unknown equality operator: " << eqop << std::endl;
			std::string par = get_parfile();
			CCTK_Error(gr->line(),par.c_str(),current_thorn.c_str(),msg.str().c_str());
		}
	} else if(pn == "compexpr") {
		if(gr->groupCount()==1)
			return meval(gr->group(0));
		smart_ptr<Value> v1 = meval(gr->group(0));
		if(gr->groupCount()>0) {
			std::string compop = gr->group(1)->substring();
			smart_ptr<Value> v2 = meval(gr->group(2));
			double d1 = v1->doubleValue();
			double d2 = v2->doubleValue();
			ret->type = PIR_BOOL;
			if(compop == "<") {
				ret->idata = (d1 < d2);
				return ret;
			} else if(compop == ">") {
				ret->idata = (d1 > d2);
				return ret;
			} else if(compop == "<=") {
				ret->idata = (d1 <= d2);
				return ret;
			} else if(compop == ">=") {
				ret->idata = (d1 >= d2);
				return ret;
			}
			std::ostringstream msg;
			msg << "Unknown comparison operator: " << compop << std::endl;
			std::string par = get_parfile();
			CCTK_Error(gr->line(),par.c_str(),current_thorn.c_str(),msg.str().c_str());
		}
	} else if(pn == "aexpr") {
		if(gr->groupCount()==1)
			return meval(gr->group(0));
		smart_ptr<Value> v1 = meval(gr->group(0));
		for(int i=1;i+1<gr->groupCount();i+=2) {
			std::string addop = gr->group(i)->substring();
			smart_ptr<Value> v2 = meval(gr->group(i+1));
			assert(v2.valid());
			if(v1->type == PIR_INT && v2->type == PIR_INT) {
				ret->type = PIR_INT;
				if(addop == "+") {
					ret->idata = v1->idata + v2->idata;
				} else if(addop == "-") {
					ret->idata = v1->idata - v2->idata;
				} else {
					std::ostringstream msg;
					msg << "Unknown add operator: " << addop << std::endl;
					std::string par = get_parfile();
					CCTK_Error(gr->line(),par.c_str(),current_thorn.c_str(),msg.str().c_str());
				}
			} else if(v1->type == PIR_STRING && v2->type == PIR_STRING) {
				ret->type = PIR_STRING;
				if(addop == "+") {
					ret->sdata = v1->sdata + v2->sdata;
				} else {
					std::ostringstream msg;
					msg << "Unknown add operator: " << addop << std::endl;
					std::string par = get_parfile();
					CCTK_Error(gr->line(),par.c_str(),current_thorn.c_str(),msg.str().c_str());
				}
			} else if(v1->intOrDouble() && v2->intOrDouble()) {
				ret->type = PIR_REAL;
				if(addop == "+") {
					ret->ddata = v1->doubleValue()+v2->doubleValue();
				} else if(addop == "-") {
					ret->ddata = v1->doubleValue()-v2->doubleValue();
				} else {
					std::ostringstream msg;
					msg << "Unknown add operator: " << addop << std::endl;
					std::string par = get_parfile();
					CCTK_Error(gr->line(),par.c_str(),current_thorn.c_str(),msg.str().c_str());
				}
			} else {
				std::ostringstream msg;
				msg << "Unknown operation: " << v1->type;
				msg << " " << addop << v2->type << std::endl;
				std::string par = get_parfile();
				CCTK_Error(gr->line(),par.c_str(),current_thorn.c_str(),msg.str().c_str());
			}
			v1 = ret;
		}
	} else if(pn == "mexpr") {
		if(gr->groupCount()==1)
			return meval(gr->group(0));
		smart_ptr<Value> v1 = meval(gr->group(0));
		for(int i=1;i+1<gr->groupCount();i+=2) {
			std::string mulop = gr->group(i)->substring();
			smart_ptr<Value> v2 = meval(gr->group(i+1));
			if(v1->type == PIR_INT && v2->type == PIR_INT) {
				ret->type = PIR_INT;
				if(mulop == "*") {
					ret->idata = v1->idata * v2->idata;
				} else if(mulop == "/") {
					ret->idata = v1->idata / v2->idata;
				} else if(mulop == "%") {
					ret->idata = v1->idata % v2->idata;
				} else {
					std::ostringstream msg;
					msg << "Unknown mul operator: " << v1->type << mulop << v2->type << std::endl;
					std::string par = get_parfile();
					CCTK_Error(gr->line(),par.c_str(),current_thorn.c_str(),msg.str().c_str());
				}
			} else if(v1->type == PIR_STRING && v2->type == PIR_INT) {
				ret->type = PIR_STRING;
				if(mulop == "*") {
					ret->sdata = "";
					for(int i=0;i<v2->idata;i++)
						ret->sdata += v1->sdata;
				} else {
					std::ostringstream msg;
					msg << "Unknown mul operator: " << v1->type << mulop << v2->type << std::endl;
					std::string par = get_parfile();
					CCTK_Error(gr->line(),par.c_str(),current_thorn.c_str(),msg.str().c_str());
				}
			} else if(v1->intOrDouble() && v2->intOrDouble()) {
				ret->type = PIR_REAL;
				if(mulop == "*") {
					ret->ddata = v1->doubleValue()*v2->doubleValue();
				} else if(mulop == "/") {
					ret->ddata = v1->doubleValue()/v2->doubleValue();
				} else {
					std::ostringstream msg;
					msg << "Unknown mul operator: " << v1->type << mulop << v2->type << std::endl;
					std::string par = get_parfile();
					CCTK_Error(gr->line(),par.c_str(),current_thorn.c_str(),msg.str().c_str());
				}
			} else {
				std::ostringstream msg;
				msg << "Unknown operation: " << v1->type << " " << mulop << v2->type << std::endl;
				std::string par = get_parfile();
				CCTK_Error(gr->line(),par.c_str(),current_thorn.c_str(),msg.str().c_str());
			}
			v1 = ret;
		}
	} else {
		std::ostringstream msg;
		std::string par = get_parfile();
		msg << "Pattern not handled[" << gr->getPatternName() << "]=" << gr->substring() <<
				" at " << gr->line() << " in " << par << std::endl;
		CCTK_Error(__LINE__,__FILE__,"piraha",msg.str().c_str());
	}
	return ret;
}


smart_ptr<Value> eval_expr(std::string input) {
	smart_ptr<Matcher> m = new Matcher(par_file_grammar,"eval",input.c_str());
	smart_ptr<Value> ret;
	if(m->matches()) {
		ret = meval(m->group(0));
	}
	return ret;
}

void check_types(const char *thorn,int line,ValueType v,int t) {
	bool ok = false;
	if(v == PIR_STRING && (t == PARAMETER_STRING || t == PARAMETER_KEYWORD))
		ok = true;
	else if(v == PIR_INT && t == PARAMETER_INT)
		ok = true;
	else if(v == PIR_BOOL && t == PARAMETER_BOOLEAN)
		ok = true;
	else if((v == PIR_INT || v == PIR_REAL) && t == PARAMETER_REAL)
		ok = true;
	if(!ok) {
		std::string par = get_parfile();
		std::ostringstream msg;
		msg << "Parameter type mismatch ";
		switch(t) {
		case PARAMETER_BOOLEAN:
			msg << "BOOL";
			break;
		case PARAMETER_INT:
			msg << "INT";
			break;
		case PARAMETER_KEYWORD:
			msg << "KEYWORD";
			break;
		case PARAMETER_STRING:
			msg << "STRING";
			break;
		case PARAMETER_REAL:
			msg << "REAL";
			break;
		default:
			msg << "type(" << t << ")";
			break;
		}
		msg << " != " << v << std::endl;
		CCTK_Error(line,par.c_str(),thorn,msg.str().c_str());
	}
}

extern "C" int cctk_PirahaParser(const char *buffer,unsigned long buffersize,int (*set_function)(const char *, const char *, int)) {
	const char *par_file_src =
			"skipper = ([ \\t\\r\\n]|\\#.*)*\n"
			"skipeol = ([ \\t\\r]|\\#.*)*\\n\n"
			"any = [^]\n"
			"stringcomment = #.*\n"
			"stringparser = ^({stringcomment}|{var}|{name}|{any})*$\n"

			"# Note that / occurs in some par files. It is my\n"
			"# feeling that this should require quote marks.\n"

			"name = [@a-zA-Z_][@/a-zA-Z0-9_-]*\n"
			"dname = [01-9][ab-zA-Z_]{2,}\n"
			"inquot = ({var}|\\\\.|[^\\\\\"])*\n"
			"fname = \\.?/[-\\./0-9a-zA-Z_]+\n"
			"quot = \"{inquot}\"|{fname}\n"
			"num = (inf|nan|([0-9]+(\\.[0-9]*|)|\\.[0-9]+)([ed][+-]?[0-9]+|))\n"
			"env = ENV\\{{name}\\}\n"
			"var = \\$({env}|{name}|\\{{name}\\})\n"

			"powexpr = {value}( \\*\\* {value})?\n"
			"mulop = [*/%]\n"
			"mexpr = {powexpr}( {mulop} {powexpr})*\n"
			"addop = [+-]\n"
			"aexpr = {mexpr}( {addop} {mexpr})*\n"
			"compop = [<>]=?\n"
			"compexpr = {aexpr}( {compop} {aexpr})?\n"
			"eqop = [!=]=\n"
			"eqexpr = {compexpr}( {eqop} {eqexpr})?\n"
			"andexpr = {eqexpr}( && {eqexpr})?\n"
			"expr = {andexpr}( \\|\\| {andexpr})?\n"
			"eval = {expr}\n"

			"paren = \\( {expr} \\)\n"
			"par = {name} :: {name}( {parindex})?\n"
			"func = {name} \\( {expr} \\)\n"
			"array = \\[ {expr}( , {expr})* \\]\n"

			"value = {unop}?({par}|{func}|{paren}|{dname}|{num}|{quot}|{name}|{var})\n"
			"unop = [-!]\n"

			"int = [0-9]+\n"
			"index = \\[ {int} \\]\n"
			"parindex = \\[ {expr} \\]\n"
			"active = (?i:ActiveThorns)\n"
			"set = ({active} = ({quot}|{name})|{par}( {index}|) = ({array}|\\+?{expr})){-skipeol}\n"
			"file = ^( !DESC {quot}|)( ({set}|{active}) )*$\n";

	compileFile(par_file_grammar,par_file_src,strlen(par_file_src));

	std::string active;
    smart_ptr<Matcher> m2 = new Matcher(par_file_grammar,"file",buffer,buffersize);
    if(m2->matches()) {
    	int line = -1;
    	for(int i=0;i<m2->groupCount();i++) {
    		smart_ptr<Group> gr = m2->group(i);
    		if(gr->group(0)->getPatternName() == "active") {
    			smart_ptr<Value> smv = meval(gr->group(1));
    			std::string val = smv->copy();
    			active += val;
    			active += ' ';
    			line = gr->line();
    		}
    	}
    	set_function("ActiveThorns",active.c_str(),line);
    	for(int i=0;i<m2->groupCount();i++) {
    		smart_ptr<Group> gr = m2->group(i);
    		if(gr->getPatternName() != "set")
    			continue;
    		smart_ptr<Group> par = gr->group("par");
    		if(par.valid()) {
    			// add value->quot->inquot
    			std::string key;
    			std::string thorn = par->group("name",0)->substring();
    			std::string name  = par->group("name",1)->substring();
    			key += thorn;
    			key += "::";
    			key += name;
				const cParamData *data = CCTK_ParameterData(name.c_str(),thorn.c_str());

    			smart_ptr<Group> index = par->group("parindex");
    			if(index.valid()) {
    				key += '[';
    				smart_ptr<Value> vv = meval(index);
    				if(vv->type != PIR_INT) {
    					std::ostringstream msg;
    					std::string par = get_parfile();
    					msg << "bad index " << vv << std::endl;
    					CCTK_Error(index->line(),par.c_str(),thorn.c_str(),msg.str().c_str());
    				}
    				std::string vvstr = vv->copy();
    				key += vvstr;
    				key += ']';
    			}

    			std::string val;
    			smart_ptr<Group> aexpr = gr->group("expr");
    			if(aexpr.valid()) {
    				current_thorn = thorn;
    				smart_ptr<Value> smv = meval(aexpr);
					val = smv->copy();
    				assert(smv.valid());
    				values[thorn][key] = smv;
    				smv->integerize();
    				if(data != NULL) {
        				if(data->type == PARAMETER_REAL)
        					smv->integerize();
        				if(data->type == PARAMETER_BOOLEAN)
        					smv->booleanize(gr);
    					check_types(thorn.c_str(),aexpr->line(),smv->type,data->type);
    				}
    				set_function(
    						strdup(key.c_str()),
    						strdup(val.c_str()),
    						gr->group(0)->line());
    			} else {
    				smart_ptr<Group> arr = gr->group("array");
    				for(int i=0;i<arr->groupCount();i++) {
    					aexpr = arr->group(i);
    					std::ostringstream keyi;
    					keyi << key << '[' << i << ']';
    					smart_ptr<Value> smv = meval(aexpr);
    					val = smv->copy();
        				values[thorn][keyi.str()] = smv;
        				if(data != NULL) {
            				if(data->type == PARAMETER_REAL)
            					smv->integerize();
            				if(data->type == PARAMETER_BOOLEAN)
            					smv->booleanize(gr);
        					check_types(thorn.c_str(),aexpr->line(),smv->type,data->type);
        				}
        				set_function(
        						strdup(keyi.str().c_str()),
        						strdup(val.c_str()),
        						aexpr->line());
    				}
    			}
    		}
    	}
    } else {
    	std::cout << "ERROR IN PARAMETER FILE:" << std::endl;
    	m2->showError();
    	return 1;
    }
    return 0;
}

}