@@ -66,6 +66,56 @@ Note that an assignment to an automatic variable must be terminated with
6666the constants set by ` setConstant ` and the variables registered by
6767` registerVariables ` .
6868
69+ The parser's ` operator() ` does not throw exceptions because it's meant to be
70+ used on both CPUs and GPUs. However, you could catch run time errors such as
71+ syntax errors during the definition and compilation stages. For example,
72+
73+ ``` c++
74+ amrexpr::Parser parser2;
75+ amrexpr::ParserExecutor<2 > exe2;
76+ try {
77+ parser2.define("a*x + b*y + b^^3"); // this will cause a syntax error
78+ parser2.setConstant("a", 4.0);
79+ parser2.setConstant("b", 2.0);
80+ parser.registerVariables({"x","y"});
81+ exe2 = parser.compile<2>(); // 2: two variables
82+ } catch (std::runtime_error const& e) {
83+ std::cout << e.what() << "\n";
84+ }
85+ if (exe2) {
86+ std::cout << "There was a syntax error in the expression. How did we come here?\n\n";
87+ } else {
88+ std::cout << "exe2 is null as expected\n\n";
89+ }
90+
91+ amrexpr::Parser parser3;
92+ amrexpr::ParserExecutor<2> exe3;
93+ try {
94+ parser3.define("a*x + b*y + z");
95+ parser3.setConstant("a", 4.0);
96+ parser3.setConstant("b", 2.0);
97+ parser3.registerVariables({"x","y"}); // forgot about z
98+ exe3 = parser3.compile<2>(); // 2: two variables
99+ } catch (std::runtime_error const& e) {
100+ std::cout << e.what() << "\n";
101+ }
102+ if (exe3) {
103+ std::cout << "There was an unknown symbol in the expression. How did we come here?\n\n";
104+ } else {
105+ std::cout << "exe3 is null as expected\n\n";
106+ }
107+ ```
108+
109+ This code block above will result in
110+
111+ ```console
112+ syntax error in Parser expression "a*x + b*y + b^^3"
113+ exe2 is null as expected
114+
115+ Unknown variable z in Parser expression "a*x + b*y + z"
116+ exe3 is null as expected
117+ ```
118+
69119## Installation
70120
71121There two ways to install ` amrexpr ` . A simple example demonstrating the use
0 commit comments