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