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