Eval is a simple, header-only, expression parser for C++.
features
- easy to integrate - just include
eval.h - simple API:
eval("your expression", ?vars, ?functions) - operators
* / + - ^ % - numbers and strings
- variable argument functions
- built-in constants and functions like
pi,sqrt(),sin(),ceil()etc. - tests
requirements
C++11 compiler
usage
basic
#include "eval.h" using namespace jgod; assert(eval("3*2 + 4") == 10);
vars
std::map<std::string, double> vars; vars["myvar"] = 2; assert(eval("3*myvar + 4", vars) == 10);
error handling
try {return eval("3.14q59");} catch (const std::invalid_argument &e) {...}
impl details
- flow
- strip whitespace
- rewrite adjacent operators
- tokenize
- Shunting-yard algorithm to build RPN queue
- process queue with RPN calculator
- values: numbers and strings
- base value type is
std::string(casts are necessary) - numbers == doubles
- "null" expressions return 0
- base value type is
- unary
+ - - function binding using
std::function - variable length functions using
std::vector std::exceptions for error handling
license
Copyright Justin Godesky. Released under the AGPLv3 License.