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 |
581
|
133 void |
|
134 tree_statement_list::print_code (ostream& os) |
|
135 { |
|
136 for (Pix p = first (); p != 0; next (p)) |
|
137 { |
|
138 tree_statement *elt = this->operator () (p); |
|
139 |
|
140 if (elt) |
|
141 elt->print_code (os); |
|
142 } |
|
143 } |
|
144 |
577
|
145 Octave_object |
|
146 tree_argument_list::convert_to_const_vector (void) |
|
147 { |
|
148 int len = length () + 1; |
|
149 |
|
150 Octave_object args; |
|
151 args.resize (len); |
|
152 |
|
153 // args[0] may eventually hold something useful, like the function |
|
154 // name. |
|
155 Pix p = first (); |
|
156 for (int k = 1; k < len; k++) |
|
157 { |
|
158 tree_expression *elt = this->operator () (p); |
|
159 if (elt) |
|
160 { |
|
161 args(k) = elt->eval (0); |
|
162 if (error_state) |
|
163 { |
|
164 ::error ("evaluating argument list element number %d", k); |
|
165 break; |
|
166 } |
|
167 next (p); |
|
168 } |
|
169 else |
|
170 { |
|
171 args(k) = tree_constant (); |
|
172 break; |
|
173 } |
|
174 } |
|
175 return args; |
|
176 } |
|
177 |
581
|
178 void |
|
179 tree_argument_list::print_code (ostream& os) |
|
180 { |
|
181 Pix p = first (); |
|
182 |
|
183 while (p) |
|
184 { |
|
185 tree_expression *elt = this->operator () (p); |
|
186 |
|
187 next (p); |
|
188 |
|
189 if (elt) |
|
190 { |
|
191 elt->print_code (os); |
|
192 |
|
193 if (p) |
|
194 os << ", "; |
|
195 } |
|
196 } |
|
197 } |
|
198 |
577
|
199 // Parameter lists. |
|
200 |
|
201 void |
|
202 tree_parameter_list::mark_as_formal_parameters (void) |
|
203 { |
|
204 for (Pix p = first (); p != 0; next (p)) |
|
205 { |
|
206 tree_identifier *elt = this->operator () (p); |
|
207 elt->mark_as_formal_parameter (); |
|
208 } |
|
209 } |
|
210 |
|
211 void |
|
212 tree_parameter_list::define_from_arg_vector (const Octave_object& args) |
|
213 { |
|
214 if (args.length () <= 0) |
|
215 return; |
|
216 |
|
217 int nargin = args.length (); |
|
218 |
|
219 int expected_nargin = length () + 1; |
|
220 |
|
221 Pix p = first (); |
|
222 |
|
223 for (int i = 1; i < expected_nargin; i++) |
|
224 { |
|
225 tree_identifier *elt = this->operator () (p); |
|
226 |
|
227 tree_constant *tmp = 0; |
|
228 |
|
229 if (i < nargin) |
|
230 { |
|
231 if (args(i).is_defined () |
|
232 && (args(i).const_type () == tree_constant_rep::magic_colon)) |
|
233 { |
|
234 ::error ("invalid use of colon in function argument list"); |
|
235 return; |
|
236 } |
|
237 tmp = new tree_constant (args(i)); |
|
238 } |
|
239 |
|
240 elt->define (tmp); |
|
241 |
|
242 next (p); |
|
243 } |
|
244 } |
|
245 |
|
246 Octave_object |
|
247 tree_parameter_list::convert_to_const_vector (void) |
|
248 { |
|
249 int nout = length (); |
|
250 |
|
251 Octave_object retval; |
|
252 retval.resize (nout); |
|
253 |
|
254 int i = 0; |
|
255 |
|
256 for (Pix p = first (); p != 0; next (p)) |
|
257 { |
|
258 tree_identifier *elt = this->operator () (p); |
|
259 |
|
260 if (elt->is_defined ()) |
|
261 retval(i) = elt->eval (0); |
|
262 |
|
263 i++; |
|
264 } |
|
265 |
|
266 return retval; |
|
267 } |
|
268 |
|
269 int |
|
270 tree_parameter_list::is_defined (void) |
|
271 { |
|
272 int status = 1; |
|
273 |
|
274 for (Pix p = first (); p != 0; next (p)) |
|
275 { |
|
276 tree_identifier *elt = this->operator () (p); |
|
277 |
|
278 if (! elt->is_defined ()) |
|
279 { |
|
280 status = 0; |
|
281 break; |
|
282 } |
|
283 } |
|
284 |
|
285 return status; |
|
286 } |
|
287 |
|
288 void |
581
|
289 tree_parameter_list::print_code (ostream& os) |
|
290 { |
|
291 Pix p = first (); |
|
292 |
|
293 while (p) |
|
294 { |
|
295 tree_identifier *elt = this->operator () (p); |
|
296 |
|
297 next (p); |
|
298 |
|
299 if (elt) |
|
300 { |
|
301 elt->print_code (os); |
|
302 |
|
303 if (p) |
|
304 os << ", "; |
|
305 } |
|
306 } |
|
307 } |
|
308 |
|
309 // Return lists. |
|
310 |
|
311 void |
|
312 tree_return_list::print_code (ostream& os) |
|
313 { |
|
314 Pix p = first (); |
|
315 |
|
316 while (p) |
|
317 { |
|
318 tree_index_expression *elt = this->operator () (p); |
|
319 |
|
320 next (p); |
|
321 |
|
322 if (elt) |
|
323 { |
|
324 elt->print_code (os); |
|
325 |
|
326 if (p) |
|
327 os << ", "; |
|
328 } |
|
329 } |
|
330 } |
|
331 |
|
332 // Global. |
|
333 |
|
334 void |
577
|
335 tree_global::eval (void) |
|
336 { |
|
337 if (ident) |
|
338 { |
|
339 ident->link_to_global (); |
|
340 } |
|
341 else if (assign_expr) |
|
342 { |
|
343 tree_identifier *id = assign_expr->left_hand_side (); |
|
344 |
|
345 if (id) |
|
346 id->link_to_global (); |
|
347 |
|
348 assign_expr->eval (0); |
|
349 } |
|
350 } |
|
351 |
|
352 void |
581
|
353 tree_global::print_code (ostream& os) |
|
354 { |
|
355 if (ident) |
|
356 ident->print_code (os); |
|
357 |
|
358 if (assign_expr) |
|
359 assign_expr->print_code (os); |
|
360 } |
|
361 |
|
362 // Global initializer lists. |
|
363 |
|
364 void |
577
|
365 tree_global_init_list::eval (void) |
|
366 { |
|
367 for (Pix p = first (); p != 0; next (p)) |
|
368 { |
|
369 tree_global *t = this->operator () (p); |
|
370 t->eval (); |
|
371 } |
|
372 } |
|
373 |
581
|
374 void |
|
375 tree_global_init_list::print_code (ostream& os) |
|
376 { |
|
377 Pix p = first (); |
|
378 |
|
379 while (p) |
|
380 { |
|
381 tree_global *elt = this->operator () (p); |
|
382 |
|
383 next (p); |
|
384 |
|
385 if (elt) |
|
386 { |
|
387 elt->print_code (os); |
|
388 |
|
389 if (p) |
|
390 os << ", "; |
|
391 } |
|
392 } |
|
393 } |
|
394 |
|
395 // If. |
|
396 |
577
|
397 int |
|
398 tree_if_clause::eval (void) |
|
399 { |
|
400 if (expr) |
|
401 { |
|
402 tree_constant t1 = expr->eval (0); |
|
403 |
|
404 if (error_state || t1.is_undefined ()) |
|
405 return 0; |
|
406 |
|
407 if (t1.rows () == 0 || t1.columns () == 0) |
|
408 { |
|
409 int flag = user_pref.propagate_empty_matrices; |
|
410 if (flag < 0) |
|
411 warning ("if: empty matrix used in conditional"); |
|
412 else if (flag == 0) |
|
413 { |
|
414 ::error ("if: empty matrix used in conditional"); |
|
415 return 0; |
|
416 } |
|
417 t1 = tree_constant (0.0); |
|
418 } |
|
419 else if (! t1.is_scalar_type ()) |
|
420 { |
|
421 tree_constant t2 = t1.all (); |
|
422 t1 = t2.all (); |
|
423 } |
|
424 |
|
425 int expr_value = 0; |
|
426 tree_constant_rep::constant_type t = t1.const_type (); |
|
427 if (t == tree_constant_rep::scalar_constant) |
|
428 expr_value = (int) t1.double_value (); |
|
429 else if (t == tree_constant_rep::complex_scalar_constant) |
|
430 expr_value = t1.complex_value () != 0.0; |
|
431 else |
|
432 panic_impossible (); |
|
433 |
|
434 if (expr_value) |
|
435 { |
|
436 if (list) |
|
437 list->eval (1); |
|
438 |
|
439 return 1; |
|
440 } |
|
441 } |
|
442 else |
|
443 { |
|
444 if (list) |
|
445 list->eval (1); |
|
446 |
|
447 return 1; |
|
448 } |
|
449 |
|
450 return 0; |
|
451 } |
|
452 |
|
453 void |
581
|
454 tree_if_clause::print_code (ostream& os) |
|
455 { |
|
456 if (expr) |
|
457 { |
|
458 expr->print_code (os); |
|
459 |
|
460 print_code_new_line (os); |
|
461 |
|
462 increment_indent_level (); |
|
463 } |
|
464 else |
|
465 { |
|
466 print_code_indent (os); |
|
467 |
|
468 os << "else"; |
|
469 |
|
470 print_code_new_line (os); |
|
471 |
|
472 increment_indent_level (); |
|
473 } |
|
474 |
|
475 if (list) |
|
476 { |
|
477 list->print_code (os); |
|
478 |
|
479 decrement_indent_level (); |
|
480 } |
|
481 } |
|
482 |
|
483 // List of if commands. |
|
484 |
|
485 void |
577
|
486 tree_if_command_list::eval (void) |
|
487 { |
|
488 for (Pix p = first (); p != 0; next (p)) |
|
489 { |
|
490 tree_if_clause *t = this->operator () (p); |
|
491 |
|
492 if (t->eval () || error_state) |
|
493 break; |
|
494 } |
|
495 } |
|
496 |
581
|
497 void |
|
498 tree_if_command_list::print_code (ostream& os) |
|
499 { |
|
500 Pix p = first (); |
|
501 |
|
502 int first_elt = 1; |
|
503 |
|
504 while (p) |
|
505 { |
|
506 tree_if_clause *elt = this->operator () (p); |
|
507 |
|
508 next (p); |
|
509 |
|
510 if (elt) |
|
511 { |
|
512 if (p && ! first_elt) |
|
513 { |
|
514 print_code_indent (os); |
|
515 |
|
516 os << "elseif "; |
|
517 } |
|
518 |
|
519 elt->print_code (os); |
|
520 } |
|
521 |
|
522 first_elt = 0; |
|
523 } |
|
524 } |
|
525 |
577
|
526 /* |
|
527 ;;; Local Variables: *** |
|
528 ;;; mode: C++ *** |
|
529 ;;; page-delimiter: "^/\\*" *** |
|
530 ;;; End: *** |
|
531 */ |