529
|
1 // tree-cmd.cc -*- C++ -*- |
494
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
494
|
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 |
1059
|
30 // Nonzero means we're breaking out of a loop or function body. |
578
|
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" |
916
|
50 #include "unwind-prot.h" |
494
|
51 |
|
52 // Decide if it's time to quit a for or while loop. |
1168
|
53 static inline int |
494
|
54 quit_loop_now (void) |
|
55 { |
|
56 // Maybe handle `continue N' someday... |
|
57 |
|
58 if (continuing) |
|
59 continuing--; |
|
60 |
|
61 int quit = (returning || breaking || continuing); |
|
62 |
|
63 if (breaking) |
|
64 breaking--; |
|
65 |
|
66 return quit; |
|
67 } |
|
68 |
|
69 // But first, some extra functions used by the tree classes. |
|
70 |
|
71 // We seem to have no use for this now. Maybe it will be needed at |
|
72 // some future date, so here it is. |
|
73 #if 0 |
578
|
74 |
|
75 // Convert a linked list of trees to a vector of pointers to trees. |
|
76 |
494
|
77 static tree ** |
|
78 list_to_vector (tree *list, int& len) |
|
79 { |
|
80 len = list->length () + 1; |
|
81 |
|
82 tree **args = new tree * [len]; |
|
83 |
|
84 // args[0] may eventually hold something useful, like the function |
|
85 // name. |
|
86 tree *tmp_list = list; |
|
87 for (int k = 1; k < len; k++) |
|
88 { |
|
89 args[k] = tmp_list; |
|
90 tmp_list = tmp_list->next_elem (); |
|
91 } |
|
92 return args; |
|
93 } |
|
94 #endif |
|
95 |
578
|
96 // Global. |
494
|
97 |
578
|
98 tree_global_command::~tree_global_command (void) |
494
|
99 { |
578
|
100 delete init_list; |
494
|
101 } |
|
102 |
|
103 void |
578
|
104 tree_global_command::eval (void) |
494
|
105 { |
578
|
106 if (init_list) |
|
107 init_list->eval (); |
494
|
108 |
|
109 if (error_state > 0) |
|
110 ::error ("evaluating global command near line %d, column %d", |
|
111 line (), column ()); |
|
112 } |
|
113 |
581
|
114 void |
|
115 tree_global_command::print_code (ostream& os) |
|
116 { |
|
117 print_code_indent (os); |
|
118 |
|
119 os << "global "; |
|
120 |
|
121 if (init_list) |
|
122 init_list->print_code (os); |
|
123 } |
|
124 |
578
|
125 // While. |
494
|
126 |
|
127 tree_while_command::~tree_while_command (void) |
|
128 { |
|
129 delete expr; |
|
130 delete list; |
|
131 } |
|
132 |
578
|
133 void |
|
134 tree_while_command::eval (void) |
494
|
135 { |
|
136 if (error_state) |
578
|
137 return; |
494
|
138 |
|
139 for (;;) |
|
140 { |
|
141 int expr_value = 0; |
529
|
142 if (! expr) |
578
|
143 return; |
494
|
144 tree_constant t1 = expr->eval (0); |
|
145 |
|
146 if (error_state) |
|
147 { |
|
148 eval_error (); |
578
|
149 return; |
494
|
150 } |
|
151 |
|
152 if (t1.rows () == 0 || t1.columns () == 0) |
|
153 { |
|
154 int flag = user_pref.propagate_empty_matrices; |
|
155 if (flag < 0) |
|
156 warning ("while: empty matrix used in conditional"); |
|
157 else if (flag == 0) |
|
158 { |
620
|
159 ::error ("empty matrix used in while condition near line\ |
|
160 %d, column %d", line (), column ()); |
578
|
161 return; |
494
|
162 } |
|
163 t1 = tree_constant (0.0); |
|
164 } |
|
165 else if (! t1.is_scalar_type ()) |
|
166 { |
|
167 tree_constant t2 = t1.all (); |
|
168 t1 = t2.all (); |
|
169 } |
|
170 |
620
|
171 if (t1.is_real_scalar ()) |
494
|
172 expr_value = (int) t1.double_value (); |
620
|
173 else if (t1.is_complex_scalar ()) |
494
|
174 expr_value = t1.complex_value () != 0.0; |
|
175 else |
620
|
176 { |
|
177 ::error ("invalid type used in while condition near line %d,\ |
|
178 column %d", line (), column ()); |
|
179 return; |
|
180 } |
494
|
181 |
|
182 if (expr_value) |
|
183 { |
529
|
184 if (list) |
494
|
185 { |
578
|
186 list->eval (1); |
494
|
187 if (error_state) |
|
188 { |
|
189 eval_error (); |
578
|
190 return; |
494
|
191 } |
|
192 } |
|
193 |
|
194 if (quit_loop_now ()) |
|
195 break; |
|
196 } |
|
197 else |
|
198 break; |
|
199 } |
|
200 } |
|
201 |
|
202 void |
|
203 tree_while_command::eval_error (void) |
|
204 { |
|
205 if (error_state > 0) |
|
206 ::error ("evaluating while command near line %d, column %d", |
|
207 line (), column ()); |
|
208 } |
|
209 |
581
|
210 void |
|
211 tree_while_command::print_code (ostream& os) |
|
212 { |
|
213 print_code_indent (os); |
|
214 |
|
215 os << "while "; |
|
216 |
|
217 if (expr) |
|
218 expr->print_code (os); |
|
219 |
|
220 print_code_new_line (os); |
|
221 |
|
222 if (list) |
|
223 { |
|
224 increment_indent_level (); |
|
225 list->print_code (os); |
|
226 decrement_indent_level (); |
|
227 } |
|
228 |
|
229 print_code_indent (os); |
|
230 |
|
231 os << "endwhile"; |
|
232 } |
|
233 |
578
|
234 // For. |
494
|
235 |
|
236 tree_for_command::~tree_for_command (void) |
|
237 { |
|
238 delete id; |
|
239 delete expr; |
|
240 delete list; |
|
241 } |
|
242 |
1168
|
243 inline void |
|
244 tree_for_command::do_for_loop_once (tree_constant& rhs, int& quit) |
|
245 { |
|
246 quit = 0; |
|
247 |
|
248 tree_constant *tmp = new tree_constant (rhs); |
|
249 tree_simple_assignment_expression tmp_ass (id, tmp, 1); |
|
250 tmp_ass.eval (0); |
|
251 |
|
252 if (error_state) |
|
253 { |
|
254 eval_error (); |
|
255 return; |
|
256 } |
|
257 |
|
258 if (list) |
|
259 { |
|
260 list->eval (1); |
|
261 if (error_state) |
|
262 { |
|
263 eval_error (); |
|
264 quit = 1; |
|
265 return; |
|
266 } |
|
267 } |
|
268 |
|
269 quit = quit_loop_now (); |
|
270 } |
|
271 |
|
272 inline void |
|
273 tree_for_command::do_for_loop_once (tree_identifier *ident, |
|
274 tree_constant& rhs, int& quit) |
|
275 { |
|
276 quit = 0; |
|
277 |
|
278 ident->assign (rhs); |
|
279 |
|
280 if (error_state) |
|
281 { |
|
282 eval_error (); |
|
283 return; |
|
284 } |
|
285 |
|
286 if (list) |
|
287 { |
|
288 list->eval (1); |
|
289 if (error_state) |
|
290 { |
|
291 eval_error (); |
|
292 quit = 1; |
|
293 return; |
|
294 } |
|
295 } |
|
296 |
|
297 quit = quit_loop_now (); |
|
298 } |
|
299 |
|
300 #define DO_LOOP(val) \ |
|
301 do \ |
|
302 { \ |
|
303 if (ident) \ |
|
304 for (int i = 0; i < steps; i++) \ |
|
305 { \ |
|
306 tree_constant rhs (val); \ |
|
307 int quit = 0; \ |
|
308 do_for_loop_once (ident, rhs, quit); \ |
|
309 if (quit) \ |
|
310 break; \ |
|
311 } \ |
|
312 else \ |
|
313 for (int i = 0; i < steps; i++) \ |
|
314 { \ |
|
315 tree_constant rhs (val); \ |
|
316 int quit = 0; \ |
|
317 do_for_loop_once (rhs, quit); \ |
|
318 if (quit) \ |
|
319 break; \ |
|
320 } \ |
|
321 } \ |
|
322 while (0) |
|
323 |
578
|
324 void |
|
325 tree_for_command::eval (void) |
494
|
326 { |
529
|
327 if (error_state || ! expr) |
578
|
328 return; |
494
|
329 |
|
330 tree_constant tmp_expr = expr->eval (0); |
|
331 |
|
332 if (error_state || tmp_expr.is_undefined ()) |
|
333 { |
|
334 eval_error (); |
578
|
335 return; |
494
|
336 } |
|
337 |
1168
|
338 tree_identifier *ident = 0; |
|
339 if (! id->arg_list ()) |
|
340 { |
|
341 tree_indirect_ref *idr = id->ident (); |
|
342 if (idr->is_identifier_only ()) |
|
343 ident = idr->ident (); |
|
344 } |
|
345 |
620
|
346 if (tmp_expr.is_scalar_type ()) |
|
347 { |
|
348 int quit = 0; |
1168
|
349 if (ident) |
|
350 do_for_loop_once (ident, tmp_expr, quit); |
|
351 else |
|
352 do_for_loop_once (tmp_expr, quit); |
620
|
353 } |
|
354 else if (tmp_expr.is_matrix_type ()) |
494
|
355 { |
620
|
356 Matrix m_tmp; |
|
357 ComplexMatrix cm_tmp; |
|
358 int nr; |
|
359 int steps; |
|
360 if (tmp_expr.is_real_matrix ()) |
|
361 { |
|
362 m_tmp = tmp_expr.matrix_value (); |
|
363 nr = m_tmp.rows (); |
|
364 steps = m_tmp.columns (); |
|
365 } |
|
366 else |
|
367 { |
|
368 cm_tmp = tmp_expr.complex_matrix_value (); |
|
369 nr = cm_tmp.rows (); |
|
370 steps = cm_tmp.columns (); |
|
371 } |
494
|
372 |
1168
|
373 if (tmp_expr.is_real_matrix ()) |
620
|
374 { |
|
375 if (nr == 1) |
1168
|
376 DO_LOOP(m_tmp (0, i)); |
620
|
377 else |
1168
|
378 DO_LOOP(m_tmp.extract (0, i, nr-1, i)); |
|
379 } |
|
380 else |
|
381 { |
|
382 if (nr == 1) |
|
383 DO_LOOP(cm_tmp (0, i)); |
|
384 else |
|
385 DO_LOOP(cm_tmp.extract (0, i, nr-1, i)); |
620
|
386 } |
|
387 } |
|
388 else if (tmp_expr.is_string ()) |
|
389 { |
494
|
390 gripe_string_invalid (); |
620
|
391 } |
|
392 else if (tmp_expr.is_range ()) |
|
393 { |
|
394 Range rng = tmp_expr.range_value (); |
494
|
395 |
620
|
396 int steps = rng.nelem (); |
|
397 double b = rng.base (); |
|
398 double increment = rng.inc (); |
494
|
399 |
1168
|
400 if (ident) |
620
|
401 { |
1168
|
402 for (int i = 0; i < steps; i++) |
|
403 { |
|
404 double tmp_val = b + i * increment; |
|
405 |
|
406 tree_constant rhs (tmp_val); |
|
407 |
|
408 int quit = 0; |
|
409 do_for_loop_once (ident, rhs, quit); |
494
|
410 |
1168
|
411 if (quit) |
|
412 break; |
|
413 } |
|
414 } |
|
415 else |
|
416 { |
|
417 for (int i = 0; i < steps; i++) |
|
418 { |
|
419 double tmp_val = b + i * increment; |
494
|
420 |
1168
|
421 tree_constant rhs (tmp_val); |
|
422 |
|
423 int quit = 0; |
|
424 do_for_loop_once (rhs, quit); |
|
425 |
|
426 if (quit) |
|
427 break; |
|
428 } |
620
|
429 } |
|
430 } |
|
431 else |
|
432 { |
|
433 ::error ("invalid type in for loop expression near line %d, column %d", |
|
434 line (), column ()); |
494
|
435 } |
|
436 } |
|
437 |
|
438 void |
|
439 tree_for_command::eval_error (void) |
|
440 { |
|
441 if (error_state > 0) |
|
442 ::error ("evaluating for command near line %d, column %d", |
|
443 line (), column ()); |
|
444 } |
|
445 |
578
|
446 void |
581
|
447 tree_for_command::print_code (ostream& os) |
|
448 { |
|
449 print_code_indent (os); |
|
450 |
|
451 os << "for "; |
|
452 |
|
453 if (id) |
|
454 id->print_code (os); |
|
455 |
|
456 os << " = "; |
|
457 |
|
458 if (expr) |
|
459 expr->print_code (os); |
|
460 |
|
461 print_code_new_line (os); |
|
462 |
|
463 if (list) |
|
464 { |
|
465 increment_indent_level (); |
|
466 list->print_code (os); |
|
467 decrement_indent_level (); |
|
468 } |
|
469 |
|
470 print_code_indent (os); |
|
471 |
|
472 os << "endfor"; |
|
473 } |
|
474 |
578
|
475 // If. |
494
|
476 |
|
477 tree_if_command::~tree_if_command (void) |
|
478 { |
|
479 delete list; |
|
480 } |
|
481 |
|
482 void |
578
|
483 tree_if_command::eval (void) |
494
|
484 { |
578
|
485 if (list) |
|
486 list->eval (); |
|
487 |
494
|
488 if (error_state > 0) |
|
489 ::error ("evaluating if command near line %d, column %d", |
|
490 line (), column ()); |
|
491 } |
|
492 |
581
|
493 void |
|
494 tree_if_command::print_code (ostream& os) |
|
495 { |
|
496 print_code_indent (os); |
|
497 |
|
498 os << "if "; |
|
499 |
|
500 if (list) |
|
501 list->print_code (os); |
|
502 |
|
503 print_code_indent (os); |
|
504 |
|
505 os << "endif"; |
|
506 } |
|
507 |
916
|
508 // Simple exception handling. |
|
509 |
|
510 tree_unwind_protect_command::~tree_unwind_protect_command (void) |
|
511 { |
|
512 delete unwind_protect_code; |
|
513 delete cleanup_code; |
|
514 } |
|
515 |
|
516 static void |
|
517 do_unwind_protect_cleanup_code (void *ptr) |
|
518 { |
|
519 tree_statement_list *list = (tree_statement_list *) ptr; |
|
520 |
|
521 // We want to run the cleanup code without error_state being set, but |
|
522 // we need to restore its value, so that any errors encountered in |
|
523 // the first part of the unwind_protect are not completely ignored. |
|
524 |
|
525 unwind_protect_int (error_state); |
|
526 |
|
527 error_state = 0; |
|
528 |
|
529 if (list) |
|
530 list->eval (1); |
|
531 |
|
532 // We don't want to ignore errors that occur in the cleanup code, so |
|
533 // if an error is encountered there, leave error_state alone. |
|
534 // Otherwise, set it back to what it was before. |
|
535 |
|
536 if (error_state) |
|
537 discard_unwind_protect (); |
|
538 else |
|
539 run_unwind_protect (); |
|
540 } |
|
541 |
|
542 void |
|
543 tree_unwind_protect_command::eval (void) |
|
544 { |
|
545 add_unwind_protect (do_unwind_protect_cleanup_code, cleanup_code); |
|
546 |
|
547 if (unwind_protect_code) |
|
548 unwind_protect_code->eval (1); |
|
549 |
|
550 run_unwind_protect (); |
|
551 } |
|
552 |
|
553 void |
|
554 tree_unwind_protect_command::print_code (ostream& os) |
|
555 { |
|
556 print_code_indent (os); |
|
557 |
|
558 os << "unwind_protect"; |
|
559 |
|
560 print_code_new_line (os); |
|
561 |
|
562 if (unwind_protect_code) |
|
563 { |
|
564 increment_indent_level (); |
|
565 unwind_protect_code->print_code (os); |
|
566 decrement_indent_level (); |
|
567 } |
|
568 |
|
569 print_code_indent (os); |
|
570 |
|
571 os << "cleanup_code"; |
|
572 |
|
573 print_code_new_line (os); |
|
574 |
|
575 if (cleanup_code) |
|
576 { |
|
577 increment_indent_level (); |
|
578 cleanup_code->print_code (os); |
|
579 decrement_indent_level (); |
|
580 } |
|
581 |
|
582 print_code_indent (os); |
|
583 |
|
584 os << "end_unwind_protect"; |
|
585 } |
|
586 |
578
|
587 // Break. |
494
|
588 |
578
|
589 void |
|
590 tree_break_command::eval (void) |
494
|
591 { |
|
592 if (! error_state) |
|
593 breaking = 1; |
|
594 } |
|
595 |
581
|
596 void |
|
597 tree_break_command::print_code (ostream& os) |
|
598 { |
|
599 print_code_indent (os); |
|
600 |
|
601 os << "break"; |
|
602 } |
|
603 |
578
|
604 // Continue. |
494
|
605 |
578
|
606 void |
|
607 tree_continue_command::eval (void) |
494
|
608 { |
|
609 if (! error_state) |
|
610 continuing = 1; |
|
611 } |
|
612 |
581
|
613 void |
|
614 tree_continue_command::print_code (ostream& os) |
|
615 { |
|
616 print_code_indent (os); |
|
617 |
|
618 os << "continue"; |
|
619 } |
|
620 |
578
|
621 // Return. |
494
|
622 |
578
|
623 void |
|
624 tree_return_command::eval (void) |
494
|
625 { |
|
626 if (! error_state) |
|
627 returning = 1; |
|
628 } |
|
629 |
581
|
630 void |
|
631 tree_return_command::print_code (ostream& os) |
|
632 { |
|
633 print_code_indent (os); |
|
634 |
|
635 os << "return"; |
|
636 } |
|
637 |
494
|
638 /* |
|
639 ;;; Local Variables: *** |
|
640 ;;; mode: C++ *** |
|
641 ;;; page-delimiter: "^/\\*" *** |
|
642 ;;; End: *** |
|
643 */ |