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 { |
|
208 int len = length () + 1; |
|
209 |
|
210 Octave_object args; |
|
211 args.resize (len); |
|
212 |
|
213 // args[0] may eventually hold something useful, like the function |
|
214 // name. |
|
215 Pix p = first (); |
|
216 for (int k = 1; k < len; k++) |
|
217 { |
|
218 tree_expression *elt = this->operator () (p); |
|
219 if (elt) |
|
220 { |
|
221 args(k) = elt->eval (0); |
|
222 if (error_state) |
|
223 { |
|
224 ::error ("evaluating argument list element number %d", k); |
|
225 break; |
|
226 } |
|
227 next (p); |
|
228 } |
|
229 else |
|
230 { |
|
231 args(k) = tree_constant (); |
|
232 break; |
|
233 } |
|
234 } |
|
235 return args; |
|
236 } |
|
237 |
581
|
238 void |
|
239 tree_argument_list::print_code (ostream& os) |
|
240 { |
|
241 Pix p = first (); |
|
242 |
|
243 while (p) |
|
244 { |
|
245 tree_expression *elt = this->operator () (p); |
|
246 |
|
247 next (p); |
|
248 |
|
249 if (elt) |
|
250 { |
|
251 elt->print_code (os); |
|
252 |
|
253 if (p) |
|
254 os << ", "; |
|
255 } |
|
256 } |
|
257 } |
|
258 |
577
|
259 // Parameter lists. |
|
260 |
|
261 void |
|
262 tree_parameter_list::mark_as_formal_parameters (void) |
|
263 { |
|
264 for (Pix p = first (); p != 0; next (p)) |
|
265 { |
|
266 tree_identifier *elt = this->operator () (p); |
|
267 elt->mark_as_formal_parameter (); |
|
268 } |
|
269 } |
|
270 |
|
271 void |
|
272 tree_parameter_list::define_from_arg_vector (const Octave_object& args) |
|
273 { |
|
274 if (args.length () <= 0) |
|
275 return; |
|
276 |
|
277 int nargin = args.length (); |
|
278 |
|
279 int expected_nargin = length () + 1; |
|
280 |
|
281 Pix p = first (); |
|
282 |
|
283 for (int i = 1; i < expected_nargin; i++) |
|
284 { |
|
285 tree_identifier *elt = this->operator () (p); |
|
286 |
|
287 tree_constant *tmp = 0; |
|
288 |
|
289 if (i < nargin) |
|
290 { |
620
|
291 if (args(i).is_defined () && args(i).is_magic_colon ()) |
577
|
292 { |
|
293 ::error ("invalid use of colon in function argument list"); |
|
294 return; |
|
295 } |
|
296 tmp = new tree_constant (args(i)); |
|
297 } |
|
298 |
|
299 elt->define (tmp); |
|
300 |
|
301 next (p); |
|
302 } |
|
303 } |
|
304 |
|
305 Octave_object |
|
306 tree_parameter_list::convert_to_const_vector (void) |
|
307 { |
|
308 int nout = length (); |
|
309 |
|
310 Octave_object retval; |
|
311 retval.resize (nout); |
|
312 |
|
313 int i = 0; |
|
314 |
|
315 for (Pix p = first (); p != 0; next (p)) |
|
316 { |
|
317 tree_identifier *elt = this->operator () (p); |
|
318 |
|
319 if (elt->is_defined ()) |
|
320 retval(i) = elt->eval (0); |
|
321 |
|
322 i++; |
|
323 } |
|
324 |
|
325 return retval; |
|
326 } |
|
327 |
|
328 int |
|
329 tree_parameter_list::is_defined (void) |
|
330 { |
|
331 int status = 1; |
|
332 |
|
333 for (Pix p = first (); p != 0; next (p)) |
|
334 { |
|
335 tree_identifier *elt = this->operator () (p); |
|
336 |
|
337 if (! elt->is_defined ()) |
|
338 { |
|
339 status = 0; |
|
340 break; |
|
341 } |
|
342 } |
|
343 |
|
344 return status; |
|
345 } |
|
346 |
|
347 void |
581
|
348 tree_parameter_list::print_code (ostream& os) |
|
349 { |
|
350 Pix p = first (); |
|
351 |
|
352 while (p) |
|
353 { |
|
354 tree_identifier *elt = this->operator () (p); |
|
355 |
|
356 next (p); |
|
357 |
|
358 if (elt) |
|
359 { |
|
360 elt->print_code (os); |
|
361 |
|
362 if (p) |
|
363 os << ", "; |
|
364 } |
|
365 } |
|
366 } |
|
367 |
|
368 // Return lists. |
|
369 |
|
370 void |
|
371 tree_return_list::print_code (ostream& os) |
|
372 { |
|
373 Pix p = first (); |
|
374 |
|
375 while (p) |
|
376 { |
|
377 tree_index_expression *elt = this->operator () (p); |
|
378 |
|
379 next (p); |
|
380 |
|
381 if (elt) |
|
382 { |
|
383 elt->print_code (os); |
|
384 |
|
385 if (p) |
|
386 os << ", "; |
|
387 } |
|
388 } |
|
389 } |
|
390 |
|
391 // Global. |
|
392 |
|
393 void |
577
|
394 tree_global::eval (void) |
|
395 { |
|
396 if (ident) |
|
397 { |
|
398 ident->link_to_global (); |
|
399 } |
|
400 else if (assign_expr) |
|
401 { |
|
402 tree_identifier *id = assign_expr->left_hand_side (); |
|
403 |
|
404 if (id) |
|
405 id->link_to_global (); |
|
406 |
|
407 assign_expr->eval (0); |
|
408 } |
|
409 } |
|
410 |
|
411 void |
581
|
412 tree_global::print_code (ostream& os) |
|
413 { |
|
414 if (ident) |
|
415 ident->print_code (os); |
|
416 |
|
417 if (assign_expr) |
|
418 assign_expr->print_code (os); |
|
419 } |
|
420 |
|
421 // Global initializer lists. |
|
422 |
|
423 void |
577
|
424 tree_global_init_list::eval (void) |
|
425 { |
|
426 for (Pix p = first (); p != 0; next (p)) |
|
427 { |
|
428 tree_global *t = this->operator () (p); |
|
429 t->eval (); |
|
430 } |
|
431 } |
|
432 |
581
|
433 void |
|
434 tree_global_init_list::print_code (ostream& os) |
|
435 { |
|
436 Pix p = first (); |
|
437 |
|
438 while (p) |
|
439 { |
|
440 tree_global *elt = this->operator () (p); |
|
441 |
|
442 next (p); |
|
443 |
|
444 if (elt) |
|
445 { |
|
446 elt->print_code (os); |
|
447 |
|
448 if (p) |
|
449 os << ", "; |
|
450 } |
|
451 } |
|
452 } |
|
453 |
|
454 // If. |
|
455 |
577
|
456 int |
|
457 tree_if_clause::eval (void) |
|
458 { |
|
459 if (expr) |
|
460 { |
|
461 tree_constant t1 = expr->eval (0); |
|
462 |
|
463 if (error_state || t1.is_undefined ()) |
|
464 return 0; |
|
465 |
|
466 if (t1.rows () == 0 || t1.columns () == 0) |
|
467 { |
|
468 int flag = user_pref.propagate_empty_matrices; |
|
469 if (flag < 0) |
|
470 warning ("if: empty matrix used in conditional"); |
|
471 else if (flag == 0) |
|
472 { |
|
473 ::error ("if: empty matrix used in conditional"); |
|
474 return 0; |
|
475 } |
|
476 t1 = tree_constant (0.0); |
|
477 } |
|
478 else if (! t1.is_scalar_type ()) |
|
479 { |
|
480 tree_constant t2 = t1.all (); |
|
481 t1 = t2.all (); |
|
482 } |
|
483 |
|
484 int expr_value = 0; |
620
|
485 |
|
486 if (t1.is_real_scalar ()) |
577
|
487 expr_value = (int) t1.double_value (); |
620
|
488 else if (t1.is_complex_scalar ()) |
577
|
489 expr_value = t1.complex_value () != 0.0; |
|
490 else |
|
491 panic_impossible (); |
|
492 |
|
493 if (expr_value) |
|
494 { |
|
495 if (list) |
|
496 list->eval (1); |
|
497 |
|
498 return 1; |
|
499 } |
|
500 } |
|
501 else |
|
502 { |
|
503 if (list) |
|
504 list->eval (1); |
|
505 |
|
506 return 1; |
|
507 } |
|
508 |
|
509 return 0; |
|
510 } |
|
511 |
|
512 void |
581
|
513 tree_if_clause::print_code (ostream& os) |
|
514 { |
|
515 if (expr) |
|
516 { |
|
517 expr->print_code (os); |
|
518 |
|
519 print_code_new_line (os); |
|
520 |
|
521 increment_indent_level (); |
|
522 } |
|
523 else |
|
524 { |
|
525 print_code_indent (os); |
|
526 |
|
527 os << "else"; |
|
528 |
|
529 print_code_new_line (os); |
|
530 |
|
531 increment_indent_level (); |
|
532 } |
|
533 |
|
534 if (list) |
|
535 { |
|
536 list->print_code (os); |
|
537 |
|
538 decrement_indent_level (); |
|
539 } |
|
540 } |
|
541 |
|
542 // List of if commands. |
|
543 |
|
544 void |
577
|
545 tree_if_command_list::eval (void) |
|
546 { |
|
547 for (Pix p = first (); p != 0; next (p)) |
|
548 { |
|
549 tree_if_clause *t = this->operator () (p); |
|
550 |
|
551 if (t->eval () || error_state) |
|
552 break; |
|
553 } |
|
554 } |
|
555 |
581
|
556 void |
|
557 tree_if_command_list::print_code (ostream& os) |
|
558 { |
|
559 Pix p = first (); |
|
560 |
|
561 int first_elt = 1; |
|
562 |
|
563 while (p) |
|
564 { |
|
565 tree_if_clause *elt = this->operator () (p); |
|
566 |
|
567 next (p); |
|
568 |
|
569 if (elt) |
|
570 { |
|
571 if (p && ! first_elt) |
|
572 { |
|
573 print_code_indent (os); |
|
574 |
|
575 os << "elseif "; |
|
576 } |
|
577 |
|
578 elt->print_code (os); |
|
579 } |
|
580 |
|
581 first_elt = 0; |
|
582 } |
|
583 } |
|
584 |
577
|
585 /* |
|
586 ;;; Local Variables: *** |
|
587 ;;; mode: C++ *** |
|
588 ;;; page-delimiter: "^/\\*" *** |
|
589 ;;; End: *** |
|
590 */ |