C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
Functions
exprtk_immutable_symbol_table_example.cpp File Reference
#include <cstdio>
#include <string>
#include <utility>
#include <algorithm>
#include <vector>
#include "exprtk.hpp"
Include dependency graph for exprtk_immutable_symbol_table_example.cpp:

Go to the source code of this file.

Functions

template<typename T >
void immutable_symtab_example ()
 
int main ()
 

Function Documentation

◆ immutable_symtab_example()

template<typename T >
void immutable_symtab_example ( )

Definition at line 30 of file exprtk_immutable_symbol_table_example.cpp.

31{
32 typedef exprtk::symbol_table<T> symbol_table_t;
33 typedef exprtk::expression<T> expression_t;
34 typedef exprtk::parser<T> parser_t;
35
36 T x = 1.1;
37 T y = 2.2;
38 T z = 3.3;
39 T w = 4.4;
40
41 symbol_table_t mutable_symbol_table;
42 symbol_table_t immutable_symbol_table(symbol_table_t::symtab_mutability_type::e_immutable);
43
44 mutable_symbol_table.add_variable("x", x);
45 mutable_symbol_table.add_variable("y", y);
46
47 immutable_symbol_table.add_variable("z", z);
48 immutable_symbol_table.add_variable("w", w);
49
50 expression_t expression;
51 expression.register_symbol_table(immutable_symbol_table);
52 expression.register_symbol_table(mutable_symbol_table );
53
54 parser_t parser;
55
56 std::vector<std::string> expressions =
57 {
58 "x := y + (z / w)", // ok - will compile
59 "y := y / x + (z / w)", // ok - will compile
60 "z := y + x - w", // Error - will not compile
61 "z == (w := y / x)", // Error - will not compile
62 };
63
64 for (const auto& expression_str : expressions)
65 {
66
67 if (!parser.compile(expression_str, expression))
68 {
69 for (std::size_t error_idx = 0; error_idx < parser.error_count(); ++error_idx)
70 {
71 const auto error = parser.get_error(error_idx);
72 printf("Error: %02d Pos: %02d Type: [%14s] Message: %s\tExpression: %s\n",
73 static_cast<unsigned int>(error_idx),
74 static_cast<unsigned int>(error.token.position),
75 exprtk::parser_error::to_str(error.mode).c_str(),
76 error.diagnostic.c_str(),
77 expression_str.c_str());
78 }
79
80 continue;
81 }
82
83 // Modify all the variables from both the immutable
84 // and mutable symbol tables
85
86 x += 1.1;
87 y += 2.2;
88 z += 3.3;
89 w += 4.4;
90
91 expression.value();
92 }
93
94 return;
95}
std::string to_str(error_mode mode)
Definition exprtk.hpp:22098

References exprtk::parser_error::to_str().

Here is the call graph for this function:

◆ main()

int main ( )

Definition at line 97 of file exprtk_immutable_symbol_table_example.cpp.

98{
99 immutable_symtab_example<double>();
100 return 0;
101}