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