2982
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
2982
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
2982
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include "error.h" |
|
28 #include "oct-obj.h" |
|
29 #include "ov.h" |
|
30 #include "pt-cmd.h" |
|
31 #include "pt-exp.h" |
|
32 #include "pt-select.h" |
|
33 #include "pt-stmt.h" |
|
34 #include "pt-walk.h" |
3724
|
35 #include "Cell.h" |
|
36 #include "ov-typeinfo.h" |
2982
|
37 |
|
38 // If clauses. |
|
39 |
|
40 tree_if_clause::~tree_if_clause (void) |
|
41 { |
|
42 delete expr; |
|
43 delete list; |
3665
|
44 delete lead_comm; |
2982
|
45 } |
|
46 |
|
47 int |
|
48 tree_if_clause::eval (void) |
|
49 { |
|
50 if (is_else_clause () || expr->is_logically_true ("if")) |
|
51 { |
|
52 if (list) |
|
53 list->eval (); |
|
54 |
|
55 return 1; |
|
56 } |
|
57 |
|
58 return 0; |
|
59 } |
|
60 |
5861
|
61 tree_if_clause * |
|
62 tree_if_clause::dup (symbol_table *sym_tab) |
|
63 { |
|
64 return new tree_if_clause (expr ? expr->dup (sym_tab) : 0, |
|
65 list ? list->dup (sym_tab) : 0, |
|
66 lead_comm ? lead_comm->dup () : 0); |
|
67 } |
|
68 |
2982
|
69 void |
|
70 tree_if_clause::accept (tree_walker& tw) |
|
71 { |
|
72 tw.visit_if_clause (*this); |
|
73 } |
|
74 |
|
75 // List of if commands. |
|
76 |
|
77 void |
|
78 tree_if_command_list::eval (void) |
|
79 { |
4219
|
80 for (iterator p = begin (); p != end (); p++) |
2982
|
81 { |
4219
|
82 tree_if_clause *t = *p; |
2982
|
83 |
|
84 if (t->eval () || error_state) |
|
85 break; |
|
86 } |
|
87 } |
|
88 |
5861
|
89 tree_if_command_list * |
|
90 tree_if_command_list::dup (symbol_table *sym_tab) |
|
91 { |
|
92 tree_if_command_list *new_icl = new tree_if_command_list (); |
|
93 |
|
94 for (iterator p = begin (); p != end (); p++) |
|
95 { |
|
96 tree_if_clause *elt = *p; |
|
97 |
|
98 new_icl->append (elt ? elt->dup (sym_tab) : 0); |
|
99 } |
|
100 |
|
101 return new_icl; |
|
102 } |
|
103 |
2982
|
104 void |
|
105 tree_if_command_list::accept (tree_walker& tw) |
|
106 { |
|
107 tw.visit_if_command_list (*this); |
|
108 } |
|
109 |
|
110 // If. |
|
111 |
|
112 tree_if_command::~tree_if_command (void) |
|
113 { |
|
114 delete list; |
3665
|
115 delete lead_comm; |
|
116 delete trail_comm; |
2982
|
117 } |
|
118 |
|
119 void |
|
120 tree_if_command::eval (void) |
|
121 { |
|
122 if (list) |
|
123 list->eval (); |
|
124 |
3965
|
125 if (error_state) |
2982
|
126 ::error ("evaluating if command near line %d, column %d", |
|
127 line (), column ()); |
|
128 } |
|
129 |
5861
|
130 tree_command * |
|
131 tree_if_command::dup (symbol_table *sym_tab) |
|
132 { |
|
133 return new tree_if_command (list ? list->dup (sym_tab) : 0, |
|
134 lead_comm ? lead_comm->dup () : 0, |
|
135 trail_comm ? trail_comm->dup () : 0, |
|
136 line (), column ()); |
|
137 } |
|
138 |
2982
|
139 void |
|
140 tree_if_command::accept (tree_walker& tw) |
|
141 { |
|
142 tw.visit_if_command (*this); |
|
143 } |
|
144 |
|
145 // Switch cases. |
|
146 |
|
147 tree_switch_case::~tree_switch_case (void) |
|
148 { |
|
149 delete label; |
|
150 delete list; |
3665
|
151 delete lead_comm; |
2982
|
152 } |
|
153 |
3724
|
154 |
|
155 // Compare two octave values, returning true if equal, false if not |
5775
|
156 // FIXME --- should be member or friend of octave_value class. |
3724
|
157 |
|
158 static bool |
|
159 equal (const octave_value& val, const octave_value& test) |
2982
|
160 { |
|
161 bool retval = false; |
|
162 |
3724
|
163 // If there is no op_eq for these types, we can't compare values. |
|
164 |
4223
|
165 if (val.rows () == test.rows () && val.columns () == test.columns ()) |
3724
|
166 { |
|
167 octave_value tmp = do_binary_op (octave_value::op_eq, val, test); |
|
168 |
|
169 if (! error_state && tmp.is_defined ()) |
|
170 retval = tmp.is_true (); |
|
171 } |
|
172 |
|
173 return retval; |
|
174 } |
|
175 |
|
176 bool |
|
177 tree_switch_case::label_matches (const octave_value& val) |
|
178 { |
2982
|
179 octave_value label_value = label->rvalue (); |
|
180 |
3724
|
181 if (! error_state && label_value.is_defined() ) |
2982
|
182 { |
3724
|
183 if (label_value.is_cell ()) |
2982
|
184 { |
3724
|
185 Cell cell (label_value.cell_value ()); |
|
186 |
5275
|
187 for (octave_idx_type i = 0; i < cell.rows (); i++) |
3724
|
188 { |
5275
|
189 for (octave_idx_type j = 0; j < cell.columns (); j++) |
3724
|
190 { |
|
191 bool match = equal (val, cell(i,j)); |
2982
|
192 |
3724
|
193 if (error_state) |
|
194 { |
|
195 eval_error (); |
|
196 return false; |
|
197 } |
|
198 else if (match) |
|
199 return true; |
|
200 } |
|
201 } |
|
202 } |
|
203 else |
|
204 { |
|
205 bool match = equal (val, label_value); |
|
206 |
|
207 if (error_state) |
2982
|
208 { |
3724
|
209 eval_error (); |
|
210 return false; |
2982
|
211 } |
|
212 else |
3724
|
213 return match; |
2982
|
214 } |
|
215 } |
|
216 else |
|
217 eval_error (); |
|
218 |
3724
|
219 return false; |
2982
|
220 } |
|
221 |
|
222 int |
|
223 tree_switch_case::eval (const octave_value& val) |
|
224 { |
|
225 int retval = 0; |
|
226 |
|
227 if (is_default_case () || label_matches (val)) |
|
228 { |
|
229 if (list) |
|
230 list->eval (); |
|
231 |
|
232 retval = 1; |
|
233 } |
|
234 |
|
235 return retval; |
|
236 } |
|
237 |
|
238 void |
|
239 tree_switch_case::eval_error (void) |
|
240 { |
|
241 ::error ("evaluating switch case label"); |
|
242 } |
|
243 |
5861
|
244 tree_switch_case * |
|
245 tree_switch_case::dup (symbol_table *sym_tab) |
|
246 { |
|
247 return new tree_switch_case (label ? label->dup (sym_tab) : 0, |
|
248 list ? list->dup (sym_tab) : 0, |
|
249 lead_comm ? lead_comm->dup () : 0); |
|
250 } |
|
251 |
2982
|
252 void |
|
253 tree_switch_case::accept (tree_walker& tw) |
|
254 { |
|
255 tw.visit_switch_case (*this); |
|
256 } |
|
257 |
|
258 // List of switch cases. |
|
259 |
|
260 void |
|
261 tree_switch_case_list::eval (const octave_value& val) |
|
262 { |
4219
|
263 for (iterator p = begin (); p != end (); p++) |
2982
|
264 { |
4219
|
265 tree_switch_case *t = *p; |
2982
|
266 |
|
267 if (t->eval (val) || error_state) |
|
268 break; |
|
269 } |
|
270 } |
|
271 |
5861
|
272 tree_switch_case_list * |
|
273 tree_switch_case_list::dup (symbol_table *sym_tab) |
|
274 { |
|
275 tree_switch_case_list *new_scl = new tree_switch_case_list (); |
|
276 |
|
277 for (iterator p = begin (); p != end (); p++) |
|
278 { |
|
279 tree_switch_case *elt = *p; |
|
280 |
|
281 new_scl->append (elt ? elt->dup (sym_tab) : 0); |
|
282 } |
|
283 |
|
284 return new_scl; |
|
285 } |
|
286 |
2982
|
287 void |
|
288 tree_switch_case_list::accept (tree_walker& tw) |
|
289 { |
|
290 tw.visit_switch_case_list (*this); |
|
291 } |
|
292 |
|
293 // Switch. |
|
294 |
|
295 tree_switch_command::~tree_switch_command (void) |
|
296 { |
|
297 delete expr; |
|
298 delete list; |
3665
|
299 delete lead_comm; |
|
300 delete trail_comm; |
2982
|
301 } |
|
302 |
|
303 void |
|
304 tree_switch_command::eval (void) |
|
305 { |
|
306 if (expr) |
|
307 { |
|
308 octave_value val = expr->rvalue (); |
|
309 |
|
310 if (! error_state) |
|
311 { |
|
312 if (list) |
|
313 list->eval (val); |
|
314 |
|
315 if (error_state) |
|
316 eval_error (); |
|
317 } |
|
318 else |
|
319 eval_error (); |
|
320 } |
|
321 else |
|
322 ::error ("missing value in switch command near line %d, column %d", |
|
323 line (), column ()); |
|
324 } |
|
325 |
|
326 void |
|
327 tree_switch_command::eval_error (void) |
|
328 { |
|
329 ::error ("evaluating switch command near line %d, column %d", |
|
330 line (), column ()); |
|
331 } |
|
332 |
5861
|
333 tree_command * |
|
334 tree_switch_command::dup (symbol_table *sym_tab) |
|
335 { |
|
336 return new tree_switch_command (expr ? expr->dup (sym_tab) : 0, |
|
337 list ? list->dup (sym_tab) : 0, |
|
338 lead_comm ? lead_comm->dup () : 0, |
|
339 trail_comm ? trail_comm->dup () : 0, |
|
340 line (), column ()); |
|
341 } |
|
342 |
2982
|
343 void |
|
344 tree_switch_command::accept (tree_walker& tw) |
|
345 { |
|
346 tw.visit_switch_command (*this); |
|
347 } |
|
348 |
|
349 /* |
|
350 ;;; Local Variables: *** |
|
351 ;;; mode: C++ *** |
|
352 ;;; End: *** |
|
353 */ |