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