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