changeset 25404:e37d857716c3

move core of interpreter::main_loop to evaluator * pt-eval.h, pt-eval.cc (tree_evaluator::repl): New fucntion, adapted from core of interpreter::main_loop. (tree_evaluator::breaking, tree_evaluator::continuing, tree_evaluator::returning): New variants to allow the corresponding variables to be set. * interpreter.cc (interpreter::main_loop): Call tree_evaluator::repl.
author John W. Eaton <jwe@octave.org>
date Tue, 22 May 2018 15:23:50 -0400
parents 2b7d7a3f5f57
children df2f3af2c6c5
files libinterp/corefcn/interpreter.cc libinterp/parse-tree/pt-eval.cc libinterp/parse-tree/pt-eval.h
diffstat 3 files changed, 142 insertions(+), 113 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/interpreter.cc	Tue May 22 13:23:49 2018 -0400
+++ b/libinterp/corefcn/interpreter.cc	Tue May 22 15:23:50 2018 -0400
@@ -923,119 +923,7 @@
 
     // The big loop.
 
-    lexer *lxr = (application::interactive () ? new lexer ()
-                                              : new lexer (stdin));
-
-    parser parser (*lxr);
-
-    int retval = 0;
-    do
-      {
-        try
-          {
-            reset_error_handler ();
-
-            parser.reset ();
-
-            if (m_symbol_table.at_top_level ())
-              m_evaluator.reset_debug_state ();
-
-            retval = parser.run ();
-
-            if (retval == 0)
-              {
-                if (parser.m_stmt_list)
-                  {
-                    parser.m_stmt_list->accept (m_evaluator);
-
-                    octave_quit ();
-
-                    if (! application::interactive ())
-                      {
-                        bool quit = (m_evaluator.returning ()
-                                     || m_evaluator.breaking ());
-
-                        if (m_evaluator.returning ())
-                          m_evaluator.returning (0);
-
-                        if (m_evaluator.breaking ())
-                          m_evaluator.breaking (m_evaluator.breaking () - 1)
-
-                        if (quit)
-                          break;
-                      }
-
-                    if (octave_completion_matches_called)
-                      octave_completion_matches_called = false;
-                    else
-                      command_editor::increment_current_command_number ();
-                  }
-                else if (parser.m_lexer.m_end_of_input)
-                  {
-                    retval = EOF;
-                    break;
-                  }
-              }
-          }
-        catch (const interrupt_exception&)
-          {
-            recover_from_exception ();
-
-            // Required newline when the user does Ctrl+C at the prompt.
-            if (application::interactive ())
-              octave_stdout << "\n";
-          }
-        catch (const index_exception& e)
-          {
-            recover_from_exception ();
-
-            std::cerr << "error: unhandled index exception: "
-                      << e.message () << " -- trying to return to prompt"
-                      << std::endl;
-          }
-        catch (const execution_exception& e)
-          {
-            std::string stack_trace = e.info ();
-
-            if (! stack_trace.empty ())
-              std::cerr << stack_trace;
-
-            if (application::interactive ())
-              recover_from_exception ();
-            else
-              {
-                // We should exit with a nonzero status.
-                retval = 1;
-                break;
-              }
-          }
-        catch (const std::bad_alloc&)
-          {
-            recover_from_exception ();
-
-            std::cerr << "error: out of memory -- trying to return to prompt"
-                      << std::endl;
-          }
-
-#if defined (DBSTOP_NANINF)
-        if (Vdebug_on_naninf)
-          {
-            if (setjump (naninf_jump) != 0)
-              debug_or_throw_exception (true);  // true = stack trace
-          }
-#endif
-      }
-    while (retval == 0);
-
-    if (retval == EOF)
-      {
-        if (application::interactive ())
-          octave_stdout << "\n";
-
-        retval = 0;
-      }
-
-    return retval;
+    return m_evaluator.repl (application::interactive ());
   }
 
   // Call a function with exceptions handled to avoid problems with
--- a/libinterp/parse-tree/pt-eval.cc	Tue May 22 13:23:49 2018 -0400
+++ b/libinterp/parse-tree/pt-eval.cc	Tue May 22 15:23:50 2018 -0400
@@ -74,6 +74,124 @@
     m_nargout_stack.clear ();
   }
 
+  int tree_evaluator::repl (bool interactive)
+  {
+    int retval = 0;
+
+    lexer *lxr = (interactive ? new lexer () : new lexer (stdin));
+
+    parser parser (*lxr);
+
+    symbol_table& symtab = m_interpreter.get_symbol_table ();
+
+    do
+      {
+        try
+          {
+            reset_error_handler ();
+
+            parser.reset ();
+
+            if (symtab.at_top_level ())
+              reset_debug_state ();
+
+            retval = parser.run ();
+
+            if (retval == 0)
+              {
+                if (parser.m_stmt_list)
+                  {
+                    parser.m_stmt_list->accept (*this);
+
+                    octave_quit ();
+
+                    if (! interactive)
+                      {
+                        bool quit = (m_returning || m_breaking);
+
+                        if (m_returning)
+                          m_returning = 0;
+
+                        if (m_breaking)
+                          m_breaking--;
+
+                        if (quit)
+                          break;
+                      }
+
+                    if (octave_completion_matches_called)
+                      octave_completion_matches_called = false;
+                    else
+                      command_editor::increment_current_command_number ();
+                  }
+                else if (parser.m_lexer.m_end_of_input)
+                  {
+                    retval = EOF;
+                    break;
+                  }
+              }
+          }
+        catch (const interrupt_exception&)
+          {
+            m_interpreter.recover_from_exception ();
+
+            // Required newline when the user does Ctrl+C at the prompt.
+            if (interactive)
+              octave_stdout << "\n";
+          }
+        catch (const index_exception& e)
+          {
+            m_interpreter.recover_from_exception ();
+
+            std::cerr << "error: unhandled index exception: "
+                      << e.message () << " -- trying to return to prompt"
+                      << std::endl;
+          }
+        catch (const execution_exception& e)
+          {
+            std::string stack_trace = e.info ();
+
+            if (! stack_trace.empty ())
+              std::cerr << stack_trace;
+
+            if (interactive)
+              m_interpreter.recover_from_exception ();
+            else
+              {
+                // We should exit with a nonzero status.
+                retval = 1;
+                break;
+              }
+          }
+        catch (const std::bad_alloc&)
+          {
+            m_interpreter.recover_from_exception ();
+
+            std::cerr << "error: out of memory -- trying to return to prompt"
+                      << std::endl;
+          }
+
+#if defined (DBSTOP_NANINF)
+        if (Vdebug_on_naninf)
+          {
+            if (setjump (naninf_jump) != 0)
+              debug_or_throw_exception (true);  // true = stack trace
+          }
+#endif
+      }
+    while (retval == 0);
+
+    if (retval == EOF)
+      {
+        if (interactive)
+          octave_stdout << "\n";
+
+        retval = 0;
+      }
+
+    return retval;
+  }
+
   void
   tree_evaluator::visit_anon_fcn_handle (tree_anon_fcn_handle& anon_fh)
   {
--- a/libinterp/parse-tree/pt-eval.h	Tue May 22 13:23:49 2018 -0400
+++ b/libinterp/parse-tree/pt-eval.h	Tue May 22 15:23:50 2018 -0400
@@ -150,6 +150,8 @@
 
     void reset (void);
 
+    int repl (bool interactive);
+
     void visit_anon_fcn_handle (tree_anon_fcn_handle&);
 
     void visit_argument_list (tree_argument_list&);
@@ -466,10 +468,31 @@
 
     int breaking (void) const { return m_breaking; }
 
+    int breaking (int n)
+    {
+      int val = m_breaking;
+      m_breaking = n;
+      return val;
+    }
+
     int continuing (void) const { return m_continuing; }
 
+    int continuing (int n)
+    {
+      int val = m_continuing;
+      m_continuing = n;
+      return val;
+    }
+
     int returning (void) const { return m_returning; }
 
+    int returning (int n)
+    {
+      int val = m_returning;
+      m_returning = n;
+      return val;
+    }
+
     octave_value echo (const octave_value_list& args, int nargout);
 
     int echo (void) const { return m_echo; }