577
|
1 // tree-misc.cc -*- C++ -*- |
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
577
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
577
|
21 |
|
22 */ |
|
23 |
1297
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
577
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
577
|
30 #endif |
|
31 |
1350
|
32 #include <iostream.h> |
1588
|
33 #include <strstream.h> |
1350
|
34 |
|
35 #ifdef HAVE_UNISTD_H |
577
|
36 #include <sys/types.h> |
|
37 #include <unistd.h> |
|
38 #endif |
|
39 |
|
40 #include "error.h" |
1352
|
41 #include "oct-obj.h" |
1670
|
42 #include "toplev.h" |
1588
|
43 #include "pager.h" |
1352
|
44 #include "tree-base.h" |
|
45 #include "tree-cmd.h" |
577
|
46 #include "tree-const.h" |
1352
|
47 #include "tree-expr.h" |
|
48 #include "tree-misc.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 |
581
|
68 void |
1588
|
69 tree_statement::maybe_echo_code (int in_function_body) |
|
70 { |
|
71 if (in_function_body |
|
72 && (user_pref.echo_executing_commands & ECHO_FUNCTIONS)) |
|
73 { |
|
74 ostrstream output_buf; |
|
75 print_code (output_buf); |
|
76 output_buf << ends; |
|
77 maybe_page_output (output_buf); |
|
78 } |
|
79 } |
|
80 |
|
81 void |
581
|
82 tree_statement::print_code (ostream& os) |
|
83 { |
|
84 if (command) |
|
85 { |
|
86 command->print_code (os); |
|
87 |
|
88 if (! print_flag) |
|
89 os << ";"; |
|
90 |
|
91 command->print_code_new_line (os); |
|
92 } |
|
93 else if (expression) |
|
94 { |
|
95 expression->print_code (os); |
|
96 |
|
97 if (! print_flag) |
|
98 os << ";"; |
|
99 |
|
100 expression->print_code_new_line (os); |
|
101 } |
|
102 } |
|
103 |
577
|
104 tree_constant |
|
105 tree_statement_list::eval (int print) |
|
106 { |
|
107 int pf; |
|
108 tree_constant retval; |
|
109 |
|
110 if (error_state) |
|
111 return retval; |
|
112 |
|
113 for (Pix p = first (); p != 0; next (p)) |
|
114 { |
|
115 tree_statement *elt = this->operator () (p); |
|
116 |
|
117 if (print == 0) |
|
118 pf = 0; |
|
119 else |
|
120 pf = elt->print_flag; |
|
121 |
|
122 tree_command *cmd = elt->command; |
|
123 tree_expression *expr = elt->expression; |
|
124 |
|
125 if (cmd || expr) |
|
126 { |
1588
|
127 elt->maybe_echo_code (function_body); |
|
128 |
577
|
129 if (cmd) |
|
130 cmd->eval (); |
|
131 else |
|
132 retval = expr->eval (pf); |
|
133 |
|
134 if (error_state) |
|
135 return tree_constant (); |
|
136 |
|
137 if (breaking || continuing) |
|
138 break; |
|
139 |
|
140 if (returning) |
|
141 break; |
|
142 } |
|
143 else |
|
144 retval = tree_constant (); |
|
145 } |
|
146 return retval; |
|
147 } |
|
148 |
672
|
149 Octave_object |
|
150 tree_statement_list::eval (int print, int nargout) |
|
151 { |
|
152 Octave_object retval; |
|
153 |
|
154 if (nargout > 1) |
|
155 { |
|
156 int pf; |
|
157 |
|
158 if (error_state) |
|
159 return retval; |
|
160 |
|
161 for (Pix p = first (); p != 0; next (p)) |
|
162 { |
|
163 tree_statement *elt = this->operator () (p); |
|
164 |
|
165 if (print == 0) |
|
166 pf = 0; |
|
167 else |
|
168 pf = elt->print_flag; |
|
169 |
|
170 tree_command *cmd = elt->command; |
|
171 tree_expression *expr = elt->expression; |
|
172 |
|
173 if (cmd || expr) |
|
174 { |
1588
|
175 elt->maybe_echo_code (function_body); |
|
176 |
672
|
177 if (cmd) |
|
178 cmd->eval (); |
|
179 else |
|
180 { |
|
181 if (expr->is_multi_val_ret_expression ()) |
|
182 { |
|
183 Octave_object args; |
|
184 tree_multi_val_ret *t = (tree_multi_val_ret *) expr; |
|
185 retval = t->eval (pf, nargout, args); |
|
186 } |
|
187 else |
|
188 retval = expr->eval (pf); |
|
189 } |
|
190 |
|
191 if (error_state) |
|
192 return tree_constant (); |
|
193 |
|
194 if (breaking || continuing) |
|
195 break; |
|
196 |
|
197 if (returning) |
|
198 break; |
|
199 } |
|
200 else |
|
201 retval = Octave_object (); |
|
202 } |
|
203 return retval; |
|
204 } |
|
205 else |
|
206 retval = eval (print); |
|
207 |
|
208 return retval; |
|
209 } |
|
210 |
581
|
211 void |
|
212 tree_statement_list::print_code (ostream& os) |
|
213 { |
|
214 for (Pix p = first (); p != 0; next (p)) |
|
215 { |
|
216 tree_statement *elt = this->operator () (p); |
|
217 |
|
218 if (elt) |
|
219 elt->print_code (os); |
|
220 } |
|
221 } |
|
222 |
577
|
223 Octave_object |
|
224 tree_argument_list::convert_to_const_vector (void) |
|
225 { |
712
|
226 int len = length (); |
577
|
227 |
1358
|
228 // XXX FIXME XXX -- would be nice to know in advance how largs args |
|
229 // needs to be even when we have a list containing an all_va_args |
|
230 // token. |
922
|
231 |
577
|
232 Octave_object args; |
|
233 args.resize (len); |
|
234 |
|
235 Pix p = first (); |
922
|
236 int j = 0; |
712
|
237 for (int k = 0; k < len; k++) |
577
|
238 { |
|
239 tree_expression *elt = this->operator () (p); |
|
240 if (elt) |
|
241 { |
922
|
242 tree_constant tmp = elt->eval (0); |
577
|
243 if (error_state) |
|
244 { |
|
245 ::error ("evaluating argument list element number %d", k); |
922
|
246 args = Octave_object (); |
577
|
247 break; |
|
248 } |
922
|
249 else |
|
250 { |
|
251 if (tmp.is_all_va_args ()) |
|
252 { |
|
253 if (curr_function) |
|
254 { |
|
255 Octave_object tva; |
|
256 tva = curr_function->octave_all_va_args (); |
|
257 int n = tva.length (); |
|
258 for (int i = 0; i < n; i++) |
|
259 args(j++) = tva(i); |
|
260 } |
|
261 else |
|
262 { |
|
263 ::error ("all_va_args is only valid inside functions"); |
|
264 args = Octave_object (); |
|
265 break; |
|
266 } |
|
267 } |
|
268 else |
|
269 args(j++) = tmp; |
|
270 } |
577
|
271 next (p); |
|
272 } |
|
273 else |
|
274 { |
927
|
275 args(j++) = tree_constant (); |
577
|
276 break; |
|
277 } |
|
278 } |
712
|
279 |
927
|
280 args.resize (j); |
|
281 |
577
|
282 return args; |
|
283 } |
|
284 |
581
|
285 void |
|
286 tree_argument_list::print_code (ostream& os) |
|
287 { |
|
288 Pix p = first (); |
|
289 |
|
290 while (p) |
|
291 { |
|
292 tree_expression *elt = this->operator () (p); |
|
293 |
|
294 next (p); |
|
295 |
|
296 if (elt) |
|
297 { |
|
298 elt->print_code (os); |
|
299 |
|
300 if (p) |
|
301 os << ", "; |
|
302 } |
|
303 } |
|
304 } |
|
305 |
577
|
306 // Parameter lists. |
|
307 |
|
308 void |
|
309 tree_parameter_list::mark_as_formal_parameters (void) |
|
310 { |
|
311 for (Pix p = first (); p != 0; next (p)) |
|
312 { |
|
313 tree_identifier *elt = this->operator () (p); |
|
314 elt->mark_as_formal_parameter (); |
|
315 } |
|
316 } |
|
317 |
|
318 void |
1127
|
319 tree_parameter_list::initialize_undefined_elements (tree_constant& val) |
1093
|
320 { |
|
321 for (Pix p = first (); p != 0; next (p)) |
|
322 { |
|
323 tree_identifier *elt = this->operator () (p); |
|
324 if (! elt->is_defined ()) |
|
325 elt->assign (val); |
|
326 } |
|
327 } |
|
328 |
|
329 void |
577
|
330 tree_parameter_list::define_from_arg_vector (const Octave_object& args) |
|
331 { |
712
|
332 int nargin = args.length (); |
|
333 |
|
334 if (nargin <= 0) |
577
|
335 return; |
|
336 |
712
|
337 int expected_nargin = length (); |
577
|
338 |
|
339 Pix p = first (); |
|
340 |
712
|
341 for (int i = 0; i < expected_nargin; i++) |
577
|
342 { |
|
343 tree_identifier *elt = this->operator () (p); |
|
344 |
|
345 tree_constant *tmp = 0; |
|
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 } |
|
354 tmp = new tree_constant (args(i)); |
|
355 } |
|
356 |
|
357 elt->define (tmp); |
|
358 |
|
359 next (p); |
|
360 } |
|
361 } |
|
362 |
|
363 Octave_object |
723
|
364 tree_parameter_list::convert_to_const_vector (tree_va_return_list *vr_list) |
577
|
365 { |
|
366 int nout = length (); |
|
367 |
723
|
368 if (vr_list) |
|
369 nout += vr_list->length (); |
|
370 |
577
|
371 Octave_object retval; |
|
372 retval.resize (nout); |
|
373 |
|
374 int i = 0; |
|
375 |
|
376 for (Pix p = first (); p != 0; next (p)) |
|
377 { |
|
378 tree_identifier *elt = this->operator () (p); |
|
379 |
|
380 if (elt->is_defined ()) |
|
381 retval(i) = elt->eval (0); |
|
382 |
|
383 i++; |
|
384 } |
|
385 |
723
|
386 if (vr_list) |
|
387 { |
1321
|
388 for (Pix p = vr_list->first (); p != 0; vr_list->next (p)) |
723
|
389 { |
|
390 retval(i) = vr_list->operator () (p); |
|
391 i++; |
|
392 } |
|
393 } |
|
394 |
577
|
395 return retval; |
|
396 } |
|
397 |
|
398 int |
|
399 tree_parameter_list::is_defined (void) |
|
400 { |
|
401 int status = 1; |
|
402 |
|
403 for (Pix p = first (); p != 0; next (p)) |
|
404 { |
|
405 tree_identifier *elt = this->operator () (p); |
|
406 |
|
407 if (! elt->is_defined ()) |
|
408 { |
|
409 status = 0; |
|
410 break; |
|
411 } |
|
412 } |
|
413 |
|
414 return status; |
|
415 } |
|
416 |
|
417 void |
581
|
418 tree_parameter_list::print_code (ostream& os) |
|
419 { |
|
420 Pix p = first (); |
|
421 |
|
422 while (p) |
|
423 { |
|
424 tree_identifier *elt = this->operator () (p); |
|
425 |
|
426 next (p); |
|
427 |
|
428 if (elt) |
|
429 { |
|
430 elt->print_code (os); |
|
431 |
|
432 if (p) |
|
433 os << ", "; |
|
434 } |
|
435 } |
|
436 } |
|
437 |
|
438 // Return lists. |
|
439 |
|
440 void |
|
441 tree_return_list::print_code (ostream& os) |
|
442 { |
|
443 Pix p = first (); |
|
444 |
|
445 while (p) |
|
446 { |
|
447 tree_index_expression *elt = this->operator () (p); |
|
448 |
|
449 next (p); |
|
450 |
|
451 if (elt) |
|
452 { |
|
453 elt->print_code (os); |
|
454 |
|
455 if (p) |
|
456 os << ", "; |
|
457 } |
|
458 } |
|
459 } |
|
460 |
|
461 // Global. |
|
462 |
|
463 void |
577
|
464 tree_global::eval (void) |
|
465 { |
|
466 if (ident) |
|
467 { |
|
468 ident->link_to_global (); |
|
469 } |
|
470 else if (assign_expr) |
|
471 { |
745
|
472 tree_identifier *id = 0; |
|
473 if (assign_expr->left_hand_side_is_identifier_only () |
|
474 && (id = assign_expr->left_hand_side_id ())) |
|
475 { |
|
476 id->link_to_global (); |
|
477 assign_expr->eval (0); |
|
478 } |
|
479 else |
|
480 error ("global: unable to make individual structure elements global"); |
577
|
481 } |
|
482 } |
|
483 |
|
484 void |
581
|
485 tree_global::print_code (ostream& os) |
|
486 { |
|
487 if (ident) |
|
488 ident->print_code (os); |
|
489 |
|
490 if (assign_expr) |
|
491 assign_expr->print_code (os); |
|
492 } |
|
493 |
|
494 // Global initializer lists. |
|
495 |
|
496 void |
577
|
497 tree_global_init_list::eval (void) |
|
498 { |
|
499 for (Pix p = first (); p != 0; next (p)) |
|
500 { |
|
501 tree_global *t = this->operator () (p); |
|
502 t->eval (); |
|
503 } |
|
504 } |
|
505 |
581
|
506 void |
|
507 tree_global_init_list::print_code (ostream& os) |
|
508 { |
|
509 Pix p = first (); |
|
510 |
|
511 while (p) |
|
512 { |
|
513 tree_global *elt = this->operator () (p); |
|
514 |
|
515 next (p); |
|
516 |
|
517 if (elt) |
|
518 { |
|
519 elt->print_code (os); |
|
520 |
|
521 if (p) |
|
522 os << ", "; |
|
523 } |
|
524 } |
|
525 } |
|
526 |
|
527 // If. |
|
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) |
|
535 list->eval (1); |
|
536 |
|
537 return 1; |
|
538 } |
|
539 |
|
540 return 0; |
|
541 } |
|
542 |
|
543 void |
581
|
544 tree_if_clause::print_code (ostream& os) |
|
545 { |
|
546 if (expr) |
1063
|
547 expr->print_code (os); |
581
|
548 |
1063
|
549 print_code_new_line (os); |
1588
|
550 |
1063
|
551 increment_indent_level (); |
581
|
552 |
|
553 if (list) |
|
554 { |
|
555 list->print_code (os); |
|
556 |
|
557 decrement_indent_level (); |
|
558 } |
|
559 } |
|
560 |
|
561 // List of if commands. |
|
562 |
|
563 void |
577
|
564 tree_if_command_list::eval (void) |
|
565 { |
|
566 for (Pix p = first (); p != 0; next (p)) |
|
567 { |
|
568 tree_if_clause *t = this->operator () (p); |
|
569 |
|
570 if (t->eval () || error_state) |
|
571 break; |
|
572 } |
|
573 } |
|
574 |
581
|
575 void |
|
576 tree_if_command_list::print_code (ostream& os) |
|
577 { |
|
578 Pix p = first (); |
|
579 |
|
580 int first_elt = 1; |
|
581 |
|
582 while (p) |
|
583 { |
|
584 tree_if_clause *elt = this->operator () (p); |
|
585 |
|
586 if (elt) |
|
587 { |
1063
|
588 if (! first_elt) |
581
|
589 { |
|
590 print_code_indent (os); |
|
591 |
1063
|
592 if (elt->is_else_clause ()) |
|
593 os << "else"; |
|
594 else |
|
595 os << "elseif "; |
581
|
596 } |
|
597 |
|
598 elt->print_code (os); |
|
599 } |
|
600 |
|
601 first_elt = 0; |
1063
|
602 next (p); |
581
|
603 } |
|
604 } |
|
605 |
577
|
606 /* |
|
607 ;;; Local Variables: *** |
|
608 ;;; mode: C++ *** |
|
609 ;;; page-delimiter: "^/\\*" *** |
|
610 ;;; End: *** |
|
611 */ |