view interpreter.cpp @ 14:1e5a1e15fa56

clean up header files, more small readline changes
author John W. Eaton <jwe@octave.org>
date Thu, 23 May 2019 18:41:04 -0400
parents 894be158b32d
children
line wrap: on
line source

#include <iostream>
#include <string>

// For sleep.
#include <unistd.h>

#include "interpreter.h"
#include "parser.h"

namespace calc
{
  interpreter::interpreter (void)
    : m_parser (*this),
      m_error_handler ([] (const char *msg)
                       { std::cerr << "error: " << msg << std::endl; }),
      m_result_handler ([] (double value)
                        { std::cout << "ans = " << value << std::endl; })
  { }

  int interpreter::parse_and_execute (const std::string& line)
  {
    return m_parser.parse_and_execute (line);
  }

  void interpreter::emit_result (double value) const
  {
    // Simulate a delay in calculation.
    sleep (1);

    m_result_handler (value);
  }

  void interpreter::emit_error (const char *msg) const
  {
    m_error_handler (msg);
  }
}