GNU Octave 11.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
pt-fcn-handle.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2003-2026 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
42
43#include "ov.h"
44#include "ov-usr-fcn.h"
45
47
48class OCTINTERP_API tree_fcn_handle : public tree_expression
49{
50public:
51
52 tree_fcn_handle (const token& tok) : m_token (tok), m_name (m_token.text ()) { }
53
54 OCTAVE_DISABLE_COPY_MOVE (tree_fcn_handle)
55
56 ~tree_fcn_handle () = default;
57
58 filepos beg_pos () const { return m_token.beg_pos (); }
59 filepos end_pos () const { return m_token.end_pos (); }
60
61 comment_list leading_comments () const { return m_token.leading_comments (); }
62 comment_list trailing_comments () const { return m_token.trailing_comments (); }
63
64 void print (std::ostream& os, bool pr_as_read_syntax = false,
65 bool pr_orig_txt = true);
66
67 void print_raw (std::ostream& os, bool pr_as_read_syntax = false,
68 bool pr_orig_txt = true);
69
70 token at_token () const { return m_token; }
71
72 std::string name () const { return m_name; }
73
74 bool rvalue_ok () const { return true; }
75
76 tree_expression * dup (symbol_scope& scope) const;
77
78 octave_value evaluate (tree_evaluator& tw, int nargout = 1);
79
81 {
82 return ovl (evaluate (tw, nargout));
83 }
84
86 {
87 tw.visit_fcn_handle (*this);
88 }
89
90private:
91
92 token m_token;
93
94 // The name of this function handle.
95 std::string m_name;
96};
97
98class OCTINTERP_API tree_anon_fcn_handle : public tree_expression
99{
100public:
101
102 tree_anon_fcn_handle (const token& at_tok, tree_parameter_list *pl, tree_expression *ex, const symbol_scope& scope, const symbol_scope& parent_scope)
103 : m_at_tok (at_tok), m_parameter_list (pl), m_expression (ex), m_scope (scope), m_parent_scope (parent_scope)
104 { }
105
106 OCTAVE_DISABLE_COPY_MOVE (tree_anon_fcn_handle)
107
109
110 filepos beg_pos () const { return m_at_tok.beg_pos (); }
111 filepos end_pos () const { return m_expression->end_pos (); }
112
113 comment_list leading_comments () const { return m_at_tok.leading_comments (); }
114 comment_list trailing_comments () const { return m_expression->trailing_comments (); }
115
116 bool rvalue_ok () const { return true; }
117
118 token at_token () const { return m_at_tok; }
119
121 {
122 return m_parameter_list;
123 }
124
125 tree_expression * expression () const { return m_expression; }
126
127 symbol_scope scope () const { return m_scope; }
128
129 symbol_scope parent_scope () const { return m_parent_scope; }
130
131 bool has_parent_scope () const { return m_parent_scope.is_valid (); }
132
133 tree_expression * dup (symbol_scope& scope) const;
134
135 octave_value evaluate (tree_evaluator& tw, int nargout = 1);
136
138 {
139 return ovl (evaluate (tw, nargout));
140 }
141
142 void accept (tree_walker& tw) { tw.visit_anon_fcn_handle (*this); }
143
144 void stash_file_name (const std::string& file) { m_file_name = file; }
145
146 std::string file_name () const { return m_file_name; }
147
148private:
149
150 token m_at_tok;
151
152 // Inputs parameters.
153 tree_parameter_list *m_parameter_list;
154
155 // Function body, limited to a single expression.
156 tree_expression *m_expression;
157
158 // Function scope.
159 symbol_scope m_scope;
160
161 // Parent scope, or an invalid scope if none.
162 symbol_scope m_parent_scope;
163
164 // Filename where the handle was defined.
165 std::string m_file_name;
166};
167
168OCTAVE_END_NAMESPACE(octave)
169
170#endif
Definition token.h:42
comment_list leading_comments() const
tree_parameter_list * parameter_list() const
symbol_scope scope() const
symbol_scope parent_scope() const
filepos beg_pos() const
bool has_parent_scope() const
comment_list trailing_comments() const
octave_value_list evaluate_n(tree_evaluator &tw, int nargout=1)
tree_anon_fcn_handle(const token &at_tok, tree_parameter_list *pl, tree_expression *ex, const symbol_scope &scope, const symbol_scope &parent_scope)
token at_token() const
tree_expression * expression() const
filepos end_pos() const
void accept(tree_walker &tw)
std::string file_name() const
void stash_file_name(const std::string &file)
virtual octave_value evaluate(tree_evaluator &tw, int nargout=1)=0
virtual tree_expression * dup(symbol_scope &scope) const =0
tree_fcn_handle(const token &tok)
std::string name() const
comment_list leading_comments() const
~tree_fcn_handle()=default
bool rvalue_ok() const
filepos beg_pos() const
token at_token() const
comment_list trailing_comments() const
octave_value_list evaluate_n(tree_evaluator &tw, int nargout=1)
filepos end_pos() const
void accept(tree_walker &tw)
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_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition ovl.h:217