Line data Source code
1 : #ifndef MATHEVAL_IMPLEMENTATION 2 : #error "Do not include evaluator.hpp directly!" 3 : #endif 4 : 5 : #pragma once 6 : 7 : #include "ast.hpp" 8 : 9 : #include <map> 10 : #include <string> 11 : 12 : namespace matheval { 13 : 14 : namespace ast { 15 : 16 : struct ConstantFolder { 17 : typedef operand result_type; 18 : 19 : result_type operator()(nil) const; 20 : 21 : result_type operator()(double n) const; 22 : 23 : result_type operator()(std::string const &c) const; 24 : 25 : result_type operator()(operation const &x, operand const &lhs) const; 26 : 27 : result_type operator()(unary_op const &x) const; 28 : 29 : result_type operator()(binary_op const &x) const; 30 : 31 : result_type operator()(ternary_op const &x) const; 32 : 33 : result_type operator()(expression const &x) const; 34 : }; 35 : 36 : struct eval { 37 : typedef double result_type; 38 : 39 86 : explicit eval(std::map<std::string, double> const &sym) : st(sym) {} 40 : 41 : double operator()(nil) const; 42 : 43 : double operator()(double n) const; 44 : 45 : double operator()(std::string const &c) const; 46 : 47 : double operator()(operation const &x, double lhs) const; 48 : 49 : double operator()(unary_op const &x) const; 50 : 51 : double operator()(binary_op const &x) const; 52 : 53 : double operator()(ternary_op const &x) const; 54 : 55 : double operator()(expression const &x) const; 56 : 57 : private: 58 : std::map<std::string, double> st; 59 : }; 60 : 61 : } // namespace ast 62 : 63 : } // namespace matheval