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