577
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
577
|
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 |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
577
|
20 |
|
21 */ |
|
22 |
1297
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
577
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
577
|
29 #endif |
|
30 |
1350
|
31 #include <iostream.h> |
2942
|
32 #include <strstream.h> |
1350
|
33 |
|
34 #ifdef HAVE_UNISTD_H |
2442
|
35 #ifdef HAVE_SYS_TYPES_H |
577
|
36 #include <sys/types.h> |
2442
|
37 #endif |
577
|
38 #include <unistd.h> |
|
39 #endif |
|
40 |
2942
|
41 #include "str-vec.h" |
|
42 |
2898
|
43 #include "defun.h" |
577
|
44 #include "error.h" |
2530
|
45 #include "input.h" |
1352
|
46 #include "oct-obj.h" |
2891
|
47 #include "oct-usr-fcn.h" |
2878
|
48 #include "ov.h" |
1742
|
49 #include "pager.h" |
|
50 #include "pt-cmd.h" |
2878
|
51 #include "pt-const.h" |
1742
|
52 #include "pt-exp.h" |
2891
|
53 #include "pt-id.h" |
|
54 #include "pt-indir.h" |
1742
|
55 #include "pt-misc.h" |
|
56 #include "pt-mvr.h" |
2878
|
57 #include "pt-pr-code.h" |
2124
|
58 #include "pt-walk.h" |
2878
|
59 #include "toplev.h" |
2898
|
60 #include "utils.h" |
2204
|
61 #include "variables.h" |
577
|
62 |
1058
|
63 // Nonzero means we're breaking out of a loop or function body. |
577
|
64 extern int breaking; |
|
65 |
|
66 // Nonzero means we're jumping to the end of a loop. |
|
67 extern int continuing; |
|
68 |
|
69 // Nonzero means we're returning from a function. |
|
70 extern int returning; |
|
71 |
2898
|
72 // If TRUE, turn off printing of results in functions (as if a |
|
73 // semicolon has been appended to each statement). |
|
74 static bool Vsilent_functions; |
|
75 |
577
|
76 // A list of commands to be executed. |
|
77 |
|
78 tree_statement::~tree_statement (void) |
|
79 { |
2124
|
80 delete cmd; |
|
81 delete expr; |
577
|
82 } |
|
83 |
1742
|
84 int |
|
85 tree_statement::line (void) |
|
86 { |
2124
|
87 return cmd ? cmd->line () : (expr ? expr->line () : -1); |
1742
|
88 } |
|
89 |
|
90 int |
|
91 tree_statement::column (void) |
|
92 { |
2124
|
93 return cmd ? cmd->column () : (expr ? expr->column () : -1); |
1742
|
94 } |
|
95 |
581
|
96 void |
1827
|
97 tree_statement::maybe_echo_code (bool in_function_body) |
1588
|
98 { |
|
99 if (in_function_body |
2204
|
100 && (Vecho_executing_commands & ECHO_FUNCTIONS)) |
2124
|
101 { |
2530
|
102 tree_print_code tpc (octave_stdout, Vps4); |
2124
|
103 |
|
104 accept (tpc); |
|
105 } |
1588
|
106 } |
|
107 |
2898
|
108 octave_value |
|
109 tree_statement::eval (bool silent, bool in_function_body) |
|
110 { |
|
111 octave_value retval; |
|
112 |
|
113 bool pf = silent ? false : print_flag; |
|
114 |
|
115 if (cmd || expr) |
|
116 { |
|
117 maybe_echo_code (in_function_body); |
|
118 |
|
119 if (cmd) |
|
120 cmd->eval (); |
|
121 else |
|
122 retval = expr->eval (pf); |
|
123 } |
|
124 |
|
125 return retval; |
|
126 } |
|
127 |
|
128 octave_value_list |
|
129 tree_statement::eval (bool silent, int nargout, bool in_function_body) |
|
130 { |
|
131 bool pf = silent ? false : print_flag; |
|
132 |
|
133 octave_value_list retval; |
|
134 |
|
135 if (cmd || expr) |
|
136 { |
|
137 maybe_echo_code (in_function_body); |
|
138 |
|
139 if (cmd) |
|
140 cmd->eval (); |
|
141 else |
|
142 { |
|
143 if (expr->is_multi_val_ret_expression ()) |
|
144 { |
|
145 octave_value_list args; |
|
146 |
|
147 tree_multi_val_ret *t = static_cast<tree_multi_val_ret *> (expr); |
|
148 |
|
149 retval = t->eval (pf, nargout, args); |
|
150 } |
|
151 else |
|
152 retval = expr->eval (pf); |
|
153 } |
|
154 } |
|
155 |
|
156 return retval; |
|
157 } |
|
158 |
1588
|
159 void |
2124
|
160 tree_statement::accept (tree_walker& tw) |
581
|
161 { |
2124
|
162 tw.visit_statement (*this); |
581
|
163 } |
|
164 |
2086
|
165 octave_value |
2898
|
166 tree_statement_list::eval (bool silent) |
577
|
167 { |
2086
|
168 octave_value retval; |
577
|
169 |
|
170 if (error_state) |
|
171 return retval; |
|
172 |
|
173 for (Pix p = first (); p != 0; next (p)) |
|
174 { |
|
175 tree_statement *elt = this->operator () (p); |
|
176 |
2898
|
177 bool silent_flag = |
|
178 silent ? true : (function_body ? Vsilent_functions : false); |
|
179 |
|
180 retval = elt->eval (silent_flag, function_body); |
577
|
181 |
2898
|
182 if (error_state) |
|
183 break; |
|
184 |
|
185 if (breaking || continuing) |
|
186 break; |
|
187 |
|
188 if (returning) |
|
189 break; |
|
190 } |
577
|
191 |
2898
|
192 return retval; |
|
193 } |
|
194 |
|
195 octave_value_list |
|
196 tree_statement_list::eval (bool no_print, int nargout) |
|
197 { |
|
198 octave_value_list retval; |
1588
|
199 |
2898
|
200 if (error_state) |
|
201 return retval; |
|
202 |
|
203 if (nargout > 1) |
|
204 { |
|
205 for (Pix p = first (); p != 0; next (p)) |
|
206 { |
|
207 tree_statement *elt = this->operator () (p); |
|
208 |
|
209 retval = elt->eval (no_print, nargout, function_body); |
577
|
210 |
|
211 if (error_state) |
2898
|
212 break; |
577
|
213 |
|
214 if (breaking || continuing) |
|
215 break; |
|
216 |
|
217 if (returning) |
|
218 break; |
|
219 } |
672
|
220 } |
|
221 else |
2898
|
222 retval = eval (no_print); |
672
|
223 |
|
224 return retval; |
|
225 } |
|
226 |
581
|
227 void |
2124
|
228 tree_statement_list::accept (tree_walker& tw) |
581
|
229 { |
2124
|
230 tw.visit_statement_list (*this); |
581
|
231 } |
|
232 |
2878
|
233 tree_argument_list::~tree_argument_list (void) |
|
234 { |
|
235 while (! empty ()) |
|
236 { |
|
237 tree_expression *t = remove_front (); |
|
238 delete t; |
|
239 } |
|
240 } |
|
241 |
2086
|
242 octave_value_list |
577
|
243 tree_argument_list::convert_to_const_vector (void) |
|
244 { |
712
|
245 int len = length (); |
577
|
246 |
1358
|
247 // XXX FIXME XXX -- would be nice to know in advance how largs args |
|
248 // needs to be even when we have a list containing an all_va_args |
|
249 // token. |
922
|
250 |
2086
|
251 octave_value_list args; |
577
|
252 args.resize (len); |
|
253 |
|
254 Pix p = first (); |
922
|
255 int j = 0; |
712
|
256 for (int k = 0; k < len; k++) |
577
|
257 { |
|
258 tree_expression *elt = this->operator () (p); |
|
259 if (elt) |
|
260 { |
2859
|
261 octave_value tmp = elt->eval (); |
577
|
262 if (error_state) |
|
263 { |
|
264 ::error ("evaluating argument list element number %d", k); |
2086
|
265 args = octave_value_list (); |
577
|
266 break; |
|
267 } |
922
|
268 else |
|
269 { |
|
270 if (tmp.is_all_va_args ()) |
|
271 { |
|
272 if (curr_function) |
|
273 { |
2086
|
274 octave_value_list tva; |
922
|
275 tva = curr_function->octave_all_va_args (); |
|
276 int n = tva.length (); |
|
277 for (int i = 0; i < n; i++) |
|
278 args(j++) = tva(i); |
|
279 } |
|
280 else |
|
281 { |
|
282 ::error ("all_va_args is only valid inside functions"); |
2086
|
283 args = octave_value_list (); |
922
|
284 break; |
|
285 } |
|
286 } |
|
287 else |
|
288 args(j++) = tmp; |
|
289 } |
577
|
290 next (p); |
|
291 } |
|
292 else |
|
293 { |
2086
|
294 args(j++) = octave_value (); |
577
|
295 break; |
|
296 } |
|
297 } |
712
|
298 |
927
|
299 args.resize (j); |
|
300 |
577
|
301 return args; |
|
302 } |
|
303 |
2942
|
304 string_vector |
|
305 tree_argument_list::get_arg_names (void) const |
|
306 { |
|
307 int len = length (); |
|
308 |
|
309 string_vector retval (len); |
|
310 |
|
311 int k = 0; |
|
312 |
|
313 for (Pix p = first (); p; next (p)) |
|
314 { |
|
315 tree_expression *elt = this->operator () (p); |
|
316 |
|
317 strstream str_buf; |
|
318 |
|
319 tree_print_code pc_buf (str_buf); |
|
320 |
|
321 elt->accept (pc_buf); |
|
322 |
|
323 str_buf << ends; |
|
324 |
|
325 const char *s = str_buf.str (); |
|
326 |
|
327 retval(k++) = s; |
|
328 |
|
329 delete [] s; |
|
330 } |
|
331 |
|
332 return retval; |
|
333 } |
|
334 |
581
|
335 void |
2124
|
336 tree_argument_list::accept (tree_walker& tw) |
581
|
337 { |
2124
|
338 tw.visit_argument_list (*this); |
581
|
339 } |
|
340 |
577
|
341 // Parameter lists. |
|
342 |
1742
|
343 tree_parameter_list::~tree_parameter_list (void) |
|
344 { |
|
345 while (! empty ()) |
|
346 { |
|
347 tree_identifier *t = remove_front (); |
|
348 delete t; |
|
349 } |
|
350 } |
|
351 |
577
|
352 void |
|
353 tree_parameter_list::mark_as_formal_parameters (void) |
|
354 { |
|
355 for (Pix p = first (); p != 0; next (p)) |
|
356 { |
|
357 tree_identifier *elt = this->operator () (p); |
|
358 elt->mark_as_formal_parameter (); |
|
359 } |
|
360 } |
|
361 |
|
362 void |
2086
|
363 tree_parameter_list::initialize_undefined_elements (octave_value& val) |
1093
|
364 { |
|
365 for (Pix p = first (); p != 0; next (p)) |
|
366 { |
|
367 tree_identifier *elt = this->operator () (p); |
2948
|
368 |
1093
|
369 if (! elt->is_defined ()) |
2948
|
370 elt->reference () . assign (octave_value::asn_eq, val); |
1093
|
371 } |
|
372 } |
|
373 |
|
374 void |
2086
|
375 tree_parameter_list::define_from_arg_vector (const octave_value_list& args) |
577
|
376 { |
712
|
377 int nargin = args.length (); |
|
378 |
|
379 if (nargin <= 0) |
577
|
380 return; |
|
381 |
712
|
382 int expected_nargin = length (); |
577
|
383 |
|
384 Pix p = first (); |
|
385 |
712
|
386 for (int i = 0; i < expected_nargin; i++) |
577
|
387 { |
|
388 tree_identifier *elt = this->operator () (p); |
|
389 |
|
390 if (i < nargin) |
|
391 { |
620
|
392 if (args(i).is_defined () && args(i).is_magic_colon ()) |
577
|
393 { |
|
394 ::error ("invalid use of colon in function argument list"); |
|
395 return; |
|
396 } |
2891
|
397 |
|
398 elt->define (args(i)); |
577
|
399 } |
2891
|
400 else |
|
401 elt->define (octave_value ()); |
577
|
402 |
|
403 next (p); |
|
404 } |
|
405 } |
|
406 |
2086
|
407 octave_value_list |
723
|
408 tree_parameter_list::convert_to_const_vector (tree_va_return_list *vr_list) |
577
|
409 { |
|
410 int nout = length (); |
|
411 |
723
|
412 if (vr_list) |
|
413 nout += vr_list->length (); |
|
414 |
2086
|
415 octave_value_list retval; |
577
|
416 retval.resize (nout); |
|
417 |
|
418 int i = 0; |
|
419 |
|
420 for (Pix p = first (); p != 0; next (p)) |
|
421 { |
|
422 tree_identifier *elt = this->operator () (p); |
|
423 |
|
424 if (elt->is_defined ()) |
2859
|
425 retval(i) = elt->eval (); |
577
|
426 |
|
427 i++; |
|
428 } |
|
429 |
723
|
430 if (vr_list) |
|
431 { |
1321
|
432 for (Pix p = vr_list->first (); p != 0; vr_list->next (p)) |
723
|
433 { |
|
434 retval(i) = vr_list->operator () (p); |
|
435 i++; |
|
436 } |
|
437 } |
|
438 |
577
|
439 return retval; |
|
440 } |
|
441 |
1827
|
442 bool |
577
|
443 tree_parameter_list::is_defined (void) |
|
444 { |
1827
|
445 bool status = true; |
577
|
446 |
|
447 for (Pix p = first (); p != 0; next (p)) |
|
448 { |
|
449 tree_identifier *elt = this->operator () (p); |
|
450 |
|
451 if (! elt->is_defined ()) |
|
452 { |
1827
|
453 status = false; |
577
|
454 break; |
|
455 } |
|
456 } |
|
457 |
|
458 return status; |
|
459 } |
|
460 |
|
461 void |
2124
|
462 tree_parameter_list::accept (tree_walker& tw) |
581
|
463 { |
2124
|
464 tw.visit_parameter_list (*this); |
581
|
465 } |
|
466 |
|
467 // Return lists. |
|
468 |
1742
|
469 tree_return_list::~tree_return_list (void) |
|
470 { |
|
471 while (! empty ()) |
|
472 { |
|
473 tree_index_expression *t = remove_front (); |
|
474 delete t; |
|
475 } |
|
476 } |
|
477 |
581
|
478 void |
2124
|
479 tree_return_list::accept (tree_walker& tw) |
581
|
480 { |
2124
|
481 tw.visit_return_list (*this); |
581
|
482 } |
|
483 |
2846
|
484 // Declarations (global, static, etc.). |
581
|
485 |
2846
|
486 tree_decl_elt::~tree_decl_elt (void) |
1742
|
487 { |
2124
|
488 delete id; |
|
489 delete ass_expr; |
1742
|
490 } |
|
491 |
581
|
492 void |
2846
|
493 tree_decl_elt::accept (tree_walker& tw) |
577
|
494 { |
2846
|
495 tw.visit_decl_elt (*this); |
|
496 } |
|
497 |
|
498 // Initializer lists for declaration statements. |
|
499 |
|
500 void |
|
501 tree_decl_init_list::eval (tree_decl_elt::eval_fcn f, bool skip_init) |
|
502 { |
|
503 for (Pix p = first (); p != 0; next (p)) |
577
|
504 { |
2846
|
505 f (*(this->operator () (p)), skip_init); |
2124
|
506 |
2846
|
507 if (error_state) |
|
508 break; |
577
|
509 } |
|
510 } |
|
511 |
|
512 void |
2846
|
513 tree_decl_init_list::accept (tree_walker& tw) |
577
|
514 { |
2846
|
515 tw.visit_decl_init_list (*this); |
581
|
516 } |
|
517 |
|
518 // If. |
|
519 |
1742
|
520 tree_if_clause::~tree_if_clause (void) |
|
521 { |
|
522 delete expr; |
|
523 delete list; |
|
524 } |
|
525 |
577
|
526 int |
|
527 tree_if_clause::eval (void) |
|
528 { |
1491
|
529 if (is_else_clause () || expr->is_logically_true ("if")) |
577
|
530 { |
|
531 if (list) |
2898
|
532 list->eval (); |
577
|
533 |
|
534 return 1; |
|
535 } |
|
536 |
|
537 return 0; |
|
538 } |
|
539 |
|
540 void |
2124
|
541 tree_if_clause::accept (tree_walker& tw) |
581
|
542 { |
2124
|
543 tw.visit_if_clause (*this); |
581
|
544 } |
|
545 |
|
546 // List of if commands. |
|
547 |
|
548 void |
577
|
549 tree_if_command_list::eval (void) |
|
550 { |
|
551 for (Pix p = first (); p != 0; next (p)) |
|
552 { |
|
553 tree_if_clause *t = this->operator () (p); |
|
554 |
|
555 if (t->eval () || error_state) |
|
556 break; |
|
557 } |
|
558 } |
|
559 |
581
|
560 void |
2124
|
561 tree_if_command_list::accept (tree_walker& tw) |
581
|
562 { |
2124
|
563 tw.visit_if_command_list (*this); |
581
|
564 } |
|
565 |
2764
|
566 // Switch. |
|
567 |
|
568 tree_switch_case::~tree_switch_case (void) |
|
569 { |
|
570 delete label; |
|
571 delete list; |
|
572 } |
|
573 |
|
574 bool |
|
575 tree_switch_case::label_matches (const octave_value& val) |
|
576 { |
|
577 bool retval = false; |
|
578 |
2859
|
579 octave_value label_value = label->eval (); |
2764
|
580 |
|
581 if (! error_state) |
|
582 { |
|
583 if (label_value.is_defined ()) |
|
584 { |
|
585 octave_value tmp = do_binary_op (octave_value::eq, |
|
586 val, label_value); |
|
587 |
|
588 if (! error_state) |
|
589 { |
|
590 if (tmp.is_defined ()) |
|
591 retval = tmp.is_true (); |
|
592 else |
|
593 eval_error (); |
|
594 } |
|
595 else |
|
596 eval_error (); |
|
597 } |
|
598 else |
|
599 eval_error (); |
|
600 } |
|
601 else |
|
602 eval_error (); |
|
603 |
|
604 return retval; |
|
605 } |
|
606 |
|
607 int |
|
608 tree_switch_case::eval (const octave_value& val) |
|
609 { |
|
610 int retval = 0; |
|
611 |
|
612 if (is_default_case () || label_matches (val)) |
|
613 { |
|
614 if (list) |
2898
|
615 list->eval (); |
2764
|
616 |
|
617 retval = 1; |
|
618 } |
|
619 |
|
620 return retval; |
|
621 } |
|
622 |
|
623 void |
|
624 tree_switch_case::eval_error (void) |
|
625 { |
|
626 ::error ("evaluating switch case label"); |
|
627 } |
|
628 |
|
629 void |
|
630 tree_switch_case::accept (tree_walker& tw) |
|
631 { |
|
632 tw.visit_switch_case (*this); |
|
633 } |
|
634 |
|
635 // List of switch cases. |
|
636 |
|
637 void |
|
638 tree_switch_case_list::eval (const octave_value& val) |
|
639 { |
|
640 for (Pix p = first (); p != 0; next (p)) |
|
641 { |
|
642 tree_switch_case *t = this->operator () (p); |
|
643 |
|
644 if (t->eval (val) || error_state) |
|
645 break; |
|
646 } |
|
647 } |
|
648 |
|
649 void |
|
650 tree_switch_case_list::accept (tree_walker& tw) |
|
651 { |
|
652 tw.visit_switch_case_list (*this); |
|
653 } |
|
654 |
2898
|
655 static int |
|
656 silent_functions (void) |
|
657 { |
|
658 Vsilent_functions = check_preference ("silent_functions"); |
|
659 |
|
660 return 0; |
|
661 } |
|
662 |
|
663 void |
|
664 symbols_of_pt_misc (void) |
|
665 { |
|
666 DEFVAR (silent_functions, 0.0, 0, silent_functions, |
|
667 "suppress printing results in called functions"); |
|
668 } |
|
669 |
577
|
670 /* |
|
671 ;;; Local Variables: *** |
|
672 ;;; mode: C++ *** |
|
673 ;;; End: *** |
|
674 */ |