GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
pt-select.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2025 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_select_h)
27#define octave_pt_select_h 1
28
29#include "octave-config.h"
30
31#include <list>
32
33#include "comment-list.h"
34#include "pt-cmd.h"
35#include "pt-stmt.h"
36#include "pt-walk.h"
37#include "token.h"
38
40
41class comment_list;
42class tree_expression;
44
45// If.
46
47class tree_if_clause : public tree
48{
49public:
50
52 : m_tok (tok), m_expr (e), m_list (sl)
53 { }
54
55 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (tree_if_clause)
56
58
59 token if_token () const { return m_tok; }
60
61 bool is_else_clause () { return ! m_expr; }
62
63 filepos beg_pos () const { return m_tok.beg_pos (); }
64 filepos end_pos () const { return m_list->end_pos (); }
65
66 tree_expression * condition () { return m_expr; }
67
68 tree_statement_list * commands () { return m_list; }
69
70 comment_list leading_comments () const { return m_tok.leading_comments (); }
71
73 {
74 tw.visit_if_clause (*this);
75 }
76
77private:
78
79 token m_tok;
80
81 // The condition to test.
82 tree_expression *m_expr = nullptr;
83
84 // The list of statements to evaluate if expr is true.
85 tree_statement_list *m_list;
86};
87
88class tree_if_command_list : public std::list<tree_if_clause *>
89{
90public:
91
93
94 tree_if_command_list (tree_if_clause *t) { push_back (t); }
95
96 OCTAVE_DISABLE_COPY_MOVE (tree_if_command_list)
97
99 {
100 while (! empty ())
101 {
102 auto p = begin ();
103 delete *p;
104 erase (p);
105 }
106 }
107
109 {
110 if (empty ())
111 return filepos ();
112
113 tree_if_clause *elt = front ();
114 return elt->beg_pos ();
115 }
116
118 {
119 if (empty ())
120 return filepos ();
121
122 tree_if_clause *elt = back ();
123 return elt->end_pos ();
124 }
125
127 {
128 if (! empty ())
129 {
130 tree_if_clause *p = front ();
131 return p->if_token ();
132 }
133
134 return token ();
135 }
136
138 {
139 tw.visit_if_command_list (*this);
140 }
141};
142
144{
145public:
146
147 tree_if_command (const token& if_tok, const token& end_tok)
148 : m_if_tok (if_tok), m_end_tok (end_tok)
149 { }
150
151 tree_if_command (const token& if_tok, tree_if_command_list *lst, const token& end_tok)
152 : m_if_tok (if_tok), m_list (lst), m_end_tok (end_tok)
153 { }
154
155 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (tree_if_command)
156
158
159 filepos beg_pos () const { return m_if_tok.beg_pos (); }
160 filepos end_pos () const { return m_end_tok.end_pos (); }
161
162 tree_if_command_list * cmd_list () { return m_list; }
163
164 comment_list leading_comments () const { return m_if_tok.leading_comments (); }
165
167 {
168 tw.visit_if_command (*this);
169 }
170
171private:
172
173 token m_if_tok;
174
175 // List of if commands (if, elseif, elseif, ... else, endif)
176 tree_if_command_list *m_list = nullptr;
177
178 token m_end_tok;
179};
180
181// Switch.
182
183class tree_switch_case : public tree
184{
185public:
186
188 : m_tok (tok), m_list (sl)
189 { }
190
192 : m_tok (tok), m_label (e), m_list (sl)
193 { }
194
195 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (tree_switch_case)
196
198
199 bool is_default_case () { return ! m_label; }
200
201 filepos beg_pos () const { return m_tok.beg_pos (); }
202 filepos end_pos () const { return m_list->end_pos (); }
203
204 tree_expression * case_label () { return m_label; }
205
206 tree_statement_list * commands () { return m_list; }
207
208 comment_list leading_comments () const { return m_tok.leading_comments (); }
209
211 {
212 tw.visit_switch_case (*this);
213 }
214
215private:
216
217 token m_tok;
218
219 // The case label.
220 tree_expression *m_label = nullptr;
221
222 // The list of statements to evaluate if the label matches.
223 tree_statement_list *m_list;
224};
225
226class tree_switch_case_list : public std::list<tree_switch_case *>
227{
228public:
229
231
233
234 OCTAVE_DISABLE_COPY_MOVE (tree_switch_case_list)
235
237 {
238 while (! empty ())
239 {
240 auto p = begin ();
241 delete *p;
242 erase (p);
243 }
244 }
245
247 {
248 if (empty ())
249 return filepos ();
250
251 tree_switch_case *elt = front ();
252 return elt->beg_pos ();
253 }
254
256 {
257 if (empty ())
258 return filepos ();
259
260 tree_switch_case *elt = back ();
261 return elt->end_pos ();
262 }
263
265 {
266 tw.visit_switch_case_list (*this);
267 }
268};
269
271{
272public:
273
274 tree_switch_command (const token& switch_tok, tree_expression *e, tree_switch_case_list *lst, const token& end_tok)
275 : m_switch_tok (switch_tok), m_expr (e), m_list (lst), m_end_tok (end_tok)
276 { }
277
278 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (tree_switch_command)
279
281
282 filepos beg_pos () const { return m_switch_tok.beg_pos (); }
283 filepos end_pos () const { return m_end_tok.end_pos (); }
284
285 tree_expression * switch_value () { return m_expr; }
286
287 tree_switch_case_list * case_list () { return m_list; }
288
289 comment_list leading_comments () const { return m_switch_tok.leading_comments (); }
290
292 {
293 tw.visit_switch_command (*this);
294 }
295
296private:
297
298 token m_switch_tok;
299
300 // Value on which to switch.
301 tree_expression *m_expr;
302
303 // List of cases (case 1, case 2, ..., default)
304 tree_switch_case_list *m_list;
305
306 token m_end_tok;
307};
308
309OCTAVE_END_NAMESPACE(octave)
310
311#endif
Definition token.h:42
filepos end_pos() const
Definition token.h:129
comment_list leading_comments() const
Definition token.h:134
filepos beg_pos() const
Definition token.h:128
void accept(tree_walker &tw)
Definition pt-select.h:72
filepos beg_pos() const
Definition pt-select.h:63
tree_if_clause(const token &tok, tree_expression *e, tree_statement_list *sl)
Definition pt-select.h:51
token if_token() const
Definition pt-select.h:59
bool is_else_clause()
Definition pt-select.h:61
filepos end_pos() const
Definition pt-select.h:64
tree_statement_list * commands()
Definition pt-select.h:68
tree_expression * condition()
Definition pt-select.h:66
comment_list leading_comments() const
Definition pt-select.h:70
filepos end_pos() const
Definition pt-select.h:117
tree_if_command_list(tree_if_clause *t)
Definition pt-select.h:94
filepos beg_pos() const
Definition pt-select.h:108
token if_token() const
Definition pt-select.h:126
void accept(tree_walker &tw)
Definition pt-select.h:137
tree_if_command_list * cmd_list()
Definition pt-select.h:162
filepos end_pos() const
Definition pt-select.h:160
void accept(tree_walker &tw)
Definition pt-select.h:166
filepos beg_pos() const
Definition pt-select.h:159
tree_if_command(const token &if_tok, tree_if_command_list *lst, const token &end_tok)
Definition pt-select.h:151
tree_if_command(const token &if_tok, const token &end_tok)
Definition pt-select.h:147
comment_list leading_comments() const
Definition pt-select.h:164
filepos end_pos() const
Definition pt-stmt.h:174
filepos end_pos() const
Definition pt-select.h:255
void accept(tree_walker &tw)
Definition pt-select.h:264
filepos beg_pos() const
Definition pt-select.h:246
tree_switch_case_list(tree_switch_case *t)
Definition pt-select.h:232
tree_switch_case(const token &tok, tree_statement_list *sl)
Definition pt-select.h:187
tree_switch_case(const token &tok, tree_expression *e, tree_statement_list *sl)
Definition pt-select.h:191
tree_expression * case_label()
Definition pt-select.h:204
filepos end_pos() const
Definition pt-select.h:202
filepos beg_pos() const
Definition pt-select.h:201
tree_statement_list * commands()
Definition pt-select.h:206
bool is_default_case()
Definition pt-select.h:199
comment_list leading_comments() const
Definition pt-select.h:208
void accept(tree_walker &tw)
Definition pt-select.h:210
filepos beg_pos() const
Definition pt-select.h:282
tree_switch_command(const token &switch_tok, tree_expression *e, tree_switch_case_list *lst, const token &end_tok)
Definition pt-select.h:274
void accept(tree_walker &tw)
Definition pt-select.h:291
tree_switch_case_list * case_list()
Definition pt-select.h:287
tree_expression * switch_value()
Definition pt-select.h:285
filepos end_pos() const
Definition pt-select.h:283
comment_list leading_comments() const
Definition pt-select.h:289
virtual void visit_if_command_list(tree_if_command_list &)
Definition pt-walk.cc:345
virtual void visit_switch_case_list(tree_switch_case_list &)
Definition pt-walk.cc:373
virtual void visit_switch_case(tree_switch_case &)
Definition pt-walk.cc:359
virtual void visit_if_clause(tree_if_clause &)
Definition pt-walk.cc:322
virtual void visit_if_command(tree_if_command &)
Definition pt-walk.cc:336
virtual void visit_switch_command(tree_switch_command &)
Definition pt-walk.cc:387
Definition pt.h:47
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn