GNU Octave 11.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-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_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 OCTINTERP_API 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 bool is_else_clause () { return ! m_expr; }
60
61 filepos beg_pos () const { return m_tok.beg_pos (); }
62 filepos end_pos () const { return m_list->end_pos (); }
63
64 comment_list leading_comments () const { return m_tok.leading_comments (); }
65 comment_list trailing_comments () const { return m_list->trailing_comments (); }
66
67 token if_token () const { return m_tok; }
68
69 tree_expression * condition () { return m_expr; }
70
71 tree_statement_list * commands () { return m_list; }
72
74 {
75 tw.visit_if_clause (*this);
76 }
77
78private:
79
80 token m_tok;
81
82 // The condition to test.
83 tree_expression *m_expr = nullptr;
84
85 // The list of statements to evaluate if expr is true.
86 tree_statement_list *m_list;
87};
88
89class OCTINTERP_API tree_if_command_list : public std::list<tree_if_clause *>
90{
91public:
92
94
95 tree_if_command_list (tree_if_clause *t) { push_back (t); }
96
97 OCTAVE_DISABLE_COPY_MOVE (tree_if_command_list)
98
100 {
101 while (! empty ())
102 {
103 auto p = begin ();
104 delete *p;
105 erase (p);
106 }
107 }
108
110 {
111 if (empty ())
112 return filepos ();
113
114 tree_if_clause *elt = front ();
115 return elt->beg_pos ();
116 }
117
119 {
120 if (empty ())
121 return filepos ();
122
123 tree_if_clause *elt = back ();
124 return elt->end_pos ();
125 }
126
128 {
129 if (empty ())
130 return comment_list ();
131
132 tree_if_clause *elt = front ();
133 return elt->leading_comments ();
134 }
135
137 {
138 if (empty ())
139 return comment_list ();
140
141 tree_if_clause *elt = back ();
142 return elt->trailing_comments ();
143 }
144
146 {
147 if (! empty ())
148 {
149 tree_if_clause *p = front ();
150 return p->if_token ();
151 }
152
153 return token ();
154 }
155
157 {
158 tw.visit_if_command_list (*this);
159 }
160};
161
162class OCTINTERP_API tree_if_command : public tree_command
163{
164public:
165
166 tree_if_command (const token& if_tok, const token& end_tok)
167 : m_if_tok (if_tok), m_end_tok (end_tok)
168 { }
169
170 tree_if_command (const token& if_tok, tree_if_command_list *lst, const token& end_tok)
171 : m_if_tok (if_tok), m_list (lst), m_end_tok (end_tok)
172 { }
173
174 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (tree_if_command)
175
177
178 filepos beg_pos () const { return m_if_tok.beg_pos (); }
179 filepos end_pos () const { return m_end_tok.end_pos (); }
180
181 comment_list leading_comments () const { return m_if_tok.leading_comments (); }
182 comment_list trailing_comments () const { return m_end_tok.trailing_comments (); }
183
184 token if_token () const { return m_if_tok; }
185
186 tree_if_command_list * cmd_list () { return m_list; }
187
188 token end_token () const { return m_end_tok; }
189
191 {
192 tw.visit_if_command (*this);
193 }
194
195private:
196
197 token m_if_tok;
198
199 // List of if commands (if, elseif, elseif, ... else, endif)
200 tree_if_command_list *m_list = nullptr;
201
202 token m_end_tok;
203};
204
205// Switch.
206
207class OCTINTERP_API tree_switch_case : public tree
208{
209public:
210
212 : m_tok (tok), m_list (sl)
213 { }
214
216 : m_tok (tok), m_label (e), m_list (sl)
217 { }
218
219 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (tree_switch_case)
220
222
223 bool is_default_case () { return ! m_label; }
224
225 filepos beg_pos () const { return m_tok.beg_pos (); }
226 filepos end_pos () const { return m_list->end_pos (); }
227
228 comment_list leading_comments () const { return m_tok.leading_comments (); }
229 comment_list trailing_comments () const { return m_list->trailing_comments (); }
230
231 token case_token () const { return m_tok; }
232
233 tree_expression * case_label () { return m_label; }
234
235 tree_statement_list * commands () { return m_list; }
236
238 {
239 tw.visit_switch_case (*this);
240 }
241
242private:
243
244 token m_tok;
245
246 // The case label.
247 tree_expression *m_label = nullptr;
248
249 // The list of statements to evaluate if the label matches.
250 tree_statement_list *m_list;
251};
252
253class OCTINTERP_API tree_switch_case_list : public std::list<tree_switch_case *>
254{
255public:
256
258
260
261 OCTAVE_DISABLE_COPY_MOVE (tree_switch_case_list)
262
264 {
265 while (! empty ())
266 {
267 auto p = begin ();
268 delete *p;
269 erase (p);
270 }
271 }
272
274 {
275 if (empty ())
276 return filepos ();
277
278 tree_switch_case *elt = front ();
279 return elt->beg_pos ();
280 }
281
283 {
284 if (empty ())
285 return filepos ();
286
287 tree_switch_case *elt = back ();
288 return elt->end_pos ();
289 }
290
292 {
293 if (empty ())
294 return comment_list ();
295
296 tree_switch_case *elt = front ();
297 return elt->leading_comments ();
298 }
299
301 {
302 if (empty ())
303 return comment_list ();
304
305 tree_switch_case *elt = back ();
306 return elt->trailing_comments ();
307 }
308
310 {
311 tw.visit_switch_case_list (*this);
312 }
313};
314
315class OCTINTERP_API tree_switch_command : public tree_command
316{
317public:
318
319 tree_switch_command (const token& switch_tok, tree_expression *e, tree_switch_case_list *lst, const token& end_tok)
320 : m_switch_tok (switch_tok), m_expr (e), m_list (lst), m_end_tok (end_tok)
321 { }
322
323 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (tree_switch_command)
324
326
327 filepos beg_pos () const { return m_switch_tok.beg_pos (); }
328 filepos end_pos () const { return m_end_tok.end_pos (); }
329
330 comment_list leading_comments () const { return m_switch_tok.leading_comments (); }
331 comment_list trailing_comments () const { return m_end_tok.trailing_comments (); }
332
333 token switch_token () const { return m_switch_tok; }
334
335 tree_expression * switch_value () { return m_expr; }
336
337 tree_switch_case_list * case_list () { return m_list; }
338
339 token end_token () const { return m_end_tok; }
340
342 {
343 tw.visit_switch_command (*this);
344 }
345
346private:
347
348 token m_switch_tok;
349
350 // Value on which to switch.
351 tree_expression *m_expr;
352
353 // List of cases (case 1, case 2, ..., default)
354 tree_switch_case_list *m_list;
355
356 token m_end_tok;
357};
358
359OCTAVE_END_NAMESPACE(octave)
360
361#endif
Definition token.h:42
void accept(tree_walker &tw)
Definition pt-select.h:73
filepos beg_pos() const
Definition pt-select.h:61
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:67
bool is_else_clause()
Definition pt-select.h:59
filepos end_pos() const
Definition pt-select.h:62
tree_statement_list * commands()
Definition pt-select.h:71
comment_list trailing_comments() const
Definition pt-select.h:65
tree_expression * condition()
Definition pt-select.h:69
comment_list leading_comments() const
Definition pt-select.h:64
comment_list leading_comments() const
Definition pt-select.h:127
filepos end_pos() const
Definition pt-select.h:118
tree_if_command_list(tree_if_clause *t)
Definition pt-select.h:95
filepos beg_pos() const
Definition pt-select.h:109
token if_token() const
Definition pt-select.h:145
void accept(tree_walker &tw)
Definition pt-select.h:156
comment_list trailing_comments() const
Definition pt-select.h:136
tree_if_command_list * cmd_list()
Definition pt-select.h:186
filepos end_pos() const
Definition pt-select.h:179
void accept(tree_walker &tw)
Definition pt-select.h:190
comment_list trailing_comments() const
Definition pt-select.h:182
filepos beg_pos() const
Definition pt-select.h:178
tree_if_command(const token &if_tok, tree_if_command_list *lst, const token &end_tok)
Definition pt-select.h:170
tree_if_command(const token &if_tok, const token &end_tok)
Definition pt-select.h:166
token end_token() const
Definition pt-select.h:188
comment_list leading_comments() const
Definition pt-select.h:181
token if_token() const
Definition pt-select.h:184
filepos end_pos() const
Definition pt-select.h:282
void accept(tree_walker &tw)
Definition pt-select.h:309
comment_list trailing_comments() const
Definition pt-select.h:300
filepos beg_pos() const
Definition pt-select.h:273
tree_switch_case_list(tree_switch_case *t)
Definition pt-select.h:259
comment_list leading_comments() const
Definition pt-select.h:291
tree_switch_case(const token &tok, tree_statement_list *sl)
Definition pt-select.h:211
tree_switch_case(const token &tok, tree_expression *e, tree_statement_list *sl)
Definition pt-select.h:215
token case_token() const
Definition pt-select.h:231
tree_expression * case_label()
Definition pt-select.h:233
filepos end_pos() const
Definition pt-select.h:226
filepos beg_pos() const
Definition pt-select.h:225
comment_list trailing_comments() const
Definition pt-select.h:229
tree_statement_list * commands()
Definition pt-select.h:235
bool is_default_case()
Definition pt-select.h:223
comment_list leading_comments() const
Definition pt-select.h:228
void accept(tree_walker &tw)
Definition pt-select.h:237
token end_token() const
Definition pt-select.h:339
token switch_token() const
Definition pt-select.h:333
filepos beg_pos() const
Definition pt-select.h:327
tree_switch_command(const token &switch_tok, tree_expression *e, tree_switch_case_list *lst, const token &end_tok)
Definition pt-select.h:319
void accept(tree_walker &tw)
Definition pt-select.h:341
tree_switch_case_list * case_list()
Definition pt-select.h:337
tree_expression * switch_value()
Definition pt-select.h:335
comment_list trailing_comments() const
Definition pt-select.h:331
filepos end_pos() const
Definition pt-select.h:328
comment_list leading_comments() const
Definition pt-select.h:330
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