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