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