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