comparison libinterp/parse-tree/oct-parse.yy @ 27020:30e9204de313

move feval functions to interpreter class * interpreter.h, interpreter.cc (interpreter::feval): New functions created from global feval functions in oct-parse.yy. Change all uses. * oct-parse.yy (feval): Forward to interpreter::feval functions.
author John W. Eaton <jwe@octave.org>
date Mon, 01 Apr 2019 13:57:10 +0000
parents 6cb675912f2b
children eb522480d44c
comparison
equal deleted inserted replaced
27019:6cb675912f2b 27020:30e9204de313
52 #include "oct-time.h" 52 #include "oct-time.h"
53 #include "quit.h" 53 #include "quit.h"
54 54
55 #include "Cell.h" 55 #include "Cell.h"
56 #include "builtin-defun-decls.h" 56 #include "builtin-defun-decls.h"
57 #include "call-stack.h"
58 #include "defun.h" 57 #include "defun.h"
59 #include "dirfns.h" 58 #include "dirfns.h"
60 #include "dynamic-ld.h" 59 #include "dynamic-ld.h"
61 #include "error.h" 60 #include "error.h"
62 #include "input.h" 61 #include "input.h"
4779 //! necessarily the same as @c nargout. 4778 //! necessarily the same as @c nargout.
4780 4779
4781 octave_value_list 4780 octave_value_list
4782 feval (const char *name, const octave_value_list& args, int nargout) 4781 feval (const char *name, const octave_value_list& args, int nargout)
4783 { 4782 {
4784 return feval (std::string (name), args, nargout); 4783 interpreter& interp = __get_interpreter__ ("feval");
4784
4785 return interp.feval (name, args, nargout);
4785 } 4786 }
4786 4787
4787 octave_value_list 4788 octave_value_list
4788 feval (const std::string& name, const octave_value_list& args, int nargout) 4789 feval (const std::string& name, const octave_value_list& args, int nargout)
4789 { 4790 {
4790 octave_value_list retval; 4791 interpreter& interp = __get_interpreter__ ("feval");
4791 4792
4792 symbol_table& symtab = __get_symbol_table__ ("feval"); 4793 return interp.feval (name, args, nargout);
4793
4794 octave_value fcn = symtab.find_function (name, args);
4795
4796 if (fcn.is_defined ())
4797 {
4798 tree_evaluator& tw = __get_evaluator__ ("feval");
4799
4800 octave_function *of = fcn.function_value ();
4801
4802 retval = of->call (tw, nargout, args);
4803 }
4804 else
4805 error ("feval: function '%s' not found", name.c_str ());
4806
4807 return retval;
4808 } 4794 }
4809 4795
4810 octave_value_list 4796 octave_value_list
4811 feval (octave_function *fcn, const octave_value_list& args, int nargout) 4797 feval (octave_function *fcn, const octave_value_list& args, int nargout)
4812 { 4798 {
4813 octave_value_list retval; 4799 interpreter& interp = __get_interpreter__ ("feval");
4814 4800
4815 if (fcn) 4801 return interp.feval (fcn, args, nargout);
4816 {
4817 tree_evaluator& tw = __get_evaluator__ ("feval");
4818
4819 retval = fcn->call (tw, nargout, args);
4820 }
4821
4822 return retval;
4823 } 4802 }
4824 4803
4825 octave_value_list 4804 octave_value_list
4826 feval (const octave_value& val, const octave_value_list& args, int nargout) 4805 feval (const octave_value& val, const octave_value_list& args, int nargout)
4827 { 4806 {
4828 // FIXME: do we really want to silently return an empty ovl if 4807 interpreter& interp = __get_interpreter__ ("feval");
4829 // the function object is undefined? It's essentially what the 4808
4830 // version above that accepts a pointer to an octave_function 4809 return interp.feval (val, args, nargout);
4831 // object does and some code was apparently written to rely on it 4810 }
4832 // (for example, __ode15__).
4833
4834 if (val.is_undefined ())
4835 return ovl ();
4836
4837 if (val.is_function ())
4838 {
4839 return feval (val.function_value (), args, nargout);
4840 }
4841 else if (val.is_function_handle ())
4842 {
4843 // This covers function handles, inline functions, and anonymous
4844 // functions.
4845
4846 std::list<octave_value_list> arg_list;
4847 arg_list.push_back (args);
4848
4849 // FIXME: could we make octave_value::subsref a const method?
4850 // It would be difficult because there are instances of
4851 // incrementing the reference count inside subsref methods,
4852 // which means they can't be const with the current way of
4853 // handling reference counting.
4854
4855 octave_value xval = val;
4856 return xval.subsref ("(", arg_list, nargout);
4857 }
4858 else if (val.is_string ())
4859 {
4860 return feval (val.string_value (), args, nargout);
4861 }
4862 else
4863 error ("feval: first argument must be a string, inline function, or a function handle");
4864
4865 return ovl ();
4866 }
4867
4868 static octave_value_list
4869 get_feval_args (const octave_value_list& args)
4870 {
4871 return args.slice (1, args.length () - 1, true);
4872 }
4873
4874 //! Evaluate an Octave function (built-in or interpreted) and return
4875 //! the list of result values.
4876 //!
4877 //! @param args The first element of @c args is the function to call.
4878 //! It may be the name of the function as a string, a function
4879 //! handle, or an inline function. The remaining arguments are
4880 //! passed to the function.
4881 //! @param nargout The number of output arguments expected.
4882 //! @return A list of output values. The length of the list is not
4883 //! necessarily the same as @c nargout.
4884 4811
4885 octave_value_list 4812 octave_value_list
4886 feval (const octave_value_list& args, int nargout) 4813 feval (const octave_value_list& args, int nargout)
4887 { 4814 {
4888 if (args.length () > 0) 4815 interpreter& interp = __get_interpreter__ ("feval");
4889 { 4816
4890 octave_value f_arg = args(0); 4817 return interp.feval (args, nargout);
4891
4892 octave_value_list tmp_args = get_feval_args (args);
4893
4894 return feval (f_arg, tmp_args, nargout);
4895 }
4896 else
4897 error ("feval: first argument must be a string, inline function, or a function handle");
4898
4899 return ovl ();
4900 } 4818 }
4901 } 4819 }
4902 4820
4903 DEFUN (feval, args, nargout, 4821 DEFMETHOD (feval, interp, args, nargout,
4904 doc: /* -*- texinfo -*- 4822 doc: /* -*- texinfo -*-
4905 @deftypefn {} {} feval (@var{name}, @dots{}) 4823 @deftypefn {} {} feval (@var{name}, @dots{})
4906 Evaluate the function named @var{name}. 4824 Evaluate the function named @var{name}.
4907 4825
4908 Any arguments after the first are passed as inputs to the named function. 4826 Any arguments after the first are passed as inputs to the named function.
4909 For example, 4827 For example,
4941 @end deftypefn */) 4859 @end deftypefn */)
4942 { 4860 {
4943 if (args.length () == 0) 4861 if (args.length () == 0)
4944 print_usage (); 4862 print_usage ();
4945 4863
4946 return octave::feval (args, nargout); 4864 return interp.feval (args, nargout);
4947 } 4865 }
4948 4866
4949 DEFMETHOD (builtin, interp, args, nargout, 4867 DEFMETHOD (builtin, interp, args, nargout,
4950 doc: /* -*- texinfo -*- 4868 doc: /* -*- texinfo -*-
4951 @deftypefn {} {[@dots{}] =} builtin (@var{f}, @dots{}) 4869 @deftypefn {} {[@dots{}] =} builtin (@var{f}, @dots{})
4982 octave::symbol_table& symtab = interp.get_symbol_table (); 4900 octave::symbol_table& symtab = interp.get_symbol_table ();
4983 4901
4984 octave_value fcn = symtab.builtin_find (name); 4902 octave_value fcn = symtab.builtin_find (name);
4985 4903
4986 if (fcn.is_defined ()) 4904 if (fcn.is_defined ())
4987 retval = octave::feval (fcn.function_value (), args.splice (0, 1), nargout); 4905 retval = interp.feval (fcn.function_value (), args.splice (0, 1), nargout);
4988 else 4906 else
4989 error ("builtin: lookup for symbol '%s' failed", name.c_str ()); 4907 error ("builtin: lookup for symbol '%s' failed", name.c_str ());
4990 4908
4991 return retval; 4909 return retval;
4992 } 4910 }