| 25 | |
| 26 | template <typename T> |
| 27 | void composite() |
| 28 | { |
| 29 | typedef exprtk::symbol_table<T> symbol_table_t; |
| 30 | typedef exprtk::expression<T> expression_t; |
| 31 | typedef exprtk::parser<T> parser_t; |
| 32 | typedef exprtk::parser_error::type err_t; |
| 33 | typedef exprtk::function_compositor<T> compositor_t; |
| 34 | typedef typename compositor_t::function function_t; |
| 35 | |
| 36 | T x = T(1); |
| 37 | T y = T(2); |
| 38 | |
| 39 | compositor_t compositor; |
| 40 | |
| 41 | symbol_table_t& symbol_table = compositor.symbol_table(); |
| 42 | symbol_table.add_constants(); |
| 43 | symbol_table.add_variable("x",x); |
| 44 | symbol_table.add_variable("y",y); |
| 45 | |
| 46 | compositor.add( |
| 47 | function_t("f") // f(x) = sin(x / pi) |
| 48 | .var("x") |
| 49 | .expression( "sin(x / pi)" )); |
| 50 | |
| 51 | compositor.add( |
| 52 | function_t("g") // g(x,y) = 3[f(x) + f(y)] |
| 53 | .vars("x", "y") |
| 54 | .expression( "3*[f(x) + f(y)]" )); |
| 55 | |
| 56 | std::string expression_string = "g(1 + f(x), f(y) / 2)"; |
| 57 | |
| 58 | expression_t expression; |
| 59 | expression.register_symbol_table(symbol_table); |
| 60 | |
| 61 | parser_t parser; |
| 62 | |
| 63 | if (!parser.compile(expression_string,expression)) |
| 64 | { |
| 65 | printf("Error: %s\tExpression: %s\n", |
| 66 | parser.error().c_str(), |
| 67 | expression_string.c_str()); |
| 68 | |
| 69 | for (std::size_t i = 0; i < parser.error_count(); ++i) |
| 70 | { |
| 71 | const err_t error = parser.get_error(i); |
| 72 | |
| 73 | printf("Error: %02d Position: %02d Type: [%14s] Msg: %s\tExpression: %s\n", |
| 74 | static_cast<unsigned int>(i), |
| 75 | static_cast<unsigned int>(error.token.position), |
| 76 | exprtk::parser_error::to_str(error.mode).c_str(), |
| 77 | error.diagnostic.c_str(), |
| 78 | expression_string.c_str()); |
| 79 | } |
| 80 | |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | const T result = expression.value(); |