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