59 return std::pow(
a,
b); }
64 expr.erase(remove_if(expr.begin(), expr.end(), isspace), expr.end());
67 panic_if(!root,
"Invalid expression\n");
85 for (
int i = expr.size() - 1;
i >= 0;
i--) {
91 if (par < 0)
return NULL;
92 if (par > 0)
continue;
94 for (
unsigned opt = 0; opt <
ops.size(); opt++) {
96 if (
ops[opt].
c == expr[
i]) {
102 if ((
l &&
r) || (!
ops[opt].binary &&
r)) {
116 if (expr.size() >= 2 && expr[0] ==
'(' && expr[expr.size() - 1] ==
')')
117 return parse(expr.substr(1, expr.size() - 2));
122 double v = strtod(expr.c_str(), &sptr);
123 if (sptr != expr.c_str()) {
133 bool contains_non_alpha =
false;
135 contains_non_alpha = contains_non_alpha or
136 !( (
c >=
'a' &&
c <=
'z') ||
137 (
c >=
'A' &&
c <=
'Z') ||
138 (
c >=
'0' &&
c <=
'9') ||
139 c ==
'$' ||
c ==
'\\' ||
c ==
'.' ||
c ==
'_');
141 if (!contains_non_alpha) {
159 return fn(
n->variable);
161 for (
auto & opt :
ops)
165 panic(
"Invalid node!\n");
172 ret += prefix +
"|-- " +
n->toStr() +
"\n";
174 ret +=
toStr(
n->r, prefix +
"| ");
176 ret +=
toStr(
n->l, prefix +
"| ");
187 variables.push_back(
n->variable);
double eval(EvalCallback fn) const
Evaluates the expression.
Node * parse(std::string expr)
Parse and create nodes from string.
std::array< OpSearch, uNeg+1 > ops
Operator list.
MathExpr(std::string expr)
std::vector< std::string > getVariables() const
Return all variables in the this expression.
std::string toStr() const
Prints an ASCII representation of the expression tree.
std::function< double(std::string)> EvalCallback
#define panic(...)
This implements a cprintf based panic() function.
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Bitfield< 3, 0 > priority
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Overload hash function for BasicBlockRange type.