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