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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
|
31 #include "error.h" |
|
32 #include "gripes.h" |
|
33 #include "oct-map.h" |
|
34 #include "oct-lvalue.h" |
|
35 #include "ov.h" |
|
36 #include "pt-arg-list.h" |
3770
|
37 #include "pt-bp.h" |
2982
|
38 #include "pt-cmd.h" |
|
39 #include "pt-exp.h" |
2985
|
40 #include "pt-jump.h" |
2982
|
41 #include "pt-loop.h" |
|
42 #include "pt-stmt.h" |
|
43 #include "pt-walk.h" |
|
44 |
|
45 // Decide if it's time to quit a for or while loop. |
|
46 static inline bool |
|
47 quit_loop_now (void) |
|
48 { |
|
49 // Maybe handle `continue N' someday... |
|
50 |
2985
|
51 if (tree_continue_command::continuing) |
|
52 tree_continue_command::continuing--; |
2982
|
53 |
2985
|
54 bool quit = (error_state |
|
55 || tree_return_command::returning |
|
56 || tree_break_command::breaking |
|
57 || tree_continue_command::continuing); |
2982
|
58 |
2985
|
59 if (tree_break_command::breaking) |
|
60 tree_break_command::breaking--; |
2982
|
61 |
|
62 return quit; |
|
63 } |
|
64 |
|
65 // While. |
|
66 |
|
67 tree_while_command::~tree_while_command (void) |
|
68 { |
|
69 delete expr; |
|
70 delete list; |
3665
|
71 delete lead_comm; |
|
72 delete trail_comm; |
2982
|
73 } |
|
74 |
|
75 void |
|
76 tree_while_command::eval (void) |
|
77 { |
|
78 if (error_state) |
|
79 return; |
|
80 |
|
81 if (! expr) |
|
82 panic_impossible (); |
|
83 |
|
84 for (;;) |
|
85 { |
|
86 if (expr->is_logically_true ("while")) |
|
87 { |
|
88 if (list) |
|
89 { |
|
90 list->eval (); |
|
91 |
|
92 if (error_state) |
|
93 { |
|
94 eval_error (); |
|
95 return; |
|
96 } |
|
97 } |
|
98 |
|
99 if (quit_loop_now ()) |
|
100 break; |
|
101 } |
|
102 else |
|
103 break; |
|
104 } |
|
105 } |
|
106 |
|
107 void |
|
108 tree_while_command::eval_error (void) |
|
109 { |
|
110 if (error_state > 0) |
|
111 ::error ("evaluating while command near line %d, column %d", |
|
112 line (), column ()); |
|
113 } |
|
114 |
|
115 void |
|
116 tree_while_command::accept (tree_walker& tw) |
|
117 { |
|
118 tw.visit_while_command (*this); |
|
119 } |
|
120 |
3484
|
121 // Do-Until |
|
122 |
|
123 void |
|
124 tree_do_until_command::eval (void) |
|
125 { |
|
126 if (error_state) |
|
127 return; |
|
128 |
|
129 if (! expr) |
|
130 panic_impossible (); |
|
131 |
|
132 for (;;) |
|
133 { |
3770
|
134 MAYBE_DO_BREAKPOINT; |
|
135 |
3484
|
136 if (list) |
|
137 { |
|
138 list->eval (); |
|
139 |
|
140 if (error_state) |
|
141 { |
|
142 eval_error (); |
|
143 return; |
|
144 } |
|
145 } |
|
146 |
|
147 if (quit_loop_now () || expr->is_logically_true ("do-until")) |
|
148 break; |
|
149 } |
|
150 } |
|
151 |
|
152 void |
|
153 tree_do_until_command::eval_error (void) |
|
154 { |
|
155 if (error_state > 0) |
|
156 ::error ("evaluating do-until command near line %d, column %d", |
|
157 line (), column ()); |
|
158 } |
|
159 |
|
160 void |
|
161 tree_do_until_command::accept (tree_walker& tw) |
|
162 { |
|
163 tw.visit_do_until_command (*this); |
|
164 } |
|
165 |
2982
|
166 // For. |
|
167 |
|
168 tree_simple_for_command::~tree_simple_for_command (void) |
|
169 { |
|
170 delete expr; |
|
171 delete list; |
3665
|
172 delete lead_comm; |
|
173 delete trail_comm; |
2982
|
174 } |
|
175 |
|
176 inline void |
|
177 tree_simple_for_command::do_for_loop_once (octave_lvalue& ult, |
|
178 const octave_value& rhs, |
|
179 bool& quit) |
|
180 { |
|
181 quit = false; |
|
182 |
3538
|
183 ult.assign (octave_value::op_asn_eq, rhs); |
2982
|
184 |
|
185 if (! error_state) |
|
186 { |
|
187 if (list) |
|
188 { |
|
189 list->eval (); |
|
190 |
|
191 if (error_state) |
|
192 eval_error (); |
|
193 } |
|
194 } |
|
195 else |
|
196 eval_error (); |
|
197 |
|
198 quit = quit_loop_now (); |
|
199 } |
|
200 |
|
201 #define DO_LOOP(arg) \ |
|
202 do \ |
|
203 { \ |
|
204 for (int i = 0; i < steps; i++) \ |
|
205 { \ |
3770
|
206 MAYBE_DO_BREAKPOINT; \ |
|
207 \ |
2982
|
208 octave_value val (arg); \ |
|
209 \ |
|
210 bool quit = false; \ |
|
211 \ |
|
212 do_for_loop_once (ult, val, quit); \ |
|
213 \ |
|
214 if (quit) \ |
|
215 break; \ |
|
216 } \ |
|
217 } \ |
|
218 while (0) |
|
219 |
|
220 void |
|
221 tree_simple_for_command::eval (void) |
|
222 { |
|
223 if (error_state) |
|
224 return; |
|
225 |
|
226 octave_value rhs = expr->rvalue (); |
|
227 |
|
228 if (error_state || rhs.is_undefined ()) |
|
229 { |
|
230 eval_error (); |
|
231 return; |
|
232 } |
|
233 |
|
234 octave_lvalue ult = lhs->lvalue (); |
|
235 |
|
236 if (error_state) |
|
237 { |
|
238 eval_error (); |
|
239 return; |
|
240 } |
|
241 |
3180
|
242 if (rhs.is_range ()) |
|
243 { |
|
244 Range rng = rhs.range_value (); |
|
245 |
|
246 int steps = rng.nelem (); |
|
247 double b = rng.base (); |
|
248 double increment = rng.inc (); |
|
249 |
|
250 for (int i = 0; i < steps; i++) |
|
251 { |
3770
|
252 MAYBE_DO_BREAKPOINT; |
|
253 |
3180
|
254 double tmp_val = b + i * increment; |
|
255 |
|
256 octave_value val (tmp_val); |
|
257 |
|
258 bool quit = false; |
|
259 |
|
260 do_for_loop_once (ult, val, quit); |
|
261 |
|
262 if (quit) |
|
263 break; |
|
264 } |
|
265 } |
|
266 else if (rhs.is_scalar_type ()) |
2982
|
267 { |
|
268 bool quit = false; |
3770
|
269 |
|
270 MAYBE_DO_BREAKPOINT; |
2982
|
271 |
|
272 do_for_loop_once (ult, rhs, quit); |
|
273 } |
3215
|
274 else if (rhs.is_string ()) |
|
275 { |
|
276 charMatrix chm_tmp = rhs.char_matrix_value (); |
|
277 int nr = chm_tmp.rows (); |
|
278 int steps = chm_tmp.columns (); |
|
279 |
|
280 if (error_state) |
|
281 return; |
|
282 |
|
283 if (nr == 1) |
|
284 DO_LOOP (chm_tmp (0, i)); |
|
285 else |
|
286 { |
|
287 for (int i = 0; i < steps; i++) |
|
288 { |
3770
|
289 MAYBE_DO_BREAKPOINT; |
|
290 |
3215
|
291 octave_value val (chm_tmp.extract (0, i, nr-1, i), true); |
|
292 |
|
293 bool quit = false; |
|
294 |
|
295 do_for_loop_once (ult, val, quit); |
|
296 |
|
297 if (quit) |
|
298 break; |
|
299 } |
|
300 } |
|
301 } |
2982
|
302 else if (rhs.is_matrix_type ()) |
|
303 { |
|
304 Matrix m_tmp; |
|
305 ComplexMatrix cm_tmp; |
|
306 |
|
307 int nr; |
|
308 int steps; |
|
309 |
3215
|
310 if (rhs.is_real_matrix ()) |
2982
|
311 { |
|
312 m_tmp = rhs.matrix_value (); |
|
313 nr = m_tmp.rows (); |
|
314 steps = m_tmp.columns (); |
|
315 } |
|
316 else |
|
317 { |
|
318 cm_tmp = rhs.complex_matrix_value (); |
|
319 nr = cm_tmp.rows (); |
|
320 steps = cm_tmp.columns (); |
|
321 } |
|
322 |
3180
|
323 if (error_state) |
|
324 return; |
|
325 |
3215
|
326 if (rhs.is_real_matrix ()) |
2982
|
327 { |
|
328 if (nr == 1) |
|
329 DO_LOOP (m_tmp (0, i)); |
|
330 else |
|
331 DO_LOOP (m_tmp.extract (0, i, nr-1, i)); |
|
332 } |
|
333 else |
|
334 { |
|
335 if (nr == 1) |
|
336 DO_LOOP (cm_tmp (0, i)); |
|
337 else |
|
338 DO_LOOP (cm_tmp.extract (0, i, nr-1, i)); |
|
339 } |
|
340 } |
|
341 else if (rhs.is_map ()) |
|
342 { |
|
343 Octave_map tmp_val (rhs.map_value ()); |
|
344 |
|
345 for (Pix p = tmp_val.first (); p != 0; tmp_val.next (p)) |
|
346 { |
3770
|
347 MAYBE_DO_BREAKPOINT; |
|
348 |
2982
|
349 octave_value val = tmp_val.contents (p); |
|
350 |
|
351 bool quit = false; |
|
352 |
|
353 do_for_loop_once (ult, val, quit); |
|
354 |
|
355 if (quit) |
|
356 break; |
|
357 } |
|
358 } |
|
359 else |
|
360 { |
|
361 ::error ("invalid type in for loop expression near line %d, column %d", |
|
362 line (), column ()); |
|
363 } |
|
364 } |
|
365 |
|
366 void |
|
367 tree_simple_for_command::eval_error (void) |
|
368 { |
|
369 if (error_state > 0) |
|
370 ::error ("evaluating for command near line %d, column %d", |
|
371 line (), column ()); |
|
372 } |
|
373 |
|
374 void |
|
375 tree_simple_for_command::accept (tree_walker& tw) |
|
376 { |
|
377 tw.visit_simple_for_command (*this); |
|
378 } |
|
379 |
|
380 tree_complex_for_command::~tree_complex_for_command (void) |
|
381 { |
|
382 delete expr; |
|
383 delete list; |
3665
|
384 delete lead_comm; |
|
385 delete trail_comm; |
2982
|
386 } |
|
387 |
|
388 void |
|
389 tree_complex_for_command::do_for_loop_once (octave_lvalue &val_ref, |
|
390 octave_lvalue &key_ref, |
|
391 const octave_value& val, |
|
392 const octave_value& key, |
|
393 bool& quit) |
|
394 { |
|
395 quit = false; |
|
396 |
3538
|
397 val_ref.assign (octave_value::op_asn_eq, val); |
|
398 key_ref.assign (octave_value::op_asn_eq, key); |
2982
|
399 |
|
400 if (! error_state) |
|
401 { |
|
402 if (list) |
|
403 { |
|
404 list->eval (); |
|
405 |
|
406 if (error_state) |
|
407 eval_error (); |
|
408 } |
|
409 } |
|
410 else |
|
411 eval_error (); |
|
412 |
|
413 quit = quit_loop_now (); |
|
414 } |
|
415 |
|
416 void |
|
417 tree_complex_for_command::eval (void) |
|
418 { |
|
419 if (error_state) |
|
420 return; |
|
421 |
|
422 octave_value rhs = expr->rvalue (); |
|
423 |
|
424 if (error_state || rhs.is_undefined ()) |
|
425 { |
|
426 eval_error (); |
|
427 return; |
|
428 } |
|
429 |
|
430 if (rhs.is_map ()) |
|
431 { |
|
432 // Cycle through structure elements. First element of id_list |
|
433 // is set to value and the second is set to the name of the |
|
434 // structure element. |
|
435 |
|
436 Pix p = lhs->first (); |
|
437 tree_expression *elt = lhs->operator () (p); |
|
438 octave_lvalue val_ref = elt->lvalue (); |
|
439 |
|
440 lhs->next (p); |
|
441 elt = lhs->operator () (p); |
|
442 octave_lvalue key_ref = elt->lvalue (); |
|
443 |
|
444 Octave_map tmp_val (rhs.map_value ()); |
|
445 |
|
446 for (p = tmp_val.first (); p != 0; tmp_val.next (p)) |
|
447 { |
|
448 octave_value key = tmp_val.key (p); |
|
449 octave_value val = tmp_val.contents (p); |
|
450 |
3770
|
451 MAYBE_DO_BREAKPOINT; |
|
452 |
2982
|
453 bool quit = false; |
|
454 |
|
455 do_for_loop_once (key_ref, val_ref, key, val, quit); |
|
456 |
|
457 if (quit) |
|
458 break; |
|
459 } |
|
460 } |
|
461 else |
|
462 error ("in statement `for [X, Y] = VAL', VAL must be a structure"); |
|
463 } |
|
464 |
|
465 void |
|
466 tree_complex_for_command::eval_error (void) |
|
467 { |
|
468 if (error_state > 0) |
|
469 ::error ("evaluating for command near line %d, column %d", |
|
470 line (), column ()); |
|
471 } |
|
472 |
|
473 void |
|
474 tree_complex_for_command::accept (tree_walker& tw) |
|
475 { |
|
476 tw.visit_complex_for_command (*this); |
|
477 } |
|
478 |
|
479 /* |
|
480 ;;; Local Variables: *** |
|
481 ;;; mode: C++ *** |
|
482 ;;; End: *** |
|
483 */ |