25#define STACK_DEPTH 256
39 fail_stack[i] =
false;
45 for (
int i = 0; i <
STACK_DEPTH; i++) fail_stack[i] =
false;
53 const double* val =
evalFP();
55 for (
int k = 0; k < 3; k++) std::cerr << val[k] <<
" ";
56 std::cerr << std::endl;
64 std::cerr <<
"Return type FP(" << dim <<
") ignoring" << std::endl;
73 fail_stack[_count] =
true;
91 void eval(
double* result) {
92 for (
int k = 0; k < 3; k++) result[k] = val[k];
95 void eval(
const char** result) {}
104 ExprVarRef* resolveVar(
const std::string& name)
const {
105 if (name[0] ==
'_') {
106 int position = atoi(name.substr(1, name.size() - 1).c_str());
107 if (position >= count()) std::cerr <<
"Use of unused result line." << std::endl;
108 if (fail_stack[position]) std::cerr <<
"Use of invalid result line." << std::endl;
109 return &(stack[position]);
111 addError(
"Use of undefined variable.", 0, 0);
116int main(
int argc,
char* argv[]) {
118 std::cout <<
"SeExpr Basic Calculator";
123 std::cout << std::endl <<
expr.count() <<
"> ";
125 getline(std::cin, str);
127 if (std::cin.eof()) {
128 std::cout << std::endl;
132 if (str ==
"quit" || str ==
"q")
break;
136 if (!
expr.isValid()) {
138 std::cerr <<
"Expression failed: " <<
expr.parseError() << std::endl;
141 std::cout <<
" " <<
expr.peek();
int main(int argc, char *argv[])
static void cleanup()
cleanup all functions
abstract class for implementing variable references
const char * evalStr(VarBlock *varBlock=nullptr) const
const ExprType & returnType() const
void addError(const std::string &error, const int startPos, const int endPos) const
const double * evalFP(VarBlock *varBlock=nullptr) const
const ExprStrNode * isString(const ExprNode *testee)
</pre >< h3 > Binding our variable reference</h3 > If we now tried to use the variable would still not be found by our expressions To make it bindable we need to override the resolveVar() function as follows</pre >< h3 > Variable setting</h3 > Next we need to make a way of setting the variable As the controlling code will use the expression it will repeatedly alternate between setting the independent variables that are used and calling evaluate(). What it has to do depends very much on the application. In this case we only need to set the independent variable x as</pre >< h2 > Evaluating expressions</h2 > Evaluating an expression is pretty easy But before we can do that we need to make an instance< pre > GrapherExpr expr("x+x^2")