changeset 27014:daab1b311a98

move core of assignin function to evaluator * pt-eval.h, pt-eval.cc (tree_evaluator::assignin): Move innter workings of assignin function here from oct-parse.yy. * interpreter.h, interpreter.cc (interpreter::assignin): New function. * oct-parse.yy (Fassignin): Extract arguments and call interpreter::assignin.
author John W. Eaton <jwe@octave.org>
date Sun, 31 Mar 2019 19:35:39 +0000
parents b37c1656aa42
children 4d9e1a832a55
files libinterp/corefcn/interpreter.cc libinterp/corefcn/interpreter.h libinterp/parse-tree/oct-parse.yy libinterp/parse-tree/pt-eval.cc libinterp/parse-tree/pt-eval.h
diffstat 5 files changed, 59 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/interpreter.cc	Tue Apr 02 14:49:58 2019 -0700
+++ b/libinterp/corefcn/interpreter.cc	Sun Mar 31 19:35:39 2019 +0000
@@ -1232,6 +1232,13 @@
     m_evaluator.assign (name, val);
   }
 
+  void interpreter::assignin (const std::string& context,
+                              const std::string& name,
+                              const octave_value& val)
+  {
+    m_evaluator.assignin (context, name, val);
+  }
+
   bool interpreter::at_top_level (void) const
   {
     return m_evaluator.at_top_level ();
--- a/libinterp/corefcn/interpreter.h	Tue Apr 02 14:49:58 2019 -0700
+++ b/libinterp/corefcn/interpreter.h	Sun Mar 31 19:35:39 2019 +0000
@@ -273,6 +273,9 @@
     void assign (const std::string& name,
                  const octave_value& val = octave_value ());
 
+    void assignin (const std::string& context, const std::string& varname, 
+                   const octave_value& val = octave_value ());
+
     bool at_top_level (void) const;
 
     bool isglobal (const std::string& name) const;
--- a/libinterp/parse-tree/oct-parse.yy	Tue Apr 02 14:49:58 2019 -0700
+++ b/libinterp/parse-tree/oct-parse.yy	Sun Mar 31 19:35:39 2019 +0000
@@ -5567,44 +5567,18 @@
 @seealso{evalin}
 @end deftypefn */)
 {
-  octave_value_list retval;
-
   if (args.length () != 3)
     print_usage ();
 
-  std::string context = args(0).xstring_value ("assignin: CONTEXT must be a string");
-
-  octave::unwind_protect frame;
-
-  octave::call_stack& cs = interp.get_call_stack ();
-
-  frame.add_method (cs, &octave::call_stack::restore_frame,
-                    cs.current_frame ());
-
-  if (context == "caller")
-    cs.goto_caller_frame ();
-  else if (context == "base")
-    cs.goto_base_frame ();
-  else
-    error ("assignin: CONTEXT must be \"caller\" or \"base\"");
-
-  std::string nm = args(1).xstring_value ("assignin: VARNAME must be a string");
-
-  if (octave::valid_identifier (nm))
-    {
-      // Put the check here so that we don't slow down assignments
-      // generally.  Any that go through Octave's parser should have
-      // already been checked.
-
-      if (octave::iskeyword (nm))
-        error ("assignin: invalid assignment to keyword '%s'", nm.c_str ());
-
-      interp.assign (nm, args(2));
-    }
-  else
-    error ("assignin: invalid variable name in argument VARNAME");
-
-  return retval;
+  std::string context
+    = args(0).xstring_value ("assignin: CONTEXT must be a string");
+
+  std::string varname
+    = args(1).xstring_value ("assignin: VARNAME must be a string");
+
+  interp.assignin (context, varname, args(2));
+
+  return octave_value_list ();
 }
 
 /*
@@ -5622,6 +5596,7 @@
 @seealso{eval, assignin}
 @end deftypefn */)
 {
+
   octave_value_list retval;
 
   int nargin = args.length ();
--- a/libinterp/parse-tree/pt-eval.cc	Tue Apr 02 14:49:58 2019 -0700
+++ b/libinterp/parse-tree/pt-eval.cc	Sun Mar 31 19:35:39 2019 +0000
@@ -900,6 +900,42 @@
   }
 
   void
+  tree_evaluator::assignin (const std::string& context,
+                            const std::string& name, const octave_value& val)
+  {
+    // FIXME: Can this be done without an unwind-protect frame, simply
+    // by geting a reference to the caller or base stack frame and
+    // calling assign on that?
+
+    octave::unwind_protect frame;
+
+    frame.add_method (m_call_stack, &call_stack::restore_frame,
+                      m_call_stack.current_frame ());
+
+    if (context == "caller")
+      m_call_stack.goto_caller_frame ();
+    else if (context == "base")
+      m_call_stack.goto_base_frame ();
+    else
+      error ("assignin: CONTEXT must be \"caller\" or \"base\"");
+
+    if (valid_identifier (name))
+      {
+        // Put the check here so that we don't slow down assignments
+        // generally.  Any that go through Octave's parser should have
+        // already been checked.
+
+        if (iskeyword (name))
+          error ("assignin: invalid assignment to keyword '%s'",
+                 name.c_str ());
+
+        assign (name, val);
+      }
+    else
+      error ("assignin: invalid variable name '%s'", name.c_str ());
+  }
+
+  void
   tree_evaluator::set_auto_fcn_var (stack_frame::auto_var_type avt,
                                     const octave_value& val)
   {
--- a/libinterp/parse-tree/pt-eval.h	Tue Apr 02 14:49:58 2019 -0700
+++ b/libinterp/parse-tree/pt-eval.h	Sun Mar 31 19:35:39 2019 +0000
@@ -405,6 +405,9 @@
     void assign (const std::string& name,
                  const octave_value& val = octave_value ());
 
+    void assignin (const std::string& context, const std::string& name,
+                   const octave_value& val = octave_value ());
+
     void set_auto_fcn_var (stack_frame::auto_var_type avt,
                            const octave_value& val = octave_value ());