Skip to content

Commit 9c75c5d

Browse files
committed
Update readme
1 parent 765ad71 commit 9c75c5d

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,56 @@ Note that an assignment to an automatic variable must be terminated with
6666
the 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

71121
There two ways to install `amrexpr`. A simple example demonstrating the use

Src/amrexpr_Parser.H

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ ParserExecutor<N>
126126
Parser::compileHost () const
127127
{
128128
if (m_data && m_data->m_parser) {
129-
assert(N == m_data->m_nvars);
129+
if (N != m_data->m_nvars) {
130+
throw std::runtime_error("amrexpr: the number of varibles in compile() and registerVariables() do not match");
131+
}
130132

131133
if (!(m_data->m_host_executor)) {
132134
int stack_size;
@@ -135,12 +137,12 @@ Parser::compileHost () const
135137
stack_size));
136138

137139
if (m_data->m_max_stack_size > AMREXPR_PARSER_STACK_SIZE) {
138-
throw std::runtime_error("amrexpr::Parser: AMREXPR_PARSER_STACK_SIZE, "
140+
throw std::runtime_error("amrexpr: AMREXPR_PARSER_STACK_SIZE, "
139141
+ std::to_string(AMREXPR_PARSER_STACK_SIZE) + ", is too small for "
140142
+ m_data->m_expression);
141143
}
142144
if (stack_size != 0) {
143-
throw std::runtime_error("amrexpr::Parser: something went wrong with parser stack! "
145+
throw std::runtime_error("amrexpr: something went wrong with parser stack! "
144146
+ std::to_string(stack_size));
145147
}
146148

0 commit comments

Comments
 (0)