Mercurial > jwe > qt-gui-with-push-parser
view interpreter.cpp @ 12:894be158b32d
define parser as a class and eliminate some global variables
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 23 May 2019 17:57:20 -0400 |
parents | 822a2fe5bb51 |
children | 1e5a1e15fa56 |
line wrap: on
line source
#include <iostream> #include <string> #include <unistd.h> #include "interpreter.h" #include "parser.h" #include "gui-main.h" #include "tty-main.h" namespace calc { interpreter *interpreter::the_interpreter = nullptr; 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; }) { if (the_interpreter) { std::cerr << "multiple interpreters are not possible!" << std::endl; exit (1); } the_interpreter = this; } 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); } }