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