1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2003-2021 The Octave Project Developers
//
// See the file COPYRIGHT.md in the top-level directory of this
// distribution or <https://octave.org/copyright/>.
//
// This file is part of Octave.
//
// Octave is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Octave is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Octave; see the file COPYING. If not, see
// <https://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////
#if defined (HAVE_CONFIG_H)
# include "config.h"
#endif
#include <ostream>
#include "interpreter-private.h"
#include "pt-anon-scopes.h"
#include "pt-fcn-handle.h"
#include "stack-frame.h"
namespace octave
{
void
tree_fcn_handle::print (std::ostream& os, bool pr_as_read_syntax,
bool pr_orig_text)
{
print_raw (os, pr_as_read_syntax, pr_orig_text);
}
void
tree_fcn_handle::print_raw (std::ostream& os, bool pr_as_read_syntax,
bool pr_orig_text)
{
os << ((pr_as_read_syntax || pr_orig_text) ? "@" : "") << m_name;
}
tree_expression *
tree_fcn_handle::dup (symbol_scope&) const
{
tree_fcn_handle *new_fh = new tree_fcn_handle (m_name, line (), column ());
new_fh->copy_base (*this);
return new_fh;
}
octave_value
tree_fcn_handle::evaluate (tree_evaluator& tw, int)
{
return tw.make_fcn_handle (m_name);
}
tree_anon_fcn_handle::~tree_anon_fcn_handle (void)
{
delete m_parameter_list;
delete m_expression;
}
tree_expression *
tree_anon_fcn_handle::dup (symbol_scope&) const
{
tree_parameter_list *param_list = parameter_list ();
tree_expression *expr = expression ();
symbol_scope af_scope = m_scope;
symbol_scope af_parent_scope = m_parent_scope;
symbol_scope new_scope;
if (af_scope)
new_scope = af_scope.dup ();
// FIXME: if new scope is nullptr, then we are in big trouble here...
tree_anon_fcn_handle *new_afh = new
tree_anon_fcn_handle (param_list ? param_list->dup (new_scope) : nullptr,
expr ? expr->dup (new_scope) : nullptr,
new_scope, af_parent_scope, line (), column ());
new_afh->copy_base (*this);
return new_afh;
}
octave_value
tree_anon_fcn_handle::evaluate (tree_evaluator& tw, int)
{
// FIXME: should CMD_LIST be limited to a single expression?
// I think that is what Matlab does.
symbol_scope new_scope;
if (m_scope)
new_scope = m_scope.dup ();
tree_parameter_list *param_list_dup
= m_parameter_list ? m_parameter_list->dup (new_scope) : nullptr;
tree_parameter_list *ret_list = nullptr;
tree_statement_list *stmt_list = nullptr;
symbol_scope parent_scope = tw.get_current_scope ();
new_scope.set_parent (parent_scope);
new_scope.set_primary_parent (parent_scope);
if (m_expression)
{
tree_expression *expr_dup = m_expression->dup (new_scope);
tree_statement *stmt = new tree_statement (expr_dup, nullptr);
stmt_list = new tree_statement_list (stmt);
}
tree_anon_scopes anon_fcn_ctx (*this);
std::set<std::string> free_vars = anon_fcn_ctx.free_variables ();
stack_frame::local_vars_map local_vars;
call_stack& cs = tw.get_call_stack ();
std::shared_ptr<stack_frame> frame = cs.get_current_stack_frame ();
for (auto& name : free_vars)
{
octave_value val = frame->varval (name);
if (val.is_defined ())
local_vars[name] = val;
}
octave_user_function *af
= new octave_user_function (new_scope, param_list_dup, ret_list,
stmt_list);
octave_function *curr_fcn = cs.current_function ();
if (curr_fcn)
{
// FIXME: maybe it would be better to just stash curr_fcn
// instead of individual bits of info about it?
// An anonymous function defined inside another nested function
// or parent of a nested function also behaves like a nested
// function.
if (curr_fcn->is_parent_function () || curr_fcn->is_nested_function ())
{
af->mark_as_nested_function ();
new_scope.set_nesting_depth (parent_scope.nesting_depth () + 1);
}
af->stash_parent_fcn_name (curr_fcn->name ());
af->stash_dir_name (curr_fcn->dir_name ());
new_scope.cache_fcn_file_name (curr_fcn->fcn_file_name ());
new_scope.cache_dir_name (curr_fcn->dir_name ());
// The following is needed so that class method dispatch works
// properly for anonymous functions that wrap class methods.
if (curr_fcn->is_class_method () || curr_fcn->is_class_constructor ())
af->stash_dispatch_class (curr_fcn->dispatch_class ());
af->stash_fcn_file_name (curr_fcn->fcn_file_name ());
}
af->mark_as_anonymous_function ();
octave_value ov_fcn (af);
return octave_value (new octave_fcn_handle (ov_fcn, local_vars));
}
}
/*
%!function r = __f2 (f, x)
%! r = f (x);
%!endfunction
%!function f = __f1 (k)
%! f = @(x) __f2 (@(y) y-k, x);
%!endfunction
%!assert ((__f1 (3)) (10) == 7)
%!test
%! g = @(t) feval (@(x) t*x, 2);
%! assert (g(0.5) == 1);
%!test
%! h = @(x) sin (x);
%! g = @(f, x) h (x);
%! f = @() g (@(x) h, pi);
%! assert (f () == sin (pi));
The next two tests are intended to test parsing of a character string
vs. hermitian operator at the beginning of an anonymous function
expression. The use of ' for the character string and the spacing is
intentional, so don't change it.
%!test
%! f = @() 'foo';
%! assert (f (), 'foo');
%!test
%! f = @()'foo';
%! assert (f (), 'foo');
*/
|