GNU Octave  9.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-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_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  OCTAVE_DISABLE_COPY_MOVE (tree_if_clause)
61 
62  ~tree_if_clause ();
63 
64  bool is_else_clause () { return ! m_expr; }
65 
66  tree_expression * condition () { return m_expr; }
67 
68  tree_statement_list * commands () { return m_list; }
69 
70  comment_list * leading_comment () { return m_lead_comm; }
71 
72  void accept (tree_walker& tw)
73  {
74  tw.visit_if_clause (*this);
75  }
76 
77 private:
78 
79  // The condition to test.
80  tree_expression *m_expr;
81 
82  // The list of statements to evaluate if expr is true.
83  tree_statement_list *m_list;
84 
85  // Comment preceding ELSE or ELSEIF token.
86  comment_list *m_lead_comm;
87 };
88 
89 class tree_if_command_list : public base_list<tree_if_clause *>
90 {
91 public:
92 
94 
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 
109  void accept (tree_walker& tw)
110  {
111  tw.visit_if_command_list (*this);
112  }
113 };
114 
116 {
117 public:
118 
119  tree_if_command (int l = -1, int c = -1)
120  : tree_command (l, c), m_list (nullptr),
121  m_lead_comm (nullptr), m_trail_comm (nullptr)
122  { }
123 
125  comment_list *tc, int l = -1, int c = -1)
126  : tree_command (l, c), m_list (lst), m_lead_comm (lc), m_trail_comm (tc)
127  { }
128 
129  OCTAVE_DISABLE_COPY_MOVE (tree_if_command)
130 
131  ~tree_if_command ();
132 
133  tree_if_command_list * cmd_list () { return m_list; }
134 
135  comment_list * leading_comment () { return m_lead_comm; }
136 
137  comment_list * trailing_comment () { return m_trail_comm; }
138 
139  void accept (tree_walker& tw)
140  {
141  tw.visit_if_command (*this);
142  }
143 
144 private:
145 
146  // List of if commands (if, elseif, elseif, ... else, endif)
147  tree_if_command_list *m_list;
148 
149  // Comment preceding IF token.
150  comment_list *m_lead_comm;
151 
152  // Comment preceding ENDIF token.
153  comment_list *m_trail_comm;
154 };
155 
156 // Switch.
157 
158 class tree_switch_case : public tree
159 {
160 public:
161 
162  tree_switch_case (int l = -1, int c = -1)
163  : tree (l, c), m_label (nullptr), m_list (nullptr), m_lead_comm (nullptr)
164  { }
165 
167  int l = -1, int c = -1)
168  : tree (l, c), m_label (nullptr), m_list (sl), m_lead_comm (lc) { }
169 
171  comment_list *lc = nullptr,
172  int l = -1, int c = -1)
173  : tree (l, c), m_label (e), m_list (sl), m_lead_comm (lc) { }
174 
175  OCTAVE_DISABLE_COPY_MOVE (tree_switch_case)
176 
178 
179  bool is_default_case () { return ! m_label; }
180 
181  tree_expression * case_label () { return m_label; }
182 
183  tree_statement_list * commands () { return m_list; }
184 
185  comment_list * leading_comment () { return m_lead_comm; }
186 
187  void accept (tree_walker& tw)
188  {
189  tw.visit_switch_case (*this);
190  }
191 
192 private:
193 
194  // The case label.
195  tree_expression *m_label;
196 
197  // The list of statements to evaluate if the label matches.
198  tree_statement_list *m_list;
199 
200  // Comment preceding CASE or OTHERWISE token.
201  comment_list *m_lead_comm;
202 };
203 
204 class tree_switch_case_list : public base_list<tree_switch_case *>
205 {
206 public:
207 
209 
211 
212  OCTAVE_DISABLE_COPY_MOVE (tree_switch_case_list)
213 
215  {
216  while (! empty ())
217  {
218  auto p = begin ();
219  delete *p;
220  erase (p);
221  }
222  }
223 
224  void accept (tree_walker& tw)
225  {
226  tw.visit_switch_case_list (*this);
227  }
228 };
229 
231 {
232 public:
233 
234  tree_switch_command (int l = -1, int c = -1)
235  : tree_command (l, c), m_expr (nullptr), m_list (nullptr),
236  m_lead_comm (nullptr), m_trail_comm (nullptr) { }
237 
239  comment_list *lc, comment_list *tc,
240  int l = -1, int c = -1)
241  : tree_command (l, c), m_expr (e), m_list (lst), m_lead_comm (lc),
242  m_trail_comm (tc) { }
243 
244  OCTAVE_DISABLE_COPY_MOVE (tree_switch_command)
245 
247 
248  tree_expression * switch_value () { return m_expr; }
249 
250  tree_switch_case_list * case_list () { return m_list; }
251 
252  comment_list * leading_comment () { return m_lead_comm; }
253 
254  comment_list * trailing_comment () { return m_trail_comm; }
255 
256  void accept (tree_walker& tw)
257  {
258  tw.visit_switch_command (*this);
259  }
260 
261 private:
262 
263  // Value on which to switch.
264  tree_expression *m_expr;
265 
266  // List of cases (case 1, case 2, ..., default)
267  tree_switch_case_list *m_list;
268 
269  // Comment preceding SWITCH token.
270  comment_list *m_lead_comm;
271 
272  // Comment preceding ENDSWITCH token.
273  comment_list *m_trail_comm;
274 };
275 
276 OCTAVE_END_NAMESPACE(octave)
277 
278 #endif
void append(const tree_if_clause * &s)
Definition: base-list.h:92
iterator erase(iterator pos)
Definition: base-list.h:55
void accept(tree_walker &tw)
Definition: pt-select.h:72
tree_expression * condition()
Definition: pt-select.h:66
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()
Definition: pt-select.h:64
tree_statement_list * commands()
Definition: pt-select.h:68
tree_if_clause(int l=-1, int c=-1)
Definition: pt-select.h:47
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
comment_list * leading_comment()
Definition: pt-select.h:70
tree_if_command_list(tree_if_clause *t)
Definition: pt-select.h:95
void accept(tree_walker &tw)
Definition: pt-select.h:109
comment_list * trailing_comment()
Definition: pt-select.h:137
tree_if_command(int l=-1, int c=-1)
Definition: pt-select.h:119
void accept(tree_walker &tw)
Definition: pt-select.h:139
tree_if_command(tree_if_command_list *lst, comment_list *lc, comment_list *tc, int l=-1, int c=-1)
Definition: pt-select.h:124
tree_if_command_list * cmd_list()
Definition: pt-select.h:133
comment_list * leading_comment()
Definition: pt-select.h:135
void accept(tree_walker &tw)
Definition: pt-select.h:224
tree_switch_case_list(tree_switch_case *t)
Definition: pt-select.h:210
tree_switch_case(int l=-1, int c=-1)
Definition: pt-select.h:162
comment_list * leading_comment()
Definition: pt-select.h:185
tree_statement_list * commands()
Definition: pt-select.h:183
tree_switch_case(tree_expression *e, tree_statement_list *sl, comment_list *lc=nullptr, int l=-1, int c=-1)
Definition: pt-select.h:170
bool is_default_case()
Definition: pt-select.h:179
tree_switch_case(tree_statement_list *sl, comment_list *lc=nullptr, int l=-1, int c=-1)
Definition: pt-select.h:166
tree_expression * case_label()
Definition: pt-select.h:181
void accept(tree_walker &tw)
Definition: pt-select.h:187
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:238
tree_switch_case_list * case_list()
Definition: pt-select.h:250
comment_list * leading_comment()
Definition: pt-select.h:252
void accept(tree_walker &tw)
Definition: pt-select.h:256
tree_expression * switch_value()
Definition: pt-select.h:248
tree_switch_command(int l=-1, int c=-1)
Definition: pt-select.h:234
comment_list * trailing_comment()
Definition: pt-select.h:254
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:45
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn