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 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
4153
|
27 #include "quit.h" |
|
28 |
2982
|
29 #include "error.h" |
|
30 #include "gripes.h" |
|
31 #include "oct-map.h" |
|
32 #include "oct-lvalue.h" |
|
33 #include "ov.h" |
|
34 #include "pt-arg-list.h" |
3770
|
35 #include "pt-bp.h" |
2982
|
36 #include "pt-cmd.h" |
|
37 #include "pt-exp.h" |
2985
|
38 #include "pt-jump.h" |
2982
|
39 #include "pt-loop.h" |
|
40 #include "pt-stmt.h" |
|
41 #include "pt-walk.h" |
3877
|
42 #include "unwind-prot.h" |
|
43 |
|
44 // TRUE means we are evaluating some kind of looping construct. |
|
45 bool evaluating_looping_command = false; |
2982
|
46 |
|
47 // Decide if it's time to quit a for or while loop. |
|
48 static inline bool |
|
49 quit_loop_now (void) |
|
50 { |
4153
|
51 OCTAVE_QUIT; |
|
52 |
2982
|
53 // Maybe handle `continue N' someday... |
|
54 |
4207
|
55 if (tree_continue_command::continuing) |
|
56 tree_continue_command::continuing--; |
2982
|
57 |
2985
|
58 bool quit = (error_state |
4207
|
59 || tree_return_command::returning |
|
60 || tree_break_command::breaking |
|
61 || tree_continue_command::continuing); |
2982
|
62 |
4207
|
63 if (tree_break_command::breaking) |
|
64 tree_break_command::breaking--; |
2982
|
65 |
|
66 return quit; |
|
67 } |
|
68 |
|
69 // While. |
|
70 |
|
71 tree_while_command::~tree_while_command (void) |
|
72 { |
|
73 delete expr; |
|
74 delete list; |
3665
|
75 delete lead_comm; |
|
76 delete trail_comm; |
2982
|
77 } |
|
78 |
|
79 void |
|
80 tree_while_command::eval (void) |
|
81 { |
|
82 if (error_state) |
|
83 return; |
|
84 |
3877
|
85 unwind_protect::begin_frame ("while_command::eval"); |
|
86 |
|
87 unwind_protect_bool (evaluating_looping_command); |
|
88 |
|
89 evaluating_looping_command = true; |
|
90 |
2982
|
91 if (! expr) |
|
92 panic_impossible (); |
|
93 |
|
94 for (;;) |
|
95 { |
|
96 if (expr->is_logically_true ("while")) |
|
97 { |
|
98 if (list) |
|
99 { |
|
100 list->eval (); |
|
101 |
|
102 if (error_state) |
|
103 { |
|
104 eval_error (); |
3877
|
105 goto cleanup; |
2982
|
106 } |
|
107 } |
|
108 |
|
109 if (quit_loop_now ()) |
|
110 break; |
|
111 } |
|
112 else |
|
113 break; |
|
114 } |
3877
|
115 |
|
116 cleanup: |
|
117 unwind_protect::run_frame ("while_command::eval"); |
2982
|
118 } |
|
119 |
|
120 void |
|
121 tree_while_command::eval_error (void) |
|
122 { |
3965
|
123 ::error ("evaluating while command near line %d, column %d", |
|
124 line (), column ()); |
2982
|
125 } |
|
126 |
|
127 void |
|
128 tree_while_command::accept (tree_walker& tw) |
|
129 { |
|
130 tw.visit_while_command (*this); |
|
131 } |
|
132 |
3484
|
133 // Do-Until |
|
134 |
|
135 void |
|
136 tree_do_until_command::eval (void) |
|
137 { |
|
138 if (error_state) |
|
139 return; |
|
140 |
3877
|
141 unwind_protect::begin_frame ("do_until_command::eval"); |
|
142 |
|
143 unwind_protect_bool (evaluating_looping_command); |
|
144 |
|
145 evaluating_looping_command = true; |
|
146 |
3484
|
147 if (! expr) |
|
148 panic_impossible (); |
|
149 |
|
150 for (;;) |
|
151 { |
3770
|
152 MAYBE_DO_BREAKPOINT; |
|
153 |
3484
|
154 if (list) |
|
155 { |
|
156 list->eval (); |
|
157 |
|
158 if (error_state) |
|
159 { |
|
160 eval_error (); |
3877
|
161 goto cleanup; |
3484
|
162 } |
|
163 } |
|
164 |
|
165 if (quit_loop_now () || expr->is_logically_true ("do-until")) |
|
166 break; |
|
167 } |
3877
|
168 |
|
169 cleanup: |
|
170 unwind_protect::run_frame ("do_until_command::eval"); |
3484
|
171 } |
|
172 |
|
173 void |
|
174 tree_do_until_command::eval_error (void) |
|
175 { |
3965
|
176 ::error ("evaluating do-until command near line %d, column %d", |
|
177 line (), column ()); |
3484
|
178 } |
|
179 |
|
180 void |
|
181 tree_do_until_command::accept (tree_walker& tw) |
|
182 { |
|
183 tw.visit_do_until_command (*this); |
|
184 } |
|
185 |
2982
|
186 // For. |
|
187 |
|
188 tree_simple_for_command::~tree_simple_for_command (void) |
|
189 { |
|
190 delete expr; |
|
191 delete list; |
3665
|
192 delete lead_comm; |
|
193 delete trail_comm; |
2982
|
194 } |
|
195 |
|
196 inline void |
|
197 tree_simple_for_command::do_for_loop_once (octave_lvalue& ult, |
|
198 const octave_value& rhs, |
|
199 bool& quit) |
|
200 { |
|
201 quit = false; |
|
202 |
3538
|
203 ult.assign (octave_value::op_asn_eq, rhs); |
2982
|
204 |
|
205 if (! error_state) |
|
206 { |
|
207 if (list) |
|
208 { |
|
209 list->eval (); |
|
210 |
|
211 if (error_state) |
|
212 eval_error (); |
|
213 } |
|
214 } |
|
215 else |
|
216 eval_error (); |
|
217 |
|
218 quit = quit_loop_now (); |
|
219 } |
|
220 |
|
221 #define DO_LOOP(arg) \ |
|
222 do \ |
|
223 { \ |
|
224 for (int i = 0; i < steps; i++) \ |
|
225 { \ |
3770
|
226 MAYBE_DO_BREAKPOINT; \ |
|
227 \ |
2982
|
228 octave_value val (arg); \ |
|
229 \ |
|
230 bool quit = false; \ |
|
231 \ |
|
232 do_for_loop_once (ult, val, quit); \ |
|
233 \ |
|
234 if (quit) \ |
|
235 break; \ |
|
236 } \ |
|
237 } \ |
|
238 while (0) |
|
239 |
4911
|
240 #define DO_ND_LOOP(arg) \ |
|
241 do \ |
|
242 { \ |
|
243 int ndims = dv.length (); \ |
|
244 Array<idx_vector> idx; \ |
4914
|
245 int steps = dv.numel () / dv (0); \ |
4911
|
246 idx.resize (ndims, idx_vector (1)); \ |
|
247 idx (0) = idx_vector (':'); \ |
|
248 \ |
|
249 for (int i = 0; i < steps; i++) \ |
|
250 { \ |
|
251 MAYBE_DO_BREAKPOINT; \ |
|
252 \ |
4914
|
253 octave_value val (arg.index(idx)); \ |
4911
|
254 \ |
|
255 bool quit = false; \ |
|
256 \ |
|
257 do_for_loop_once (ult, val, quit); \ |
|
258 quit = (i == steps - 1 ? true : quit); \ |
|
259 \ |
|
260 if (quit) \ |
|
261 break; \ |
|
262 \ |
|
263 for (int j = 1; j < ndims; j++) \ |
|
264 { \ |
4914
|
265 idx(j) = idx_vector (idx(j)(0) + 2); \ |
|
266 if (idx(j)(0) < dv(j)) \ |
4911
|
267 break; \ |
|
268 else \ |
4914
|
269 idx(j) = idx_vector (1); \ |
4911
|
270 } \ |
|
271 } \ |
|
272 } \ |
|
273 while (0) |
|
274 |
2982
|
275 void |
|
276 tree_simple_for_command::eval (void) |
|
277 { |
|
278 if (error_state) |
|
279 return; |
|
280 |
3877
|
281 unwind_protect::begin_frame ("simple_for_command::eval"); |
|
282 |
|
283 unwind_protect_bool (evaluating_looping_command); |
|
284 |
|
285 evaluating_looping_command = true; |
|
286 |
2982
|
287 octave_value rhs = expr->rvalue (); |
|
288 |
|
289 if (error_state || rhs.is_undefined ()) |
|
290 { |
|
291 eval_error (); |
3877
|
292 goto cleanup; |
2982
|
293 } |
|
294 |
3877
|
295 { |
|
296 octave_lvalue ult = lhs->lvalue (); |
3180
|
297 |
3877
|
298 if (error_state) |
|
299 { |
|
300 eval_error (); |
|
301 goto cleanup; |
|
302 } |
|
303 |
|
304 if (rhs.is_range ()) |
|
305 { |
|
306 Range rng = rhs.range_value (); |
3180
|
307 |
3877
|
308 int steps = rng.nelem (); |
|
309 double b = rng.base (); |
|
310 double increment = rng.inc (); |
3770
|
311 |
3877
|
312 for (int i = 0; i < steps; i++) |
|
313 { |
|
314 MAYBE_DO_BREAKPOINT; |
3180
|
315 |
3877
|
316 double tmp_val = b + i * increment; |
|
317 |
|
318 octave_value val (tmp_val); |
3180
|
319 |
3877
|
320 bool quit = false; |
3180
|
321 |
3877
|
322 do_for_loop_once (ult, val, quit); |
3180
|
323 |
3877
|
324 if (quit) |
|
325 break; |
|
326 } |
|
327 } |
|
328 else if (rhs.is_scalar_type ()) |
|
329 { |
|
330 bool quit = false; |
|
331 |
|
332 MAYBE_DO_BREAKPOINT; |
2982
|
333 |
3877
|
334 do_for_loop_once (ult, rhs, quit); |
|
335 } |
|
336 else if (rhs.is_string ()) |
|
337 { |
|
338 charMatrix chm_tmp = rhs.char_matrix_value (); |
|
339 int nr = chm_tmp.rows (); |
|
340 int steps = chm_tmp.columns (); |
3215
|
341 |
3877
|
342 if (error_state) |
|
343 goto cleanup; |
3215
|
344 |
3877
|
345 if (nr == 1) |
|
346 DO_LOOP (chm_tmp (0, i)); |
|
347 else |
|
348 { |
|
349 for (int i = 0; i < steps; i++) |
|
350 { |
|
351 MAYBE_DO_BREAKPOINT; |
3770
|
352 |
3877
|
353 octave_value val (chm_tmp.extract (0, i, nr-1, i), true); |
3215
|
354 |
3877
|
355 bool quit = false; |
3215
|
356 |
3877
|
357 do_for_loop_once (ult, val, quit); |
3215
|
358 |
3877
|
359 if (quit) |
|
360 break; |
|
361 } |
|
362 } |
|
363 } |
|
364 else if (rhs.is_matrix_type ()) |
|
365 { |
4911
|
366 NDArray m_tmp; |
|
367 ComplexNDArray cm_tmp; |
|
368 dim_vector dv; |
2982
|
369 |
3998
|
370 if (rhs.is_real_type ()) |
3877
|
371 { |
4911
|
372 m_tmp = rhs.array_value (); |
|
373 dv = m_tmp.dims (); |
3877
|
374 } |
|
375 else |
|
376 { |
4911
|
377 cm_tmp = rhs.complex_array_value (); |
|
378 dv = cm_tmp.dims (); |
3877
|
379 } |
2982
|
380 |
3877
|
381 if (error_state) |
|
382 goto cleanup; |
3180
|
383 |
5246
|
384 if (dv.numel () > 0) |
|
385 { |
|
386 if (rhs.is_real_type ()) |
|
387 DO_ND_LOOP(m_tmp); |
|
388 else |
|
389 DO_ND_LOOP(cm_tmp); |
|
390 } |
3877
|
391 } |
|
392 else if (rhs.is_map ()) |
|
393 { |
|
394 Octave_map tmp_val (rhs.map_value ()); |
2982
|
395 |
4219
|
396 for (Octave_map::iterator p = tmp_val.begin (); |
|
397 p != tmp_val.end (); |
|
398 p++) |
3877
|
399 { |
|
400 MAYBE_DO_BREAKPOINT; |
3770
|
401 |
4513
|
402 Cell val_lst = tmp_val.contents (p); |
4121
|
403 |
|
404 octave_value val |
|
405 = (val_lst.length () == 1) ? val_lst(0) : octave_value (val_lst); |
2982
|
406 |
3877
|
407 bool quit = false; |
2982
|
408 |
3877
|
409 do_for_loop_once (ult, val, quit); |
2982
|
410 |
3877
|
411 if (quit) |
|
412 break; |
|
413 } |
|
414 } |
4911
|
415 else if (rhs.is_cell ()) |
|
416 { |
|
417 Cell c_tmp = rhs.cell_value (); |
5248
|
418 |
4911
|
419 dim_vector dv = c_tmp.dims (); |
5248
|
420 |
|
421 if (dv.numel () > 0) |
|
422 DO_ND_LOOP(c_tmp); |
4911
|
423 } |
3877
|
424 else |
|
425 { |
|
426 ::error ("invalid type in for loop expression near line %d, column %d", |
|
427 line (), column ()); |
|
428 } |
|
429 } |
|
430 |
|
431 cleanup: |
|
432 unwind_protect::run_frame ("simple_for_command::eval"); |
2982
|
433 } |
|
434 |
|
435 void |
|
436 tree_simple_for_command::eval_error (void) |
|
437 { |
3965
|
438 ::error ("evaluating for command near line %d, column %d", |
|
439 line (), column ()); |
2982
|
440 } |
|
441 |
|
442 void |
|
443 tree_simple_for_command::accept (tree_walker& tw) |
|
444 { |
|
445 tw.visit_simple_for_command (*this); |
|
446 } |
|
447 |
|
448 tree_complex_for_command::~tree_complex_for_command (void) |
|
449 { |
|
450 delete expr; |
|
451 delete list; |
3665
|
452 delete lead_comm; |
|
453 delete trail_comm; |
2982
|
454 } |
|
455 |
|
456 void |
|
457 tree_complex_for_command::do_for_loop_once (octave_lvalue &val_ref, |
|
458 octave_lvalue &key_ref, |
|
459 const octave_value& val, |
|
460 const octave_value& key, |
|
461 bool& quit) |
|
462 { |
|
463 quit = false; |
|
464 |
3538
|
465 val_ref.assign (octave_value::op_asn_eq, val); |
|
466 key_ref.assign (octave_value::op_asn_eq, key); |
2982
|
467 |
|
468 if (! error_state) |
|
469 { |
|
470 if (list) |
|
471 { |
|
472 list->eval (); |
|
473 |
|
474 if (error_state) |
|
475 eval_error (); |
|
476 } |
|
477 } |
|
478 else |
|
479 eval_error (); |
|
480 |
|
481 quit = quit_loop_now (); |
|
482 } |
|
483 |
|
484 void |
|
485 tree_complex_for_command::eval (void) |
|
486 { |
|
487 if (error_state) |
|
488 return; |
|
489 |
3877
|
490 unwind_protect::begin_frame ("complex_for_command::eval"); |
|
491 |
|
492 unwind_protect_bool (evaluating_looping_command); |
|
493 |
|
494 evaluating_looping_command = true; |
|
495 |
2982
|
496 octave_value rhs = expr->rvalue (); |
|
497 |
|
498 if (error_state || rhs.is_undefined ()) |
|
499 { |
|
500 eval_error (); |
3877
|
501 goto cleanup; |
2982
|
502 } |
|
503 |
|
504 if (rhs.is_map ()) |
|
505 { |
|
506 // Cycle through structure elements. First element of id_list |
|
507 // is set to value and the second is set to the name of the |
|
508 // structure element. |
|
509 |
4219
|
510 tree_argument_list::iterator p = lhs->begin (); |
|
511 tree_expression *elt = *p++; |
2982
|
512 octave_lvalue val_ref = elt->lvalue (); |
4219
|
513 elt = *p; |
2982
|
514 octave_lvalue key_ref = elt->lvalue (); |
|
515 |
|
516 Octave_map tmp_val (rhs.map_value ()); |
|
517 |
4300
|
518 for (Octave_map::iterator q = tmp_val.begin (); q != tmp_val.end (); q++) |
2982
|
519 { |
4219
|
520 octave_value key = tmp_val.key (q); |
4121
|
521 |
4513
|
522 Cell val_lst = tmp_val.contents (q); |
4121
|
523 |
4561
|
524 int n = tmp_val.numel (); |
4121
|
525 |
|
526 octave_value val = (n == 1) ? val_lst(0) : octave_value (val_lst); |
2982
|
527 |
3770
|
528 MAYBE_DO_BREAKPOINT; |
|
529 |
2982
|
530 bool quit = false; |
|
531 |
|
532 do_for_loop_once (key_ref, val_ref, key, val, quit); |
|
533 |
|
534 if (quit) |
|
535 break; |
|
536 } |
|
537 } |
|
538 else |
|
539 error ("in statement `for [X, Y] = VAL', VAL must be a structure"); |
3877
|
540 |
|
541 cleanup: |
|
542 unwind_protect::run_frame ("complex_for_command::eval"); |
2982
|
543 } |
|
544 |
|
545 void |
|
546 tree_complex_for_command::eval_error (void) |
|
547 { |
3965
|
548 ::error ("evaluating for command near line %d, column %d", |
|
549 line (), column ()); |
2982
|
550 } |
|
551 |
|
552 void |
|
553 tree_complex_for_command::accept (tree_walker& tw) |
|
554 { |
|
555 tw.visit_complex_for_command (*this); |
|
556 } |
|
557 |
|
558 /* |
|
559 ;;; Local Variables: *** |
|
560 ;;; mode: C++ *** |
|
561 ;;; End: *** |
|
562 */ |