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 |
|
118 // For. |
|
119 |
|
120 tree_simple_for_command::~tree_simple_for_command (void) |
|
121 { |
|
122 delete expr; |
|
123 delete list; |
|
124 } |
|
125 |
|
126 inline void |
|
127 tree_simple_for_command::do_for_loop_once (octave_lvalue& ult, |
|
128 const octave_value& rhs, |
|
129 bool& quit) |
|
130 { |
|
131 quit = false; |
|
132 |
|
133 ult.assign (octave_value::asn_eq, rhs); |
|
134 |
|
135 if (! error_state) |
|
136 { |
|
137 if (list) |
|
138 { |
|
139 list->eval (); |
|
140 |
|
141 if (error_state) |
|
142 eval_error (); |
|
143 } |
|
144 } |
|
145 else |
|
146 eval_error (); |
|
147 |
|
148 quit = quit_loop_now (); |
|
149 } |
|
150 |
|
151 #define DO_LOOP(arg) \ |
|
152 do \ |
|
153 { \ |
|
154 for (int i = 0; i < steps; i++) \ |
|
155 { \ |
|
156 octave_value val (arg); \ |
|
157 \ |
|
158 bool quit = false; \ |
|
159 \ |
|
160 do_for_loop_once (ult, val, quit); \ |
|
161 \ |
|
162 if (quit) \ |
|
163 break; \ |
|
164 } \ |
|
165 } \ |
|
166 while (0) |
|
167 |
|
168 void |
|
169 tree_simple_for_command::eval (void) |
|
170 { |
|
171 if (error_state) |
|
172 return; |
|
173 |
|
174 octave_value rhs = expr->rvalue (); |
|
175 |
|
176 if (error_state || rhs.is_undefined ()) |
|
177 { |
|
178 eval_error (); |
|
179 return; |
|
180 } |
|
181 |
|
182 octave_lvalue ult = lhs->lvalue (); |
|
183 |
|
184 if (error_state) |
|
185 { |
|
186 eval_error (); |
|
187 return; |
|
188 } |
|
189 |
|
190 if (rhs.is_scalar_type ()) |
|
191 { |
|
192 bool quit = false; |
|
193 |
|
194 do_for_loop_once (ult, rhs, quit); |
|
195 } |
|
196 else if (rhs.is_matrix_type ()) |
|
197 { |
|
198 Matrix m_tmp; |
|
199 ComplexMatrix cm_tmp; |
|
200 |
|
201 int nr; |
|
202 int steps; |
|
203 |
|
204 if (rhs.is_real_matrix ()) |
|
205 { |
|
206 m_tmp = rhs.matrix_value (); |
|
207 nr = m_tmp.rows (); |
|
208 steps = m_tmp.columns (); |
|
209 } |
|
210 else |
|
211 { |
|
212 cm_tmp = rhs.complex_matrix_value (); |
|
213 nr = cm_tmp.rows (); |
|
214 steps = cm_tmp.columns (); |
|
215 } |
|
216 |
|
217 if (rhs.is_real_matrix ()) |
|
218 { |
|
219 if (nr == 1) |
|
220 DO_LOOP (m_tmp (0, i)); |
|
221 else |
|
222 DO_LOOP (m_tmp.extract (0, i, nr-1, i)); |
|
223 } |
|
224 else |
|
225 { |
|
226 if (nr == 1) |
|
227 DO_LOOP (cm_tmp (0, i)); |
|
228 else |
|
229 DO_LOOP (cm_tmp.extract (0, i, nr-1, i)); |
|
230 } |
|
231 } |
|
232 else if (rhs.is_string ()) |
|
233 { |
|
234 gripe_string_invalid (); |
|
235 } |
|
236 else if (rhs.is_range ()) |
|
237 { |
|
238 Range rng = rhs.range_value (); |
|
239 |
|
240 int steps = rng.nelem (); |
|
241 double b = rng.base (); |
|
242 double increment = rng.inc (); |
|
243 |
|
244 for (int i = 0; i < steps; i++) |
|
245 { |
|
246 double tmp_val = b + i * increment; |
|
247 |
|
248 octave_value val (tmp_val); |
|
249 |
|
250 bool quit = false; |
|
251 |
|
252 do_for_loop_once (ult, val, quit); |
|
253 |
|
254 if (quit) |
|
255 break; |
|
256 } |
|
257 } |
|
258 else if (rhs.is_map ()) |
|
259 { |
|
260 Octave_map tmp_val (rhs.map_value ()); |
|
261 |
|
262 for (Pix p = tmp_val.first (); p != 0; tmp_val.next (p)) |
|
263 { |
|
264 octave_value val = tmp_val.contents (p); |
|
265 |
|
266 bool quit = false; |
|
267 |
|
268 do_for_loop_once (ult, val, quit); |
|
269 |
|
270 if (quit) |
|
271 break; |
|
272 } |
|
273 } |
|
274 else |
|
275 { |
|
276 ::error ("invalid type in for loop expression near line %d, column %d", |
|
277 line (), column ()); |
|
278 } |
|
279 } |
|
280 |
|
281 void |
|
282 tree_simple_for_command::eval_error (void) |
|
283 { |
|
284 if (error_state > 0) |
|
285 ::error ("evaluating for command near line %d, column %d", |
|
286 line (), column ()); |
|
287 } |
|
288 |
|
289 void |
|
290 tree_simple_for_command::accept (tree_walker& tw) |
|
291 { |
|
292 tw.visit_simple_for_command (*this); |
|
293 } |
|
294 |
|
295 tree_complex_for_command::~tree_complex_for_command (void) |
|
296 { |
|
297 delete expr; |
|
298 delete list; |
|
299 } |
|
300 |
|
301 void |
|
302 tree_complex_for_command::do_for_loop_once (octave_lvalue &val_ref, |
|
303 octave_lvalue &key_ref, |
|
304 const octave_value& val, |
|
305 const octave_value& key, |
|
306 bool& quit) |
|
307 { |
|
308 quit = false; |
|
309 |
|
310 val_ref.assign (octave_value::asn_eq, val); |
|
311 key_ref.assign (octave_value::asn_eq, key); |
|
312 |
|
313 if (! error_state) |
|
314 { |
|
315 if (list) |
|
316 { |
|
317 list->eval (); |
|
318 |
|
319 if (error_state) |
|
320 eval_error (); |
|
321 } |
|
322 } |
|
323 else |
|
324 eval_error (); |
|
325 |
|
326 quit = quit_loop_now (); |
|
327 } |
|
328 |
|
329 void |
|
330 tree_complex_for_command::eval (void) |
|
331 { |
|
332 if (error_state) |
|
333 return; |
|
334 |
|
335 octave_value rhs = expr->rvalue (); |
|
336 |
|
337 if (error_state || rhs.is_undefined ()) |
|
338 { |
|
339 eval_error (); |
|
340 return; |
|
341 } |
|
342 |
|
343 if (rhs.is_map ()) |
|
344 { |
|
345 // Cycle through structure elements. First element of id_list |
|
346 // is set to value and the second is set to the name of the |
|
347 // structure element. |
|
348 |
|
349 Pix p = lhs->first (); |
|
350 tree_expression *elt = lhs->operator () (p); |
|
351 octave_lvalue val_ref = elt->lvalue (); |
|
352 |
|
353 lhs->next (p); |
|
354 elt = lhs->operator () (p); |
|
355 octave_lvalue key_ref = elt->lvalue (); |
|
356 |
|
357 Octave_map tmp_val (rhs.map_value ()); |
|
358 |
|
359 for (p = tmp_val.first (); p != 0; tmp_val.next (p)) |
|
360 { |
|
361 octave_value key = tmp_val.key (p); |
|
362 octave_value val = tmp_val.contents (p); |
|
363 |
|
364 bool quit = false; |
|
365 |
|
366 do_for_loop_once (key_ref, val_ref, key, val, quit); |
|
367 |
|
368 if (quit) |
|
369 break; |
|
370 } |
|
371 } |
|
372 else |
|
373 error ("in statement `for [X, Y] = VAL', VAL must be a structure"); |
|
374 } |
|
375 |
|
376 void |
|
377 tree_complex_for_command::eval_error (void) |
|
378 { |
|
379 if (error_state > 0) |
|
380 ::error ("evaluating for command near line %d, column %d", |
|
381 line (), column ()); |
|
382 } |
|
383 |
|
384 void |
|
385 tree_complex_for_command::accept (tree_walker& tw) |
|
386 { |
|
387 tw.visit_complex_for_command (*this); |
|
388 } |
|
389 |
|
390 /* |
|
391 ;;; Local Variables: *** |
|
392 ;;; mode: C++ *** |
|
393 ;;; End: *** |
|
394 */ |