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> |
|
32 |
|
33 #ifdef HAVE_UNISTD_H |
2442
|
34 #ifdef HAVE_SYS_TYPES_H |
577
|
35 #include <sys/types.h> |
2442
|
36 #endif |
577
|
37 #include <unistd.h> |
|
38 #endif |
|
39 |
|
40 #include "error.h" |
2530
|
41 #include "input.h" |
1352
|
42 #include "oct-obj.h" |
2891
|
43 #include "oct-usr-fcn.h" |
2878
|
44 #include "ov.h" |
1742
|
45 #include "pager.h" |
|
46 #include "pt-cmd.h" |
2878
|
47 #include "pt-const.h" |
1742
|
48 #include "pt-exp.h" |
2891
|
49 #include "pt-id.h" |
|
50 #include "pt-indir.h" |
1742
|
51 #include "pt-misc.h" |
|
52 #include "pt-mvr.h" |
2878
|
53 #include "pt-pr-code.h" |
2124
|
54 #include "pt-walk.h" |
2878
|
55 #include "toplev.h" |
2204
|
56 #include "variables.h" |
577
|
57 |
1058
|
58 // Nonzero means we're breaking out of a loop or function body. |
577
|
59 extern int breaking; |
|
60 |
|
61 // Nonzero means we're jumping to the end of a loop. |
|
62 extern int continuing; |
|
63 |
|
64 // Nonzero means we're returning from a function. |
|
65 extern int returning; |
|
66 |
|
67 // A list of commands to be executed. |
|
68 |
|
69 tree_statement::~tree_statement (void) |
|
70 { |
2124
|
71 delete cmd; |
|
72 delete expr; |
577
|
73 } |
|
74 |
1742
|
75 int |
|
76 tree_statement::line (void) |
|
77 { |
2124
|
78 return cmd ? cmd->line () : (expr ? expr->line () : -1); |
1742
|
79 } |
|
80 |
|
81 int |
|
82 tree_statement::column (void) |
|
83 { |
2124
|
84 return cmd ? cmd->column () : (expr ? expr->column () : -1); |
1742
|
85 } |
|
86 |
581
|
87 void |
1827
|
88 tree_statement::maybe_echo_code (bool in_function_body) |
1588
|
89 { |
|
90 if (in_function_body |
2204
|
91 && (Vecho_executing_commands & ECHO_FUNCTIONS)) |
2124
|
92 { |
2530
|
93 tree_print_code tpc (octave_stdout, Vps4); |
2124
|
94 |
|
95 accept (tpc); |
|
96 } |
1588
|
97 } |
|
98 |
|
99 void |
2124
|
100 tree_statement::accept (tree_walker& tw) |
581
|
101 { |
2124
|
102 tw.visit_statement (*this); |
581
|
103 } |
|
104 |
2086
|
105 octave_value |
1827
|
106 tree_statement_list::eval (bool print) |
577
|
107 { |
1827
|
108 bool pf; |
2086
|
109 octave_value retval; |
577
|
110 |
|
111 if (error_state) |
|
112 return retval; |
|
113 |
|
114 for (Pix p = first (); p != 0; next (p)) |
|
115 { |
|
116 tree_statement *elt = this->operator () (p); |
|
117 |
1827
|
118 if (! print) |
|
119 pf = false; |
577
|
120 else |
|
121 pf = elt->print_flag; |
|
122 |
2124
|
123 tree_command *cmd = elt->command (); |
|
124 tree_expression *expr = elt->expression (); |
577
|
125 |
|
126 if (cmd || expr) |
|
127 { |
1588
|
128 elt->maybe_echo_code (function_body); |
|
129 |
577
|
130 if (cmd) |
|
131 cmd->eval (); |
|
132 else |
|
133 retval = expr->eval (pf); |
|
134 |
|
135 if (error_state) |
2086
|
136 return octave_value (); |
577
|
137 |
|
138 if (breaking || continuing) |
|
139 break; |
|
140 |
|
141 if (returning) |
|
142 break; |
|
143 } |
|
144 else |
2086
|
145 retval = octave_value (); |
577
|
146 } |
|
147 return retval; |
|
148 } |
|
149 |
2086
|
150 octave_value_list |
1827
|
151 tree_statement_list::eval (bool print, int nargout) |
672
|
152 { |
2086
|
153 octave_value_list retval; |
672
|
154 |
|
155 if (nargout > 1) |
|
156 { |
1827
|
157 bool pf; |
672
|
158 |
|
159 if (error_state) |
|
160 return retval; |
|
161 |
|
162 for (Pix p = first (); p != 0; next (p)) |
|
163 { |
|
164 tree_statement *elt = this->operator () (p); |
|
165 |
1827
|
166 if (! print) |
|
167 pf = false; |
672
|
168 else |
|
169 pf = elt->print_flag; |
|
170 |
2124
|
171 tree_command *cmd = elt->command (); |
|
172 tree_expression *expr = elt->expression (); |
672
|
173 |
|
174 if (cmd || expr) |
|
175 { |
1588
|
176 elt->maybe_echo_code (function_body); |
|
177 |
672
|
178 if (cmd) |
|
179 cmd->eval (); |
|
180 else |
|
181 { |
|
182 if (expr->is_multi_val_ret_expression ()) |
|
183 { |
2086
|
184 octave_value_list args; |
2800
|
185 tree_multi_val_ret *t; |
|
186 t = static_cast<tree_multi_val_ret *> (expr); |
672
|
187 retval = t->eval (pf, nargout, args); |
|
188 } |
|
189 else |
|
190 retval = expr->eval (pf); |
|
191 } |
|
192 |
|
193 if (error_state) |
2086
|
194 return octave_value (); |
672
|
195 |
|
196 if (breaking || continuing) |
|
197 break; |
|
198 |
|
199 if (returning) |
|
200 break; |
|
201 } |
|
202 else |
2086
|
203 retval = octave_value_list (); |
672
|
204 } |
|
205 return retval; |
|
206 } |
|
207 else |
|
208 retval = eval (print); |
|
209 |
|
210 return retval; |
|
211 } |
|
212 |
581
|
213 void |
2124
|
214 tree_statement_list::accept (tree_walker& tw) |
581
|
215 { |
2124
|
216 tw.visit_statement_list (*this); |
581
|
217 } |
|
218 |
2878
|
219 tree_argument_list::~tree_argument_list (void) |
|
220 { |
|
221 while (! empty ()) |
|
222 { |
|
223 tree_expression *t = remove_front (); |
|
224 delete t; |
|
225 } |
|
226 } |
|
227 |
2086
|
228 octave_value_list |
577
|
229 tree_argument_list::convert_to_const_vector (void) |
|
230 { |
712
|
231 int len = length (); |
577
|
232 |
1358
|
233 // XXX FIXME XXX -- would be nice to know in advance how largs args |
|
234 // needs to be even when we have a list containing an all_va_args |
|
235 // token. |
922
|
236 |
2086
|
237 octave_value_list args; |
577
|
238 args.resize (len); |
|
239 |
|
240 Pix p = first (); |
922
|
241 int j = 0; |
712
|
242 for (int k = 0; k < len; k++) |
577
|
243 { |
|
244 tree_expression *elt = this->operator () (p); |
|
245 if (elt) |
|
246 { |
2859
|
247 octave_value tmp = elt->eval (); |
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 |
581
|
290 void |
2124
|
291 tree_argument_list::accept (tree_walker& tw) |
581
|
292 { |
2124
|
293 tw.visit_argument_list (*this); |
581
|
294 } |
|
295 |
577
|
296 // Parameter lists. |
|
297 |
1742
|
298 tree_parameter_list::~tree_parameter_list (void) |
|
299 { |
|
300 while (! empty ()) |
|
301 { |
|
302 tree_identifier *t = remove_front (); |
|
303 delete t; |
|
304 } |
|
305 } |
|
306 |
577
|
307 void |
|
308 tree_parameter_list::mark_as_formal_parameters (void) |
|
309 { |
|
310 for (Pix p = first (); p != 0; next (p)) |
|
311 { |
|
312 tree_identifier *elt = this->operator () (p); |
|
313 elt->mark_as_formal_parameter (); |
|
314 } |
|
315 } |
|
316 |
|
317 void |
2086
|
318 tree_parameter_list::initialize_undefined_elements (octave_value& val) |
1093
|
319 { |
|
320 for (Pix p = first (); p != 0; next (p)) |
|
321 { |
|
322 tree_identifier *elt = this->operator () (p); |
|
323 if (! elt->is_defined ()) |
2371
|
324 { |
|
325 octave_variable_reference tmp (elt); |
2878
|
326 tmp.assign (octave_value::asn_eq, val); |
2371
|
327 } |
1093
|
328 } |
|
329 } |
|
330 |
|
331 void |
2086
|
332 tree_parameter_list::define_from_arg_vector (const octave_value_list& args) |
577
|
333 { |
712
|
334 int nargin = args.length (); |
|
335 |
|
336 if (nargin <= 0) |
577
|
337 return; |
|
338 |
712
|
339 int expected_nargin = length (); |
577
|
340 |
|
341 Pix p = first (); |
|
342 |
712
|
343 for (int i = 0; i < expected_nargin; i++) |
577
|
344 { |
|
345 tree_identifier *elt = this->operator () (p); |
|
346 |
|
347 if (i < nargin) |
|
348 { |
620
|
349 if (args(i).is_defined () && args(i).is_magic_colon ()) |
577
|
350 { |
|
351 ::error ("invalid use of colon in function argument list"); |
|
352 return; |
|
353 } |
2891
|
354 |
|
355 elt->define (args(i)); |
577
|
356 } |
2891
|
357 else |
|
358 elt->define (octave_value ()); |
577
|
359 |
|
360 next (p); |
|
361 } |
|
362 } |
|
363 |
2086
|
364 octave_value_list |
723
|
365 tree_parameter_list::convert_to_const_vector (tree_va_return_list *vr_list) |
577
|
366 { |
|
367 int nout = length (); |
|
368 |
723
|
369 if (vr_list) |
|
370 nout += vr_list->length (); |
|
371 |
2086
|
372 octave_value_list retval; |
577
|
373 retval.resize (nout); |
|
374 |
|
375 int i = 0; |
|
376 |
|
377 for (Pix p = first (); p != 0; next (p)) |
|
378 { |
|
379 tree_identifier *elt = this->operator () (p); |
|
380 |
|
381 if (elt->is_defined ()) |
2859
|
382 retval(i) = elt->eval (); |
577
|
383 |
|
384 i++; |
|
385 } |
|
386 |
723
|
387 if (vr_list) |
|
388 { |
1321
|
389 for (Pix p = vr_list->first (); p != 0; vr_list->next (p)) |
723
|
390 { |
|
391 retval(i) = vr_list->operator () (p); |
|
392 i++; |
|
393 } |
|
394 } |
|
395 |
577
|
396 return retval; |
|
397 } |
|
398 |
1827
|
399 bool |
577
|
400 tree_parameter_list::is_defined (void) |
|
401 { |
1827
|
402 bool status = true; |
577
|
403 |
|
404 for (Pix p = first (); p != 0; next (p)) |
|
405 { |
|
406 tree_identifier *elt = this->operator () (p); |
|
407 |
|
408 if (! elt->is_defined ()) |
|
409 { |
1827
|
410 status = false; |
577
|
411 break; |
|
412 } |
|
413 } |
|
414 |
|
415 return status; |
|
416 } |
|
417 |
|
418 void |
2124
|
419 tree_parameter_list::accept (tree_walker& tw) |
581
|
420 { |
2124
|
421 tw.visit_parameter_list (*this); |
581
|
422 } |
|
423 |
|
424 // Return lists. |
|
425 |
1742
|
426 tree_return_list::~tree_return_list (void) |
|
427 { |
|
428 while (! empty ()) |
|
429 { |
|
430 tree_index_expression *t = remove_front (); |
|
431 delete t; |
|
432 } |
|
433 } |
|
434 |
581
|
435 void |
2124
|
436 tree_return_list::accept (tree_walker& tw) |
581
|
437 { |
2124
|
438 tw.visit_return_list (*this); |
581
|
439 } |
|
440 |
2846
|
441 // Declarations (global, static, etc.). |
581
|
442 |
2846
|
443 tree_decl_elt::~tree_decl_elt (void) |
1742
|
444 { |
2124
|
445 delete id; |
|
446 delete ass_expr; |
1742
|
447 } |
|
448 |
581
|
449 void |
2846
|
450 tree_decl_elt::accept (tree_walker& tw) |
577
|
451 { |
2846
|
452 tw.visit_decl_elt (*this); |
|
453 } |
|
454 |
|
455 // Initializer lists for declaration statements. |
|
456 |
|
457 void |
|
458 tree_decl_init_list::eval (tree_decl_elt::eval_fcn f, bool skip_init) |
|
459 { |
|
460 for (Pix p = first (); p != 0; next (p)) |
577
|
461 { |
2846
|
462 f (*(this->operator () (p)), skip_init); |
2124
|
463 |
2846
|
464 if (error_state) |
|
465 break; |
577
|
466 } |
|
467 } |
|
468 |
|
469 void |
2846
|
470 tree_decl_init_list::accept (tree_walker& tw) |
577
|
471 { |
2846
|
472 tw.visit_decl_init_list (*this); |
581
|
473 } |
|
474 |
|
475 // If. |
|
476 |
1742
|
477 tree_if_clause::~tree_if_clause (void) |
|
478 { |
|
479 delete expr; |
|
480 delete list; |
|
481 } |
|
482 |
577
|
483 int |
|
484 tree_if_clause::eval (void) |
|
485 { |
1491
|
486 if (is_else_clause () || expr->is_logically_true ("if")) |
577
|
487 { |
|
488 if (list) |
1827
|
489 list->eval (true); |
577
|
490 |
|
491 return 1; |
|
492 } |
|
493 |
|
494 return 0; |
|
495 } |
|
496 |
|
497 void |
2124
|
498 tree_if_clause::accept (tree_walker& tw) |
581
|
499 { |
2124
|
500 tw.visit_if_clause (*this); |
581
|
501 } |
|
502 |
|
503 // List of if commands. |
|
504 |
|
505 void |
577
|
506 tree_if_command_list::eval (void) |
|
507 { |
|
508 for (Pix p = first (); p != 0; next (p)) |
|
509 { |
|
510 tree_if_clause *t = this->operator () (p); |
|
511 |
|
512 if (t->eval () || error_state) |
|
513 break; |
|
514 } |
|
515 } |
|
516 |
581
|
517 void |
2124
|
518 tree_if_command_list::accept (tree_walker& tw) |
581
|
519 { |
2124
|
520 tw.visit_if_command_list (*this); |
581
|
521 } |
|
522 |
2764
|
523 // Switch. |
|
524 |
|
525 tree_switch_case::~tree_switch_case (void) |
|
526 { |
|
527 delete label; |
|
528 delete list; |
|
529 } |
|
530 |
|
531 bool |
|
532 tree_switch_case::label_matches (const octave_value& val) |
|
533 { |
|
534 bool retval = false; |
|
535 |
2859
|
536 octave_value label_value = label->eval (); |
2764
|
537 |
|
538 if (! error_state) |
|
539 { |
|
540 if (label_value.is_defined ()) |
|
541 { |
|
542 octave_value tmp = do_binary_op (octave_value::eq, |
|
543 val, label_value); |
|
544 |
|
545 if (! error_state) |
|
546 { |
|
547 if (tmp.is_defined ()) |
|
548 retval = tmp.is_true (); |
|
549 else |
|
550 eval_error (); |
|
551 } |
|
552 else |
|
553 eval_error (); |
|
554 } |
|
555 else |
|
556 eval_error (); |
|
557 } |
|
558 else |
|
559 eval_error (); |
|
560 |
|
561 return retval; |
|
562 } |
|
563 |
|
564 int |
|
565 tree_switch_case::eval (const octave_value& val) |
|
566 { |
|
567 int retval = 0; |
|
568 |
|
569 if (is_default_case () || label_matches (val)) |
|
570 { |
|
571 if (list) |
|
572 list->eval (true); |
|
573 |
|
574 retval = 1; |
|
575 } |
|
576 |
|
577 return retval; |
|
578 } |
|
579 |
|
580 void |
|
581 tree_switch_case::eval_error (void) |
|
582 { |
|
583 ::error ("evaluating switch case label"); |
|
584 } |
|
585 |
|
586 void |
|
587 tree_switch_case::accept (tree_walker& tw) |
|
588 { |
|
589 tw.visit_switch_case (*this); |
|
590 } |
|
591 |
|
592 // List of switch cases. |
|
593 |
|
594 void |
|
595 tree_switch_case_list::eval (const octave_value& val) |
|
596 { |
|
597 for (Pix p = first (); p != 0; next (p)) |
|
598 { |
|
599 tree_switch_case *t = this->operator () (p); |
|
600 |
|
601 if (t->eval (val) || error_state) |
|
602 break; |
|
603 } |
|
604 } |
|
605 |
|
606 void |
|
607 tree_switch_case_list::accept (tree_walker& tw) |
|
608 { |
|
609 tw.visit_switch_case_list (*this); |
|
610 } |
|
611 |
577
|
612 /* |
|
613 ;;; Local Variables: *** |
|
614 ;;; mode: C++ *** |
|
615 ;;; End: *** |
|
616 */ |