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