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 |
|
305 tree_parameter_list::convert_to_const_vector (void) |
|
306 { |
|
307 int nout = length (); |
|
308 |
|
309 Octave_object retval; |
|
310 retval.resize (nout); |
|
311 |
|
312 int i = 0; |
|
313 |
|
314 for (Pix p = first (); p != 0; next (p)) |
|
315 { |
|
316 tree_identifier *elt = this->operator () (p); |
|
317 |
|
318 if (elt->is_defined ()) |
|
319 retval(i) = elt->eval (0); |
|
320 |
|
321 i++; |
|
322 } |
|
323 |
|
324 return retval; |
|
325 } |
|
326 |
|
327 int |
|
328 tree_parameter_list::is_defined (void) |
|
329 { |
|
330 int status = 1; |
|
331 |
|
332 for (Pix p = first (); p != 0; next (p)) |
|
333 { |
|
334 tree_identifier *elt = this->operator () (p); |
|
335 |
|
336 if (! elt->is_defined ()) |
|
337 { |
|
338 status = 0; |
|
339 break; |
|
340 } |
|
341 } |
|
342 |
|
343 return status; |
|
344 } |
|
345 |
|
346 void |
581
|
347 tree_parameter_list::print_code (ostream& os) |
|
348 { |
|
349 Pix p = first (); |
|
350 |
|
351 while (p) |
|
352 { |
|
353 tree_identifier *elt = this->operator () (p); |
|
354 |
|
355 next (p); |
|
356 |
|
357 if (elt) |
|
358 { |
|
359 elt->print_code (os); |
|
360 |
|
361 if (p) |
|
362 os << ", "; |
|
363 } |
|
364 } |
|
365 } |
|
366 |
|
367 // Return lists. |
|
368 |
|
369 void |
|
370 tree_return_list::print_code (ostream& os) |
|
371 { |
|
372 Pix p = first (); |
|
373 |
|
374 while (p) |
|
375 { |
|
376 tree_index_expression *elt = this->operator () (p); |
|
377 |
|
378 next (p); |
|
379 |
|
380 if (elt) |
|
381 { |
|
382 elt->print_code (os); |
|
383 |
|
384 if (p) |
|
385 os << ", "; |
|
386 } |
|
387 } |
|
388 } |
|
389 |
|
390 // Global. |
|
391 |
|
392 void |
577
|
393 tree_global::eval (void) |
|
394 { |
|
395 if (ident) |
|
396 { |
|
397 ident->link_to_global (); |
|
398 } |
|
399 else if (assign_expr) |
|
400 { |
|
401 tree_identifier *id = assign_expr->left_hand_side (); |
|
402 |
|
403 if (id) |
|
404 id->link_to_global (); |
|
405 |
|
406 assign_expr->eval (0); |
|
407 } |
|
408 } |
|
409 |
|
410 void |
581
|
411 tree_global::print_code (ostream& os) |
|
412 { |
|
413 if (ident) |
|
414 ident->print_code (os); |
|
415 |
|
416 if (assign_expr) |
|
417 assign_expr->print_code (os); |
|
418 } |
|
419 |
|
420 // Global initializer lists. |
|
421 |
|
422 void |
577
|
423 tree_global_init_list::eval (void) |
|
424 { |
|
425 for (Pix p = first (); p != 0; next (p)) |
|
426 { |
|
427 tree_global *t = this->operator () (p); |
|
428 t->eval (); |
|
429 } |
|
430 } |
|
431 |
581
|
432 void |
|
433 tree_global_init_list::print_code (ostream& os) |
|
434 { |
|
435 Pix p = first (); |
|
436 |
|
437 while (p) |
|
438 { |
|
439 tree_global *elt = this->operator () (p); |
|
440 |
|
441 next (p); |
|
442 |
|
443 if (elt) |
|
444 { |
|
445 elt->print_code (os); |
|
446 |
|
447 if (p) |
|
448 os << ", "; |
|
449 } |
|
450 } |
|
451 } |
|
452 |
|
453 // If. |
|
454 |
577
|
455 int |
|
456 tree_if_clause::eval (void) |
|
457 { |
|
458 if (expr) |
|
459 { |
|
460 tree_constant t1 = expr->eval (0); |
|
461 |
|
462 if (error_state || t1.is_undefined ()) |
|
463 return 0; |
|
464 |
|
465 if (t1.rows () == 0 || t1.columns () == 0) |
|
466 { |
|
467 int flag = user_pref.propagate_empty_matrices; |
|
468 if (flag < 0) |
|
469 warning ("if: empty matrix used in conditional"); |
|
470 else if (flag == 0) |
|
471 { |
|
472 ::error ("if: empty matrix used in conditional"); |
|
473 return 0; |
|
474 } |
|
475 t1 = tree_constant (0.0); |
|
476 } |
|
477 else if (! t1.is_scalar_type ()) |
|
478 { |
|
479 tree_constant t2 = t1.all (); |
|
480 t1 = t2.all (); |
|
481 } |
|
482 |
|
483 int expr_value = 0; |
620
|
484 |
|
485 if (t1.is_real_scalar ()) |
577
|
486 expr_value = (int) t1.double_value (); |
620
|
487 else if (t1.is_complex_scalar ()) |
577
|
488 expr_value = t1.complex_value () != 0.0; |
|
489 else |
|
490 panic_impossible (); |
|
491 |
|
492 if (expr_value) |
|
493 { |
|
494 if (list) |
|
495 list->eval (1); |
|
496 |
|
497 return 1; |
|
498 } |
|
499 } |
|
500 else |
|
501 { |
|
502 if (list) |
|
503 list->eval (1); |
|
504 |
|
505 return 1; |
|
506 } |
|
507 |
|
508 return 0; |
|
509 } |
|
510 |
|
511 void |
581
|
512 tree_if_clause::print_code (ostream& os) |
|
513 { |
|
514 if (expr) |
|
515 { |
|
516 expr->print_code (os); |
|
517 |
|
518 print_code_new_line (os); |
|
519 |
|
520 increment_indent_level (); |
|
521 } |
|
522 else |
|
523 { |
|
524 print_code_indent (os); |
|
525 |
|
526 os << "else"; |
|
527 |
|
528 print_code_new_line (os); |
|
529 |
|
530 increment_indent_level (); |
|
531 } |
|
532 |
|
533 if (list) |
|
534 { |
|
535 list->print_code (os); |
|
536 |
|
537 decrement_indent_level (); |
|
538 } |
|
539 } |
|
540 |
|
541 // List of if commands. |
|
542 |
|
543 void |
577
|
544 tree_if_command_list::eval (void) |
|
545 { |
|
546 for (Pix p = first (); p != 0; next (p)) |
|
547 { |
|
548 tree_if_clause *t = this->operator () (p); |
|
549 |
|
550 if (t->eval () || error_state) |
|
551 break; |
|
552 } |
|
553 } |
|
554 |
581
|
555 void |
|
556 tree_if_command_list::print_code (ostream& os) |
|
557 { |
|
558 Pix p = first (); |
|
559 |
|
560 int first_elt = 1; |
|
561 |
|
562 while (p) |
|
563 { |
|
564 tree_if_clause *elt = this->operator () (p); |
|
565 |
|
566 next (p); |
|
567 |
|
568 if (elt) |
|
569 { |
|
570 if (p && ! first_elt) |
|
571 { |
|
572 print_code_indent (os); |
|
573 |
|
574 os << "elseif "; |
|
575 } |
|
576 |
|
577 elt->print_code (os); |
|
578 } |
|
579 |
|
580 first_elt = 0; |
|
581 } |
|
582 } |
|
583 |
577
|
584 /* |
|
585 ;;; Local Variables: *** |
|
586 ;;; mode: C++ *** |
|
587 ;;; page-delimiter: "^/\\*" *** |
|
588 ;;; End: *** |
|
589 */ |