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" |
3877
|
44 #include "unwind-prot.h" |
|
45 |
|
46 // TRUE means we are evaluating some kind of looping construct. |
|
47 bool evaluating_looping_command = false; |
2982
|
48 |
|
49 // Decide if it's time to quit a for or while loop. |
|
50 static inline bool |
|
51 quit_loop_now (void) |
|
52 { |
|
53 // Maybe handle `continue N' someday... |
|
54 |
2985
|
55 if (tree_continue_command::continuing) |
|
56 tree_continue_command::continuing--; |
2982
|
57 |
2985
|
58 bool quit = (error_state |
|
59 || tree_return_command::returning |
|
60 || tree_break_command::breaking |
|
61 || tree_continue_command::continuing); |
2982
|
62 |
2985
|
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 |
|
240 void |
|
241 tree_simple_for_command::eval (void) |
|
242 { |
|
243 if (error_state) |
|
244 return; |
|
245 |
3877
|
246 unwind_protect::begin_frame ("simple_for_command::eval"); |
|
247 |
|
248 unwind_protect_bool (evaluating_looping_command); |
|
249 |
|
250 evaluating_looping_command = true; |
|
251 |
2982
|
252 octave_value rhs = expr->rvalue (); |
|
253 |
|
254 if (error_state || rhs.is_undefined ()) |
|
255 { |
|
256 eval_error (); |
3877
|
257 goto cleanup; |
2982
|
258 } |
|
259 |
3877
|
260 { |
|
261 octave_lvalue ult = lhs->lvalue (); |
3180
|
262 |
3877
|
263 if (error_state) |
|
264 { |
|
265 eval_error (); |
|
266 goto cleanup; |
|
267 } |
|
268 |
|
269 if (rhs.is_range ()) |
|
270 { |
|
271 Range rng = rhs.range_value (); |
3180
|
272 |
3877
|
273 int steps = rng.nelem (); |
|
274 double b = rng.base (); |
|
275 double increment = rng.inc (); |
3770
|
276 |
3877
|
277 for (int i = 0; i < steps; i++) |
|
278 { |
|
279 MAYBE_DO_BREAKPOINT; |
3180
|
280 |
3877
|
281 double tmp_val = b + i * increment; |
|
282 |
|
283 octave_value val (tmp_val); |
3180
|
284 |
3877
|
285 bool quit = false; |
3180
|
286 |
3877
|
287 do_for_loop_once (ult, val, quit); |
3180
|
288 |
3877
|
289 if (quit) |
|
290 break; |
|
291 } |
|
292 } |
|
293 else if (rhs.is_scalar_type ()) |
|
294 { |
|
295 bool quit = false; |
|
296 |
|
297 MAYBE_DO_BREAKPOINT; |
2982
|
298 |
3877
|
299 do_for_loop_once (ult, rhs, quit); |
|
300 } |
|
301 else if (rhs.is_string ()) |
|
302 { |
|
303 charMatrix chm_tmp = rhs.char_matrix_value (); |
|
304 int nr = chm_tmp.rows (); |
|
305 int steps = chm_tmp.columns (); |
3215
|
306 |
3877
|
307 if (error_state) |
|
308 goto cleanup; |
3215
|
309 |
3877
|
310 if (nr == 1) |
|
311 DO_LOOP (chm_tmp (0, i)); |
|
312 else |
|
313 { |
|
314 for (int i = 0; i < steps; i++) |
|
315 { |
|
316 MAYBE_DO_BREAKPOINT; |
3770
|
317 |
3877
|
318 octave_value val (chm_tmp.extract (0, i, nr-1, i), true); |
3215
|
319 |
3877
|
320 bool quit = false; |
3215
|
321 |
3877
|
322 do_for_loop_once (ult, val, quit); |
3215
|
323 |
3877
|
324 if (quit) |
|
325 break; |
|
326 } |
|
327 } |
|
328 } |
|
329 else if (rhs.is_matrix_type ()) |
|
330 { |
|
331 Matrix m_tmp; |
|
332 ComplexMatrix cm_tmp; |
2982
|
333 |
3877
|
334 int nr; |
|
335 int steps; |
2982
|
336 |
3877
|
337 if (rhs.is_real_matrix ()) |
|
338 { |
|
339 m_tmp = rhs.matrix_value (); |
|
340 nr = m_tmp.rows (); |
|
341 steps = m_tmp.columns (); |
|
342 } |
|
343 else |
|
344 { |
|
345 cm_tmp = rhs.complex_matrix_value (); |
|
346 nr = cm_tmp.rows (); |
|
347 steps = cm_tmp.columns (); |
|
348 } |
2982
|
349 |
3877
|
350 if (error_state) |
|
351 goto cleanup; |
3180
|
352 |
3877
|
353 if (rhs.is_real_matrix ()) |
|
354 { |
|
355 if (nr == 1) |
|
356 DO_LOOP (m_tmp (0, i)); |
|
357 else |
|
358 DO_LOOP (m_tmp.extract (0, i, nr-1, i)); |
|
359 } |
|
360 else |
|
361 { |
|
362 if (nr == 1) |
|
363 DO_LOOP (cm_tmp (0, i)); |
|
364 else |
|
365 DO_LOOP (cm_tmp.extract (0, i, nr-1, i)); |
|
366 } |
|
367 } |
|
368 else if (rhs.is_map ()) |
|
369 { |
|
370 Octave_map tmp_val (rhs.map_value ()); |
2982
|
371 |
3877
|
372 for (Pix p = tmp_val.first (); p != 0; tmp_val.next (p)) |
|
373 { |
|
374 MAYBE_DO_BREAKPOINT; |
3770
|
375 |
3877
|
376 octave_value val = tmp_val.contents (p); |
2982
|
377 |
3877
|
378 bool quit = false; |
2982
|
379 |
3877
|
380 do_for_loop_once (ult, val, quit); |
2982
|
381 |
3877
|
382 if (quit) |
|
383 break; |
|
384 } |
|
385 } |
|
386 else |
|
387 { |
|
388 ::error ("invalid type in for loop expression near line %d, column %d", |
|
389 line (), column ()); |
|
390 } |
|
391 } |
|
392 |
|
393 cleanup: |
|
394 unwind_protect::run_frame ("simple_for_command::eval"); |
2982
|
395 } |
|
396 |
|
397 void |
|
398 tree_simple_for_command::eval_error (void) |
|
399 { |
3965
|
400 ::error ("evaluating for command near line %d, column %d", |
|
401 line (), column ()); |
2982
|
402 } |
|
403 |
|
404 void |
|
405 tree_simple_for_command::accept (tree_walker& tw) |
|
406 { |
|
407 tw.visit_simple_for_command (*this); |
|
408 } |
|
409 |
|
410 tree_complex_for_command::~tree_complex_for_command (void) |
|
411 { |
|
412 delete expr; |
|
413 delete list; |
3665
|
414 delete lead_comm; |
|
415 delete trail_comm; |
2982
|
416 } |
|
417 |
|
418 void |
|
419 tree_complex_for_command::do_for_loop_once (octave_lvalue &val_ref, |
|
420 octave_lvalue &key_ref, |
|
421 const octave_value& val, |
|
422 const octave_value& key, |
|
423 bool& quit) |
|
424 { |
|
425 quit = false; |
|
426 |
3538
|
427 val_ref.assign (octave_value::op_asn_eq, val); |
|
428 key_ref.assign (octave_value::op_asn_eq, key); |
2982
|
429 |
|
430 if (! error_state) |
|
431 { |
|
432 if (list) |
|
433 { |
|
434 list->eval (); |
|
435 |
|
436 if (error_state) |
|
437 eval_error (); |
|
438 } |
|
439 } |
|
440 else |
|
441 eval_error (); |
|
442 |
|
443 quit = quit_loop_now (); |
|
444 } |
|
445 |
|
446 void |
|
447 tree_complex_for_command::eval (void) |
|
448 { |
|
449 if (error_state) |
|
450 return; |
|
451 |
3877
|
452 unwind_protect::begin_frame ("complex_for_command::eval"); |
|
453 |
|
454 unwind_protect_bool (evaluating_looping_command); |
|
455 |
|
456 evaluating_looping_command = true; |
|
457 |
2982
|
458 octave_value rhs = expr->rvalue (); |
|
459 |
|
460 if (error_state || rhs.is_undefined ()) |
|
461 { |
|
462 eval_error (); |
3877
|
463 goto cleanup; |
2982
|
464 } |
|
465 |
|
466 if (rhs.is_map ()) |
|
467 { |
|
468 // Cycle through structure elements. First element of id_list |
|
469 // is set to value and the second is set to the name of the |
|
470 // structure element. |
|
471 |
|
472 Pix p = lhs->first (); |
|
473 tree_expression *elt = lhs->operator () (p); |
|
474 octave_lvalue val_ref = elt->lvalue (); |
|
475 |
|
476 lhs->next (p); |
|
477 elt = lhs->operator () (p); |
|
478 octave_lvalue key_ref = elt->lvalue (); |
|
479 |
|
480 Octave_map tmp_val (rhs.map_value ()); |
|
481 |
|
482 for (p = tmp_val.first (); p != 0; tmp_val.next (p)) |
|
483 { |
|
484 octave_value key = tmp_val.key (p); |
|
485 octave_value val = tmp_val.contents (p); |
|
486 |
3770
|
487 MAYBE_DO_BREAKPOINT; |
|
488 |
2982
|
489 bool quit = false; |
|
490 |
|
491 do_for_loop_once (key_ref, val_ref, key, val, quit); |
|
492 |
|
493 if (quit) |
|
494 break; |
|
495 } |
|
496 } |
|
497 else |
|
498 error ("in statement `for [X, Y] = VAL', VAL must be a structure"); |
3877
|
499 |
|
500 cleanup: |
|
501 unwind_protect::run_frame ("complex_for_command::eval"); |
2982
|
502 } |
|
503 |
|
504 void |
|
505 tree_complex_for_command::eval_error (void) |
|
506 { |
3965
|
507 ::error ("evaluating for command near line %d, column %d", |
|
508 line (), column ()); |
2982
|
509 } |
|
510 |
|
511 void |
|
512 tree_complex_for_command::accept (tree_walker& tw) |
|
513 { |
|
514 tw.visit_complex_for_command (*this); |
|
515 } |
|
516 |
|
517 /* |
|
518 ;;; Local Variables: *** |
|
519 ;;; mode: C++ *** |
|
520 ;;; End: *** |
|
521 */ |