GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
pt-fcn-handle.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2003-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <ostream>
31 
32 #include "interpreter-private.h"
33 #include "pt-anon-scopes.h"
34 #include "pt-fcn-handle.h"
35 #include "stack-frame.h"
36 
38 
39 void
40 tree_fcn_handle::print (std::ostream& os, bool pr_as_read_syntax,
41  bool pr_orig_text)
42 {
43  print_raw (os, pr_as_read_syntax, pr_orig_text);
44 }
45 
46 void
47 tree_fcn_handle::print_raw (std::ostream& os, bool pr_as_read_syntax,
48  bool pr_orig_text)
49 {
50  os << ((pr_as_read_syntax || pr_orig_text) ? "@" : "") << m_name;
51 }
52 
55 {
56  tree_fcn_handle *new_fh = new tree_fcn_handle (m_name, line (), column ());
57 
58  new_fh->copy_base (*this);
59 
60  return new_fh;
61 }
62 
65 {
66  return tw.make_fcn_handle (m_name);
67 }
68 
70 {
71  delete m_parameter_list;
72  delete m_expression;
73 }
74 
77 {
78  tree_parameter_list *param_list = parameter_list ();
79  tree_expression *expr = expression ();
80 
81  symbol_scope af_scope = m_scope;
82  symbol_scope af_parent_scope = m_parent_scope;
83 
84  symbol_scope new_scope = af_scope ? af_scope.dup () : symbol_scope::invalid ();
85 
86  // FIXME: if new scope is nullptr, then we are in big trouble here...
87 
88  tree_anon_fcn_handle *new_afh = new
89  tree_anon_fcn_handle (param_list ? param_list->dup (new_scope) : nullptr,
90  expr ? expr->dup (new_scope) : nullptr,
91  new_scope, af_parent_scope, line (), column ());
92 
93  new_afh->copy_base (*this);
94 
95  return new_afh;
96 }
97 
100 {
101  return tw.evaluate_anon_fcn_handle (*this);
102 }
103 
104 OCTAVE_END_NAMESPACE(octave)
105 
106 /*
107 %!function r = __f2 (f, x)
108 %! r = f (x);
109 %!endfunction
110 %!function f = __f1 (k)
111 %! f = @(x) __f2 (@(y) y-k, x);
112 %!endfunction
113 
114 %!assert ((__f1 (3)) (10) == 7)
115 
116 %!test
117 %! g = @(t) feval (@(x) t*x, 2);
118 %! assert (g(0.5) == 1);
119 
120 %!test
121 %! h = @(x) sin (x);
122 %! g = @(f, x) h (x);
123 %! f = @() g (@(x) h, pi);
124 %! assert (f () == sin (pi));
125 
126 The next two tests are intended to test parsing of a character string
127 vs. hermitian operator at the beginning of an anonymous function
128 expression. The use of ' for the character string and the spacing is
129 intentional, so don't change it.
130 
131 %!test
132 %! f = @() 'foo';
133 %! assert (f (), 'foo');
134 
135 %!test
136 %! f = @()'foo';
137 %! assert (f (), 'foo');
138 */
static symbol_scope invalid()
Definition: symscope.h:400
symbol_scope dup() const
Definition: symscope.h:472
octave_value evaluate(tree_evaluator &tw, int nargout=1)
tree_anon_fcn_handle(int l=-1, int c=-1)
Definition: pt-fcn-handle.h:96
tree_parameter_list * parameter_list() const
tree_expression * expression() const
tree_expression * dup(symbol_scope &scope) const
octave_value evaluate_anon_fcn_handle(tree_anon_fcn_handle &afh)
Definition: pt-eval.cc:3351
octave_value make_fcn_handle(const std::string &nm)
Definition: pt-eval.cc:1603
virtual tree_expression * dup(symbol_scope &scope) const =0
virtual void copy_base(const tree_expression &e)
Definition: pt-exp.h:128
tree_expression * dup(symbol_scope &scope) const
tree_fcn_handle(int l=-1, int c=-1)
Definition: pt-fcn-handle.h:52
octave_value evaluate(tree_evaluator &tw, int nargout=1)
void print(std::ostream &os, bool pr_as_read_syntax=false, bool pr_orig_txt=true)
void print_raw(std::ostream &os, bool pr_as_read_syntax=false, bool pr_orig_txt=true)
tree_parameter_list * dup(symbol_scope &scope) const
Definition: pt-misc.cc:69
virtual int column() const
Definition: pt.h:58
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn