11#include <unordered_map>
40using dic_type = std::unordered_map<string, Item>;
51#define EVAL HepTool::Evaluator
53#define REMOVE_BLANKS \
54for(pointer=name;;pointer++) if (!isspace(*pointer)) break; \
55for(n=strlen(pointer);n>0;n--) if (!isspace(*(pointer+n-1))) break
59 c = (pointer > end) ? '\0' : *pointer; \
60 if (!isspace(c)) break; \
63#define EVAL_EXIT(STATUS,POSITION) endp = POSITION; return STATUS
66static const char sss[
MAX_N_PAR+2] =
"012345";
68enum {
ENDL,
LBRA,
OR,
AND,
EQ,
NE,
GE,
GT,
LE,
LT,
73static int variable(
const string & name,
double & result,
90 dic_type::const_iterator iter = dictionary.find(name);
91 if (iter == dictionary.end())
92 return EVAL::ERROR_UNKNOWN_VARIABLE;
93 Item item = iter->second;
100 pchar exp_end = exp_begin + strlen(exp_begin) - 1;
101 if (engine(exp_begin, exp_end, result, exp_end, dictionary) == EVAL::OK)
104 return EVAL::ERROR_CALCULATION_ERROR;
107 return EVAL::ERROR_CALCULATION_ERROR;
111static int function(
const string & name, stack<double> & par,
112 double & result,
const dic_type & dictionary)
129 int npar = par.size();
130 if (npar >
MAX_N_PAR)
return EVAL::ERROR_UNKNOWN_FUNCTION;
132 dic_type::const_iterator iter = dictionary.find(sss[npar]+name);
133 if (iter == dictionary.end())
return EVAL::ERROR_UNKNOWN_FUNCTION;
134 Item item = iter->second;
137 for(
int i=0; i<npar; i++) {
pp[i] = par.top(); par.pop(); }
139 if (item.
function == 0)
return EVAL::ERROR_CALCULATION_ERROR;
142 result = ((double (*)())item.
function)();
145 result = ((double (*)(double))item.
function)(pp[0]);
148 result = ((double (*)(double,double))item.
function)(pp[1], pp[0]);
151 result = ((double (*)(double,double,double))item.
function)
155 result = ((double (*)(double,double,double,double))item.
function)
156 (pp[3],pp[2],pp[1],pp[0]);
159 result = ((double (*)(double,double,double,double,double))item.
function)
160 (pp[4],pp[3],pp[2],pp[1],pp[0]);
163 return (errno == 0) ? EVAL::OK : EVAL::ERROR_CALCULATION_ERROR;
166static int operand(
pchar begin,
pchar end,
double & result,
186 pchar pointer = begin;
192 if (!isalpha(*pointer)) {
194 result = strtod(pointer, (
char **)(&pointer));
198 EVAL_EXIT( EVAL::ERROR_CALCULATION_ERROR, begin );
204 while(pointer <= end) {
206 if (c !=
'_' && !isalnum(c))
break;
219 EVAL_STATUS = variable(name, result, dictionary);
220 EVAL_EXIT( EVAL_STATUS, (EVAL_STATUS == EVAL::OK) ? --pointer : begin);
228 pchar par_begin = pointer+1, par_end;
231 c = (pointer > end) ?
'\0' : *pointer;
234 EVAL_EXIT( EVAL::ERROR_UNPAIRED_PARENTHESIS,
pos.top() );
236 pos.push(pointer);
break;
238 if (
pos.size() == 1) {
240 EVAL_STATUS = engine(par_begin, par_end, value, par_end, dictionary);
241 if (EVAL_STATUS == EVAL::WARNING_BLANK_STRING)
242 {
EVAL_EXIT( EVAL::ERROR_EMPTY_PARAMETER, --par_end ); }
243 if (EVAL_STATUS != EVAL::OK)
246 par_begin = pointer + 1;
250 if (
pos.size() > 1) {
255 EVAL_STATUS = engine(par_begin, par_end, value, par_end, dictionary);
256 switch (EVAL_STATUS) {
260 case EVAL::WARNING_BLANK_STRING:
262 {
EVAL_EXIT( EVAL::ERROR_EMPTY_PARAMETER, --par_end ); }
267 EVAL_STATUS =
function(name, par, result, dictionary);
268 EVAL_EXIT( EVAL_STATUS, (EVAL_STATUS == EVAL::OK) ? pointer : begin);
288static int maker(
int op, stack<double> & val)
290 if (val.size() < 2)
return EVAL::ERROR_SYNTAX_ERROR;
291 double val2 = val.top(); val.pop();
292 double val1 = val.top();
295 val.top() = (val1 || val2) ? 1. : 0.;
298 val.top() = (val1 && val2) ? 1. : 0.;
301 val.top() = (val1 == val2) ? 1. : 0.;
304 val.top() = (val1 != val2) ? 1. : 0.;
307 val.top() = (val1 >= val2) ? 1. : 0.;
310 val.top() = (val1 > val2) ? 1. : 0.;
313 val.top() = (val1 <= val2) ? 1. : 0.;
316 val.top() = (val1 < val2) ? 1. : 0.;
319 val.top() = val1 + val2;
322 val.top() = val1 - val2;
325 val.top() = val1 * val2;
328 if (val2 == 0.0)
return EVAL::ERROR_CALCULATION_ERROR;
329 val.top() = val1 / val2;
333 val.top() = std::pow(val1,val2);
334 if (errno == 0)
return EVAL::OK;
335 else return EVAL::ERROR_CALCULATION_ERROR;
337 val.top() = val1 + val2;
340 val.top() = val1 - val2;
343 return EVAL::ERROR_CALCULATION_ERROR;
363static int engine(
pchar begin,
pchar end,
double & result,
366 enum SyntaxTableEntry {
368 NumberVariableOrFunction = 1,
369 UnaryPlusOrMinus = 2,
372 static const int SyntaxTable[19][19] = {
374 { 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 1 },
375 { 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 1 },
376 { 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 1 },
377 { 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 1 },
378 { 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 1 },
379 { 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 1 },
380 { 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 1 },
381 { 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 1 },
382 { 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 1 },
383 { 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 1 },
384 { 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 1 },
385 { 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 1 },
386 { 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 1 },
387 { 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 1 },
388 { 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 1 },
389 { 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 1 },
390 { 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 1 },
391 { 3, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0 },
392 { 3, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0 }
394 enum ActionTableEntry {
395 UnbalancedParentheses = -1,
396 ExpressionCompleted = 0,
397 HigherPrecedenceOperator = 1,
398 SamePrecedenceOperator = 2,
399 CloseProcessedParenthesesOrExpression = 3,
400 LowerPrecedenceOperator = 4
402 static const int ActionTable[17][18] = {
404 { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,-1 },
405 {-1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3 },
406 { 4, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4 },
407 { 4, 1, 4, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4 },
408 { 4, 1, 4, 4, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4 },
409 { 4, 1, 4, 4, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4 },
410 { 4, 1, 4, 4, 4, 4, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 4 },
411 { 4, 1, 4, 4, 4, 4, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 4 },
412 { 4, 1, 4, 4, 4, 4, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 4 },
413 { 4, 1, 4, 4, 4, 4, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 4 },
414 { 4, 1, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 1, 1, 1, 1, 1, 4 },
415 { 4, 1, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 1, 1, 1, 1, 1, 4 },
416 { 4, 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 1, 1, 4, 4, 1, 4 },
417 { 4, 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 1, 1, 4, 4, 1, 4 },
418 { 4, 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 1, 1, 2, 2, 1, 4 },
419 { 4, 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 1, 1, 2, 2, 1, 4 },
420 { 4, 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 1, 1, 4, 4, 4, 4 }
427 pchar pointer = begin;
428 int iWhat, iCur, iPrev = 0, iTop, EVAL_STATUS;
431 op.push(0);
pos.push(pointer);
433 if (c ==
'\0') {
EVAL_EXIT( EVAL::WARNING_BLANK_STRING, begin ); }
438 c = (pointer > end) ?
'\0' : *pointer;
439 if (isspace(c))
continue;
441 case '\0': iCur =
ENDL;
break;
442 case '(': iCur =
LBRA;
break;
444 if (*(pointer+1) ==
'|') {
445 pointer++; iCur =
OR;
break;
447 EVAL_EXIT( EVAL::ERROR_UNEXPECTED_SYMBOL, pointer );
450 if (*(pointer+1) ==
'&') {
451 pointer++; iCur =
AND;
break;
453 EVAL_EXIT( EVAL::ERROR_UNEXPECTED_SYMBOL, pointer );
456 if (*(pointer+1) ==
'=') {
457 pointer++; iCur =
EQ;
break;
459 EVAL_EXIT( EVAL::ERROR_UNEXPECTED_SYMBOL, pointer );
462 if (*(pointer+1) ==
'=') {
463 pointer++; iCur =
NE;
break;
465 EVAL_EXIT( EVAL::ERROR_UNEXPECTED_SYMBOL, pointer );
468 if (*(pointer+1) ==
'=') { pointer++; iCur =
GE; }
else { iCur =
GT; }
471 if (*(pointer+1) ==
'=') { pointer++; iCur =
LE; }
else { iCur =
LT; }
473 case '+': iCur =
PLUS;
break;
474 case '-': iCur =
MINUS;
break;
476 if (*(pointer+1) ==
'*') { pointer++; iCur =
POW; }
else{ iCur =
MULT; }
478 case '/': iCur =
DIV;
break;
479 case '^': iCur =
POW;
break;
480 case ')': iCur =
RBRA;
break;
482 if (c ==
'.' || isalnum(c)) {
485 EVAL_EXIT( EVAL::ERROR_UNEXPECTED_SYMBOL, pointer );
491 iWhat = SyntaxTable[iPrev][iCur];
495 EVAL_EXIT( EVAL::ERROR_SYNTAX_ERROR, pointer );
497 EVAL_STATUS = operand(pointer, end, value, pointer, dictionary);
498 if (EVAL_STATUS != EVAL::OK) {
EVAL_EXIT( EVAL_STATUS, pointer ); }
514 if (op.size() == 0) {
EVAL_EXIT( EVAL::ERROR_SYNTAX_ERROR, pointer ); }
516 switch (ActionTable[iTop][iCur]) {
518 if (op.size() > 1) pointer =
pos.top();
519 EVAL_EXIT( EVAL::ERROR_UNPAIRED_PARENTHESIS, pointer );
521 if (val.size() == 1) {
525 EVAL_EXIT( EVAL::ERROR_SYNTAX_ERROR, pointer );
528 op.push(iCur);
pos.push(pointer);
531 EVAL_STATUS = maker(iTop, val);
532 if (EVAL_STATUS != EVAL::OK) {
535 op.top() = iCur;
pos.top() = pointer;
541 EVAL_STATUS = maker(iTop, val);
542 if (EVAL_STATUS != EVAL::OK) {
554static void setItem(
const char *
prefix,
const char * name,
557 if (name == 0 || *name ==
'\0') {
572 for(
int i=0; i<
n; i++) {
573 char c = *(pointer+i);
574 if (c !=
'_' && !isalnum(c)) {
582 string item_name =
prefix + string(pointer,n);
583 dic_type::iterator iter = (s->
theDictionary).find(item_name);
586 if (item_name == name) {
587 s->
theStatus = EVAL::WARNING_EXISTING_VARIABLE;
589 s->
theStatus = EVAL::WARNING_EXISTING_FUNCTION;
623 if (expression != 0) {
637 return ((
Struct *)(p))->theStatus;
642 return ((
Struct *)(p))->thePosition - ((
Struct *)(p))->theExpression;
657 char prefix[] =
"Evaluator : ";
658 std::ostringstream errn;
662 errn <<
prefix <<
"invalid name";
665 errn <<
prefix <<
"syntax error";
668 errn <<
prefix <<
"unpaired parenthesis";
671 errn <<
prefix <<
"unexpected symbol";
674 errn <<
prefix <<
"unknown variable";
677 errn <<
prefix <<
"unknown function";
680 errn <<
prefix <<
"empty parameter in function call";
683 errn <<
prefix <<
"calculation error";
693{ setItem(
"", name,
Item(value), (
Struct *)p); }
696{ setItem(
"", name,
Item(expression), (
Struct *)p); }
706 double (*fun)(
double))
710 double (*fun)(
double,
double))
714 double (*fun)(
double,
double,
double))
718 double (*fun)(
double,
double,
double,
double))
722 double (*fun)(
double,
double,
double,
double,
double))
727 if (name == 0 || *name ==
'\0')
return false;
729 if (n == 0)
return false;
738 if (name == 0 || *name ==
'\0')
return false;
739 if (npar < 0 || npar >
MAX_N_PAR)
return false;
741 if (n == 0)
return false;
743 return ((s->
theDictionary).find(sss[npar]+
string(pointer,n)) ==
749 if (name == 0 || *name ==
'\0')
return;
758 if (name == 0 || *name ==
'\0')
return;
759 if (npar < 0 || npar >
MAX_N_PAR)
return;
std::unordered_map< string, Item > dic_type
#define EVAL_EXIT(STATUS, POSITION)
G4double(*)(G4double) function
const char * name(G4int ptype)