GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
pt-fcn-handle.h
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 (octave_pt_fcn_handle_h)
27 #define octave_pt_fcn_handle_h 1
28 
29 #include "octave-config.h"
30 
31 #include <iosfwd>
32 #include <string>
33 
34 #include "pt-bp.h"
35 #include "pt-exp.h"
36 #include "pt-misc.h"
37 #include "pt-stmt.h"
38 #include "pt-walk.h"
39 #include "symscope.h"
40 
41 class octave_value_list;
42 
43 #include "ov.h"
44 #include "ov-usr-fcn.h"
45 
47 
49 {
50 public:
51 
52  tree_fcn_handle (int l = -1, int c = -1)
53  : tree_expression (l, c), m_name () { }
54 
55  tree_fcn_handle (const std::string& n, int l = -1, int c = -1)
56  : tree_expression (l, c), m_name (n) { }
57 
58  OCTAVE_DISABLE_COPY_MOVE (tree_fcn_handle)
59 
60  ~tree_fcn_handle () = default;
61 
62  void print (std::ostream& os, bool pr_as_read_syntax = false,
63  bool pr_orig_txt = true);
64 
65  void print_raw (std::ostream& os, bool pr_as_read_syntax = false,
66  bool pr_orig_txt = true);
67 
68  std::string name () const { return m_name; }
69 
70  bool rvalue_ok () const { return true; }
71 
72  tree_expression * dup (symbol_scope& scope) const;
73 
74  octave_value evaluate (tree_evaluator& tw, int nargout = 1);
75 
77  {
78  return ovl (evaluate (tw, nargout));
79  }
80 
81  void accept (tree_walker& tw)
82  {
83  tw.visit_fcn_handle (*this);
84  }
85 
86 private:
87 
88  // The name of this function handle.
89  std::string m_name;
90 };
91 
93 {
94 public:
95 
96  tree_anon_fcn_handle (int l = -1, int c = -1)
97  : tree_expression (l, c), m_parameter_list (nullptr),
98  m_expression (nullptr), m_scope (symbol_scope::anonymous ()),
99  m_parent_scope (symbol_scope::invalid ()), m_file_name ()
100  { }
101 
103  const symbol_scope& scope,
104  const symbol_scope& parent_scope,
105  int l = -1, int c = -1)
106  : tree_expression (l, c), m_parameter_list (pl), m_expression (ex),
107  m_scope (scope), m_parent_scope (parent_scope), m_file_name ()
108  { }
109 
110  OCTAVE_DISABLE_COPY_MOVE (tree_anon_fcn_handle)
111 
113 
114  bool rvalue_ok () const { return true; }
115 
117  {
118  return m_parameter_list;
119  }
120 
121  tree_expression * expression () const { return m_expression; }
122 
123  symbol_scope scope () const { return m_scope; }
124 
125  symbol_scope parent_scope () const { return m_parent_scope; }
126 
127  bool has_parent_scope () const { return m_parent_scope.is_valid (); }
128 
130 
131  octave_value evaluate (tree_evaluator& tw, int nargout = 1);
132 
134  {
135  return ovl (evaluate (tw, nargout));
136  }
137 
138  void accept (tree_walker& tw) { tw.visit_anon_fcn_handle (*this); }
139 
140  void stash_file_name (const std::string& file) { m_file_name = file; }
141 
142  std::string file_name () const { return m_file_name; }
143 
144 private:
145 
146  // Inputs parameters.
147  tree_parameter_list *m_parameter_list;
148 
149  // Function body, limited to a single expression.
150  tree_expression *m_expression;
151 
152  // Function scope.
153  symbol_scope m_scope;
154 
155  // Parent scope, or an invalid scope if none.
156  symbol_scope m_parent_scope;
157 
158  // Filename where the handle was defined.
159  std::string m_file_name;
160 };
161 
162 OCTAVE_END_NAMESPACE(octave)
163 
164 #endif
bool is_valid() const
Definition: symscope.h:410
octave_value evaluate(tree_evaluator &tw, int nargout=1)
symbol_scope scope() const
symbol_scope parent_scope() const
bool has_parent_scope() const
bool rvalue_ok() const
tree_anon_fcn_handle(int l=-1, int c=-1)
Definition: pt-fcn-handle.h:96
octave_value_list evaluate_n(tree_evaluator &tw, int nargout=1)
tree_anon_fcn_handle(tree_parameter_list *pl, tree_expression *ex, const symbol_scope &scope, const symbol_scope &parent_scope, int l=-1, int c=-1)
void accept(tree_walker &tw)
tree_parameter_list * parameter_list() const
std::string file_name() const
tree_expression * expression() const
void stash_file_name(const std::string &file)
tree_expression * dup(symbol_scope &scope) const
std::string name() const
Definition: pt-fcn-handle.h:68
tree_fcn_handle(int l=-1, int c=-1)
Definition: pt-fcn-handle.h:52
~tree_fcn_handle()=default
bool rvalue_ok() const
Definition: pt-fcn-handle.h:70
tree_fcn_handle(const std::string &n, int l=-1, int c=-1)
Definition: pt-fcn-handle.h:55
octave_value_list evaluate_n(tree_evaluator &tw, int nargout=1)
Definition: pt-fcn-handle.h:76
void accept(tree_walker &tw)
Definition: pt-fcn-handle.h:81
virtual void visit_fcn_handle(tree_fcn_handle &)
Definition: pt-walk.cc:509
virtual void visit_anon_fcn_handle(tree_anon_fcn_handle &)
Definition: pt-walk.cc:34
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
octave_idx_type n
Definition: mx-inlines.cc:761
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:219