Calculator  Step 3
Classes | Functions
parse.hpp File Reference
#include <istream>
#include <locale>
#include <ostream>
#include <stdexcept>
#include <string>

Go to the source code of this file.

Classes

class  parse_error
 
class  parser
 

Functions

void parse_loop (std::istream &input, std::ostream &output)
 

Function Documentation

void parse_loop ( std::istream &  input,
std::ostream &  output 
)

Parse loop. Read expressions from input and print results to output.

Parameters
inputThe input stream.
outputThe output stream.

Definition at line 272 of file parse.cpp.

References node::evaluate().

Referenced by BOOST_AUTO_TEST_CASE(), and main().

273 {
274  std::string line{};
275  // No portable way to test whether the console is an interactive terminal
276  // vs. a non-interactive file. If you have a system-specific way to test,
277  // output the prompt only for the interactive case.
278  for (output << "> "; std::getline(input, line); output << "> ") {
279  std::istringstream input{std::move(line)};
280  parser p{input};
281  try {
282  node n;
283  while (p.get_expr(n))
284  output << n.evaluate() << '\n';
285  } catch(parse_error const& ex) {
286  output << ex.what() << '\n';
287  } catch(std::exception const& ex) {
288  output << "exception: " << ex.what() << '\n';
289  }
290  }
291 }
Definition: node.hpp:16
Definition: parse.hpp:30
double evaluate() const
Definition: node.cpp:50