529
|
1 // tree-cmd.cc -*- C++ -*- |
494
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993, 1994 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
|
26 #endif |
|
27 |
|
28 #include <iostream.h> |
|
29 |
578
|
30 // Nonzero means we're breaking out of a loop. |
|
31 int breaking = 0; |
|
32 |
|
33 // Nonzero means we're jumping to the end of a loop. |
|
34 int continuing = 0; |
|
35 |
|
36 // Nonzero means we're returning from a function. Global because it |
|
37 // is also needed in tree-expr.cc. |
|
38 int returning = 0; |
|
39 |
494
|
40 #include "user-prefs.h" |
|
41 #include "variables.h" |
|
42 #include "symtab.h" |
|
43 #include "error.h" |
|
44 #include "gripes.h" |
584
|
45 #include "tree-base.h" |
578
|
46 #include "tree-expr.h" |
494
|
47 #include "tree-cmd.h" |
578
|
48 #include "tree-misc.h" |
494
|
49 #include "tree-const.h" |
|
50 |
|
51 // Decide if it's time to quit a for or while loop. |
|
52 static int |
|
53 quit_loop_now (void) |
|
54 { |
|
55 // Maybe handle `continue N' someday... |
|
56 |
|
57 if (continuing) |
|
58 continuing--; |
|
59 |
|
60 int quit = (returning || breaking || continuing); |
|
61 |
|
62 if (breaking) |
|
63 breaking--; |
|
64 |
|
65 return quit; |
|
66 } |
|
67 |
|
68 // But first, some extra functions used by the tree classes. |
|
69 |
|
70 // We seem to have no use for this now. Maybe it will be needed at |
|
71 // some future date, so here it is. |
|
72 #if 0 |
578
|
73 |
|
74 // Convert a linked list of trees to a vector of pointers to trees. |
|
75 |
494
|
76 static tree ** |
|
77 list_to_vector (tree *list, int& len) |
|
78 { |
|
79 len = list->length () + 1; |
|
80 |
|
81 tree **args = new tree * [len]; |
|
82 |
|
83 // args[0] may eventually hold something useful, like the function |
|
84 // name. |
|
85 tree *tmp_list = list; |
|
86 for (int k = 1; k < len; k++) |
|
87 { |
|
88 args[k] = tmp_list; |
|
89 tmp_list = tmp_list->next_elem (); |
|
90 } |
|
91 return args; |
|
92 } |
|
93 #endif |
|
94 |
578
|
95 // Global. |
494
|
96 |
578
|
97 tree_global_command::~tree_global_command (void) |
494
|
98 { |
578
|
99 delete init_list; |
494
|
100 } |
|
101 |
|
102 void |
578
|
103 tree_global_command::eval (void) |
494
|
104 { |
578
|
105 if (init_list) |
|
106 init_list->eval (); |
494
|
107 |
|
108 if (error_state > 0) |
|
109 ::error ("evaluating global command near line %d, column %d", |
|
110 line (), column ()); |
|
111 } |
|
112 |
581
|
113 void |
|
114 tree_global_command::print_code (ostream& os) |
|
115 { |
|
116 print_code_indent (os); |
|
117 |
|
118 os << "global "; |
|
119 |
|
120 if (init_list) |
|
121 init_list->print_code (os); |
|
122 } |
|
123 |
578
|
124 // While. |
494
|
125 |
|
126 tree_while_command::~tree_while_command (void) |
|
127 { |
|
128 delete expr; |
|
129 delete list; |
|
130 } |
|
131 |
578
|
132 void |
|
133 tree_while_command::eval (void) |
494
|
134 { |
|
135 if (error_state) |
578
|
136 return; |
494
|
137 |
|
138 for (;;) |
|
139 { |
|
140 int expr_value = 0; |
529
|
141 if (! expr) |
578
|
142 return; |
494
|
143 tree_constant t1 = expr->eval (0); |
|
144 |
|
145 if (error_state) |
|
146 { |
|
147 eval_error (); |
578
|
148 return; |
494
|
149 } |
|
150 |
|
151 if (t1.rows () == 0 || t1.columns () == 0) |
|
152 { |
|
153 int flag = user_pref.propagate_empty_matrices; |
|
154 if (flag < 0) |
|
155 warning ("while: empty matrix used in conditional"); |
|
156 else if (flag == 0) |
|
157 { |
620
|
158 ::error ("empty matrix used in while condition near line\ |
|
159 %d, column %d", line (), column ()); |
578
|
160 return; |
494
|
161 } |
|
162 t1 = tree_constant (0.0); |
|
163 } |
|
164 else if (! t1.is_scalar_type ()) |
|
165 { |
|
166 tree_constant t2 = t1.all (); |
|
167 t1 = t2.all (); |
|
168 } |
|
169 |
620
|
170 if (t1.is_real_scalar ()) |
494
|
171 expr_value = (int) t1.double_value (); |
620
|
172 else if (t1.is_complex_scalar ()) |
494
|
173 expr_value = t1.complex_value () != 0.0; |
|
174 else |
620
|
175 { |
|
176 ::error ("invalid type used in while condition near line %d,\ |
|
177 column %d", line (), column ()); |
|
178 return; |
|
179 } |
494
|
180 |
|
181 if (expr_value) |
|
182 { |
529
|
183 if (list) |
494
|
184 { |
578
|
185 list->eval (1); |
494
|
186 if (error_state) |
|
187 { |
|
188 eval_error (); |
578
|
189 return; |
494
|
190 } |
|
191 } |
|
192 |
|
193 if (quit_loop_now ()) |
|
194 break; |
|
195 } |
|
196 else |
|
197 break; |
|
198 } |
|
199 } |
|
200 |
|
201 void |
|
202 tree_while_command::eval_error (void) |
|
203 { |
|
204 if (error_state > 0) |
|
205 ::error ("evaluating while command near line %d, column %d", |
|
206 line (), column ()); |
|
207 } |
|
208 |
581
|
209 void |
|
210 tree_while_command::print_code (ostream& os) |
|
211 { |
|
212 print_code_indent (os); |
|
213 |
|
214 os << "while "; |
|
215 |
|
216 if (expr) |
|
217 expr->print_code (os); |
|
218 |
|
219 print_code_new_line (os); |
|
220 |
|
221 if (list) |
|
222 { |
|
223 increment_indent_level (); |
|
224 list->print_code (os); |
|
225 decrement_indent_level (); |
|
226 } |
|
227 |
|
228 print_code_indent (os); |
|
229 |
|
230 os << "endwhile"; |
|
231 } |
|
232 |
578
|
233 // For. |
494
|
234 |
|
235 tree_for_command::~tree_for_command (void) |
|
236 { |
|
237 delete id; |
|
238 delete expr; |
|
239 delete list; |
|
240 } |
|
241 |
578
|
242 void |
|
243 tree_for_command::eval (void) |
494
|
244 { |
529
|
245 if (error_state || ! expr) |
578
|
246 return; |
494
|
247 |
|
248 tree_constant tmp_expr = expr->eval (0); |
|
249 |
|
250 if (error_state || tmp_expr.is_undefined ()) |
|
251 { |
|
252 eval_error (); |
578
|
253 return; |
494
|
254 } |
|
255 |
620
|
256 if (tmp_expr.is_scalar_type ()) |
|
257 { |
|
258 tree_constant *rhs = new tree_constant (tmp_expr); |
|
259 int quit = 0; |
|
260 do_for_loop_once (rhs, quit); |
|
261 } |
|
262 else if (tmp_expr.is_matrix_type ()) |
494
|
263 { |
620
|
264 Matrix m_tmp; |
|
265 ComplexMatrix cm_tmp; |
|
266 int nr; |
|
267 int steps; |
|
268 if (tmp_expr.is_real_matrix ()) |
|
269 { |
|
270 m_tmp = tmp_expr.matrix_value (); |
|
271 nr = m_tmp.rows (); |
|
272 steps = m_tmp.columns (); |
|
273 } |
|
274 else |
|
275 { |
|
276 cm_tmp = tmp_expr.complex_matrix_value (); |
|
277 nr = cm_tmp.rows (); |
|
278 steps = cm_tmp.columns (); |
|
279 } |
494
|
280 |
620
|
281 for (int i = 0; i < steps; i++) |
|
282 { |
|
283 tree_constant *rhs; |
494
|
284 |
620
|
285 if (nr == 1) |
|
286 { |
|
287 if (tmp_expr.is_real_matrix ()) |
|
288 rhs = new tree_constant (m_tmp (0, i)); |
|
289 else |
|
290 rhs = new tree_constant (cm_tmp (0, i)); |
|
291 } |
|
292 else |
|
293 { |
|
294 if (tmp_expr.is_real_matrix ()) |
|
295 rhs = new tree_constant (m_tmp.extract (0, i, nr-1, i)); |
|
296 else |
|
297 rhs = new tree_constant (cm_tmp.extract (0, i, nr-1, i)); |
|
298 } |
494
|
299 |
620
|
300 int quit = 0; |
|
301 do_for_loop_once (rhs, quit); |
|
302 if (quit) |
|
303 break; |
|
304 } |
|
305 } |
|
306 else if (tmp_expr.is_string ()) |
|
307 { |
494
|
308 gripe_string_invalid (); |
620
|
309 } |
|
310 else if (tmp_expr.is_range ()) |
|
311 { |
|
312 Range rng = tmp_expr.range_value (); |
494
|
313 |
620
|
314 int steps = rng.nelem (); |
|
315 double b = rng.base (); |
|
316 double increment = rng.inc (); |
494
|
317 |
620
|
318 for (int i = 0; i < steps; i++) |
|
319 { |
|
320 double tmp_val = b + i * increment; |
494
|
321 |
620
|
322 tree_constant *rhs = new tree_constant (tmp_val); |
494
|
323 |
620
|
324 int quit = 0; |
|
325 do_for_loop_once (rhs, quit); |
|
326 if (quit) |
|
327 break; |
|
328 } |
|
329 } |
|
330 else |
|
331 { |
|
332 ::error ("invalid type in for loop expression near line %d, column %d", |
|
333 line (), column ()); |
494
|
334 } |
|
335 } |
|
336 |
|
337 void |
|
338 tree_for_command::eval_error (void) |
|
339 { |
|
340 if (error_state > 0) |
|
341 ::error ("evaluating for command near line %d, column %d", |
|
342 line (), column ()); |
|
343 } |
|
344 |
578
|
345 void |
494
|
346 tree_for_command::do_for_loop_once (tree_constant *rhs, int& quit) |
|
347 { |
|
348 quit = 0; |
|
349 |
578
|
350 tree_simple_assignment_expression tmp_ass (id, rhs, 1); |
494
|
351 |
|
352 tmp_ass.eval (0); |
|
353 |
|
354 if (error_state) |
|
355 { |
|
356 eval_error (); |
578
|
357 return; |
494
|
358 } |
|
359 |
529
|
360 if (list) |
494
|
361 { |
578
|
362 list->eval (1); |
494
|
363 if (error_state) |
|
364 { |
|
365 eval_error (); |
|
366 quit = 1; |
578
|
367 return; |
494
|
368 } |
|
369 } |
|
370 |
|
371 quit = quit_loop_now (); |
|
372 } |
|
373 |
581
|
374 void |
|
375 tree_for_command::print_code (ostream& os) |
|
376 { |
|
377 print_code_indent (os); |
|
378 |
|
379 os << "for "; |
|
380 |
|
381 if (id) |
|
382 id->print_code (os); |
|
383 |
|
384 os << " = "; |
|
385 |
|
386 if (expr) |
|
387 expr->print_code (os); |
|
388 |
|
389 print_code_new_line (os); |
|
390 |
|
391 if (list) |
|
392 { |
|
393 increment_indent_level (); |
|
394 list->print_code (os); |
|
395 decrement_indent_level (); |
|
396 } |
|
397 |
|
398 print_code_indent (os); |
|
399 |
|
400 os << "endfor"; |
|
401 } |
|
402 |
578
|
403 // If. |
494
|
404 |
|
405 tree_if_command::~tree_if_command (void) |
|
406 { |
|
407 delete list; |
|
408 } |
|
409 |
|
410 void |
578
|
411 tree_if_command::eval (void) |
494
|
412 { |
578
|
413 if (list) |
|
414 list->eval (); |
|
415 |
494
|
416 if (error_state > 0) |
|
417 ::error ("evaluating if command near line %d, column %d", |
|
418 line (), column ()); |
|
419 } |
|
420 |
581
|
421 void |
|
422 tree_if_command::print_code (ostream& os) |
|
423 { |
|
424 print_code_indent (os); |
|
425 |
|
426 os << "if "; |
|
427 |
|
428 if (list) |
|
429 list->print_code (os); |
|
430 |
|
431 print_code_indent (os); |
|
432 |
|
433 os << "endif"; |
|
434 } |
|
435 |
578
|
436 // Break. |
494
|
437 |
578
|
438 void |
|
439 tree_break_command::eval (void) |
494
|
440 { |
|
441 if (! error_state) |
|
442 breaking = 1; |
|
443 } |
|
444 |
581
|
445 void |
|
446 tree_break_command::print_code (ostream& os) |
|
447 { |
|
448 print_code_indent (os); |
|
449 |
|
450 os << "break"; |
|
451 } |
|
452 |
578
|
453 // Continue. |
494
|
454 |
578
|
455 void |
|
456 tree_continue_command::eval (void) |
494
|
457 { |
|
458 if (! error_state) |
|
459 continuing = 1; |
|
460 } |
|
461 |
581
|
462 void |
|
463 tree_continue_command::print_code (ostream& os) |
|
464 { |
|
465 print_code_indent (os); |
|
466 |
|
467 os << "continue"; |
|
468 } |
|
469 |
578
|
470 // Return. |
494
|
471 |
578
|
472 void |
|
473 tree_return_command::eval (void) |
494
|
474 { |
|
475 if (! error_state) |
|
476 returning = 1; |
|
477 } |
|
478 |
581
|
479 void |
|
480 tree_return_command::print_code (ostream& os) |
|
481 { |
|
482 print_code_indent (os); |
|
483 |
|
484 os << "return"; |
|
485 } |
|
486 |
494
|
487 /* |
|
488 ;;; Local Variables: *** |
|
489 ;;; mode: C++ *** |
|
490 ;;; page-delimiter: "^/\\*" *** |
|
491 ;;; End: *** |
|
492 */ |