GNU Octave  8.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-2023 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;
85 
86  if (af_scope)
87  new_scope = af_scope.dup ();
88 
89  // FIXME: if new scope is nullptr, then we are in big trouble here...
90 
91  tree_anon_fcn_handle *new_afh = new
92  tree_anon_fcn_handle (param_list ? param_list->dup (new_scope) : nullptr,
93  expr ? expr->dup (new_scope) : nullptr,
94  new_scope, af_parent_scope, line (), column ());
95 
96  new_afh->copy_base (*this);
97 
98  return new_afh;
99 }
100 
103 {
104  return tw.evaluate_anon_fcn_handle (*this);
105 }
106 
108 
109 /*
110 %!function r = __f2 (f, x)
111 %! r = f (x);
112 %!endfunction
113 %!function f = __f1 (k)
114 %! f = @(x) __f2 (@(y) y-k, x);
115 %!endfunction
116 
117 %!assert ((__f1 (3)) (10) == 7)
118 
119 %!test
120 %! g = @(t) feval (@(x) t*x, 2);
121 %! assert (g(0.5) == 1);
122 
123 %!test
124 %! h = @(x) sin (x);
125 %! g = @(f, x) h (x);
126 %! f = @() g (@(x) h, pi);
127 %! assert (f () == sin (pi));
128 
129 The next two tests are intended to test parsing of a character string
130 vs. hermitian operator at the beginning of an anonymous function
131 expression. The use of ' for the character string and the spacing is
132 intentional, so don't change it.
133 
134 %!test
135 %! f = @() 'foo';
136 %! assert (f (), 'foo');
137 
138 %!test
139 %! f = @()'foo';
140 %! assert (f (), 'foo');
141 */
OCTAVE_END_NAMESPACE(octave)
symbol_scope dup(void) const
Definition: symscope.h:457
octave_value evaluate(tree_evaluator &tw, int nargout=1)
symbol_scope m_scope
symbol_scope m_parent_scope
tree_expression * expression(void) const
tree_anon_fcn_handle(int l=-1, int c=-1)
tree_parameter_list * m_parameter_list
tree_parameter_list * parameter_list(void) const
tree_expression * dup(symbol_scope &scope) const
tree_expression * m_expression
octave_value evaluate_anon_fcn_handle(tree_anon_fcn_handle &afh)
Definition: pt-eval.cc:3329
octave_value make_fcn_handle(const std::string &nm)
Definition: pt-eval.cc:1605
virtual tree_expression * dup(symbol_scope &scope) const =0
virtual void copy_base(const tree_expression &e)
Definition: pt-exp.h:132
tree_expression * dup(symbol_scope &scope) const
std::string m_name
Definition: pt-fcn-handle.h:93
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(void) const
Definition: pt.h:62
virtual int line(void) const
Definition: pt.h:60
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn