Mercurial > octave
changeset 29619:5cf985f5760a
move get_line_and_eval to parse tree evaluator
* pt-eval.h, pt-eval.cc (tree_evaluator::get_line_and_eval):
Move fucntion body here from interpreter.cc.
* interpreter.cc (interpreter::get_line_and_eval): Forward to
tree_evaluator::get_line_and_eval.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 05 May 2021 20:28:44 -0400 |
parents | a31cf1ec1349 |
children | ad720f9c12b3 |
files | libinterp/corefcn/interpreter.cc libinterp/parse-tree/pt-eval.cc libinterp/parse-tree/pt-eval.h |
diffstat | 3 files changed, 99 insertions(+), 89 deletions(-) [+] |
line wrap: on
line diff
--- a/libinterp/corefcn/interpreter.cc Wed May 05 15:39:17 2021 -0700 +++ b/libinterp/corefcn/interpreter.cc Wed May 05 20:28:44 2021 -0400 @@ -29,14 +29,9 @@ #include <cstdio> +#include <iostream> #include <set> #include <string> -#include <iostream> - -// The following headers are only needed for the new experimental -// terminal widget. -#include <condition_variable> -#include <mutex> #include <thread> #include "cmd-edit.h" @@ -782,89 +777,7 @@ void interpreter::get_line_and_eval (void) { - std::mutex mtx; - std::unique_lock<std::mutex> lock (mtx); - std::condition_variable cv; - bool incomplete_parse = false; - bool evaluation_pending = false; - bool exiting = false; - - while (true) - { - // FIXME: Detect EOF? Use readline? If - // so, then we need to disable idle event loop hook function - // execution. - - std::string ps - = incomplete_parse ? m_input_system.PS2 () : m_input_system.PS1 (); - - std::cout << command_editor::decode_prompt_string (ps); - - std::string input; - std::getline (std::cin, input); - - if (input.empty ()) - continue; - - incomplete_parse = false; - evaluation_pending = true; - exiting = false; - - m_event_manager.post_event - ([&] (interpreter& interp) - { - // INTERPRETER THREAD - - std::lock_guard<std::mutex> local_lock (mtx); - - try - { - interp.parse_and_execute (input, incomplete_parse); - } - catch (const exit_exception&) - { - evaluation_pending = false; - exiting = true; - cv.notify_all (); - throw; - } - catch (const execution_exception& ee) - { - m_error_system.save_exception (ee); - m_error_system.display_exception (ee); - - if (m_interactive) - { - recover_from_exception (); - evaluation_pending = false; - cv.notify_all (); - } - else - { - evaluation_pending = false; - cv.notify_all (); - throw exit_exception (1); - } - } - catch (...) - { - evaluation_pending = false; - cv.notify_all (); - throw; - } - - evaluation_pending = false; - cv.notify_all (); - }); - - // Wait until evaluation is finished before prompting for input - // again. - - cv.wait (lock, [&] { return ! evaluation_pending; }); - - if (exiting) - break; - } + m_evaluator.get_line_and_eval (); } // Note: the following class is currently only used with the new
--- a/libinterp/parse-tree/pt-eval.cc Wed May 05 15:39:17 2021 -0700 +++ b/libinterp/parse-tree/pt-eval.cc Wed May 05 20:28:44 2021 -0400 @@ -29,9 +29,12 @@ #include <cctype> +#include <condition_variable> #include <iostream> #include <list> +#include <mutex> #include <string> +#include <thread> #include "cmd-edit.h" #include "file-ops.h" @@ -633,6 +636,97 @@ evmgr.pre_input_event (); } + void tree_evaluator::get_line_and_eval (void) + { + std::mutex mtx; + std::unique_lock<std::mutex> lock (mtx); + std::condition_variable cv; + bool incomplete_parse = false; + bool evaluation_pending = false; + bool exiting = false; + + input_system& input_sys = m_interpreter.get_input_system (); + event_manager& evmgr = m_interpreter.get_event_manager (); + + while (true) + { + // FIXME: Detect EOF? Use readline? If + // so, then we need to disable idle event loop hook function + // execution. + + std::string ps = incomplete_parse ? input_sys.PS2 () : input_sys.PS1 (); + + std::cout << command_editor::decode_prompt_string (ps); + + std::string input; + std::getline (std::cin, input); + + if (input.empty ()) + continue; + + incomplete_parse = false; + evaluation_pending = true; + exiting = false; + + evmgr.post_event + ([&] (interpreter& interp) + { + // INTERPRETER THREAD + + std::lock_guard<std::mutex> local_lock (mtx); + + try + { + interp.parse_and_execute (input, incomplete_parse); + } + catch (const exit_exception&) + { + evaluation_pending = false; + exiting = true; + cv.notify_all (); + throw; + } + catch (const execution_exception& ee) + { + error_system& es = m_interpreter.get_error_system (); + + es.save_exception (ee); + es.display_exception (ee); + + if (m_interpreter.interactive ()) + { + m_interpreter.recover_from_exception (); + evaluation_pending = false; + cv.notify_all (); + } + else + { + evaluation_pending = false; + cv.notify_all (); + throw exit_exception (1); + } + } + catch (...) + { + evaluation_pending = false; + cv.notify_all (); + throw; + } + + evaluation_pending = false; + cv.notify_all (); + }); + + // Wait until evaluation is finished before prompting for input + // again. + + cv.wait (lock, [&] { return ! evaluation_pending; }); + + if (exiting) + break; + } + } + int tree_evaluator::repl (void) { // The big loop. Read, Eval, Print, Loop. Normally user @@ -831,6 +925,7 @@ if (m_interpreter.interactive ()) { + std::cerr << "recover from exception and reset parser" << std::endl; m_interpreter.recover_from_exception (); m_parser->reset (); }
--- a/libinterp/parse-tree/pt-eval.h Wed May 05 15:39:17 2021 -0700 +++ b/libinterp/parse-tree/pt-eval.h Wed May 05 20:28:44 2021 -0400 @@ -172,6 +172,8 @@ void parse_and_execute (const std::string& input, bool& incomplete_parse); + void get_line_and_eval (void); + int repl (void); bool in_top_level_repl (void) const { return m_in_top_level_repl; }