Calculator  Step 2
Functions
parse.cpp File Reference
#include <sstream>
#include "parse.hpp"

Go to the source code of this file.

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 267 of file parse.cpp.

References parser::get_expr().

Referenced by BOOST_AUTO_TEST_CASE(), and main().

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