pt-fcn-handle.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2003-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #include <iostream>
00028 
00029 #include "error.h"
00030 #include "oct-obj.h"
00031 #include "ov-fcn-handle.h"
00032 #include "pt-fcn-handle.h"
00033 #include "pager.h"
00034 #include "pt-const.h"
00035 #include "pt-walk.h"
00036 #include "variables.h"
00037 
00038 void
00039 tree_fcn_handle::print (std::ostream& os, bool pr_as_read_syntax,
00040                         bool pr_orig_text)
00041 {
00042   print_raw (os, pr_as_read_syntax, pr_orig_text);
00043 }
00044 
00045 void
00046 tree_fcn_handle::print_raw (std::ostream& os, bool pr_as_read_syntax,
00047                             bool pr_orig_text)
00048 {
00049   os << ((pr_as_read_syntax || pr_orig_text) ? "@" : "") << nm;
00050 }
00051 
00052 octave_value
00053 tree_fcn_handle::rvalue1 (int)
00054 {
00055   return make_fcn_handle (nm);
00056 }
00057 
00058 octave_value_list
00059 tree_fcn_handle::rvalue (int nargout)
00060 {
00061   octave_value_list retval;
00062 
00063   if (nargout > 1)
00064     error ("invalid number of output arguments for function handle expression");
00065   else
00066     retval = rvalue1 (nargout);
00067 
00068   return retval;
00069 }
00070 
00071 tree_expression *
00072 tree_fcn_handle::dup (symbol_table::scope_id,
00073                       symbol_table::context_id) const
00074 {
00075   tree_fcn_handle *new_fh = new tree_fcn_handle (nm, line (), column ());
00076 
00077   new_fh->copy_base (*this);
00078 
00079   return new_fh;
00080 }
00081 
00082 void
00083 tree_fcn_handle::accept (tree_walker& tw)
00084 {
00085   tw.visit_fcn_handle (*this);
00086 }
00087 
00088 octave_value
00089 tree_anon_fcn_handle::rvalue1 (int)
00090 {
00091   // FIXME -- should CMD_LIST be limited to a single expression?
00092   // I think that is what Matlab does.
00093 
00094   tree_parameter_list *param_list = parameter_list ();
00095   tree_parameter_list *ret_list = return_list ();
00096   tree_statement_list *cmd_list = body ();
00097   symbol_table::scope_id this_scope = scope ();
00098 
00099   symbol_table::scope_id new_scope = symbol_table::dup_scope (this_scope);
00100 
00101   if (new_scope > 0)
00102     symbol_table::inherit (new_scope, symbol_table::current_scope (),
00103                            symbol_table::current_context ());
00104 
00105   octave_user_function *uf
00106     = new octave_user_function (new_scope,
00107                                 param_list ? param_list->dup (new_scope, 0) : 0,
00108                                 ret_list ? ret_list->dup (new_scope, 0) : 0,
00109                                 cmd_list ? cmd_list->dup (new_scope, 0) : 0);
00110 
00111   octave_function *curr_fcn = octave_call_stack::current ();
00112 
00113   if (curr_fcn)
00114     {
00115       // FIXME -- maybe it would be better to just stash curr_fcn
00116       // instead of individual bits of info about it?
00117 
00118       uf->stash_parent_fcn_name (curr_fcn->name ());
00119       uf->stash_dir_name (curr_fcn->dir_name ());
00120 
00121       symbol_table::scope_id parent_scope = curr_fcn->parent_fcn_scope ();
00122 
00123       if (parent_scope < 0)
00124         parent_scope = curr_fcn->scope ();
00125 
00126       uf->stash_parent_fcn_scope (parent_scope);
00127 
00128       if (curr_fcn->is_class_method () || curr_fcn->is_class_constructor ())
00129         uf->stash_dispatch_class (curr_fcn->dispatch_class ());
00130     }
00131 
00132   uf->mark_as_anonymous_function ();
00133   uf->stash_fcn_file_name (file_name);
00134   uf->stash_fcn_location (line (), column ());
00135 
00136   octave_value ov_fcn (uf);
00137 
00138   octave_value fh (octave_fcn_binder::maybe_binder (ov_fcn));
00139 
00140   return fh;
00141 }
00142 
00143 /*
00144 %!function r = __f2 (f, x)
00145 %!  r = f (x);
00146 %!endfunction
00147 %!function f = __f1 (k)
00148 %!  f = @(x) __f2 (@(y) y-k, x);
00149 %!endfunction
00150 
00151 %!assert ((__f1 (3)) (10) == 7)
00152 
00153 %!test
00154 %! g = @(t) feval (@(x) t*x, 2);
00155 %! assert (g(0.5) == 1);
00156 
00157 %!test
00158 %! h = @(x) sin (x);
00159 %! g = @(f, x) h (x);
00160 %! f = @() g (@(x) h, pi);
00161 %! assert (f () == sin (pi));
00162 */
00163 
00164 octave_value_list
00165 tree_anon_fcn_handle::rvalue (int nargout)
00166 {
00167   octave_value_list retval;
00168 
00169   if (nargout > 1)
00170     error ("invalid number of output arguments for anonymous function handle expression");
00171   else
00172     retval = rvalue1 (nargout);
00173 
00174   return retval;
00175 }
00176 
00177 tree_expression *
00178 tree_anon_fcn_handle::dup (symbol_table::scope_id,
00179                            symbol_table::context_id) const
00180 {
00181   tree_parameter_list *param_list = parameter_list ();
00182   tree_parameter_list *ret_list = return_list ();
00183   tree_statement_list *cmd_list = body ();
00184   symbol_table::scope_id this_scope = scope ();
00185 
00186   symbol_table::scope_id new_scope = symbol_table::dup_scope (this_scope);
00187 
00188   if (new_scope > 0)
00189     symbol_table::inherit (new_scope, symbol_table::current_scope (),
00190                            symbol_table::current_context ());
00191 
00192   tree_anon_fcn_handle *new_afh = new
00193     tree_anon_fcn_handle (param_list ? param_list->dup (new_scope, 0) : 0,
00194                           ret_list ? ret_list->dup (new_scope, 0) : 0,
00195                           cmd_list ? cmd_list->dup (new_scope, 0) : 0,
00196                           new_scope, line (), column ());
00197 
00198   new_afh->copy_base (*this);
00199 
00200   return new_afh;
00201 }
00202 
00203 void
00204 tree_anon_fcn_handle::accept (tree_walker& tw)
00205 {
00206   tw.visit_anon_fcn_handle (*this);
00207 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines