Calculator  Step 2
Classes | Functions
parse.hpp File Reference
#include <istream>
#include <locale>
#include <ostream>
#include <stdexcept>
#include <string>
#include <vector>
#include "variables.hpp"

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 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