494
|
1 // Tree class. -*- 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 |
|
37 #include <iostream.h> |
|
38 #include <strstream.h> |
|
39 #include <limits.h> |
|
40 #include <ctype.h> |
|
41 #include <stdio.h> |
|
42 |
|
43 #include "variables.h" |
|
44 #include "user-prefs.h" |
|
45 #include "error.h" |
|
46 #include "pager.h" |
|
47 #include "tree.h" |
|
48 #include "tree-expr.h" |
|
49 #include "tree-const.h" |
|
50 #include "input.h" |
|
51 #include "symtab.h" |
|
52 #include "utils.h" |
|
53 #include "octave.h" |
|
54 #include "octave-hist.h" |
|
55 #include "unwind-prot.h" |
|
56 #include "parse.h" |
|
57 #include "lex.h" |
|
58 |
|
59 extern "C" |
|
60 { |
|
61 #include <readline/readline.h> |
|
62 } |
|
63 |
|
64 // Nonzero means we're returning from a function. |
|
65 extern int returning; |
|
66 |
|
67 // But first, some extra functions used by the tree classes. |
|
68 |
|
69 // We seem to have no use for this now. Maybe it will be needed at |
|
70 // some future date, so here it is. |
|
71 #if 0 |
|
72 /* |
|
73 * Convert a linked list of trees to a vector of pointers to trees. |
|
74 */ |
|
75 static tree ** |
|
76 list_to_vector (tree *list, int& len) |
|
77 { |
|
78 len = list->length () + 1; |
|
79 |
|
80 tree **args = new tree * [len]; |
|
81 |
|
82 // args[0] may eventually hold something useful, like the function |
|
83 // name. |
|
84 tree *tmp_list = list; |
|
85 for (int k = 1; k < len; k++) |
|
86 { |
|
87 args[k] = tmp_list; |
|
88 tmp_list = tmp_list->next_elem (); |
|
89 } |
|
90 return args; |
|
91 } |
|
92 #endif |
|
93 |
|
94 static int |
|
95 print_as_scalar (const tree_constant& val) |
|
96 { |
|
97 int nr = val.rows (); |
|
98 int nc = val.columns (); |
|
99 return (val.is_scalar_type () |
|
100 || val.is_string_type () |
|
101 || (val.is_matrix_type () |
|
102 && ((nr == 1 && nc == 1) |
|
103 || nr == 0 |
|
104 || nc == 0))); |
|
105 } |
|
106 |
|
107 /* |
|
108 * Make sure that all arguments have values. |
|
109 */ |
|
110 static int |
506
|
111 all_args_defined (const Octave_object& args) |
494
|
112 { |
506
|
113 int nargin = args.length (); |
|
114 |
|
115 while (--nargin > 0) |
494
|
116 { |
506
|
117 if (args(nargin).is_undefined ()) |
494
|
118 return 0; |
|
119 } |
|
120 return 1; |
|
121 } |
|
122 |
|
123 /* |
|
124 * Are any of the arguments `:'? |
|
125 */ |
|
126 static int |
506
|
127 any_arg_is_magic_colon (const Octave_object& args) |
494
|
128 { |
506
|
129 int nargin = args.length (); |
|
130 |
|
131 while (--nargin > 0) |
494
|
132 { |
506
|
133 if (args(nargin).const_type () == tree_constant_rep::magic_colon) |
494
|
134 return 1; |
|
135 } |
|
136 return 0; |
|
137 } |
|
138 |
|
139 // NOTE: functions for the tree_constant_rep and tree_constant classes |
|
140 // are now defined in tree-const.cc. This should help speed up |
|
141 // compilation when working only on the tree_constant class. |
|
142 |
|
143 /* |
|
144 * General matrices. This list type is much more work to handle than |
|
145 * constant matrices, but it allows us to construct matrices from |
|
146 * other matrices, variables, and functions. |
|
147 */ |
|
148 tree_matrix::tree_matrix (void) |
|
149 { |
|
150 dir = tree::md_none; |
|
151 element = (tree_expression *) NULL; |
|
152 next = (tree_matrix *) NULL; |
|
153 } |
|
154 |
|
155 tree_matrix::tree_matrix (tree_expression *e, tree::matrix_dir d) |
|
156 { |
|
157 dir = d; |
|
158 element = e; |
|
159 next = (tree_matrix *) NULL; |
|
160 } |
|
161 |
|
162 tree_matrix::~tree_matrix (void) |
|
163 { |
|
164 delete element; |
|
165 delete next; |
|
166 } |
|
167 |
|
168 tree_matrix * |
|
169 tree_matrix::chain (tree_expression *t, tree::matrix_dir d) |
|
170 { |
|
171 tree_matrix *tmp = new tree_matrix (t, d); |
|
172 tmp->next = this; |
|
173 return tmp; |
|
174 } |
|
175 |
|
176 tree_matrix * |
|
177 tree_matrix::reverse (void) |
|
178 { |
|
179 tree_matrix *list = this; |
|
180 tree_matrix *next; |
|
181 tree_matrix *prev = (tree_matrix *) NULL; |
|
182 |
|
183 while (list != (tree_matrix *) NULL) |
|
184 { |
|
185 next = list->next; |
|
186 list->next = prev; |
|
187 prev = list; |
|
188 list = next; |
|
189 } |
|
190 return prev; |
|
191 } |
|
192 |
|
193 int |
|
194 tree_matrix::length (void) |
|
195 { |
|
196 tree_matrix *list = this; |
|
197 int len = 0; |
|
198 while (list != (tree_matrix *) NULL) |
|
199 { |
|
200 len++; |
|
201 list = list->next; |
|
202 } |
|
203 return len; |
|
204 } |
|
205 |
|
206 tree_return_list * |
|
207 tree_matrix::to_return_list (void) |
|
208 { |
|
209 tree_return_list *retval = (tree_return_list *) NULL; |
|
210 tree_matrix *list; |
|
211 for (list = this; list != (tree_matrix *) NULL; list = list->next) |
|
212 { |
|
213 tree_expression *elem = list->element; |
|
214 if (elem->is_identifier ()) |
|
215 { |
|
216 tree_identifier *id = (tree_identifier *) elem; |
|
217 if (list == this) |
|
218 retval = new tree_return_list (id); |
|
219 else |
|
220 retval = retval->chain (id); |
|
221 } |
|
222 else if (elem->is_index_expression ()) |
|
223 // && (((tree_index_expression *) elem) -> arg_list () |
|
224 // == (tree_argument_list *) NULL)) |
|
225 { |
|
226 tree_index_expression *idx_expr = (tree_index_expression *) elem; |
|
227 if (list == this) |
|
228 retval = new tree_return_list (idx_expr); |
|
229 else |
|
230 retval = retval->chain (idx_expr); |
|
231 } |
|
232 else |
|
233 { |
|
234 delete retval; |
|
235 retval = (tree_return_list *) NULL; |
|
236 break; |
|
237 } |
|
238 } |
|
239 |
|
240 if (retval != (tree_return_list *) NULL) |
|
241 retval = retval->reverse (); |
|
242 return retval; |
|
243 } |
|
244 |
|
245 // Just about as ugly as it gets. |
|
246 |
|
247 struct const_matrix_list |
|
248 { |
|
249 tree::matrix_dir dir; |
|
250 tree_constant elem; |
|
251 int nr; |
|
252 int nc; |
|
253 }; |
|
254 |
|
255 // Less ugly than before, anyway. |
|
256 |
|
257 tree_constant |
|
258 tree_matrix::eval (int print) |
|
259 { |
|
260 tree_constant retval; |
|
261 |
|
262 if (error_state) |
|
263 return retval; |
|
264 |
|
265 // Just count the elements without looking at them. |
|
266 |
|
267 int total_len = length (); |
|
268 |
|
269 // Easier to deal with this later instead of a tree_matrix structure. |
|
270 |
|
271 const_matrix_list *list = new const_matrix_list [total_len]; |
|
272 |
|
273 // Stats we want to keep track of. |
|
274 |
|
275 int all_strings = 1; |
|
276 |
|
277 int found_complex = 0; |
|
278 |
|
279 int row_total = 0; |
|
280 int col_total = 0; |
|
281 |
|
282 int row_height = 0; |
|
283 |
|
284 int cols_this_row = 0; |
|
285 |
|
286 int first_row = 1; |
|
287 |
|
288 int empties_ok = user_pref.empty_list_elements_ok; |
|
289 |
|
290 tree_matrix *ptr = this; |
|
291 |
|
292 // Stuff for the result matrix or string. Declared here so that we |
|
293 // don't get warnings from gcc about the goto crossing the |
|
294 // initialization of these values. |
|
295 |
|
296 int put_row = 0; |
|
297 int put_col = 0; |
|
298 |
|
299 int prev_nr = 0; |
|
300 int prev_nc = 0; |
|
301 |
|
302 Matrix m; |
|
303 ComplexMatrix cm; |
|
304 |
|
305 char *string = (char *) NULL; |
|
306 char *str_ptr = (char *) NULL; |
|
307 |
|
308 // Eliminate empties and gather stats. |
|
309 |
|
310 int found_new_row_in_empties = 0; |
|
311 |
|
312 int len = 0; |
|
313 for (int i = 0; i < total_len; i++) |
|
314 { |
|
315 tree_expression *elem = ptr->element; |
|
316 if (elem == (tree_expression *) NULL) |
|
317 { |
|
318 retval = tree_constant (Matrix ()); |
|
319 goto done; |
|
320 } |
|
321 |
|
322 tree_constant tmp = elem->eval (0); |
|
323 if (error_state || tmp.is_undefined ()) |
|
324 { |
|
325 retval = tree_constant (); |
|
326 goto done; |
|
327 } |
|
328 |
|
329 int nr = tmp.rows (); |
|
330 int nc = tmp.columns (); |
|
331 |
|
332 matrix_dir direct = ptr->dir; |
|
333 |
|
334 if (nr == 0 || nc == 0) |
|
335 { |
|
336 if (empties_ok < 0) |
|
337 warning ("empty matrix found in matrix list"); |
|
338 else if (empties_ok == 0) |
|
339 { |
|
340 ::error ("empty matrix found in matrix list"); |
|
341 retval = tree_constant (); |
|
342 goto done; |
|
343 } |
|
344 |
|
345 if (direct == md_down) |
|
346 found_new_row_in_empties = 1; |
|
347 |
|
348 goto next; |
|
349 } |
|
350 |
|
351 if (found_new_row_in_empties) |
|
352 { |
|
353 found_new_row_in_empties = 0; |
|
354 list[len].dir = md_down; |
|
355 } |
|
356 else |
|
357 list[len].dir = direct; |
|
358 |
|
359 list[len].elem = tmp; |
|
360 list[len].nr = nr; |
|
361 list[len].nc = nc; |
|
362 |
|
363 if (all_strings && ! tmp.is_string_type ()) |
|
364 all_strings = 0; |
|
365 |
|
366 if (! found_complex && tmp.is_complex_type ()) |
|
367 found_complex = 1; |
|
368 |
|
369 len++; |
|
370 |
|
371 next: |
|
372 |
|
373 ptr = ptr->next; |
|
374 } |
|
375 |
|
376 // if (all_strings) |
|
377 // cerr << "all strings\n"; |
|
378 |
|
379 // Compute size of result matrix, and check to see that the dimensions |
|
380 // of all the elements will match up properly. |
|
381 |
|
382 for (i = 0; i < len; i++) |
|
383 { |
|
384 matrix_dir direct = list[i].dir; |
|
385 int nr = list[i].nr; |
|
386 int nc = list[i].nc; |
|
387 |
|
388 if (i == 0) |
|
389 { |
|
390 row_total = nr; |
|
391 col_total = nc; |
|
392 |
|
393 row_height = nr; |
|
394 cols_this_row = nc; |
|
395 } |
|
396 else |
|
397 { |
|
398 switch (direct) |
|
399 { |
|
400 case md_right: |
|
401 { |
|
402 if (nr != row_height) |
|
403 { |
|
404 ::error ("number of rows must match"); |
|
405 goto done; |
|
406 } |
|
407 else |
|
408 { |
|
409 cols_this_row += nc; |
|
410 if (first_row) |
|
411 col_total = cols_this_row; |
|
412 } |
|
413 } |
|
414 break; |
|
415 case md_down: |
|
416 { |
|
417 if (cols_this_row != col_total) |
|
418 { |
|
419 ::error ("number of columns must match"); |
|
420 goto done; |
|
421 } |
|
422 first_row = 0; |
|
423 row_total += nr; |
|
424 row_height = nr; |
|
425 cols_this_row = nc; |
|
426 } |
|
427 break; |
|
428 default: |
|
429 panic_impossible (); |
|
430 break; |
|
431 } |
|
432 } |
|
433 } |
|
434 |
|
435 // Don\'t forget to check to see if the last element will fit. |
|
436 |
|
437 if (cols_this_row != col_total) |
|
438 { |
|
439 ::error ("number of columns must match"); |
|
440 goto done; |
|
441 } |
|
442 |
|
443 // Now, extract the values from the individual elements and insert |
|
444 // them in the result matrix. |
|
445 |
|
446 if (all_strings && row_total == 1 && col_total > 0) |
|
447 { |
|
448 string = str_ptr = new char [col_total + 1]; |
|
449 string[col_total] = '\0'; |
|
450 } |
|
451 else if (found_complex) |
|
452 cm.resize (row_total, col_total, 0.0); |
|
453 else |
|
454 m.resize (row_total, col_total, 0.0); |
|
455 |
|
456 for (i = 0; i < len; i++) |
|
457 { |
|
458 tree_constant tmp = list[i].elem; |
|
459 tree_constant_rep::constant_type tmp_type = tmp.const_type (); |
|
460 |
|
461 int nr = list[i].nr; |
|
462 int nc = list[i].nc; |
|
463 |
|
464 if (nr == 0 || nc == 0) |
|
465 continue; |
|
466 |
|
467 if (i == 0) |
|
468 { |
|
469 put_row = 0; |
|
470 put_col = 0; |
|
471 } |
|
472 else |
|
473 { |
|
474 switch (list[i].dir) |
|
475 { |
|
476 case md_right: |
|
477 put_col += prev_nc; |
|
478 break; |
|
479 case md_down: |
|
480 put_row += prev_nr; |
|
481 put_col = 0; |
|
482 break; |
|
483 default: |
|
484 panic_impossible (); |
|
485 break; |
|
486 } |
|
487 } |
|
488 |
|
489 if (found_complex) |
|
490 { |
|
491 switch (tmp_type) |
|
492 { |
|
493 case tree_constant_rep::scalar_constant: |
|
494 cm (put_row, put_col) = tmp.double_value (); |
|
495 break; |
|
496 case tree_constant_rep::string_constant: |
|
497 if (all_strings && str_ptr != (char *) NULL) |
|
498 { |
|
499 memcpy (str_ptr, tmp.string_value (), nc); |
|
500 str_ptr += nc; |
|
501 break; |
|
502 } |
|
503 case tree_constant_rep::range_constant: |
|
504 tmp_type = tmp.force_numeric (1); |
|
505 if (tmp_type == tree_constant_rep::scalar_constant) |
|
506 m (put_row, put_col) = tmp.double_value (); |
|
507 else if (tmp_type == tree_constant_rep::matrix_constant) |
|
508 m.insert (tmp.matrix_value (), put_row, put_col); |
|
509 else |
|
510 panic_impossible (); |
|
511 break; |
|
512 case tree_constant_rep::matrix_constant: |
|
513 cm.insert (tmp.matrix_value (), put_row, put_col); |
|
514 break; |
|
515 case tree_constant_rep::complex_scalar_constant: |
|
516 cm (put_row, put_col) = tmp.complex_value (); |
|
517 break; |
|
518 case tree_constant_rep::complex_matrix_constant: |
|
519 cm.insert (tmp.complex_matrix_value (), put_row, put_col); |
|
520 break; |
|
521 case tree_constant_rep::magic_colon: |
|
522 default: |
|
523 panic_impossible (); |
|
524 break; |
|
525 } |
|
526 } |
|
527 else |
|
528 { |
|
529 switch (tmp_type) |
|
530 { |
|
531 case tree_constant_rep::scalar_constant: |
|
532 m (put_row, put_col) = tmp.double_value (); |
|
533 break; |
|
534 case tree_constant_rep::string_constant: |
|
535 if (all_strings && str_ptr != (char *) NULL) |
|
536 { |
|
537 memcpy (str_ptr, tmp.string_value (), nc); |
|
538 str_ptr += nc; |
|
539 break; |
|
540 } |
|
541 case tree_constant_rep::range_constant: |
|
542 tmp_type = tmp.force_numeric (1); |
|
543 if (tmp_type == tree_constant_rep::scalar_constant) |
|
544 m (put_row, put_col) = tmp.double_value (); |
|
545 else if (tmp_type == tree_constant_rep::matrix_constant) |
|
546 m.insert (tmp.matrix_value (), put_row, put_col); |
|
547 else |
|
548 panic_impossible (); |
|
549 break; |
|
550 case tree_constant_rep::matrix_constant: |
|
551 m.insert (tmp.matrix_value (), put_row, put_col); |
|
552 break; |
|
553 case tree_constant_rep::complex_scalar_constant: |
|
554 case tree_constant_rep::complex_matrix_constant: |
|
555 case tree_constant_rep::magic_colon: |
|
556 default: |
|
557 panic_impossible (); |
|
558 break; |
|
559 } |
|
560 } |
|
561 |
|
562 prev_nr = nr; |
|
563 prev_nc = nc; |
|
564 } |
|
565 |
|
566 if (all_strings && string != (char *) NULL) |
|
567 retval = tree_constant (string); |
|
568 else if (found_complex) |
|
569 retval = tree_constant (cm); |
|
570 else |
|
571 retval = tree_constant (m); |
|
572 |
|
573 done: |
|
574 delete [] list; |
|
575 |
|
576 return retval; |
|
577 } |
|
578 |
|
579 tree_constant |
506
|
580 tree_fvc::assign (tree_constant& t, const Octave_object& args) |
494
|
581 { |
|
582 panic_impossible (); |
|
583 return tree_constant (); |
|
584 } |
|
585 |
|
586 /* |
|
587 * Builtin functions. |
|
588 */ |
|
589 tree_builtin::tree_builtin (const char *nm = (char *) NULL) |
|
590 { |
|
591 nargin_max = -1; |
|
592 nargout_max = -1; |
|
593 text_fcn = (Text_fcn) NULL; |
|
594 general_fcn = (General_fcn) NULL; |
|
595 if (nm != (char *) NULL) |
|
596 my_name = strsave (nm); |
|
597 } |
|
598 |
|
599 tree_builtin::tree_builtin (int i_max, int o_max, Mapper_fcn& m_fcn, |
|
600 const char *nm = (char *) NULL) |
|
601 { |
|
602 nargin_max = i_max; |
|
603 nargout_max = o_max; |
|
604 mapper_fcn = m_fcn; |
|
605 text_fcn = (Text_fcn) NULL; |
|
606 general_fcn = (General_fcn) NULL; |
|
607 if (nm != (char *) NULL) |
|
608 my_name = strsave (nm); |
|
609 } |
|
610 |
|
611 tree_builtin::tree_builtin (int i_max, int o_max, Text_fcn t_fcn, |
|
612 const char *nm = (char *) NULL) |
|
613 { |
|
614 nargin_max = i_max; |
|
615 nargout_max = o_max; |
|
616 text_fcn = t_fcn; |
|
617 general_fcn = (General_fcn) NULL; |
|
618 if (nm != (char *) NULL) |
|
619 my_name = strsave (nm); |
|
620 } |
|
621 |
|
622 tree_builtin::tree_builtin (int i_max, int o_max, General_fcn g_fcn, |
|
623 const char *nm = (char *) NULL) |
|
624 { |
|
625 nargin_max = i_max; |
|
626 nargout_max = o_max; |
|
627 text_fcn = (Text_fcn) NULL; |
|
628 general_fcn = g_fcn; |
|
629 if (nm != (char *) NULL) |
|
630 my_name = strsave (nm); |
|
631 } |
|
632 |
|
633 tree_builtin::~tree_builtin (void) |
|
634 { |
|
635 } |
|
636 |
|
637 #if 0 |
|
638 int |
|
639 tree_builtin::is_builtin (void) const |
|
640 { |
|
641 return 1; |
|
642 } |
|
643 #endif |
|
644 |
|
645 tree_constant |
|
646 tree_builtin::eval (int print) |
|
647 { |
|
648 tree_constant retval; |
|
649 |
|
650 if (error_state) |
|
651 return retval; |
|
652 |
|
653 if (text_fcn != (Text_fcn) NULL) |
|
654 { |
|
655 char **argv = new char * [1]; |
|
656 argv[0] = strsave (my_name); |
500
|
657 Octave_object tmp = (*text_fcn) (1, argv, 1); |
|
658 if (tmp.length () > 0) |
|
659 retval = tmp(0); |
494
|
660 delete [] argv; |
|
661 } |
|
662 else if (general_fcn != (General_fcn) NULL) |
|
663 { |
500
|
664 Octave_object args (1); |
|
665 args(0) = tree_constant (my_name); |
506
|
666 Octave_object tmp = (*general_fcn) (args, 1); |
500
|
667 if (tmp.length () > 0) |
|
668 retval = tmp(0); |
494
|
669 } |
|
670 else // Assume mapper function |
|
671 ::error ("%s: argument expected", my_name); |
|
672 |
|
673 return retval; |
|
674 } |
|
675 |
500
|
676 Octave_object |
506
|
677 tree_builtin::eval (int print, int nargout, const Octave_object& args) |
494
|
678 { |
500
|
679 Octave_object retval; |
494
|
680 |
|
681 if (error_state) |
|
682 return retval; |
|
683 |
506
|
684 int nargin = args.length (); |
|
685 |
494
|
686 if (text_fcn != (Text_fcn) NULL) |
|
687 { |
|
688 // XXX FIXME XXX -- what if some arg is not a string? |
|
689 |
|
690 int argc = nargin; |
|
691 char **argv = new char * [argc + 1]; |
|
692 argv[0] = strsave (my_name); |
|
693 for (int i = 1; i < argc; i++) |
500
|
694 argv[i] = strsave (args(i).string_value ()); |
494
|
695 argv[argc] = (char *) NULL; |
|
696 |
|
697 retval = (*text_fcn) (argc, argv, nargout); |
|
698 |
|
699 for (i = 0; i < argc; i++) |
|
700 delete [] argv[i]; |
|
701 delete [] argv; |
|
702 } |
|
703 else if (general_fcn != (General_fcn) NULL) |
|
704 { |
506
|
705 if (any_arg_is_magic_colon (args)) |
494
|
706 ::error ("invalid use of colon in function argument list"); |
|
707 else |
506
|
708 retval = (*general_fcn) (args, nargout); |
494
|
709 } |
|
710 else |
|
711 { |
|
712 if (nargin > nargin_max) |
|
713 ::error ("%s: too many arguments", my_name); |
500
|
714 else if (nargin > 0 && args.length () > 0 && args(1).is_defined ()) |
494
|
715 { |
500
|
716 tree_constant tmp = args(1).mapper (mapper_fcn, 0); |
|
717 retval.resize (1); |
|
718 retval(0) = tmp; |
494
|
719 } |
|
720 } |
|
721 |
|
722 return retval; |
|
723 } |
|
724 |
|
725 char * |
|
726 tree_builtin::name (void) const |
|
727 { |
|
728 return my_name; |
|
729 } |
|
730 |
|
731 int |
|
732 tree_builtin::max_expected_args (void) |
|
733 { |
|
734 int ea = nargin_max; |
|
735 if (nargin_max < 0) |
|
736 ea = INT_MAX; |
|
737 else |
|
738 ea = nargin_max; |
|
739 return ea; |
|
740 } |
|
741 |
|
742 /* |
|
743 * Symbols from the symbol table. |
|
744 */ |
|
745 tree_identifier::tree_identifier (int l = -1, int c = -1) |
|
746 { |
|
747 sym = (symbol_record *) NULL; |
|
748 line_num = l; |
|
749 column_num = c; |
|
750 maybe_do_ans_assign = 0; |
|
751 } |
|
752 |
|
753 tree_identifier::tree_identifier (symbol_record *s, int l = -1, int c = -1) |
|
754 { |
|
755 sym = s; |
|
756 line_num = l; |
|
757 column_num = c; |
|
758 maybe_do_ans_assign = 0; |
|
759 } |
|
760 |
|
761 tree_identifier::~tree_identifier (void) |
|
762 { |
|
763 } |
|
764 |
|
765 int |
|
766 tree_identifier::is_identifier (void) const |
|
767 { |
|
768 return 1; |
|
769 } |
|
770 |
|
771 char * |
|
772 tree_identifier::name (void) const |
|
773 { |
|
774 return sym->name (); |
|
775 } |
|
776 |
|
777 void |
|
778 tree_identifier::rename (const char *n) |
|
779 { |
|
780 sym->rename (n); |
|
781 } |
|
782 |
|
783 tree_identifier * |
|
784 tree_identifier::define (tree_constant *t) |
|
785 { |
|
786 int status = sym->define (t); |
|
787 if (status) |
|
788 return this; |
|
789 else |
|
790 return (tree_identifier *) NULL; |
|
791 } |
|
792 |
|
793 tree_identifier * |
|
794 tree_identifier::define (tree_function *t) |
|
795 { |
|
796 int status = sym->define (t); |
|
797 if (status) |
|
798 return this; |
|
799 else |
|
800 return (tree_identifier *) NULL; |
|
801 } |
|
802 |
|
803 void |
|
804 tree_identifier::document (char *s) |
|
805 { |
|
806 if (sym != (symbol_record *) NULL && s != (char *) NULL) |
|
807 { |
|
808 char *tmp = strsave (s); |
|
809 sym->document (tmp); |
|
810 } |
|
811 } |
|
812 |
|
813 tree_constant |
|
814 tree_identifier::assign (tree_constant& rhs) |
|
815 { |
|
816 int status = 0; |
|
817 |
|
818 if (rhs.is_defined ()) |
|
819 { |
|
820 if (! sym->is_defined ()) |
|
821 { |
|
822 if (! (sym->is_formal_parameter () |
|
823 || sym->is_linked_to_global ())) |
|
824 { |
|
825 link_to_builtin_variable (sym); |
|
826 } |
|
827 } |
|
828 else if (sym->is_function ()) |
|
829 { |
|
830 sym->clear (); |
|
831 } |
|
832 |
|
833 tree_constant *tmp = new tree_constant (rhs); |
|
834 status = sym->define (tmp); |
|
835 } |
|
836 |
|
837 if (status) |
|
838 return rhs; |
|
839 else |
|
840 return tree_constant (); |
|
841 } |
|
842 |
|
843 tree_constant |
506
|
844 tree_identifier::assign (tree_constant& rhs, const Octave_object& args) |
494
|
845 { |
|
846 tree_constant retval; |
|
847 |
|
848 if (rhs.is_defined ()) |
|
849 { |
|
850 if (! sym->is_defined ()) |
|
851 { |
|
852 if (! (sym->is_formal_parameter () |
|
853 || sym->is_linked_to_global ())) |
|
854 { |
|
855 link_to_builtin_variable (sym); |
|
856 } |
|
857 } |
|
858 else if (sym->is_function ()) |
|
859 { |
|
860 sym->clear (); |
|
861 } |
|
862 |
|
863 if (sym->is_variable () && sym->is_defined ()) |
|
864 { |
|
865 tree_fvc *tmp = sym->def (); |
506
|
866 retval = tmp->assign (rhs, args); |
494
|
867 } |
|
868 else |
|
869 { |
|
870 assert (! sym->is_defined ()); |
|
871 |
|
872 if (! user_pref.resize_on_range_error) |
|
873 { |
|
874 ::error ("indexed assignment to previously undefined variables"); |
|
875 ::error ("is only possible when resize_on_range_error is true"); |
|
876 return retval; |
|
877 } |
|
878 |
|
879 tree_constant *tmp = new tree_constant (); |
506
|
880 retval = tmp->assign (rhs, args); |
494
|
881 if (retval.is_defined ()) |
|
882 sym->define (tmp); |
|
883 } |
|
884 } |
|
885 |
|
886 return retval; |
|
887 } |
|
888 |
500
|
889 int |
|
890 tree_identifier::is_defined (void) |
|
891 { |
|
892 return (sym != (symbol_record *) NULL && sym->is_defined ()); |
|
893 } |
|
894 |
494
|
895 void |
|
896 tree_identifier::bump_value (tree::expression_type etype) |
|
897 { |
|
898 if (sym != (symbol_record *) NULL) |
|
899 { |
|
900 tree_fvc *tmp = sym->def (); |
|
901 if (tmp != NULL_TREE) |
|
902 tmp->bump_value (etype); |
|
903 } |
|
904 } |
|
905 |
|
906 int |
|
907 tree_identifier::parse_fcn_file (int exec_script = 1) |
|
908 { |
|
909 curr_fcn_file_name = name (); |
|
910 char *ff = fcn_file_in_path (curr_fcn_file_name); |
|
911 int script_file_executed = parse_fcn_file (ff, exec_script); |
|
912 delete [] ff; |
|
913 |
|
914 if (! (error_state || script_file_executed)) |
|
915 force_link_to_function (name ()); |
|
916 |
|
917 return script_file_executed; |
|
918 } |
|
919 |
|
920 static void |
|
921 gobble_leading_white_space (FILE *ffile) |
|
922 { |
|
923 int in_comment = 0; |
|
924 int c; |
|
925 while ((c = getc (ffile)) != EOF) |
|
926 { |
|
927 if (in_comment) |
|
928 { |
|
929 if (c == '\n') |
|
930 in_comment = 0; |
|
931 } |
|
932 else |
|
933 { |
|
934 if (c == ' ' || c == '\t' || c == '\n') |
|
935 continue; |
|
936 else if (c == '%' || c == '#') |
|
937 in_comment = 1; |
|
938 else |
|
939 { |
|
940 ungetc (c, ffile); |
|
941 break; |
|
942 } |
|
943 } |
|
944 } |
|
945 } |
|
946 |
|
947 static int |
|
948 is_function_file (FILE *ffile) |
|
949 { |
|
950 int status = 0; |
|
951 |
|
952 gobble_leading_white_space (ffile); |
|
953 |
|
954 long pos = ftell (ffile); |
|
955 |
|
956 char buf [10]; |
|
957 fgets (buf, 10, ffile); |
|
958 int len = strlen (buf); |
|
959 if (len > 8 && strncmp (buf, "function", 8) == 0 |
|
960 && ! (isalnum (buf[8]) || buf[8] == '_')) |
|
961 status = 1; |
|
962 |
|
963 fseek (ffile, pos, SEEK_SET); |
|
964 |
|
965 return status; |
|
966 } |
|
967 |
|
968 int |
|
969 tree_identifier::parse_fcn_file (char *ff, int exec_script = 1) |
|
970 { |
|
971 begin_unwind_frame ("parse_fcn_file"); |
|
972 |
|
973 int script_file_executed = 0; |
|
974 |
|
975 if (ff != (char *) NULL) |
|
976 { |
|
977 // Open function file and parse. |
|
978 |
|
979 int old_reading_fcn_file_state = reading_fcn_file; |
|
980 |
|
981 unwind_protect_ptr (rl_instream); |
|
982 unwind_protect_ptr (ff_instream); |
|
983 |
|
984 unwind_protect_int (using_readline); |
|
985 unwind_protect_int (input_line_number); |
|
986 unwind_protect_int (current_input_column); |
|
987 unwind_protect_int (reading_fcn_file); |
|
988 |
|
989 using_readline = 0; |
|
990 reading_fcn_file = 1; |
|
991 input_line_number = 0; |
|
992 current_input_column = 1; |
|
993 |
|
994 FILE *ffile = get_input_from_file (ff, 0); |
|
995 |
|
996 if (ffile != (FILE *) NULL) |
|
997 { |
|
998 // Check to see if this file defines a function or is just a list of |
|
999 // commands. |
|
1000 |
|
1001 if (is_function_file (ffile)) |
|
1002 { |
|
1003 parse_fcn_file (ffile, ff); |
|
1004 } |
|
1005 else if (exec_script) |
|
1006 { |
|
1007 // The value of `reading_fcn_file' will be restored to the proper value |
|
1008 // when we unwind from this frame. |
|
1009 reading_fcn_file = old_reading_fcn_file_state; |
|
1010 |
|
1011 unwind_protect_int (reading_script_file); |
|
1012 reading_script_file = 1; |
|
1013 |
|
1014 parse_and_execute (ffile, 1); |
|
1015 |
|
1016 script_file_executed = 1; |
|
1017 } |
|
1018 fclose (ffile); |
|
1019 } |
|
1020 |
|
1021 run_unwind_frame ("parse_fcn_file"); |
|
1022 } |
|
1023 |
|
1024 return script_file_executed; |
|
1025 } |
|
1026 |
|
1027 void |
|
1028 tree_identifier::parse_fcn_file (FILE *ffile, char *ff) |
|
1029 { |
|
1030 begin_unwind_frame ("parse_fcn_file_2"); |
|
1031 |
|
1032 unwind_protect_int (echo_input); |
|
1033 unwind_protect_int (saving_history); |
|
1034 unwind_protect_int (reading_fcn_file); |
|
1035 |
|
1036 echo_input = 0; |
|
1037 saving_history = 0; |
|
1038 reading_fcn_file = 1; |
|
1039 |
|
1040 YY_BUFFER_STATE old_buf = current_buffer (); |
|
1041 YY_BUFFER_STATE new_buf = create_buffer (ffile); |
|
1042 |
|
1043 add_unwind_protect (restore_input_buffer, (void *) old_buf); |
|
1044 add_unwind_protect (delete_input_buffer, (void *) new_buf); |
|
1045 |
|
1046 switch_to_buffer (new_buf); |
|
1047 |
|
1048 unwind_protect_ptr (curr_sym_tab); |
|
1049 |
|
1050 reset_parser (); |
|
1051 |
|
1052 int status = yyparse (); |
|
1053 |
|
1054 if (status != 0) |
|
1055 { |
|
1056 ::error ("parse error while reading function file %s", ff); |
|
1057 global_sym_tab->clear (curr_fcn_file_name); |
|
1058 } |
|
1059 |
|
1060 run_unwind_frame ("parse_fcn_file_2"); |
|
1061 } |
|
1062 |
|
1063 void |
|
1064 tree_identifier::eval_undefined_error (void) |
|
1065 { |
|
1066 char *nm = sym->name (); |
|
1067 int l = line (); |
|
1068 int c = column (); |
|
1069 if (l == -1 && c == -1) |
|
1070 ::error ("`%s' undefined"); |
|
1071 else |
|
1072 ::error ("`%s' undefined near line %d column %d", nm, l, c); |
|
1073 } |
|
1074 |
|
1075 /* |
|
1076 * Try to find a definition for an identifier. Here's how: |
|
1077 * |
|
1078 * * If the identifier is already defined and is a function defined |
|
1079 * in an function file that has been modified since the last time |
|
1080 * we parsed it, parse it again. |
|
1081 * |
|
1082 * * If the identifier is not defined, try to find a builtin |
|
1083 * variable or an already compiled function with the same name. |
|
1084 * |
|
1085 * * If the identifier is still undefined, try looking for an |
|
1086 * function file to parse. |
|
1087 */ |
|
1088 tree_fvc * |
|
1089 tree_identifier::do_lookup (int& script_file_executed) |
|
1090 { |
|
1091 script_file_executed = 0; |
|
1092 |
|
1093 if (! sym->is_linked_to_global ()) |
|
1094 { |
|
1095 if (sym->is_defined ()) |
|
1096 { |
|
1097 if (sym->is_function () && symbol_out_of_date (sym)) |
|
1098 { |
|
1099 script_file_executed = parse_fcn_file (); |
|
1100 } |
|
1101 } |
|
1102 else if (! sym->is_formal_parameter ()) |
|
1103 { |
|
1104 link_to_builtin_or_function (sym); |
|
1105 |
|
1106 if (! sym->is_defined ()) |
|
1107 { |
|
1108 script_file_executed = parse_fcn_file (); |
|
1109 } |
|
1110 else if (sym->is_function () && symbol_out_of_date (sym)) |
|
1111 { |
|
1112 script_file_executed = parse_fcn_file (); |
|
1113 } |
|
1114 } |
|
1115 } |
|
1116 |
|
1117 tree_fvc *ans = (tree_fvc *) NULL; |
|
1118 |
|
1119 if (! script_file_executed) |
|
1120 ans = sym->def (); |
|
1121 |
|
1122 return ans; |
|
1123 } |
|
1124 |
|
1125 void |
|
1126 tree_identifier::mark_as_formal_parameter (void) |
|
1127 { |
|
1128 if (sym != (symbol_record *) NULL) |
|
1129 sym->mark_as_formal_parameter (); |
|
1130 } |
|
1131 |
|
1132 void |
|
1133 tree_identifier::mark_for_possible_ans_assign (void) |
|
1134 { |
|
1135 maybe_do_ans_assign = 1; |
|
1136 } |
|
1137 |
|
1138 tree_constant |
|
1139 tree_identifier::eval (int print) |
|
1140 { |
|
1141 tree_constant retval; |
|
1142 |
|
1143 if (error_state) |
|
1144 return retval; |
|
1145 |
|
1146 int script_file_executed = 0; |
|
1147 |
|
1148 tree_fvc *ans = do_lookup (script_file_executed); |
|
1149 |
|
1150 if (! script_file_executed) |
|
1151 { |
|
1152 if (ans == (tree_fvc *) NULL) |
|
1153 eval_undefined_error (); |
|
1154 else |
|
1155 { |
|
1156 int nargout = maybe_do_ans_assign ? 0 : 1; |
|
1157 |
504
|
1158 // int nargin = (ans->is_constant ()) ? 0 : 1; |
500
|
1159 Octave_object tmp_args; |
506
|
1160 Octave_object tmp = ans->eval (0, nargout, tmp_args); |
500
|
1161 |
|
1162 if (tmp.length () > 0) |
|
1163 retval = tmp(0); |
494
|
1164 } |
|
1165 } |
|
1166 |
|
1167 if (! error_state && retval.is_defined ()) |
|
1168 { |
|
1169 if (maybe_do_ans_assign && ! ans->is_constant ()) |
|
1170 { |
|
1171 symbol_record *sr = global_sym_tab->lookup ("ans", 1, 0); |
|
1172 |
|
1173 assert (sr != (symbol_record *) NULL); |
|
1174 |
|
1175 tree_identifier *ans_id = new tree_identifier (sr); |
|
1176 |
|
1177 tree_constant *tmp = new tree_constant (retval); |
|
1178 |
|
1179 tree_simple_assignment_expression tmp_ass (ans_id, tmp); |
|
1180 |
|
1181 tmp_ass.eval (print); |
|
1182 |
|
1183 delete ans_id; // XXX FIXME XXX |
|
1184 } |
|
1185 else |
|
1186 { |
|
1187 if (print) |
|
1188 { |
|
1189 int pad_after = 0; |
|
1190 if (user_pref.print_answer_id_name) |
|
1191 { |
|
1192 char *result_tag = name (); |
|
1193 |
|
1194 if (print_as_scalar (retval)) |
|
1195 { |
|
1196 ostrstream output_buf; |
|
1197 output_buf << result_tag << " = " << ends; |
|
1198 maybe_page_output (output_buf); |
|
1199 } |
|
1200 else |
|
1201 { |
|
1202 pad_after = 1; |
|
1203 ostrstream output_buf; |
|
1204 output_buf << result_tag << " =\n\n" << ends; |
|
1205 maybe_page_output (output_buf); |
|
1206 } |
|
1207 } |
|
1208 |
|
1209 retval.eval (print); |
|
1210 |
|
1211 if (pad_after) |
|
1212 { |
|
1213 ostrstream output_buf; |
|
1214 output_buf << "\n" << ends; |
|
1215 maybe_page_output (output_buf); |
|
1216 } |
|
1217 } |
|
1218 } |
|
1219 } |
|
1220 return retval; |
|
1221 } |
|
1222 |
500
|
1223 Octave_object |
506
|
1224 tree_identifier::eval (int print, int nargout, const Octave_object& args) |
494
|
1225 { |
500
|
1226 Octave_object retval; |
494
|
1227 |
|
1228 if (error_state) |
|
1229 return retval; |
|
1230 |
|
1231 int script_file_executed = 0; |
|
1232 |
|
1233 tree_fvc *ans = do_lookup (script_file_executed); |
|
1234 |
|
1235 if (! script_file_executed) |
|
1236 { |
|
1237 if (ans == (tree_fvc *) NULL) |
|
1238 eval_undefined_error (); |
|
1239 else |
|
1240 { |
|
1241 if (maybe_do_ans_assign && nargout == 1) |
|
1242 { |
|
1243 |
|
1244 // Don't count the output arguments that we create automatically. |
|
1245 |
|
1246 nargout = 0; |
|
1247 |
506
|
1248 retval = ans->eval (0, nargout, args); |
494
|
1249 |
500
|
1250 if (retval.length () > 0 && retval(0).is_defined ()) |
494
|
1251 { |
|
1252 symbol_record *sr = global_sym_tab->lookup ("ans", 1, 0); |
|
1253 |
|
1254 assert (sr != (symbol_record *) NULL); |
|
1255 |
|
1256 tree_identifier *ans_id = new tree_identifier (sr); |
|
1257 |
500
|
1258 tree_constant *tmp = new tree_constant (retval(0)); |
494
|
1259 |
|
1260 tree_simple_assignment_expression tmp_ass (ans_id, tmp); |
|
1261 |
|
1262 tmp_ass.eval (print); |
|
1263 |
|
1264 delete ans_id; // XXX FIXME XXX |
|
1265 } |
|
1266 } |
|
1267 else |
506
|
1268 retval = ans->eval (print, nargout, args); |
494
|
1269 } |
|
1270 } |
|
1271 |
|
1272 return retval; |
|
1273 } |
|
1274 |
|
1275 /* |
|
1276 * User defined functions. |
|
1277 */ |
|
1278 tree_function::tree_function (void) |
|
1279 { |
|
1280 call_depth = 0; |
|
1281 param_list = (tree_parameter_list *) NULL; |
|
1282 ret_list = (tree_parameter_list *) NULL; |
|
1283 sym_tab = (symbol_table *) NULL; |
|
1284 cmd_list = NULL_TREE; |
|
1285 file_name = (char *) NULL; |
|
1286 fcn_name = (char *) NULL; |
|
1287 t_parsed = 0; |
|
1288 system_fcn_file = 0; |
|
1289 varargs_ok = 0; |
|
1290 num_named_args = 0; |
|
1291 num_args_passed = 0; |
|
1292 curr_arg_number = 0; |
|
1293 } |
|
1294 |
|
1295 tree_function::tree_function (tree *cl, symbol_table *st) |
|
1296 { |
|
1297 call_depth = 0; |
|
1298 param_list = (tree_parameter_list *) NULL; |
|
1299 ret_list = (tree_parameter_list *) NULL; |
|
1300 sym_tab = st; |
|
1301 cmd_list = cl; |
|
1302 file_name = (char *) NULL; |
|
1303 fcn_name = (char *) NULL; |
|
1304 t_parsed = 0; |
|
1305 system_fcn_file = 0; |
|
1306 varargs_ok = 0; |
|
1307 num_named_args = 0; |
|
1308 num_args_passed = 0; |
|
1309 curr_arg_number = 0; |
|
1310 } |
|
1311 |
|
1312 tree_function::~tree_function (void) |
|
1313 { |
|
1314 } |
|
1315 |
|
1316 tree_function * |
|
1317 tree_function::define (tree *t) |
|
1318 { |
|
1319 cmd_list = t; |
|
1320 return this; |
|
1321 } |
|
1322 |
|
1323 tree_function * |
|
1324 tree_function::define_param_list (tree_parameter_list *t) |
|
1325 { |
|
1326 param_list = t; |
|
1327 varargs_ok = (param_list != (tree_parameter_list *) NULL |
|
1328 && param_list->takes_varargs ()); |
|
1329 |
|
1330 if (varargs_ok) |
|
1331 num_named_args = param_list->length (); |
|
1332 |
|
1333 return this; |
|
1334 } |
|
1335 |
|
1336 tree_function * |
|
1337 tree_function::define_ret_list (tree_parameter_list *t) |
|
1338 { |
|
1339 ret_list = t; |
|
1340 return this; |
|
1341 } |
|
1342 |
|
1343 void |
|
1344 tree_function::stash_fcn_file_name (char *s) |
|
1345 { |
|
1346 delete [] file_name; |
|
1347 file_name = strsave (s); |
|
1348 } |
|
1349 |
|
1350 void |
|
1351 tree_function::stash_fcn_file_time (time_t t) |
|
1352 { |
|
1353 t_parsed = t; |
|
1354 } |
|
1355 |
|
1356 char * |
|
1357 tree_function::fcn_file_name (void) |
|
1358 { |
|
1359 return file_name; |
|
1360 } |
|
1361 |
|
1362 time_t |
|
1363 tree_function::time_parsed (void) |
|
1364 { |
|
1365 return t_parsed; |
|
1366 } |
|
1367 |
|
1368 void |
|
1369 tree_function::mark_as_system_fcn_file (void) |
|
1370 { |
|
1371 if (file_name != (char *) NULL) |
|
1372 { |
|
1373 // We really should stash the whole path to the file we found, when we |
|
1374 // looked it up, to avoid possible race conditions... XXX FIXME XXX |
|
1375 // |
|
1376 // We probably also don't need to get the library directory every |
|
1377 // time, but since this function is only called when the function file |
|
1378 // is parsed, it probably doesn't matter that much. |
|
1379 |
|
1380 char *oct_lib = octave_lib_dir (); |
|
1381 int len = strlen (oct_lib); |
|
1382 |
|
1383 char *ff_name = fcn_file_in_path (file_name); |
|
1384 |
|
1385 if (strncmp (oct_lib, ff_name, len) == 0) |
|
1386 system_fcn_file = 1; |
|
1387 |
|
1388 delete [] ff_name; |
|
1389 } |
|
1390 else |
|
1391 system_fcn_file = 0; |
|
1392 } |
|
1393 |
|
1394 int |
|
1395 tree_function::is_system_fcn_file (void) const |
|
1396 { |
|
1397 return system_fcn_file; |
|
1398 } |
|
1399 |
|
1400 int |
|
1401 tree_function::takes_varargs (void) const |
|
1402 { |
|
1403 return varargs_ok; |
|
1404 } |
|
1405 |
|
1406 void |
|
1407 tree_function::octave_va_start (void) |
|
1408 { |
|
1409 curr_arg_number = num_named_args + 1; |
|
1410 } |
|
1411 |
|
1412 tree_constant |
|
1413 tree_function::octave_va_arg (void) |
|
1414 { |
|
1415 tree_constant retval; |
|
1416 |
|
1417 if (curr_arg_number < num_args_passed) |
|
1418 { |
502
|
1419 retval = args_passed (curr_arg_number); |
494
|
1420 curr_arg_number++; |
|
1421 } |
|
1422 else |
|
1423 ::error ("error getting arg number %d -- only %d provided", |
|
1424 curr_arg_number, num_args_passed-1); |
|
1425 |
|
1426 return retval; |
|
1427 } |
|
1428 |
|
1429 void |
|
1430 tree_function::stash_function_name (char *s) |
|
1431 { |
|
1432 delete [] fcn_name; |
|
1433 fcn_name = strsave (s); |
|
1434 } |
|
1435 |
|
1436 char * |
|
1437 tree_function::function_name (void) |
|
1438 { |
|
1439 return fcn_name; |
|
1440 } |
|
1441 |
|
1442 tree_constant |
|
1443 tree_function::eval (int print) |
|
1444 { |
|
1445 tree_constant retval; |
|
1446 |
|
1447 if (error_state || cmd_list == NULL_TREE) |
|
1448 return retval; |
|
1449 |
500
|
1450 Octave_object tmp_args; |
506
|
1451 Octave_object tmp = eval (print, 1, tmp_args); |
500
|
1452 |
|
1453 if (! error_state && tmp.length () > 0) |
|
1454 retval = tmp(0); |
494
|
1455 |
|
1456 return retval; |
|
1457 } |
|
1458 |
|
1459 // For unwind protect. |
|
1460 |
|
1461 static void |
|
1462 pop_symbol_table_context (void *table) |
|
1463 { |
|
1464 symbol_table *tmp = (symbol_table *) table; |
|
1465 tmp->pop_context (); |
|
1466 } |
|
1467 |
|
1468 static void |
|
1469 clear_symbol_table (void *table) |
|
1470 { |
|
1471 symbol_table *tmp = (symbol_table *) table; |
|
1472 tmp->clear (); |
|
1473 } |
|
1474 |
500
|
1475 Octave_object |
506
|
1476 tree_function::eval (int print, int nargout, const Octave_object& args) |
494
|
1477 { |
500
|
1478 Octave_object retval; |
494
|
1479 |
|
1480 if (error_state) |
|
1481 return retval; |
|
1482 |
|
1483 if (cmd_list == NULL_TREE) |
|
1484 return retval; |
|
1485 |
506
|
1486 int nargin = args.length (); |
|
1487 |
494
|
1488 begin_unwind_frame ("func_eval"); |
|
1489 |
|
1490 unwind_protect_int (call_depth); |
|
1491 call_depth++; |
|
1492 |
|
1493 if (call_depth > 1) |
|
1494 { |
|
1495 sym_tab->push_context (); |
|
1496 add_unwind_protect (pop_symbol_table_context, (void *) sym_tab); |
|
1497 } |
|
1498 |
|
1499 // Force symbols to be undefined again when this function exits. |
|
1500 |
|
1501 add_unwind_protect (clear_symbol_table, (void *) sym_tab); |
|
1502 |
|
1503 // Save old and set current symbol table context, for eval_undefined_error(). |
|
1504 |
|
1505 unwind_protect_ptr (curr_sym_tab); |
|
1506 curr_sym_tab = sym_tab; |
|
1507 |
|
1508 unwind_protect_ptr (curr_function); |
|
1509 curr_function = this; |
|
1510 |
500
|
1511 // unwind_protect_ptr (args_passed); |
494
|
1512 args_passed = args; |
|
1513 |
|
1514 unwind_protect_int (num_args_passed); |
|
1515 num_args_passed = nargin; |
|
1516 |
|
1517 unwind_protect_int (num_named_args); |
|
1518 unwind_protect_int (curr_arg_number); |
|
1519 |
504
|
1520 if (param_list != (tree_parameter_list *) NULL |
|
1521 && ! param_list->varargs_only ()) |
494
|
1522 { |
506
|
1523 param_list->define_from_arg_vector (args); |
494
|
1524 if (error_state) |
|
1525 goto abort; |
|
1526 } |
|
1527 |
|
1528 // The following code is in a separate scope to avoid warnings from |
|
1529 // G++ about `goto abort' crossing the initialization of some |
|
1530 // variables. |
|
1531 |
|
1532 { |
|
1533 bind_nargin_and_nargout (sym_tab, nargin, nargout); |
|
1534 |
|
1535 // Evaluate the commands that make up the function. Always turn on |
|
1536 // printing for commands inside functions. Maybe this should be |
|
1537 // toggled by a user-leval variable? |
|
1538 |
|
1539 int pf = ! user_pref.silent_functions; |
|
1540 tree_constant last_computed_value = cmd_list->eval (pf); |
|
1541 |
|
1542 if (returning) |
|
1543 returning = 0; |
|
1544 |
|
1545 if (error_state) |
|
1546 { |
|
1547 traceback_error (); |
|
1548 goto abort; |
|
1549 } |
|
1550 |
|
1551 // Copy return values out. |
|
1552 |
|
1553 if (ret_list != (tree_parameter_list *) NULL) |
|
1554 { |
|
1555 retval = ret_list->convert_to_const_vector (); |
|
1556 } |
|
1557 else if (user_pref.return_last_computed_value) |
|
1558 { |
500
|
1559 retval.resize (1); |
|
1560 retval(0) = last_computed_value; |
494
|
1561 } |
|
1562 } |
|
1563 |
|
1564 abort: |
|
1565 run_unwind_frame ("func_eval"); |
|
1566 |
|
1567 return retval; |
|
1568 } |
|
1569 |
|
1570 int |
|
1571 tree_function::max_expected_args (void) |
|
1572 { |
|
1573 if (param_list != NULL_TREE) |
|
1574 { |
|
1575 if (param_list->takes_varargs ()) |
|
1576 return -1; |
|
1577 else |
|
1578 return param_list->length () + 1; |
|
1579 } |
|
1580 else |
|
1581 return 1; |
|
1582 } |
|
1583 |
|
1584 void |
|
1585 tree_function::traceback_error (void) |
|
1586 { |
|
1587 if (error_state >= 0) |
|
1588 error_state = -1; |
|
1589 |
|
1590 if (fcn_name != (char *) NULL) |
|
1591 { |
|
1592 if (file_name != (char *) NULL) |
|
1593 ::error ("called from `%s' in file `%s'", fcn_name, file_name); |
|
1594 else |
|
1595 ::error ("called from `%s'", fcn_name); |
|
1596 } |
|
1597 else |
|
1598 { |
|
1599 if (file_name != (char *) NULL) |
|
1600 ::error ("called from file `%s'", file_name); |
|
1601 else |
|
1602 ::error ("called from `?unknown?'"); |
|
1603 } |
|
1604 } |
|
1605 |
|
1606 /* |
|
1607 * Expressions. |
|
1608 */ |
|
1609 tree_expression::tree_expression (void) |
|
1610 { |
|
1611 etype = tree::unknown; |
|
1612 } |
|
1613 |
|
1614 tree_expression::~tree_expression (void) |
|
1615 { |
|
1616 } |
|
1617 |
|
1618 tree_constant |
|
1619 tree_expression::eval (int print) |
|
1620 { |
|
1621 panic ("invalid evaluation of generic expression"); |
|
1622 return tree_constant (); |
|
1623 } |
|
1624 |
500
|
1625 Octave_object |
506
|
1626 tree_expression::eval (int print, int nargout, const Octave_object& args) |
500
|
1627 { |
|
1628 panic_impossible (); |
|
1629 return Octave_object (); |
|
1630 } |
|
1631 |
494
|
1632 /* |
|
1633 * Prefix expressions. |
|
1634 */ |
|
1635 tree_prefix_expression::tree_prefix_expression (int l = -1, int c = -1) |
|
1636 { |
|
1637 id = (tree_identifier *) NULL; |
|
1638 etype = unknown; |
|
1639 line_num = l; |
|
1640 column_num = c; |
|
1641 } |
|
1642 |
|
1643 tree_prefix_expression::tree_prefix_expression (tree_identifier *t, |
|
1644 tree::expression_type et, |
|
1645 int l = -1, int c = -1) |
|
1646 { |
|
1647 id = t; |
|
1648 etype = et; |
|
1649 line_num = l; |
|
1650 column_num = c; |
|
1651 } |
|
1652 |
|
1653 tree_prefix_expression::~tree_prefix_expression (void) |
|
1654 { |
|
1655 delete id; |
|
1656 } |
|
1657 |
|
1658 tree_constant |
|
1659 tree_prefix_expression::eval (int print) |
|
1660 { |
|
1661 tree_constant retval; |
|
1662 |
|
1663 if (error_state) |
|
1664 return retval; |
|
1665 |
|
1666 if (id != (tree_identifier *) NULL) |
|
1667 { |
|
1668 id->bump_value (etype); |
|
1669 retval = id->eval (print); |
|
1670 if (error_state) |
|
1671 { |
|
1672 retval = tree_constant (); |
|
1673 if (error_state) |
|
1674 eval_error (); |
|
1675 } |
|
1676 } |
|
1677 return retval; |
|
1678 } |
|
1679 |
|
1680 void |
|
1681 tree_prefix_expression::eval_error (void) |
|
1682 { |
|
1683 if (error_state > 0) |
|
1684 { |
|
1685 char *op; |
|
1686 switch (etype) |
|
1687 { |
|
1688 case tree::increment: op = "++"; break; |
|
1689 case tree::decrement: op = "--"; break; |
|
1690 default: op = "unknown"; break; |
|
1691 } |
|
1692 |
|
1693 ::error ("evaluating prefix operator `%s' near line %d, column %d", |
|
1694 op, line (), column ()); |
|
1695 } |
|
1696 } |
|
1697 |
|
1698 int |
|
1699 tree_prefix_expression::is_prefix_expression (void) const |
|
1700 { |
|
1701 return 1; |
|
1702 } |
|
1703 |
|
1704 /* |
|
1705 * Postfix expressions. |
|
1706 */ |
|
1707 tree_postfix_expression::tree_postfix_expression (int l = -1, int c = -1) |
|
1708 { |
|
1709 id = (tree_identifier *) NULL; |
|
1710 etype = unknown; |
|
1711 line_num = l; |
|
1712 column_num = c; |
|
1713 } |
|
1714 |
|
1715 tree_postfix_expression::tree_postfix_expression (tree_identifier *t, |
|
1716 tree::expression_type et, |
|
1717 int l = -1, int c = -1) |
|
1718 { |
|
1719 id = t; |
|
1720 etype = et; |
|
1721 line_num = l; |
|
1722 column_num = c; |
|
1723 } |
|
1724 |
|
1725 tree_postfix_expression::~tree_postfix_expression (void) |
|
1726 { |
|
1727 delete id; |
|
1728 } |
|
1729 |
|
1730 tree_constant |
|
1731 tree_postfix_expression::eval (int print) |
|
1732 { |
|
1733 tree_constant retval; |
|
1734 |
|
1735 if (error_state) |
|
1736 return retval; |
|
1737 |
|
1738 if (id != (tree_identifier *) NULL) |
|
1739 { |
|
1740 retval = id->eval (print); |
|
1741 id->bump_value (etype); |
|
1742 if (error_state) |
|
1743 { |
|
1744 retval = tree_constant (); |
|
1745 if (error_state) |
|
1746 eval_error (); |
|
1747 } |
|
1748 } |
|
1749 return retval; |
|
1750 } |
|
1751 |
|
1752 void |
|
1753 tree_postfix_expression::eval_error (void) |
|
1754 { |
|
1755 if (error_state > 0) |
|
1756 { |
|
1757 char *op; |
|
1758 switch (etype) |
|
1759 { |
|
1760 case tree::increment: op = "++"; break; |
|
1761 case tree::decrement: op = "--"; break; |
|
1762 default: op = "unknown"; break; |
|
1763 } |
|
1764 |
|
1765 ::error ("evaluating postfix operator `%s' near line %d, column %d", |
|
1766 op, line (), column ()); |
|
1767 } |
|
1768 } |
|
1769 |
|
1770 /* |
|
1771 * Unary expressions. |
|
1772 */ |
|
1773 tree_unary_expression::tree_unary_expression (int l = -1, int c = -1) |
|
1774 { |
|
1775 etype = tree::unknown; |
|
1776 op = (tree_expression *) NULL; |
|
1777 line_num = l; |
|
1778 column_num = c; |
|
1779 } |
|
1780 |
|
1781 tree_unary_expression::tree_unary_expression (tree_expression *a, |
|
1782 tree::expression_type t, |
|
1783 int l = -1, int c = -1) |
|
1784 { |
|
1785 etype = t; |
|
1786 op = a; |
|
1787 line_num = l; |
|
1788 column_num = c; |
|
1789 } |
|
1790 |
|
1791 tree_unary_expression::~tree_unary_expression (void) |
|
1792 { |
|
1793 delete op; |
|
1794 } |
|
1795 |
|
1796 tree_constant |
|
1797 tree_unary_expression::eval (int print) |
|
1798 { |
|
1799 if (error_state) |
|
1800 return tree_constant (); |
|
1801 |
|
1802 tree_constant ans; |
|
1803 |
|
1804 switch (etype) |
|
1805 { |
|
1806 case tree::not: |
|
1807 case tree::uminus: |
|
1808 case tree::hermitian: |
|
1809 case tree::transpose: |
|
1810 if (op != (tree_expression *) NULL) |
|
1811 { |
|
1812 tree_constant u = op->eval (0); |
|
1813 if (error_state) |
|
1814 eval_error (); |
|
1815 else if (u.is_defined ()) |
|
1816 { |
|
1817 ans = do_unary_op (u, etype); |
|
1818 if (error_state) |
|
1819 { |
|
1820 ans = tree_constant (); |
|
1821 if (error_state) |
|
1822 eval_error (); |
|
1823 } |
|
1824 } |
|
1825 } |
|
1826 break; |
|
1827 default: |
|
1828 ::error ("unary operator %d not implemented", etype); |
|
1829 break; |
|
1830 } |
|
1831 |
|
1832 return ans; |
|
1833 } |
|
1834 |
|
1835 void |
|
1836 tree_unary_expression::eval_error (void) |
|
1837 { |
|
1838 if (error_state > 0) |
|
1839 { |
|
1840 char *op; |
|
1841 switch (etype) |
|
1842 { |
|
1843 case tree::not: op = "!"; break; |
|
1844 case tree::uminus: op = "-"; break; |
|
1845 case tree::hermitian: op = "'"; break; |
|
1846 case tree::transpose: op = ".'"; break; |
|
1847 default: op = "unknown"; break; |
|
1848 } |
|
1849 |
|
1850 ::error ("evaluating unary operator `%s' near line %d, column %d", |
|
1851 op, line (), column ()); |
|
1852 } |
|
1853 } |
|
1854 |
|
1855 /* |
|
1856 * Binary expressions. |
|
1857 */ |
|
1858 tree_binary_expression::tree_binary_expression (int l = -1, int c = -1) |
|
1859 { |
|
1860 etype = tree::unknown; |
|
1861 op1 = (tree_expression *) NULL; |
|
1862 op2 = (tree_expression *) NULL; |
|
1863 line_num = l; |
|
1864 column_num = c; |
|
1865 } |
|
1866 |
|
1867 tree_binary_expression::tree_binary_expression (tree_expression *a, |
|
1868 tree_expression *b, |
|
1869 tree::expression_type t, |
|
1870 int l = -1, int c = -1) |
|
1871 { |
|
1872 etype = t; |
|
1873 op1 = a; |
|
1874 op2 = b; |
|
1875 line_num = l; |
|
1876 column_num = c; |
|
1877 } |
|
1878 |
|
1879 tree_binary_expression::~tree_binary_expression (void) |
|
1880 { |
|
1881 delete op1; |
|
1882 delete op2; |
|
1883 } |
|
1884 |
|
1885 tree_constant |
|
1886 tree_binary_expression::eval (int print) |
|
1887 { |
|
1888 if (error_state) |
|
1889 return tree_constant (); |
|
1890 |
|
1891 tree_constant ans; |
|
1892 switch (etype) |
|
1893 { |
|
1894 case tree::add: |
|
1895 case tree::subtract: |
|
1896 case tree::multiply: |
|
1897 case tree::el_mul: |
|
1898 case tree::divide: |
|
1899 case tree::el_div: |
|
1900 case tree::leftdiv: |
|
1901 case tree::el_leftdiv: |
|
1902 case tree::power: |
|
1903 case tree::elem_pow: |
|
1904 case tree::cmp_lt: |
|
1905 case tree::cmp_le: |
|
1906 case tree::cmp_eq: |
|
1907 case tree::cmp_ge: |
|
1908 case tree::cmp_gt: |
|
1909 case tree::cmp_ne: |
|
1910 case tree::and: |
|
1911 case tree::or: |
|
1912 if (op1 != (tree_expression *) NULL) |
|
1913 { |
|
1914 tree_constant a = op1->eval (0); |
|
1915 if (error_state) |
|
1916 eval_error (); |
|
1917 else if (a.is_defined () && op2 != (tree_expression *) NULL) |
|
1918 { |
|
1919 tree_constant b = op2->eval (0); |
|
1920 if (error_state) |
|
1921 eval_error (); |
|
1922 else if (b.is_defined ()) |
|
1923 { |
|
1924 ans = do_binary_op (a, b, etype); |
|
1925 if (error_state) |
|
1926 { |
|
1927 ans = tree_constant (); |
|
1928 if (error_state) |
|
1929 eval_error (); |
|
1930 } |
|
1931 } |
|
1932 } |
|
1933 } |
|
1934 break; |
|
1935 case tree::and_and: |
|
1936 case tree::or_or: |
|
1937 { |
|
1938 int result = 0; |
|
1939 if (op1 != NULL_TREE) |
|
1940 { |
|
1941 tree_constant a = op1->eval (0); |
|
1942 if (error_state) |
|
1943 { |
|
1944 eval_error (); |
|
1945 break; |
|
1946 } |
|
1947 |
|
1948 int a_true = a.is_true (); |
|
1949 if (error_state) |
|
1950 { |
|
1951 eval_error (); |
|
1952 break; |
|
1953 } |
|
1954 |
|
1955 if (a_true) |
|
1956 { |
|
1957 if (etype == tree::or_or) |
|
1958 { |
|
1959 result = 1; |
|
1960 goto done; |
|
1961 } |
|
1962 } |
|
1963 else |
|
1964 { |
|
1965 if (etype == tree::and_and) |
|
1966 { |
|
1967 result = 0; |
|
1968 goto done; |
|
1969 } |
|
1970 } |
|
1971 |
|
1972 if (op2 != NULL_TREE) |
|
1973 { |
|
1974 tree_constant b = op2->eval (0); |
|
1975 if (error_state) |
|
1976 { |
|
1977 eval_error (); |
|
1978 break; |
|
1979 } |
|
1980 |
|
1981 result = b.is_true (); |
|
1982 if (error_state) |
|
1983 { |
|
1984 eval_error (); |
|
1985 break; |
|
1986 } |
|
1987 } |
|
1988 } |
|
1989 done: |
|
1990 ans = tree_constant ((double) result); |
|
1991 } |
|
1992 break; |
|
1993 default: |
|
1994 ::error ("binary operator %d not implemented", etype); |
|
1995 break; |
|
1996 } |
|
1997 |
|
1998 return ans; |
|
1999 } |
|
2000 |
|
2001 void |
|
2002 tree_binary_expression::eval_error (void) |
|
2003 { |
|
2004 if (error_state > 0) |
|
2005 { |
|
2006 char *op; |
|
2007 switch (etype) |
|
2008 { |
|
2009 case tree::add: op = "+"; break; |
|
2010 case tree::subtract: op = "-"; break; |
|
2011 case tree::multiply: op = "*"; break; |
|
2012 case tree::el_mul: op = ".*"; break; |
|
2013 case tree::divide: op = "/"; break; |
|
2014 case tree::el_div: op = "./"; break; |
|
2015 case tree::leftdiv: op = "\\"; break; |
|
2016 case tree::el_leftdiv: op = ".\\"; break; |
|
2017 case tree::power: op = "^"; break; |
|
2018 case tree::elem_pow: op = ".^"; break; |
|
2019 case tree::cmp_lt: op = "<"; break; |
|
2020 case tree::cmp_le: op = "<="; break; |
|
2021 case tree::cmp_eq: op = "=="; break; |
|
2022 case tree::cmp_ge: op = ">="; break; |
|
2023 case tree::cmp_gt: op = ">"; break; |
|
2024 case tree::cmp_ne: op = "!="; break; |
|
2025 case tree::and_and: op = "&&"; break; |
|
2026 case tree::or_or: op = "||"; break; |
|
2027 case tree::and: op = "&"; break; |
|
2028 case tree::or: op = "|"; break; |
|
2029 default: op = "unknown"; break; |
|
2030 } |
|
2031 |
|
2032 ::error ("evaluating binary operator `%s' near line %d, column %d", |
|
2033 op, line (), column ()); |
|
2034 } |
|
2035 } |
|
2036 |
|
2037 /* |
|
2038 * Assignment expressions. |
|
2039 */ |
|
2040 tree_assignment_expression::tree_assignment_expression (void) |
|
2041 { |
|
2042 in_parens = 0; |
|
2043 etype = tree::assignment; |
|
2044 } |
|
2045 |
|
2046 tree_assignment_expression::~tree_assignment_expression (void) |
|
2047 { |
|
2048 } |
|
2049 |
|
2050 tree_constant |
|
2051 tree_assignment_expression::eval (int print) |
|
2052 { |
|
2053 panic ("invalid evaluation of generic expression"); |
|
2054 return tree_constant (); |
|
2055 } |
|
2056 |
|
2057 int |
|
2058 tree_assignment_expression::is_assignment_expression (void) const |
|
2059 { |
|
2060 return 1; |
|
2061 } |
|
2062 |
|
2063 /* |
|
2064 * Simple assignment expressions. |
|
2065 */ |
|
2066 tree_simple_assignment_expression::tree_simple_assignment_expression |
|
2067 (int l = -1, int c = -1) |
|
2068 { |
|
2069 etype = tree::assignment; |
|
2070 lhs = (tree_identifier *) NULL; |
|
2071 index = (tree_argument_list *) NULL; |
|
2072 rhs = (tree_expression *) NULL; |
|
2073 line_num = l; |
|
2074 column_num = c; |
|
2075 } |
|
2076 |
|
2077 tree_simple_assignment_expression::tree_simple_assignment_expression |
|
2078 (tree_identifier *i, tree_expression *r, int l = -1, int c = -1) |
|
2079 { |
|
2080 etype = tree::assignment; |
|
2081 lhs = i; |
|
2082 index = (tree_argument_list *) NULL; |
|
2083 rhs = r; |
|
2084 line_num = l; |
|
2085 column_num = c; |
|
2086 } |
|
2087 |
|
2088 tree_simple_assignment_expression::tree_simple_assignment_expression |
|
2089 (tree_index_expression *idx_expr, tree_expression *r, int l = -1, int c = -1) |
|
2090 { |
|
2091 etype = tree::assignment; |
|
2092 lhs = idx_expr->ident (); |
|
2093 index = idx_expr->arg_list (); |
|
2094 rhs = r; |
|
2095 line_num = l; |
|
2096 column_num = c; |
|
2097 } |
|
2098 |
|
2099 tree_simple_assignment_expression::~tree_simple_assignment_expression (void) |
|
2100 { |
|
2101 // delete lhs; |
|
2102 // delete index; |
|
2103 delete rhs; |
|
2104 } |
|
2105 |
|
2106 tree_constant |
|
2107 tree_simple_assignment_expression::eval (int print) |
|
2108 { |
|
2109 assert (etype == tree::assignment); |
|
2110 |
|
2111 tree_constant ans; |
|
2112 tree_constant retval; |
|
2113 |
|
2114 if (error_state) |
|
2115 return retval; |
|
2116 |
|
2117 if (rhs != (tree_expression *) NULL) |
|
2118 { |
|
2119 tree_constant rhs_val = rhs->eval (0); |
|
2120 if (error_state) |
|
2121 { |
|
2122 if (error_state) |
|
2123 eval_error (); |
|
2124 } |
|
2125 else if (index == NULL_TREE) |
|
2126 { |
|
2127 ans = lhs->assign (rhs_val); |
|
2128 if (error_state) |
|
2129 eval_error (); |
|
2130 } |
|
2131 else |
|
2132 { |
|
2133 // Extract the arguments into a simple vector. |
506
|
2134 Octave_object args = index->convert_to_const_vector (); |
|
2135 |
|
2136 int nargin = args.length (); |
494
|
2137 |
|
2138 if (error_state) |
|
2139 eval_error (); |
506
|
2140 else if (nargin > 1) |
494
|
2141 { |
506
|
2142 ans = lhs->assign (rhs_val, args); |
494
|
2143 if (error_state) |
|
2144 eval_error (); |
|
2145 } |
|
2146 } |
|
2147 } |
|
2148 |
|
2149 if (! error_state && ans.is_defined ()) |
|
2150 { |
|
2151 int pad_after = 0; |
|
2152 if (print && user_pref.print_answer_id_name) |
|
2153 { |
|
2154 if (print_as_scalar (ans)) |
|
2155 { |
|
2156 ostrstream output_buf; |
|
2157 output_buf << lhs->name () << " = " << ends; |
|
2158 maybe_page_output (output_buf); |
|
2159 } |
|
2160 else |
|
2161 { |
|
2162 pad_after = 1; |
|
2163 ostrstream output_buf; |
|
2164 output_buf << lhs->name () << " =\n\n" << ends; |
|
2165 maybe_page_output (output_buf); |
|
2166 } |
|
2167 } |
|
2168 |
|
2169 retval = ans.eval (print); |
|
2170 |
|
2171 if (print && pad_after) |
|
2172 { |
|
2173 ostrstream output_buf; |
|
2174 output_buf << "\n" << ends; |
|
2175 maybe_page_output (output_buf); |
|
2176 } |
|
2177 } |
|
2178 |
|
2179 return retval; |
|
2180 } |
|
2181 |
|
2182 void |
|
2183 tree_simple_assignment_expression::eval_error (void) |
|
2184 { |
|
2185 if (error_state > 0) |
|
2186 { |
|
2187 int l = line (); |
|
2188 int c = column (); |
|
2189 if (l != -1 && c != -1) |
|
2190 ::error ("evaluating assignment expression near line %d, column %d", |
|
2191 l, c); |
|
2192 // else |
|
2193 // error ("evaluating assignment expression"); |
|
2194 } |
|
2195 } |
|
2196 |
|
2197 /* |
|
2198 * Multi-valued assignmnt expressions. |
|
2199 */ |
|
2200 tree_multi_assignment_expression::tree_multi_assignment_expression |
|
2201 (int l = -1, int c = -1) |
|
2202 { |
|
2203 etype = tree::multi_assignment; |
|
2204 lhs = (tree_return_list *) NULL; |
|
2205 rhs = (tree_expression *) NULL; |
|
2206 line_num = l; |
|
2207 column_num = c; |
|
2208 } |
|
2209 |
|
2210 tree_multi_assignment_expression::tree_multi_assignment_expression |
|
2211 (tree_return_list *lst, tree_expression *r, int l = -1, int c = -1) |
|
2212 { |
|
2213 etype = tree::multi_assignment; |
|
2214 lhs = lst; |
|
2215 rhs = r; |
|
2216 line_num = l; |
|
2217 column_num = c; |
|
2218 } |
|
2219 |
|
2220 tree_multi_assignment_expression::~tree_multi_assignment_expression (void) |
|
2221 { |
|
2222 delete lhs; |
|
2223 delete rhs; |
|
2224 } |
|
2225 |
|
2226 tree_constant |
|
2227 tree_multi_assignment_expression::eval (int print) |
|
2228 { |
|
2229 tree_constant retval; |
|
2230 |
|
2231 if (error_state) |
|
2232 return retval; |
|
2233 |
500
|
2234 Octave_object tmp_args; |
506
|
2235 Octave_object result = eval (print, 1, tmp_args); |
500
|
2236 |
|
2237 if (result.length () > 0) |
|
2238 retval = result(0); |
494
|
2239 |
|
2240 return retval; |
|
2241 } |
|
2242 |
500
|
2243 Octave_object |
494
|
2244 tree_multi_assignment_expression::eval (int print, int nargout, |
506
|
2245 const Octave_object& args) |
494
|
2246 { |
|
2247 assert (etype == tree::multi_assignment); |
|
2248 |
|
2249 if (error_state || rhs == (tree_expression *) NULL) |
500
|
2250 return Octave_object (); |
494
|
2251 |
|
2252 nargout = lhs->length (); |
500
|
2253 Octave_object tmp_args; |
506
|
2254 Octave_object results = rhs->eval (0, nargout, tmp_args); |
494
|
2255 |
|
2256 if (error_state) |
|
2257 eval_error (); |
|
2258 |
|
2259 int ma_line = line (); |
|
2260 int ma_column = column (); |
|
2261 |
500
|
2262 if (results.length () > 0) |
494
|
2263 { |
|
2264 tree_return_list *elem; |
|
2265 int i = 0; |
|
2266 int pad_after = 0; |
|
2267 int last_was_scalar_type = 0; |
|
2268 for (elem = lhs; elem != (tree_return_list *) NULL; |
|
2269 elem = elem->next_elem ()) |
|
2270 { |
|
2271 tree_index_expression *lhs_expr = elem->idx_expr (); |
|
2272 if (i < nargout) |
|
2273 { |
500
|
2274 // XXX FIXME? XXX -- this is apparently the way Matlab works, but |
|
2275 // maybe we should have the option of skipping the assignment instead. |
|
2276 |
|
2277 tree_constant *tmp = NULL_TREE_CONST; |
|
2278 if (results(i).is_undefined ()) |
494
|
2279 { |
500
|
2280 Matrix m; |
|
2281 tmp = new tree_constant (m); |
494
|
2282 } |
|
2283 else |
500
|
2284 tmp = new tree_constant (results(i)); |
|
2285 |
|
2286 tree_simple_assignment_expression tmp_expr |
|
2287 (lhs_expr, tmp, ma_line, ma_column); |
|
2288 |
|
2289 results(i) = tmp_expr.eval (0); // May change |
|
2290 |
|
2291 if (error_state) |
|
2292 break; |
|
2293 |
|
2294 if (print && pad_after) |
494
|
2295 { |
500
|
2296 ostrstream output_buf; |
|
2297 output_buf << "\n" << '\0'; |
|
2298 maybe_page_output (output_buf); |
|
2299 } |
|
2300 |
|
2301 if (print && user_pref.print_answer_id_name) |
|
2302 { |
|
2303 tree_identifier *tmp_id = lhs_expr->ident (); |
|
2304 char *tmp_nm = tmp_id->name (); |
|
2305 |
|
2306 if (print_as_scalar (results(i))) |
494
|
2307 { |
|
2308 ostrstream output_buf; |
500
|
2309 output_buf << tmp_nm << " = " << '\0'; |
494
|
2310 maybe_page_output (output_buf); |
500
|
2311 last_was_scalar_type = 1; |
494
|
2312 } |
500
|
2313 else |
494
|
2314 { |
500
|
2315 ostrstream output_buf; |
|
2316 output_buf << tmp_nm << " =\n\n" << '\0'; |
|
2317 maybe_page_output (output_buf); |
|
2318 last_was_scalar_type = 0; |
494
|
2319 } |
|
2320 } |
500
|
2321 |
|
2322 results(i).eval (print); |
|
2323 |
|
2324 pad_after++; |
494
|
2325 i++; |
|
2326 } |
|
2327 else |
|
2328 { |
|
2329 tree_simple_assignment_expression tmp_expr |
|
2330 (lhs_expr, NULL_TREE_CONST, ma_line, ma_column); |
|
2331 |
|
2332 tmp_expr.eval (0); |
|
2333 |
|
2334 if (error_state) |
|
2335 break; |
|
2336 |
|
2337 if (last_was_scalar_type && i == 1) |
|
2338 pad_after = 0; |
|
2339 |
|
2340 break; |
|
2341 } |
|
2342 } |
|
2343 |
|
2344 if (print && pad_after) |
|
2345 { |
|
2346 ostrstream output_buf; |
|
2347 output_buf << "\n" << '\0'; |
|
2348 maybe_page_output (output_buf); |
|
2349 } |
|
2350 } |
|
2351 |
|
2352 return results; |
|
2353 } |
|
2354 |
|
2355 void |
|
2356 tree_multi_assignment_expression::eval_error (void) |
|
2357 { |
|
2358 if (error_state > 0) |
|
2359 ::error ("evaluating assignment expression near line %d, column %d", |
|
2360 line (), column ()); |
|
2361 } |
|
2362 |
|
2363 /* |
|
2364 * Colon expressions. |
|
2365 */ |
|
2366 tree_colon_expression::tree_colon_expression (int l = -1, int c = -1) |
|
2367 { |
|
2368 etype = tree::colon; |
|
2369 op1 = (tree_expression *) NULL; |
|
2370 op2 = (tree_expression *) NULL; |
|
2371 op3 = (tree_expression *) NULL; |
|
2372 line_num = l; |
|
2373 column_num = c; |
|
2374 } |
|
2375 |
|
2376 tree_colon_expression::tree_colon_expression (tree_expression *a, tree_expression *b, |
|
2377 int l = -1, int c = -1) |
|
2378 { |
|
2379 etype = tree::colon; |
|
2380 op1 = a; // base |
|
2381 op2 = b; // limit |
|
2382 op3 = (tree_expression *) NULL; // increment if not empty. |
|
2383 line_num = l; |
|
2384 column_num = c; |
|
2385 } |
|
2386 |
|
2387 tree_colon_expression::~tree_colon_expression (void) |
|
2388 { |
|
2389 delete op1; |
|
2390 delete op2; |
|
2391 delete op3; |
|
2392 } |
|
2393 |
|
2394 tree_colon_expression * |
|
2395 tree_colon_expression::chain (tree_expression *t) |
|
2396 { |
|
2397 tree_colon_expression *retval = (tree_colon_expression *) NULL; |
|
2398 if (op1 == NULL_TREE || op3 != NULL_TREE) |
|
2399 ::error ("invalid colon expression"); |
|
2400 else |
|
2401 { |
|
2402 op3 = op2; // Stupid syntax. |
|
2403 op2 = t; |
|
2404 |
|
2405 retval = this; |
|
2406 } |
|
2407 return retval; |
|
2408 } |
|
2409 |
|
2410 tree_constant |
|
2411 tree_colon_expression::eval (int print) |
|
2412 { |
|
2413 tree_constant retval; |
|
2414 |
|
2415 if (error_state || op1 == NULL_TREE || op2 == NULL_TREE) |
|
2416 return retval; |
|
2417 |
|
2418 tree_constant tmp; |
|
2419 |
|
2420 tmp = op1->eval (0); |
|
2421 |
|
2422 if (tmp.is_undefined ()) |
|
2423 { |
|
2424 eval_error ("invalid null value in colon expression"); |
|
2425 return retval; |
|
2426 } |
|
2427 |
|
2428 tmp = tmp.make_numeric (); |
|
2429 if (tmp.const_type () != tree_constant_rep::scalar_constant |
|
2430 && tmp.const_type () != tree_constant_rep::complex_scalar_constant) |
|
2431 { |
|
2432 eval_error ("base for colon expression must be a scalar"); |
|
2433 return retval; |
|
2434 } |
|
2435 double base = tmp.double_value (); |
|
2436 |
|
2437 tmp = op2->eval (0); |
|
2438 |
|
2439 if (tmp.is_undefined ()) |
|
2440 { |
|
2441 eval_error ("invalid null value in colon expression"); |
|
2442 return retval; |
|
2443 } |
|
2444 |
|
2445 tmp = tmp.make_numeric (); |
|
2446 if (tmp.const_type () != tree_constant_rep::scalar_constant |
|
2447 && tmp.const_type () != tree_constant_rep::complex_scalar_constant) |
|
2448 { |
|
2449 eval_error ("limit for colon expression must be a scalar"); |
|
2450 return retval; |
|
2451 } |
|
2452 double limit = tmp.double_value (); |
|
2453 |
|
2454 double inc = 1.0; |
|
2455 if (op3 != NULL_TREE) |
|
2456 { |
|
2457 tmp = op3->eval (0); |
|
2458 |
|
2459 if (tmp.is_undefined ()) |
|
2460 { |
|
2461 eval_error ("invalid null value in colon expression"); |
|
2462 return retval; |
|
2463 } |
|
2464 |
|
2465 tmp = tmp.make_numeric (); |
|
2466 if (tmp.const_type () != tree_constant_rep::scalar_constant |
|
2467 && tmp.const_type () != tree_constant_rep::complex_scalar_constant) |
|
2468 { |
|
2469 eval_error ("increment for colon expression must be a scalar"); |
|
2470 return retval; |
|
2471 } |
|
2472 else |
|
2473 inc = tmp.double_value (); |
|
2474 } |
|
2475 |
|
2476 retval = tree_constant (base, limit, inc); |
|
2477 |
|
2478 if (error_state) |
|
2479 { |
|
2480 if (error_state) |
|
2481 eval_error ("evaluating colon expression"); |
|
2482 return tree_constant (); |
|
2483 } |
|
2484 |
|
2485 return retval; |
|
2486 } |
|
2487 |
|
2488 void |
|
2489 tree_colon_expression::eval_error (const char *s) |
|
2490 { |
|
2491 if (error_state > 0) |
|
2492 ::error ("%s near line %d column %d", s, line (), column ()); |
|
2493 } |
|
2494 |
|
2495 /* |
|
2496 * Index expressions. |
|
2497 */ |
|
2498 tree_index_expression::tree_index_expression (int l = -1, int c = -1) |
|
2499 { |
|
2500 id = (tree_identifier *) NULL; |
|
2501 list = (tree_argument_list *) NULL; |
|
2502 line_num = l; |
|
2503 column_num = c; |
|
2504 } |
|
2505 |
|
2506 tree_index_expression::tree_index_expression (tree_identifier *i, |
|
2507 tree_argument_list *lst, |
|
2508 int l = -1, int c = -1) |
|
2509 { |
|
2510 id = i; |
|
2511 list = lst; |
|
2512 line_num = l; |
|
2513 column_num = c; |
|
2514 } |
|
2515 |
|
2516 tree_index_expression::tree_index_expression (tree_identifier *i, |
|
2517 int l = -1, int c = -1) |
|
2518 { |
|
2519 id = i; |
|
2520 list = (tree_argument_list *) NULL; |
|
2521 line_num = l; |
|
2522 column_num = c; |
|
2523 } |
|
2524 |
|
2525 tree_index_expression::~tree_index_expression (void) |
|
2526 { |
|
2527 delete id; |
|
2528 delete list; |
|
2529 } |
|
2530 |
|
2531 int |
|
2532 tree_index_expression::is_index_expression (void) const |
|
2533 { |
|
2534 return 1; |
|
2535 } |
|
2536 |
|
2537 tree_identifier * |
|
2538 tree_index_expression::ident (void) |
|
2539 { |
|
2540 return id; |
|
2541 } |
|
2542 |
|
2543 tree_argument_list * |
|
2544 tree_index_expression::arg_list (void) |
|
2545 { |
|
2546 return list; |
|
2547 } |
|
2548 |
|
2549 void |
|
2550 tree_index_expression::mark_for_possible_ans_assign (void) |
|
2551 { |
|
2552 id->mark_for_possible_ans_assign (); |
|
2553 } |
|
2554 |
|
2555 |
|
2556 tree_constant |
|
2557 tree_index_expression::eval (int print) |
|
2558 { |
|
2559 tree_constant retval; |
|
2560 |
|
2561 if (error_state) |
|
2562 return retval; |
|
2563 |
|
2564 if (list == (tree_argument_list *) NULL) |
|
2565 { |
|
2566 retval = id->eval (print); |
|
2567 if (error_state) |
|
2568 eval_error (); |
|
2569 } |
|
2570 else |
|
2571 { |
|
2572 // Extract the arguments into a simple vector. |
506
|
2573 Octave_object args = list->convert_to_const_vector (); |
494
|
2574 // Don't pass null arguments. |
506
|
2575 int nargin = args.length (); |
494
|
2576 if (error_state) |
|
2577 eval_error (); |
506
|
2578 else if (nargin > 1 && all_args_defined (args)) |
494
|
2579 { |
506
|
2580 Octave_object tmp = id->eval (print, 1, args); |
494
|
2581 |
|
2582 if (error_state) |
|
2583 eval_error (); |
|
2584 |
500
|
2585 if (tmp.length () > 0) |
|
2586 retval = tmp(0); |
494
|
2587 } |
|
2588 } |
|
2589 return retval; |
|
2590 } |
|
2591 |
500
|
2592 Octave_object |
506
|
2593 tree_index_expression::eval (int print, int nargout, const Octave_object& args) |
494
|
2594 { |
500
|
2595 Octave_object retval; |
494
|
2596 |
|
2597 if (error_state) |
|
2598 return retval; |
|
2599 |
|
2600 if (list == (tree_argument_list *) NULL) |
|
2601 { |
500
|
2602 Octave_object tmp_args; |
506
|
2603 retval = id->eval (print, nargout, tmp_args); |
494
|
2604 if (error_state) |
|
2605 eval_error (); |
|
2606 } |
|
2607 else |
|
2608 { |
|
2609 // Extract the arguments into a simple vector. |
506
|
2610 Octave_object args = list->convert_to_const_vector (); |
494
|
2611 // Don't pass null arguments. |
|
2612 if (error_state) |
|
2613 eval_error (); |
506
|
2614 else if (args.length () > 1 && all_args_defined (args)) |
494
|
2615 { |
506
|
2616 retval = id->eval (print, nargout, args); |
494
|
2617 if (error_state) |
|
2618 eval_error (); |
|
2619 } |
|
2620 } |
|
2621 return retval; |
|
2622 } |
|
2623 |
|
2624 void |
|
2625 tree_index_expression::eval_error (void) |
|
2626 { |
|
2627 if (error_state > 0) |
|
2628 { |
|
2629 int l = line (); |
|
2630 int c = column (); |
|
2631 char *fmt; |
|
2632 if (l != -1 && c != -1) |
|
2633 { |
|
2634 if (list != (tree_argument_list *) NULL) |
|
2635 fmt = "evaluating index expression near line %d, column %d"; |
|
2636 else |
|
2637 fmt = "evaluating expression near line %d, column %d"; |
|
2638 |
|
2639 ::error (fmt, l, c); |
|
2640 } |
|
2641 else |
|
2642 { |
|
2643 if (list != (tree_argument_list *) NULL) |
|
2644 ::error ("evaluating index expression"); |
|
2645 else |
|
2646 ::error ("evaluating expression"); |
|
2647 } |
|
2648 } |
|
2649 } |
|
2650 |
|
2651 /* |
|
2652 * Argument lists. |
|
2653 */ |
|
2654 tree_argument_list::tree_argument_list (void) |
|
2655 { |
|
2656 arg = NULL_TREE; |
|
2657 next = (tree_argument_list *) NULL; |
|
2658 } |
|
2659 |
|
2660 tree_argument_list::tree_argument_list (tree *t) |
|
2661 { |
|
2662 arg = t; |
|
2663 next = (tree_argument_list *) NULL; |
|
2664 } |
|
2665 |
|
2666 tree_argument_list::~tree_argument_list (void) |
|
2667 { |
|
2668 delete arg; |
|
2669 delete next; |
|
2670 } |
|
2671 |
|
2672 tree_argument_list * |
|
2673 tree_argument_list::chain (tree *t) |
|
2674 { |
|
2675 tree_argument_list *tmp = new tree_argument_list (t); |
|
2676 tmp->next = this; |
|
2677 return tmp; |
|
2678 } |
|
2679 |
|
2680 tree_argument_list * |
|
2681 tree_argument_list::reverse (void) |
|
2682 { |
|
2683 tree_argument_list *list = this; |
|
2684 tree_argument_list *next; |
|
2685 tree_argument_list *prev = (tree_argument_list *) NULL; |
|
2686 |
|
2687 while (list != (tree_argument_list *) NULL) |
|
2688 { |
|
2689 next = list->next; |
|
2690 list->next = prev; |
|
2691 prev = list; |
|
2692 list = next; |
|
2693 } |
|
2694 return prev; |
|
2695 } |
|
2696 |
|
2697 int |
|
2698 tree_argument_list::length (void) |
|
2699 { |
|
2700 tree_argument_list *list = this; |
|
2701 int len = 0; |
|
2702 while (list != (tree_argument_list *) NULL) |
|
2703 { |
|
2704 len++; |
|
2705 list = list->next; |
|
2706 } |
|
2707 return len; |
|
2708 } |
|
2709 |
|
2710 tree_argument_list * |
|
2711 tree_argument_list::next_elem (void) |
|
2712 { |
|
2713 return next; |
|
2714 } |
|
2715 |
|
2716 /* |
|
2717 * Convert a linked list of trees to a vector of pointers to trees, |
|
2718 * evaluating them along the way. |
|
2719 */ |
500
|
2720 Octave_object |
506
|
2721 tree_argument_list::convert_to_const_vector (void) |
494
|
2722 { |
506
|
2723 int len = length () + 1; |
494
|
2724 |
500
|
2725 Octave_object args (len); |
494
|
2726 |
|
2727 // args[0] may eventually hold something useful, like the function |
|
2728 // name. |
|
2729 tree_argument_list *tmp_list = this; |
|
2730 for (int k = 1; k < len; k++) |
|
2731 { |
|
2732 if (tmp_list != (tree_argument_list *) NULL) |
|
2733 { |
500
|
2734 args(k) = tmp_list->eval (0); |
494
|
2735 if (error_state) |
|
2736 { |
|
2737 ::error ("evaluating argument list element number %d", k); |
|
2738 break; |
|
2739 } |
|
2740 tmp_list = tmp_list->next; |
|
2741 } |
|
2742 else |
|
2743 { |
500
|
2744 args(k) = tree_constant (); |
494
|
2745 break; |
|
2746 } |
|
2747 } |
|
2748 return args; |
|
2749 } |
|
2750 |
|
2751 tree_constant |
|
2752 tree_argument_list::eval (int print) |
|
2753 { |
|
2754 if (error_state || arg == NULL_TREE) |
|
2755 return tree_constant (); |
|
2756 else |
|
2757 return arg->eval (print); |
|
2758 } |
|
2759 |
|
2760 /* |
|
2761 * Parameter lists. |
|
2762 */ |
|
2763 tree_parameter_list::tree_parameter_list (void) |
|
2764 { |
|
2765 marked_for_varargs = 0; |
|
2766 param = (tree_identifier *) NULL; |
|
2767 next = (tree_parameter_list *) NULL; |
|
2768 } |
|
2769 |
|
2770 tree_parameter_list::tree_parameter_list (tree_identifier *t) |
|
2771 { |
|
2772 marked_for_varargs = 0; |
|
2773 param = t; |
|
2774 next = (tree_parameter_list *) NULL; |
|
2775 } |
|
2776 |
|
2777 tree_parameter_list::~tree_parameter_list (void) |
|
2778 { |
|
2779 delete param; |
|
2780 delete next; |
|
2781 } |
|
2782 |
|
2783 tree_parameter_list * |
|
2784 tree_parameter_list::chain (tree_identifier *t) |
|
2785 { |
|
2786 tree_parameter_list *tmp = new tree_parameter_list (t); |
|
2787 tmp->next = this; |
|
2788 return tmp; |
|
2789 } |
|
2790 |
|
2791 tree_parameter_list * |
|
2792 tree_parameter_list::reverse (void) |
|
2793 { |
|
2794 tree_parameter_list *list = this; |
|
2795 tree_parameter_list *next; |
|
2796 tree_parameter_list *prev = (tree_parameter_list *) NULL; |
|
2797 |
|
2798 while (list != (tree_parameter_list *) NULL) |
|
2799 { |
|
2800 next = list->next; |
|
2801 list->next = prev; |
|
2802 prev = list; |
|
2803 list = next; |
|
2804 } |
|
2805 return prev; |
|
2806 } |
|
2807 |
|
2808 int |
|
2809 tree_parameter_list::length (void) |
|
2810 { |
|
2811 tree_parameter_list *list = this; |
|
2812 int len = 0; |
|
2813 while (list != (tree_parameter_list *) NULL) |
|
2814 { |
|
2815 len++; |
|
2816 list = list->next; |
|
2817 } |
|
2818 return len; |
|
2819 } |
|
2820 |
|
2821 char * |
|
2822 tree_parameter_list::name (void) const |
|
2823 { |
|
2824 return param->name (); |
|
2825 } |
|
2826 |
|
2827 void |
|
2828 tree_parameter_list::mark_as_formal_parameters (void) |
|
2829 { |
|
2830 param->mark_as_formal_parameter (); |
|
2831 if (next != (tree_parameter_list *) NULL) |
|
2832 next->mark_as_formal_parameters (); |
|
2833 } |
|
2834 |
|
2835 void |
|
2836 tree_parameter_list::mark_varargs (void) |
|
2837 { |
|
2838 marked_for_varargs = 1; |
|
2839 } |
|
2840 |
|
2841 int |
|
2842 tree_parameter_list::takes_varargs (void) const |
|
2843 { |
|
2844 return marked_for_varargs; |
|
2845 } |
|
2846 |
504
|
2847 void |
|
2848 tree_parameter_list::mark_varargs_only (void) |
|
2849 { |
|
2850 marked_for_varargs = -1; |
|
2851 } |
|
2852 |
|
2853 int |
|
2854 tree_parameter_list::varargs_only (void) |
|
2855 { |
|
2856 return (marked_for_varargs < 0); |
|
2857 } |
|
2858 |
494
|
2859 tree_identifier * |
|
2860 tree_parameter_list::define (tree_constant *t) |
|
2861 { |
|
2862 return param->define (t); |
|
2863 } |
|
2864 |
|
2865 void |
506
|
2866 tree_parameter_list::define_from_arg_vector (const Octave_object& args) |
494
|
2867 { |
500
|
2868 if (args.length () <= 0) |
494
|
2869 return; |
|
2870 |
506
|
2871 int nargin = args.length (); |
|
2872 |
494
|
2873 int expected_nargin = length () + 1; |
|
2874 |
|
2875 tree_parameter_list *ptr = this; |
|
2876 |
|
2877 for (int i = 1; i < expected_nargin; i++) |
|
2878 { |
|
2879 tree_constant *tmp = NULL_TREE_CONST; |
|
2880 |
|
2881 if (i < nargin) |
|
2882 { |
500
|
2883 if (args(i).is_defined () |
|
2884 && (args(i).const_type () == tree_constant_rep::magic_colon)) |
494
|
2885 { |
|
2886 ::error ("invalid use of colon in function argument list"); |
|
2887 return; |
|
2888 } |
500
|
2889 tmp = new tree_constant (args(i)); |
494
|
2890 } |
|
2891 |
|
2892 ptr->define (tmp); |
|
2893 ptr = ptr->next; |
|
2894 } |
|
2895 } |
|
2896 |
500
|
2897 Octave_object |
494
|
2898 tree_parameter_list::convert_to_const_vector (void) |
|
2899 { |
|
2900 int nout = length (); |
|
2901 |
500
|
2902 Octave_object retval (nout); |
494
|
2903 |
|
2904 int i = 0; |
|
2905 |
|
2906 tree_parameter_list *elem = this; |
|
2907 |
500
|
2908 for ( ; elem != (tree_parameter_list *) NULL; elem = elem->next) |
|
2909 { |
|
2910 if (elem->is_defined ()) |
|
2911 retval(i) = elem->eval (0); |
|
2912 i++; |
|
2913 } |
494
|
2914 |
|
2915 return retval; |
|
2916 } |
|
2917 |
500
|
2918 int |
|
2919 tree_parameter_list::is_defined (void) |
|
2920 { |
|
2921 return (param != (tree_identifier *) NULL && param->is_defined ()); |
|
2922 } |
|
2923 |
494
|
2924 tree_parameter_list * |
|
2925 tree_parameter_list::next_elem (void) |
|
2926 { |
|
2927 return next; |
|
2928 } |
|
2929 |
|
2930 tree_constant |
|
2931 tree_parameter_list::eval (int print) |
|
2932 { |
|
2933 if (error_state || param == NULL_TREE) |
|
2934 return tree_constant (); |
|
2935 else |
|
2936 return param->eval (print); |
|
2937 } |
|
2938 |
|
2939 /* |
|
2940 * Return lists. |
|
2941 */ |
|
2942 tree_return_list::tree_return_list (void) |
|
2943 { |
|
2944 retval = (tree_index_expression *) NULL; |
|
2945 next = (tree_return_list *) NULL; |
|
2946 } |
|
2947 |
|
2948 tree_return_list::tree_return_list (tree_identifier *t) |
|
2949 { |
|
2950 retval = new tree_index_expression (t); |
|
2951 next = (tree_return_list *) NULL; |
|
2952 } |
|
2953 |
|
2954 tree_return_list::tree_return_list (tree_index_expression *t) |
|
2955 { |
|
2956 retval = t; |
|
2957 next = (tree_return_list *) NULL; |
|
2958 } |
|
2959 |
|
2960 tree_return_list::~tree_return_list (void) |
|
2961 { |
|
2962 delete retval; |
|
2963 delete next; |
|
2964 } |
|
2965 |
|
2966 tree_return_list * |
|
2967 tree_return_list::chain (tree_identifier *t) |
|
2968 { |
|
2969 tree_return_list *tmp = new tree_return_list (t); |
|
2970 tmp->next = this; |
|
2971 return tmp; |
|
2972 } |
|
2973 |
|
2974 tree_return_list * |
|
2975 tree_return_list::chain (tree_index_expression *t) |
|
2976 { |
|
2977 tree_return_list *tmp = new tree_return_list (t); |
|
2978 tmp->next = this; |
|
2979 return tmp; |
|
2980 } |
|
2981 |
|
2982 tree_return_list * |
|
2983 tree_return_list::reverse (void) |
|
2984 { |
|
2985 tree_return_list *list = this; |
|
2986 tree_return_list *next; |
|
2987 tree_return_list *prev = (tree_return_list *) NULL; |
|
2988 |
|
2989 while (list != (tree_return_list *) NULL) |
|
2990 { |
|
2991 next = list->next; |
|
2992 list->next = prev; |
|
2993 prev = list; |
|
2994 list = next; |
|
2995 } |
|
2996 return prev; |
|
2997 } |
|
2998 |
|
2999 int |
|
3000 tree_return_list::length (void) |
|
3001 { |
|
3002 tree_return_list *list = this; |
|
3003 int len = 0; |
|
3004 while (list != (tree_return_list *) NULL) |
|
3005 { |
|
3006 len++; |
|
3007 list = list->next; |
|
3008 } |
|
3009 return len; |
|
3010 } |
|
3011 |
|
3012 tree_index_expression * |
|
3013 tree_return_list::idx_expr (void) |
|
3014 { |
|
3015 return retval; |
|
3016 } |
|
3017 |
|
3018 tree_return_list * |
|
3019 tree_return_list::next_elem (void) |
|
3020 { |
|
3021 return next; |
|
3022 } |
|
3023 |
|
3024 tree_constant |
|
3025 tree_return_list::eval (int print) |
|
3026 { |
|
3027 panic ("invalid evaluation of return list"); |
|
3028 return tree_constant (); |
|
3029 } |
|
3030 |
|
3031 /* |
|
3032 ;;; Local Variables: *** |
|
3033 ;;; mode: C++ *** |
|
3034 ;;; page-delimiter: "^/\\*" *** |
|
3035 ;;; End: *** |
|
3036 */ |