529
|
1 // tree-expr.cc -*- C++ -*- |
494
|
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" |
543
|
45 #include "help.h" |
494
|
46 #include "error.h" |
|
47 #include "pager.h" |
584
|
48 #include "tree-base.h" |
494
|
49 #include "tree-expr.h" |
578
|
50 #include "tree-misc.h" |
494
|
51 #include "tree-const.h" |
|
52 #include "input.h" |
|
53 #include "symtab.h" |
|
54 #include "utils.h" |
|
55 #include "octave.h" |
|
56 #include "octave-hist.h" |
|
57 #include "unwind-prot.h" |
|
58 #include "parse.h" |
|
59 #include "lex.h" |
529
|
60 #include "defun.h" |
494
|
61 |
|
62 // Nonzero means we're returning from a function. |
|
63 extern int returning; |
|
64 |
|
65 // But first, some extra functions used by the tree classes. |
|
66 |
|
67 // We seem to have no use for this now. Maybe it will be needed at |
|
68 // some future date, so here it is. |
|
69 #if 0 |
578
|
70 // Convert a linked list of trees to a vector of pointers to trees. |
|
71 |
494
|
72 static tree ** |
|
73 list_to_vector (tree *list, int& len) |
|
74 { |
|
75 len = list->length () + 1; |
|
76 |
|
77 tree **args = new tree * [len]; |
|
78 |
|
79 // args[0] may eventually hold something useful, like the function |
|
80 // name. |
|
81 tree *tmp_list = list; |
|
82 for (int k = 1; k < len; k++) |
|
83 { |
|
84 args[k] = tmp_list; |
|
85 tmp_list = tmp_list->next_elem (); |
|
86 } |
|
87 return args; |
|
88 } |
|
89 #endif |
|
90 |
|
91 static int |
|
92 print_as_scalar (const tree_constant& val) |
|
93 { |
|
94 int nr = val.rows (); |
|
95 int nc = val.columns (); |
|
96 return (val.is_scalar_type () |
610
|
97 || val.is_string () |
494
|
98 || (val.is_matrix_type () |
|
99 && ((nr == 1 && nc == 1) |
|
100 || nr == 0 |
|
101 || nc == 0))); |
|
102 } |
|
103 |
578
|
104 // Make sure that all arguments have values. |
|
105 |
494
|
106 static int |
506
|
107 all_args_defined (const Octave_object& args) |
494
|
108 { |
506
|
109 int nargin = args.length (); |
|
110 |
|
111 while (--nargin > 0) |
494
|
112 { |
506
|
113 if (args(nargin).is_undefined ()) |
494
|
114 return 0; |
|
115 } |
|
116 return 1; |
|
117 } |
|
118 |
578
|
119 // Are any of the arguments `:'? |
|
120 |
494
|
121 static int |
506
|
122 any_arg_is_magic_colon (const Octave_object& args) |
494
|
123 { |
506
|
124 int nargin = args.length (); |
|
125 |
|
126 while (--nargin > 0) |
494
|
127 { |
506
|
128 if (args(nargin).const_type () == tree_constant_rep::magic_colon) |
494
|
129 return 1; |
|
130 } |
|
131 return 0; |
|
132 } |
|
133 |
581
|
134 // Expressions. |
|
135 |
|
136 tree_constant |
|
137 tree_expression::eval (int print) |
|
138 { |
|
139 panic ("invalid evaluation of generic expression"); |
|
140 return tree_constant (); |
|
141 } |
|
142 |
|
143 Octave_object |
|
144 tree_expression::eval (int print, int nargout, const Octave_object& args) |
|
145 { |
|
146 panic ("invalid evaluation of generic expression"); |
|
147 return Octave_object (); |
|
148 } |
494
|
149 |
578
|
150 // General matrices. This list type is much more work to handle than |
|
151 // constant matrices, but it allows us to construct matrices from |
|
152 // other matrices, variables, and functions. |
494
|
153 |
|
154 tree_matrix::~tree_matrix (void) |
|
155 { |
|
156 delete element; |
|
157 delete next; |
|
158 } |
|
159 |
|
160 tree_matrix * |
578
|
161 tree_matrix::chain (tree_expression *t, tree_matrix::dir d) |
494
|
162 { |
|
163 tree_matrix *tmp = new tree_matrix (t, d); |
|
164 tmp->next = this; |
|
165 return tmp; |
|
166 } |
|
167 |
|
168 tree_matrix * |
|
169 tree_matrix::reverse (void) |
|
170 { |
|
171 tree_matrix *list = this; |
|
172 tree_matrix *next; |
529
|
173 tree_matrix *prev = 0; |
|
174 |
|
175 while (list) |
494
|
176 { |
|
177 next = list->next; |
|
178 list->next = prev; |
|
179 prev = list; |
|
180 list = next; |
|
181 } |
|
182 return prev; |
|
183 } |
|
184 |
|
185 int |
|
186 tree_matrix::length (void) |
|
187 { |
|
188 tree_matrix *list = this; |
|
189 int len = 0; |
529
|
190 while (list) |
494
|
191 { |
|
192 len++; |
|
193 list = list->next; |
|
194 } |
|
195 return len; |
|
196 } |
|
197 |
|
198 tree_return_list * |
|
199 tree_matrix::to_return_list (void) |
|
200 { |
529
|
201 tree_return_list *retval = 0; |
578
|
202 |
494
|
203 tree_matrix *list; |
578
|
204 |
529
|
205 for (list = this; list; list = list->next) |
494
|
206 { |
|
207 tree_expression *elem = list->element; |
578
|
208 |
|
209 int is_id = elem->is_identifier (); |
|
210 |
|
211 int is_idx_expr = elem->is_index_expression (); |
|
212 |
|
213 if (is_id || is_idx_expr) |
494
|
214 { |
578
|
215 tree_index_expression *idx_expr; |
|
216 if (is_id) |
|
217 { |
|
218 tree_identifier *id = (tree_identifier *) elem; |
|
219 idx_expr = new tree_index_expression (id); |
|
220 } |
494
|
221 else |
578
|
222 idx_expr = (tree_index_expression *) elem; |
|
223 |
494
|
224 if (list == this) |
|
225 retval = new tree_return_list (idx_expr); |
|
226 else |
578
|
227 retval->append (idx_expr); |
494
|
228 } |
|
229 else |
|
230 { |
|
231 delete retval; |
529
|
232 retval = 0; |
494
|
233 break; |
|
234 } |
|
235 } |
|
236 |
|
237 return retval; |
|
238 } |
|
239 |
|
240 // Just about as ugly as it gets. |
|
241 |
|
242 struct const_matrix_list |
|
243 { |
581
|
244 tree_matrix::dir direction; |
494
|
245 tree_constant elem; |
|
246 int nr; |
|
247 int nc; |
|
248 }; |
|
249 |
|
250 // Less ugly than before, anyway. |
|
251 |
|
252 tree_constant |
|
253 tree_matrix::eval (int print) |
|
254 { |
|
255 tree_constant retval; |
|
256 |
|
257 if (error_state) |
|
258 return retval; |
|
259 |
|
260 // Just count the elements without looking at them. |
|
261 |
|
262 int total_len = length (); |
|
263 |
|
264 // Easier to deal with this later instead of a tree_matrix structure. |
|
265 |
|
266 const_matrix_list *list = new const_matrix_list [total_len]; |
|
267 |
|
268 // Stats we want to keep track of. |
|
269 |
|
270 int all_strings = 1; |
|
271 |
|
272 int found_complex = 0; |
|
273 |
|
274 int row_total = 0; |
|
275 int col_total = 0; |
|
276 |
|
277 int row_height = 0; |
|
278 |
|
279 int cols_this_row = 0; |
|
280 |
|
281 int first_row = 1; |
|
282 |
|
283 int empties_ok = user_pref.empty_list_elements_ok; |
|
284 |
|
285 tree_matrix *ptr = this; |
|
286 |
|
287 // Stuff for the result matrix or string. Declared here so that we |
|
288 // don't get warnings from gcc about the goto crossing the |
|
289 // initialization of these values. |
|
290 |
|
291 int put_row = 0; |
|
292 int put_col = 0; |
|
293 |
|
294 int prev_nr = 0; |
|
295 int prev_nc = 0; |
|
296 |
|
297 Matrix m; |
|
298 ComplexMatrix cm; |
|
299 |
529
|
300 char *string = 0; |
|
301 char *str_ptr = 0; |
494
|
302 |
|
303 // Eliminate empties and gather stats. |
|
304 |
|
305 int found_new_row_in_empties = 0; |
|
306 |
|
307 int len = 0; |
|
308 for (int i = 0; i < total_len; i++) |
|
309 { |
|
310 tree_expression *elem = ptr->element; |
529
|
311 if (! elem) |
494
|
312 { |
|
313 retval = tree_constant (Matrix ()); |
|
314 goto done; |
|
315 } |
|
316 |
581
|
317 Octave_object otmp = elem->eval (0); |
|
318 tree_constant tmp = otmp(0); |
494
|
319 if (error_state || tmp.is_undefined ()) |
|
320 { |
|
321 retval = tree_constant (); |
|
322 goto done; |
|
323 } |
|
324 |
|
325 int nr = tmp.rows (); |
|
326 int nc = tmp.columns (); |
|
327 |
581
|
328 dir direct = ptr->direction; |
494
|
329 |
|
330 if (nr == 0 || nc == 0) |
|
331 { |
|
332 if (empties_ok < 0) |
|
333 warning ("empty matrix found in matrix list"); |
|
334 else if (empties_ok == 0) |
|
335 { |
|
336 ::error ("empty matrix found in matrix list"); |
|
337 retval = tree_constant (); |
|
338 goto done; |
|
339 } |
|
340 |
|
341 if (direct == md_down) |
|
342 found_new_row_in_empties = 1; |
|
343 |
|
344 goto next; |
|
345 } |
|
346 |
|
347 if (found_new_row_in_empties) |
|
348 { |
|
349 found_new_row_in_empties = 0; |
581
|
350 list[len].direction = md_down; |
494
|
351 } |
|
352 else |
581
|
353 list[len].direction = direct; |
494
|
354 |
|
355 list[len].elem = tmp; |
|
356 list[len].nr = nr; |
|
357 list[len].nc = nc; |
|
358 |
610
|
359 if (all_strings && ! tmp.is_string ()) |
494
|
360 all_strings = 0; |
|
361 |
|
362 if (! found_complex && tmp.is_complex_type ()) |
|
363 found_complex = 1; |
|
364 |
|
365 len++; |
|
366 |
|
367 next: |
|
368 |
|
369 ptr = ptr->next; |
|
370 } |
|
371 |
|
372 // if (all_strings) |
|
373 // cerr << "all strings\n"; |
|
374 |
|
375 // Compute size of result matrix, and check to see that the dimensions |
|
376 // of all the elements will match up properly. |
|
377 |
|
378 for (i = 0; i < len; i++) |
|
379 { |
581
|
380 dir direct = list[i].direction; |
578
|
381 |
494
|
382 int nr = list[i].nr; |
|
383 int nc = list[i].nc; |
|
384 |
|
385 if (i == 0) |
|
386 { |
|
387 row_total = nr; |
|
388 col_total = nc; |
|
389 |
|
390 row_height = nr; |
|
391 cols_this_row = nc; |
|
392 } |
|
393 else |
|
394 { |
|
395 switch (direct) |
|
396 { |
|
397 case md_right: |
|
398 { |
|
399 if (nr != row_height) |
|
400 { |
|
401 ::error ("number of rows must match"); |
|
402 goto done; |
|
403 } |
|
404 else |
|
405 { |
|
406 cols_this_row += nc; |
|
407 if (first_row) |
|
408 col_total = cols_this_row; |
|
409 } |
|
410 } |
|
411 break; |
|
412 case md_down: |
|
413 { |
|
414 if (cols_this_row != col_total) |
|
415 { |
|
416 ::error ("number of columns must match"); |
|
417 goto done; |
|
418 } |
|
419 first_row = 0; |
|
420 row_total += nr; |
|
421 row_height = nr; |
|
422 cols_this_row = nc; |
|
423 } |
|
424 break; |
|
425 default: |
|
426 panic_impossible (); |
|
427 break; |
|
428 } |
|
429 } |
|
430 } |
|
431 |
|
432 // Don\'t forget to check to see if the last element will fit. |
|
433 |
|
434 if (cols_this_row != col_total) |
|
435 { |
|
436 ::error ("number of columns must match"); |
|
437 goto done; |
|
438 } |
|
439 |
|
440 // Now, extract the values from the individual elements and insert |
|
441 // them in the result matrix. |
|
442 |
|
443 if (all_strings && row_total == 1 && col_total > 0) |
|
444 { |
|
445 string = str_ptr = new char [col_total + 1]; |
|
446 string[col_total] = '\0'; |
|
447 } |
|
448 else if (found_complex) |
|
449 cm.resize (row_total, col_total, 0.0); |
|
450 else |
|
451 m.resize (row_total, col_total, 0.0); |
|
452 |
|
453 for (i = 0; i < len; i++) |
|
454 { |
|
455 tree_constant tmp = list[i].elem; |
|
456 tree_constant_rep::constant_type tmp_type = tmp.const_type (); |
|
457 |
|
458 int nr = list[i].nr; |
|
459 int nc = list[i].nc; |
|
460 |
|
461 if (nr == 0 || nc == 0) |
|
462 continue; |
|
463 |
|
464 if (i == 0) |
|
465 { |
|
466 put_row = 0; |
|
467 put_col = 0; |
|
468 } |
|
469 else |
|
470 { |
581
|
471 switch (list[i].direction) |
494
|
472 { |
|
473 case md_right: |
|
474 put_col += prev_nc; |
|
475 break; |
|
476 case md_down: |
|
477 put_row += prev_nr; |
|
478 put_col = 0; |
|
479 break; |
|
480 default: |
|
481 panic_impossible (); |
|
482 break; |
|
483 } |
|
484 } |
|
485 |
|
486 if (found_complex) |
|
487 { |
|
488 switch (tmp_type) |
|
489 { |
|
490 case tree_constant_rep::scalar_constant: |
|
491 cm (put_row, put_col) = tmp.double_value (); |
|
492 break; |
|
493 case tree_constant_rep::string_constant: |
529
|
494 if (all_strings && str_ptr) |
494
|
495 { |
|
496 memcpy (str_ptr, tmp.string_value (), nc); |
|
497 str_ptr += nc; |
|
498 break; |
|
499 } |
|
500 case tree_constant_rep::range_constant: |
|
501 tmp_type = tmp.force_numeric (1); |
|
502 if (tmp_type == tree_constant_rep::scalar_constant) |
|
503 m (put_row, put_col) = tmp.double_value (); |
|
504 else if (tmp_type == tree_constant_rep::matrix_constant) |
|
505 m.insert (tmp.matrix_value (), put_row, put_col); |
|
506 else |
|
507 panic_impossible (); |
|
508 break; |
|
509 case tree_constant_rep::matrix_constant: |
|
510 cm.insert (tmp.matrix_value (), put_row, put_col); |
|
511 break; |
|
512 case tree_constant_rep::complex_scalar_constant: |
|
513 cm (put_row, put_col) = tmp.complex_value (); |
|
514 break; |
|
515 case tree_constant_rep::complex_matrix_constant: |
|
516 cm.insert (tmp.complex_matrix_value (), put_row, put_col); |
|
517 break; |
|
518 case tree_constant_rep::magic_colon: |
|
519 default: |
|
520 panic_impossible (); |
|
521 break; |
|
522 } |
|
523 } |
|
524 else |
|
525 { |
|
526 switch (tmp_type) |
|
527 { |
|
528 case tree_constant_rep::scalar_constant: |
|
529 m (put_row, put_col) = tmp.double_value (); |
|
530 break; |
|
531 case tree_constant_rep::string_constant: |
529
|
532 if (all_strings && str_ptr) |
494
|
533 { |
|
534 memcpy (str_ptr, tmp.string_value (), nc); |
|
535 str_ptr += nc; |
|
536 break; |
|
537 } |
|
538 case tree_constant_rep::range_constant: |
|
539 tmp_type = tmp.force_numeric (1); |
|
540 if (tmp_type == tree_constant_rep::scalar_constant) |
|
541 m (put_row, put_col) = tmp.double_value (); |
|
542 else if (tmp_type == tree_constant_rep::matrix_constant) |
|
543 m.insert (tmp.matrix_value (), put_row, put_col); |
|
544 else |
|
545 panic_impossible (); |
|
546 break; |
|
547 case tree_constant_rep::matrix_constant: |
|
548 m.insert (tmp.matrix_value (), put_row, put_col); |
|
549 break; |
|
550 case tree_constant_rep::complex_scalar_constant: |
|
551 case tree_constant_rep::complex_matrix_constant: |
|
552 case tree_constant_rep::magic_colon: |
|
553 default: |
|
554 panic_impossible (); |
|
555 break; |
|
556 } |
|
557 } |
|
558 |
|
559 prev_nr = nr; |
|
560 prev_nc = nc; |
|
561 } |
|
562 |
529
|
563 if (all_strings && string) |
494
|
564 retval = tree_constant (string); |
|
565 else if (found_complex) |
|
566 retval = tree_constant (cm); |
|
567 else |
|
568 retval = tree_constant (m); |
|
569 |
|
570 done: |
|
571 delete [] list; |
|
572 |
|
573 return retval; |
|
574 } |
|
575 |
581
|
576 void |
|
577 tree_matrix::print_code (ostream& os) |
|
578 { |
|
579 print_code_indent (os); |
|
580 |
|
581 if (in_parens) |
|
582 os << "("; |
|
583 |
|
584 os << "["; |
|
585 |
|
586 tree_matrix *list = this; |
|
587 |
|
588 while (list) |
|
589 { |
|
590 list->element->print_code (os); |
|
591 |
|
592 list = list->next; |
|
593 |
|
594 if (list) |
|
595 { |
|
596 switch (list->direction) |
|
597 { |
|
598 case md_right: |
|
599 os << ", "; |
|
600 break; |
|
601 case md_down: |
|
602 os << "; "; |
|
603 break; |
|
604 default: |
|
605 break; |
|
606 } |
|
607 } |
|
608 } |
|
609 |
|
610 os << "]"; |
|
611 |
|
612 if (in_parens) |
|
613 os << ")"; |
|
614 } |
|
615 |
|
616 // A base class for objects that can be evaluated with argument lists. |
|
617 |
494
|
618 tree_constant |
506
|
619 tree_fvc::assign (tree_constant& t, const Octave_object& args) |
494
|
620 { |
|
621 panic_impossible (); |
|
622 return tree_constant (); |
|
623 } |
|
624 |
578
|
625 // Symbols from the symbol table. |
494
|
626 |
|
627 char * |
|
628 tree_identifier::name (void) const |
|
629 { |
581
|
630 return sym ? sym->name () : 0; |
494
|
631 } |
|
632 |
|
633 tree_identifier * |
|
634 tree_identifier::define (tree_constant *t) |
|
635 { |
|
636 int status = sym->define (t); |
581
|
637 return status ? this : 0; |
494
|
638 } |
|
639 |
|
640 tree_identifier * |
|
641 tree_identifier::define (tree_function *t) |
|
642 { |
|
643 int status = sym->define (t); |
581
|
644 return status ? this : 0; |
494
|
645 } |
|
646 |
|
647 void |
|
648 tree_identifier::document (char *s) |
|
649 { |
529
|
650 if (sym && s) |
494
|
651 { |
|
652 char *tmp = strsave (s); |
|
653 sym->document (tmp); |
|
654 } |
|
655 } |
|
656 |
|
657 tree_constant |
|
658 tree_identifier::assign (tree_constant& rhs) |
|
659 { |
|
660 int status = 0; |
|
661 |
|
662 if (rhs.is_defined ()) |
|
663 { |
|
664 if (! sym->is_defined ()) |
|
665 { |
|
666 if (! (sym->is_formal_parameter () |
|
667 || sym->is_linked_to_global ())) |
|
668 { |
|
669 link_to_builtin_variable (sym); |
|
670 } |
|
671 } |
|
672 else if (sym->is_function ()) |
|
673 { |
|
674 sym->clear (); |
|
675 } |
|
676 |
|
677 tree_constant *tmp = new tree_constant (rhs); |
|
678 status = sym->define (tmp); |
|
679 } |
|
680 |
|
681 if (status) |
|
682 return rhs; |
|
683 else |
|
684 return tree_constant (); |
|
685 } |
|
686 |
|
687 tree_constant |
506
|
688 tree_identifier::assign (tree_constant& rhs, const Octave_object& args) |
494
|
689 { |
|
690 tree_constant retval; |
|
691 |
|
692 if (rhs.is_defined ()) |
|
693 { |
|
694 if (! sym->is_defined ()) |
|
695 { |
|
696 if (! (sym->is_formal_parameter () |
|
697 || sym->is_linked_to_global ())) |
|
698 { |
|
699 link_to_builtin_variable (sym); |
|
700 } |
|
701 } |
|
702 else if (sym->is_function ()) |
|
703 { |
|
704 sym->clear (); |
|
705 } |
|
706 |
|
707 if (sym->is_variable () && sym->is_defined ()) |
|
708 { |
|
709 tree_fvc *tmp = sym->def (); |
506
|
710 retval = tmp->assign (rhs, args); |
494
|
711 } |
|
712 else |
|
713 { |
|
714 assert (! sym->is_defined ()); |
|
715 |
|
716 if (! user_pref.resize_on_range_error) |
|
717 { |
|
718 ::error ("indexed assignment to previously undefined variables"); |
|
719 ::error ("is only possible when resize_on_range_error is true"); |
|
720 return retval; |
|
721 } |
|
722 |
|
723 tree_constant *tmp = new tree_constant (); |
506
|
724 retval = tmp->assign (rhs, args); |
494
|
725 if (retval.is_defined ()) |
|
726 sym->define (tmp); |
|
727 } |
|
728 } |
|
729 |
|
730 return retval; |
|
731 } |
|
732 |
500
|
733 int |
|
734 tree_identifier::is_defined (void) |
|
735 { |
529
|
736 return (sym && sym->is_defined ()); |
500
|
737 } |
|
738 |
494
|
739 void |
578
|
740 tree_identifier::bump_value (tree_expression::type etype) |
494
|
741 { |
529
|
742 if (sym) |
494
|
743 { |
|
744 tree_fvc *tmp = sym->def (); |
529
|
745 if (tmp) |
494
|
746 tmp->bump_value (etype); |
|
747 } |
|
748 } |
|
749 |
|
750 void |
|
751 tree_identifier::eval_undefined_error (void) |
|
752 { |
|
753 char *nm = sym->name (); |
|
754 int l = line (); |
|
755 int c = column (); |
|
756 if (l == -1 && c == -1) |
581
|
757 ::error ("`%s' undefined", nm); |
494
|
758 else |
|
759 ::error ("`%s' undefined near line %d column %d", nm, l, c); |
|
760 } |
|
761 |
578
|
762 // Try to find a definition for an identifier. Here's how: |
|
763 // |
|
764 // * If the identifier is already defined and is a function defined |
|
765 // in an function file that has been modified since the last time |
|
766 // we parsed it, parse it again. |
|
767 // |
|
768 // * If the identifier is not defined, try to find a builtin |
|
769 // variable or an already compiled function with the same name. |
|
770 // |
|
771 // * If the identifier is still undefined, try looking for an |
|
772 // function file to parse. |
|
773 // |
|
774 // * On systems that support dynamic linking, we prefer .oct files |
|
775 // over .m files. |
|
776 |
494
|
777 tree_fvc * |
|
778 tree_identifier::do_lookup (int& script_file_executed) |
|
779 { |
|
780 script_file_executed = 0; |
|
781 |
581
|
782 int script_file_executed = lookup (sym); |
494
|
783 |
529
|
784 tree_fvc *ans = 0; |
494
|
785 |
|
786 if (! script_file_executed) |
|
787 ans = sym->def (); |
|
788 |
|
789 return ans; |
|
790 } |
|
791 |
|
792 void |
|
793 tree_identifier::mark_as_formal_parameter (void) |
|
794 { |
529
|
795 if (sym) |
494
|
796 sym->mark_as_formal_parameter (); |
|
797 } |
|
798 |
|
799 tree_constant |
|
800 tree_identifier::eval (int print) |
|
801 { |
|
802 tree_constant retval; |
|
803 |
|
804 if (error_state) |
|
805 return retval; |
|
806 |
|
807 int script_file_executed = 0; |
|
808 |
|
809 tree_fvc *ans = do_lookup (script_file_executed); |
|
810 |
|
811 if (! script_file_executed) |
|
812 { |
529
|
813 if (ans) |
494
|
814 { |
|
815 int nargout = maybe_do_ans_assign ? 0 : 1; |
|
816 |
508
|
817 int nargin = (ans->is_constant ()) ? 0 : 1; |
565
|
818 Octave_object tmp_args; |
|
819 tmp_args.resize (nargin); |
506
|
820 Octave_object tmp = ans->eval (0, nargout, tmp_args); |
500
|
821 |
|
822 if (tmp.length () > 0) |
|
823 retval = tmp(0); |
494
|
824 } |
529
|
825 else |
|
826 eval_undefined_error (); |
494
|
827 } |
|
828 |
|
829 if (! error_state && retval.is_defined ()) |
|
830 { |
|
831 if (maybe_do_ans_assign && ! ans->is_constant ()) |
|
832 { |
581
|
833 |
|
834 // XXX FIXME XXX -- need a procedure to do this, probably in |
|
835 // variables.cc, to isolate the code that does lookups... |
|
836 |
494
|
837 symbol_record *sr = global_sym_tab->lookup ("ans", 1, 0); |
|
838 |
529
|
839 assert (sr); |
|
840 |
494
|
841 tree_identifier *ans_id = new tree_identifier (sr); |
|
842 |
|
843 tree_constant *tmp = new tree_constant (retval); |
|
844 |
581
|
845 tree_simple_assignment_expression tmp_ass (ans_id, tmp, 0, 1); |
494
|
846 |
|
847 tmp_ass.eval (print); |
|
848 |
|
849 delete ans_id; // XXX FIXME XXX |
|
850 } |
|
851 else |
|
852 { |
|
853 if (print) |
|
854 { |
|
855 int pad_after = 0; |
|
856 if (user_pref.print_answer_id_name) |
|
857 { |
|
858 char *result_tag = name (); |
|
859 |
|
860 if (print_as_scalar (retval)) |
|
861 { |
|
862 ostrstream output_buf; |
|
863 output_buf << result_tag << " = " << ends; |
|
864 maybe_page_output (output_buf); |
|
865 } |
|
866 else |
|
867 { |
|
868 pad_after = 1; |
|
869 ostrstream output_buf; |
|
870 output_buf << result_tag << " =\n\n" << ends; |
|
871 maybe_page_output (output_buf); |
|
872 } |
|
873 } |
|
874 |
|
875 retval.eval (print); |
|
876 |
|
877 if (pad_after) |
|
878 { |
|
879 ostrstream output_buf; |
|
880 output_buf << "\n" << ends; |
|
881 maybe_page_output (output_buf); |
|
882 } |
|
883 } |
|
884 } |
|
885 } |
|
886 return retval; |
|
887 } |
|
888 |
500
|
889 Octave_object |
506
|
890 tree_identifier::eval (int print, int nargout, const Octave_object& args) |
494
|
891 { |
500
|
892 Octave_object retval; |
494
|
893 |
|
894 if (error_state) |
|
895 return retval; |
|
896 |
|
897 int script_file_executed = 0; |
|
898 |
|
899 tree_fvc *ans = do_lookup (script_file_executed); |
|
900 |
|
901 if (! script_file_executed) |
|
902 { |
529
|
903 if (ans) |
494
|
904 { |
|
905 if (maybe_do_ans_assign && nargout == 1) |
|
906 { |
|
907 |
|
908 // Don't count the output arguments that we create automatically. |
|
909 |
|
910 nargout = 0; |
|
911 |
506
|
912 retval = ans->eval (0, nargout, args); |
494
|
913 |
500
|
914 if (retval.length () > 0 && retval(0).is_defined ()) |
494
|
915 { |
581
|
916 |
|
917 // XXX FIXME XXX -- need a procedure to do this, probably in |
|
918 // variables.cc, to isolate the code that does lookups... |
|
919 |
494
|
920 symbol_record *sr = global_sym_tab->lookup ("ans", 1, 0); |
|
921 |
529
|
922 assert (sr); |
494
|
923 |
|
924 tree_identifier *ans_id = new tree_identifier (sr); |
|
925 |
500
|
926 tree_constant *tmp = new tree_constant (retval(0)); |
494
|
927 |
581
|
928 tree_simple_assignment_expression tmp_ass (ans_id, |
|
929 tmp, 0, 1); |
494
|
930 |
|
931 tmp_ass.eval (print); |
|
932 |
|
933 delete ans_id; // XXX FIXME XXX |
|
934 } |
|
935 } |
|
936 else |
506
|
937 retval = ans->eval (print, nargout, args); |
494
|
938 } |
529
|
939 else |
|
940 eval_undefined_error (); |
494
|
941 } |
|
942 |
|
943 return retval; |
|
944 } |
|
945 |
581
|
946 void |
|
947 tree_identifier::print_code (ostream& os) |
|
948 { |
|
949 print_code_indent (os); |
|
950 |
|
951 if (in_parens) |
|
952 os << "("; |
|
953 |
|
954 char *nm = name (); |
|
955 os << (nm) ? nm : "(null)"; |
|
956 |
|
957 if (in_parens) |
|
958 os << ")"; |
|
959 } |
|
960 |
|
961 // Index expressions. |
|
962 |
|
963 tree_index_expression::~tree_index_expression (void) |
|
964 { |
|
965 delete id; |
|
966 delete list; |
|
967 } |
|
968 |
|
969 tree_constant |
|
970 tree_index_expression::eval (int print) |
|
971 { |
|
972 tree_constant retval; |
|
973 |
|
974 if (error_state) |
|
975 return retval; |
|
976 |
|
977 if (list) |
|
978 { |
|
979 // Extract the arguments into a simple vector. |
|
980 Octave_object args = list->convert_to_const_vector (); |
|
981 // Don't pass null arguments. |
|
982 int nargin = args.length (); |
|
983 if (error_state) |
|
984 eval_error (); |
|
985 else if (nargin > 1 && all_args_defined (args)) |
|
986 { |
|
987 Octave_object tmp = id->eval (print, 1, args); |
|
988 |
|
989 if (error_state) |
|
990 eval_error (); |
|
991 |
|
992 if (tmp.length () > 0) |
|
993 retval = tmp(0); |
|
994 } |
|
995 } |
|
996 else |
|
997 { |
|
998 retval = id->eval (print); |
|
999 if (error_state) |
|
1000 eval_error (); |
|
1001 } |
|
1002 |
|
1003 return retval; |
|
1004 } |
|
1005 |
|
1006 Octave_object |
|
1007 tree_index_expression::eval (int print, int nargout, const Octave_object& args) |
|
1008 { |
|
1009 Octave_object retval; |
|
1010 |
|
1011 if (error_state) |
|
1012 return retval; |
|
1013 |
|
1014 if (list) |
|
1015 { |
|
1016 // Extract the arguments into a simple vector. |
|
1017 Octave_object args = list->convert_to_const_vector (); |
|
1018 // Don't pass null arguments. |
|
1019 if (error_state) |
|
1020 eval_error (); |
|
1021 else if (args.length () > 1 && all_args_defined (args)) |
|
1022 { |
|
1023 retval = id->eval (print, nargout, args); |
|
1024 if (error_state) |
|
1025 eval_error (); |
|
1026 } |
|
1027 } |
|
1028 else |
|
1029 { |
|
1030 Octave_object tmp_args; |
|
1031 retval = id->eval (print, nargout, tmp_args); |
|
1032 if (error_state) |
|
1033 eval_error (); |
|
1034 } |
|
1035 |
|
1036 return retval; |
|
1037 } |
|
1038 |
|
1039 void |
|
1040 tree_index_expression::eval_error (void) |
|
1041 { |
|
1042 if (error_state > 0) |
|
1043 { |
|
1044 int l = line (); |
|
1045 int c = column (); |
|
1046 char *fmt; |
|
1047 if (l != -1 && c != -1) |
|
1048 { |
|
1049 if (list) |
|
1050 fmt = "evaluating index expression near line %d, column %d"; |
|
1051 else |
|
1052 fmt = "evaluating expression near line %d, column %d"; |
|
1053 |
|
1054 ::error (fmt, l, c); |
|
1055 } |
|
1056 else |
|
1057 { |
|
1058 if (list) |
|
1059 ::error ("evaluating index expression"); |
|
1060 else |
|
1061 ::error ("evaluating expression"); |
|
1062 } |
|
1063 } |
|
1064 } |
|
1065 |
|
1066 void |
|
1067 tree_index_expression::print_code (ostream& os) |
|
1068 { |
|
1069 print_code_indent (os); |
|
1070 |
|
1071 if (in_parens) |
|
1072 os << "("; |
|
1073 |
|
1074 if (id) |
|
1075 id->print_code (os); |
|
1076 |
|
1077 if (list) |
|
1078 { |
|
1079 os << " ("; |
|
1080 list->print_code (os); |
|
1081 os << ")"; |
|
1082 } |
|
1083 |
|
1084 if (in_parens) |
|
1085 os << ")"; |
|
1086 } |
|
1087 |
|
1088 // Prefix expressions. |
|
1089 |
|
1090 tree_constant |
|
1091 tree_prefix_expression::eval (int print) |
|
1092 { |
|
1093 tree_constant retval; |
|
1094 |
|
1095 if (error_state) |
|
1096 return retval; |
|
1097 |
|
1098 if (id) |
|
1099 { |
|
1100 id->bump_value (etype); |
|
1101 retval = id->eval (print); |
|
1102 if (error_state) |
|
1103 { |
|
1104 retval = tree_constant (); |
|
1105 if (error_state) |
|
1106 eval_error (); |
|
1107 } |
|
1108 } |
|
1109 return retval; |
|
1110 } |
|
1111 |
|
1112 char * |
|
1113 tree_prefix_expression::oper (void) const |
|
1114 { |
|
1115 static char *op; |
|
1116 switch (etype) |
|
1117 { |
|
1118 case tree_expression::increment: op = "++"; break; |
|
1119 case tree_expression::decrement: op = "--"; break; |
|
1120 default: op = "unknown"; break; |
|
1121 } |
|
1122 return op; |
|
1123 } |
|
1124 |
|
1125 void |
|
1126 tree_prefix_expression::eval_error (void) |
|
1127 { |
|
1128 if (error_state > 0) |
|
1129 { |
|
1130 char *op = oper (); |
|
1131 |
|
1132 ::error ("evaluating prefix operator `%s' near line %d, column %d", |
|
1133 op, line (), column ()); |
|
1134 } |
|
1135 } |
|
1136 |
|
1137 void |
|
1138 tree_prefix_expression::print_code (ostream& os) |
|
1139 { |
|
1140 print_code_indent (os); |
|
1141 |
|
1142 if (in_parens) |
|
1143 os << "("; |
|
1144 |
|
1145 os << oper (); |
|
1146 |
|
1147 if (id) |
|
1148 id->print_code (os); |
|
1149 |
|
1150 if (in_parens) |
|
1151 os << ")"; |
|
1152 } |
|
1153 |
|
1154 // Postfix expressions. |
|
1155 |
|
1156 tree_constant |
|
1157 tree_postfix_expression::eval (int print) |
|
1158 { |
|
1159 tree_constant retval; |
|
1160 |
|
1161 if (error_state) |
|
1162 return retval; |
|
1163 |
|
1164 if (id) |
|
1165 { |
|
1166 retval = id->eval (print); |
|
1167 id->bump_value (etype); |
|
1168 if (error_state) |
|
1169 { |
|
1170 retval = tree_constant (); |
|
1171 if (error_state) |
|
1172 eval_error (); |
|
1173 } |
|
1174 } |
|
1175 return retval; |
|
1176 } |
|
1177 |
|
1178 char * |
|
1179 tree_postfix_expression::oper (void) const |
|
1180 { |
|
1181 static char *op; |
|
1182 switch (etype) |
|
1183 { |
|
1184 case tree_expression::increment: op = "++"; break; |
|
1185 case tree_expression::decrement: op = "--"; break; |
|
1186 default: op = "unknown"; break; |
|
1187 } |
|
1188 return op; |
|
1189 } |
|
1190 |
|
1191 void |
|
1192 tree_postfix_expression::eval_error (void) |
|
1193 { |
|
1194 if (error_state > 0) |
|
1195 { |
|
1196 char *op = oper (); |
|
1197 |
|
1198 ::error ("evaluating postfix operator `%s' near line %d, column %d", |
|
1199 op, line (), column ()); |
|
1200 } |
|
1201 } |
|
1202 |
|
1203 void |
|
1204 tree_postfix_expression::print_code (ostream& os) |
|
1205 { |
|
1206 print_code_indent (os); |
|
1207 |
|
1208 if (in_parens) |
|
1209 os << "("; |
|
1210 |
|
1211 if (id) |
|
1212 id->print_code (os); |
|
1213 |
|
1214 os << oper (); |
|
1215 |
|
1216 if (in_parens) |
|
1217 os << ")"; |
|
1218 } |
|
1219 |
|
1220 // Unary expressions. |
|
1221 |
|
1222 tree_constant |
|
1223 tree_unary_expression::eval (int print) |
|
1224 { |
|
1225 if (error_state) |
|
1226 return tree_constant (); |
|
1227 |
|
1228 tree_constant ans; |
|
1229 |
|
1230 switch (etype) |
|
1231 { |
|
1232 case tree_expression::not: |
|
1233 case tree_expression::uminus: |
|
1234 case tree_expression::hermitian: |
|
1235 case tree_expression::transpose: |
|
1236 if (op) |
|
1237 { |
|
1238 Octave_object tmp = op->eval (0); |
|
1239 tree_constant u = tmp(0); |
|
1240 if (error_state) |
|
1241 eval_error (); |
|
1242 else if (u.is_defined ()) |
|
1243 { |
|
1244 ans = do_unary_op (u, etype); |
|
1245 if (error_state) |
|
1246 { |
|
1247 ans = tree_constant (); |
|
1248 if (error_state) |
|
1249 eval_error (); |
|
1250 } |
|
1251 } |
|
1252 } |
|
1253 break; |
|
1254 default: |
|
1255 ::error ("unary operator %d not implemented", etype); |
|
1256 break; |
|
1257 } |
|
1258 |
|
1259 return ans; |
|
1260 } |
|
1261 |
|
1262 char * |
|
1263 tree_unary_expression::oper (void) const |
|
1264 { |
|
1265 static char *op; |
|
1266 switch (etype) |
|
1267 { |
|
1268 case tree_expression::not: op = "!"; break; |
|
1269 case tree_expression::uminus: op = "-"; break; |
|
1270 case tree_expression::hermitian: op = "'"; break; |
|
1271 case tree_expression::transpose: op = ".'"; break; |
|
1272 default: op = "unknown"; break; |
|
1273 } |
|
1274 return op; |
|
1275 } |
|
1276 |
|
1277 void |
|
1278 tree_unary_expression::eval_error (void) |
|
1279 { |
|
1280 if (error_state > 0) |
|
1281 { |
|
1282 char *op = oper (); |
|
1283 |
|
1284 ::error ("evaluating unary operator `%s' near line %d, column %d", |
|
1285 op, line (), column ()); |
|
1286 } |
|
1287 } |
|
1288 |
|
1289 void |
|
1290 tree_unary_expression::print_code (ostream& os) |
|
1291 { |
|
1292 print_code_indent (os); |
|
1293 |
|
1294 if (in_parens) |
|
1295 os << "("; |
|
1296 |
|
1297 switch (etype) |
|
1298 { |
|
1299 case tree_expression::not: |
|
1300 case tree_expression::uminus: |
|
1301 os << oper (); |
|
1302 if (op) |
|
1303 op->print_code (os); |
|
1304 break; |
|
1305 case tree_expression::hermitian: |
|
1306 case tree_expression::transpose: |
|
1307 if (op) |
|
1308 op->print_code (os); |
|
1309 os << oper (); |
|
1310 break; |
|
1311 default: |
|
1312 panic_impossible (); |
|
1313 break; |
|
1314 } |
|
1315 |
|
1316 if (in_parens) |
|
1317 os << ")"; |
|
1318 } |
|
1319 |
|
1320 // Binary expressions. |
|
1321 |
|
1322 tree_constant |
|
1323 tree_binary_expression::eval (int print) |
|
1324 { |
|
1325 if (error_state) |
|
1326 return tree_constant (); |
|
1327 |
|
1328 tree_constant ans; |
|
1329 switch (etype) |
|
1330 { |
|
1331 case tree_expression::add: |
|
1332 case tree_expression::subtract: |
|
1333 case tree_expression::multiply: |
|
1334 case tree_expression::el_mul: |
|
1335 case tree_expression::divide: |
|
1336 case tree_expression::el_div: |
|
1337 case tree_expression::leftdiv: |
|
1338 case tree_expression::el_leftdiv: |
|
1339 case tree_expression::power: |
|
1340 case tree_expression::elem_pow: |
|
1341 case tree_expression::cmp_lt: |
|
1342 case tree_expression::cmp_le: |
|
1343 case tree_expression::cmp_eq: |
|
1344 case tree_expression::cmp_ge: |
|
1345 case tree_expression::cmp_gt: |
|
1346 case tree_expression::cmp_ne: |
|
1347 case tree_expression::and: |
|
1348 case tree_expression::or: |
|
1349 if (op1) |
|
1350 { |
|
1351 Octave_object tmp = op1->eval (0); |
|
1352 tree_constant a = tmp(0); |
|
1353 if (error_state) |
|
1354 eval_error (); |
|
1355 else if (a.is_defined () && op2) |
|
1356 { |
|
1357 tmp = op2->eval (0); |
|
1358 tree_constant b = tmp (0); |
|
1359 if (error_state) |
|
1360 eval_error (); |
|
1361 else if (b.is_defined ()) |
|
1362 { |
|
1363 ans = do_binary_op (a, b, etype); |
|
1364 if (error_state) |
|
1365 { |
|
1366 ans = tree_constant (); |
|
1367 if (error_state) |
|
1368 eval_error (); |
|
1369 } |
|
1370 } |
|
1371 } |
|
1372 } |
|
1373 break; |
|
1374 case tree_expression::and_and: |
|
1375 case tree_expression::or_or: |
|
1376 { |
|
1377 int result = 0; |
|
1378 if (op1) |
|
1379 { |
|
1380 Octave_object tmp = op1->eval (0); |
|
1381 tree_constant a = tmp(0); |
|
1382 if (error_state) |
|
1383 { |
|
1384 eval_error (); |
|
1385 break; |
|
1386 } |
|
1387 |
|
1388 int a_true = a.is_true (); |
|
1389 if (error_state) |
|
1390 { |
|
1391 eval_error (); |
|
1392 break; |
|
1393 } |
|
1394 |
|
1395 if (a_true) |
|
1396 { |
|
1397 if (etype == tree_expression::or_or) |
|
1398 { |
|
1399 result = 1; |
|
1400 goto done; |
|
1401 } |
|
1402 } |
|
1403 else |
|
1404 { |
|
1405 if (etype == tree_expression::and_and) |
|
1406 { |
|
1407 result = 0; |
|
1408 goto done; |
|
1409 } |
|
1410 } |
|
1411 |
|
1412 if (op2) |
|
1413 { |
|
1414 tmp = op2->eval (0); |
|
1415 tree_constant b = tmp(0); |
|
1416 if (error_state) |
|
1417 { |
|
1418 eval_error (); |
|
1419 break; |
|
1420 } |
|
1421 |
|
1422 result = b.is_true (); |
|
1423 if (error_state) |
|
1424 { |
|
1425 eval_error (); |
|
1426 break; |
|
1427 } |
|
1428 } |
|
1429 } |
|
1430 done: |
|
1431 ans = tree_constant ((double) result); |
|
1432 } |
|
1433 break; |
|
1434 default: |
|
1435 ::error ("binary operator %d not implemented", etype); |
|
1436 break; |
|
1437 } |
|
1438 |
|
1439 return ans; |
|
1440 } |
|
1441 |
|
1442 char * |
|
1443 tree_binary_expression::oper (void) const |
|
1444 { |
|
1445 static char *op; |
|
1446 switch (etype) |
|
1447 { |
|
1448 case tree_expression::add: op = "+"; break; |
|
1449 case tree_expression::subtract: op = "-"; break; |
|
1450 case tree_expression::multiply: op = "*"; break; |
|
1451 case tree_expression::el_mul: op = ".*"; break; |
|
1452 case tree_expression::divide: op = "/"; break; |
|
1453 case tree_expression::el_div: op = "./"; break; |
|
1454 case tree_expression::leftdiv: op = "\\"; break; |
|
1455 case tree_expression::el_leftdiv: op = ".\\"; break; |
|
1456 case tree_expression::power: op = "^"; break; |
|
1457 case tree_expression::elem_pow: op = ".^"; break; |
|
1458 case tree_expression::cmp_lt: op = "<"; break; |
|
1459 case tree_expression::cmp_le: op = "<="; break; |
|
1460 case tree_expression::cmp_eq: op = "=="; break; |
|
1461 case tree_expression::cmp_ge: op = ">="; break; |
|
1462 case tree_expression::cmp_gt: op = ">"; break; |
|
1463 case tree_expression::cmp_ne: op = "!="; break; |
|
1464 case tree_expression::and_and: op = "&&"; break; |
|
1465 case tree_expression::or_or: op = "||"; break; |
|
1466 case tree_expression::and: op = "&"; break; |
|
1467 case tree_expression::or: op = "|"; break; |
|
1468 default: op = "unknown"; break; |
|
1469 } |
|
1470 return op; |
|
1471 } |
|
1472 |
|
1473 void |
|
1474 tree_binary_expression::eval_error (void) |
|
1475 { |
|
1476 if (error_state > 0) |
|
1477 { |
|
1478 char *op = oper (); |
|
1479 |
|
1480 ::error ("evaluating binary operator `%s' near line %d, column %d", |
|
1481 op, line (), column ()); |
|
1482 } |
|
1483 } |
|
1484 |
|
1485 void |
|
1486 tree_binary_expression::print_code (ostream& os) |
|
1487 { |
|
1488 print_code_indent (os); |
|
1489 |
|
1490 if (in_parens) |
|
1491 os << "("; |
|
1492 |
|
1493 if (op1) |
|
1494 op1->print_code (os); |
|
1495 |
|
1496 os << " " << oper () << " "; |
|
1497 |
|
1498 if (op2) |
|
1499 op2->print_code (os); |
|
1500 |
|
1501 if (in_parens) |
|
1502 os << ")"; |
|
1503 } |
|
1504 |
|
1505 // Assignment expressions. |
|
1506 |
|
1507 tree_constant |
|
1508 tree_assignment_expression::eval (int print) |
|
1509 { |
|
1510 panic ("invalid evaluation of generic expression"); |
|
1511 return tree_constant (); |
|
1512 } |
|
1513 |
|
1514 // Simple assignment expressions. |
|
1515 |
|
1516 tree_simple_assignment_expression::~tree_simple_assignment_expression (void) |
|
1517 { |
|
1518 if (! preserve) |
|
1519 { |
|
1520 delete lhs; |
|
1521 delete index; |
|
1522 } |
|
1523 delete rhs; |
|
1524 } |
|
1525 |
|
1526 tree_constant |
|
1527 tree_simple_assignment_expression::eval (int print) |
|
1528 { |
|
1529 assert (etype == tree_expression::assignment); |
|
1530 |
|
1531 tree_constant ans; |
|
1532 tree_constant retval; |
|
1533 |
|
1534 if (error_state) |
|
1535 return retval; |
|
1536 |
|
1537 if (rhs) |
|
1538 { |
|
1539 Octave_object tmp = rhs->eval (0); |
|
1540 tree_constant rhs_val = tmp(0); |
|
1541 if (error_state) |
|
1542 { |
|
1543 if (error_state) |
|
1544 eval_error (); |
|
1545 } |
|
1546 else if (! index) |
|
1547 { |
|
1548 ans = lhs->assign (rhs_val); |
|
1549 if (error_state) |
|
1550 eval_error (); |
|
1551 } |
|
1552 else |
|
1553 { |
|
1554 // Extract the arguments into a simple vector. |
|
1555 Octave_object args = index->convert_to_const_vector (); |
|
1556 |
|
1557 int nargin = args.length (); |
|
1558 |
|
1559 if (error_state) |
|
1560 eval_error (); |
|
1561 else if (nargin > 1) |
|
1562 { |
|
1563 ans = lhs->assign (rhs_val, args); |
|
1564 if (error_state) |
|
1565 eval_error (); |
|
1566 } |
|
1567 } |
|
1568 } |
|
1569 |
|
1570 if (! error_state && ans.is_defined ()) |
|
1571 { |
|
1572 int pad_after = 0; |
|
1573 if (print && user_pref.print_answer_id_name) |
|
1574 { |
|
1575 if (print_as_scalar (ans)) |
|
1576 { |
|
1577 ostrstream output_buf; |
|
1578 output_buf << lhs->name () << " = " << ends; |
|
1579 maybe_page_output (output_buf); |
|
1580 } |
|
1581 else |
|
1582 { |
|
1583 pad_after = 1; |
|
1584 ostrstream output_buf; |
|
1585 output_buf << lhs->name () << " =\n\n" << ends; |
|
1586 maybe_page_output (output_buf); |
|
1587 } |
|
1588 } |
|
1589 |
|
1590 retval = ans.eval (print); |
|
1591 |
|
1592 if (print && pad_after) |
|
1593 { |
|
1594 ostrstream output_buf; |
|
1595 output_buf << "\n" << ends; |
|
1596 maybe_page_output (output_buf); |
|
1597 } |
|
1598 } |
|
1599 |
|
1600 return retval; |
|
1601 } |
|
1602 |
|
1603 void |
|
1604 tree_simple_assignment_expression::eval_error (void) |
|
1605 { |
|
1606 if (error_state > 0) |
|
1607 { |
|
1608 int l = line (); |
|
1609 int c = column (); |
|
1610 if (l != -1 && c != -1) |
|
1611 ::error ("evaluating assignment expression near line %d, column %d", |
|
1612 l, c); |
|
1613 // else |
|
1614 // error ("evaluating assignment expression"); |
|
1615 } |
|
1616 } |
|
1617 |
|
1618 void |
|
1619 tree_simple_assignment_expression::print_code (ostream& os) |
|
1620 { |
|
1621 print_code_indent (os); |
|
1622 |
|
1623 if (in_parens) |
|
1624 os << "("; |
|
1625 |
|
1626 if (! is_ans_assign ()) |
|
1627 { |
|
1628 if (lhs) |
|
1629 lhs->print_code (os); |
|
1630 |
|
1631 if (index) |
|
1632 { |
|
1633 os << " ("; |
|
1634 index->print_code (os); |
|
1635 os << ")"; |
|
1636 } |
|
1637 |
|
1638 os << " = "; |
|
1639 } |
|
1640 |
|
1641 if (rhs) |
|
1642 rhs->print_code (os); |
|
1643 |
|
1644 if (in_parens) |
|
1645 os << ")"; |
|
1646 } |
|
1647 |
|
1648 // Multi-valued assignmnt expressions. |
|
1649 |
|
1650 tree_multi_assignment_expression::~tree_multi_assignment_expression (void) |
|
1651 { |
|
1652 delete lhs; |
|
1653 delete rhs; |
|
1654 } |
|
1655 |
|
1656 tree_constant |
|
1657 tree_multi_assignment_expression::eval (int print) |
|
1658 { |
|
1659 tree_constant retval; |
|
1660 |
|
1661 if (error_state) |
|
1662 return retval; |
|
1663 |
|
1664 Octave_object tmp_args; |
|
1665 Octave_object result = eval (print, 1, tmp_args); |
|
1666 |
|
1667 if (result.length () > 0) |
|
1668 retval = result(0); |
|
1669 |
|
1670 return retval; |
|
1671 } |
|
1672 |
|
1673 Octave_object |
|
1674 tree_multi_assignment_expression::eval (int print, int nargout, |
|
1675 const Octave_object& args) |
|
1676 { |
|
1677 assert (etype == tree_expression::multi_assignment); |
|
1678 |
|
1679 if (error_state || ! rhs) |
|
1680 return Octave_object (); |
|
1681 |
|
1682 nargout = lhs->length (); |
|
1683 Octave_object tmp_args; |
|
1684 Octave_object results = rhs->eval (0, nargout, tmp_args); |
|
1685 |
|
1686 if (error_state) |
|
1687 eval_error (); |
|
1688 |
|
1689 int ma_line = line (); |
|
1690 int ma_column = column (); |
|
1691 |
|
1692 if (results.length () > 0) |
|
1693 { |
|
1694 int i = 0; |
|
1695 int pad_after = 0; |
|
1696 int last_was_scalar_type = 0; |
|
1697 for (Pix p = lhs->first (); p != 0; lhs->next (p)) |
|
1698 { |
|
1699 tree_index_expression *lhs_expr = (*lhs) (p); |
|
1700 |
|
1701 if (i < nargout) |
|
1702 { |
|
1703 // XXX FIXME? XXX -- this is apparently the way Matlab works, but |
|
1704 // maybe we should have the option of skipping the assignment instead. |
|
1705 |
|
1706 tree_constant *tmp = 0; |
|
1707 if (results(i).is_undefined ()) |
|
1708 { |
|
1709 Matrix m; |
|
1710 tmp = new tree_constant (m); |
|
1711 } |
|
1712 else |
|
1713 tmp = new tree_constant (results(i)); |
|
1714 |
|
1715 tree_simple_assignment_expression tmp_expr |
|
1716 (lhs_expr, tmp, 1, 0, ma_line, ma_column); |
|
1717 |
|
1718 results(i) = tmp_expr.eval (0); // May change |
|
1719 |
|
1720 if (error_state) |
|
1721 break; |
|
1722 |
|
1723 if (print && pad_after) |
|
1724 { |
|
1725 ostrstream output_buf; |
|
1726 output_buf << "\n" << '\0'; |
|
1727 maybe_page_output (output_buf); |
|
1728 } |
|
1729 |
|
1730 if (print && user_pref.print_answer_id_name) |
|
1731 { |
|
1732 tree_identifier *tmp_id = lhs_expr->ident (); |
|
1733 char *tmp_nm = tmp_id->name (); |
|
1734 |
|
1735 if (print_as_scalar (results(i))) |
|
1736 { |
|
1737 ostrstream output_buf; |
|
1738 output_buf << tmp_nm << " = " << '\0'; |
|
1739 maybe_page_output (output_buf); |
|
1740 last_was_scalar_type = 1; |
|
1741 } |
|
1742 else |
|
1743 { |
|
1744 ostrstream output_buf; |
|
1745 output_buf << tmp_nm << " =\n\n" << '\0'; |
|
1746 maybe_page_output (output_buf); |
|
1747 last_was_scalar_type = 0; |
|
1748 } |
|
1749 } |
|
1750 |
|
1751 results(i).eval (print); |
|
1752 |
|
1753 pad_after++; |
|
1754 i++; |
|
1755 } |
|
1756 else |
|
1757 { |
|
1758 tree_simple_assignment_expression tmp_expr |
|
1759 (lhs_expr, 0, 1, 0, ma_line, ma_column); |
|
1760 |
|
1761 tmp_expr.eval (0); |
|
1762 |
|
1763 if (error_state) |
|
1764 break; |
|
1765 |
|
1766 if (last_was_scalar_type && i == 1) |
|
1767 pad_after = 0; |
|
1768 |
|
1769 break; |
|
1770 } |
|
1771 } |
|
1772 |
|
1773 if (print && pad_after) |
|
1774 { |
|
1775 ostrstream output_buf; |
|
1776 output_buf << "\n" << '\0'; |
|
1777 maybe_page_output (output_buf); |
|
1778 } |
|
1779 } |
|
1780 |
|
1781 return results; |
|
1782 } |
|
1783 |
|
1784 void |
|
1785 tree_multi_assignment_expression::eval_error (void) |
|
1786 { |
|
1787 if (error_state > 0) |
|
1788 ::error ("evaluating assignment expression near line %d, column %d", |
|
1789 line (), column ()); |
|
1790 } |
|
1791 |
|
1792 void |
|
1793 tree_multi_assignment_expression::print_code (ostream& os) |
|
1794 { |
|
1795 print_code_indent (os); |
|
1796 |
|
1797 if (in_parens) |
|
1798 os << "("; |
|
1799 |
|
1800 if (lhs) |
|
1801 { |
|
1802 int len = lhs->length (); |
|
1803 |
|
1804 if (len > 1) |
|
1805 os << "["; |
|
1806 |
|
1807 lhs->print_code (os); |
|
1808 |
|
1809 if (len > 1) |
|
1810 os << "]"; |
|
1811 } |
|
1812 |
|
1813 os << " = "; |
|
1814 |
|
1815 if (rhs) |
|
1816 rhs->print_code (os); |
|
1817 |
|
1818 if (in_parens) |
|
1819 os << ")"; |
|
1820 } |
|
1821 |
|
1822 // Colon expressions. |
|
1823 |
|
1824 tree_colon_expression * |
|
1825 tree_colon_expression::chain (tree_expression *t) |
|
1826 { |
|
1827 tree_colon_expression *retval = 0; |
|
1828 if (! op1 || op3) |
|
1829 ::error ("invalid colon expression"); |
|
1830 else |
|
1831 { |
|
1832 op3 = op2; // Stupid syntax. |
|
1833 op2 = t; |
|
1834 |
|
1835 retval = this; |
|
1836 } |
|
1837 return retval; |
|
1838 } |
|
1839 |
|
1840 tree_constant |
|
1841 tree_colon_expression::eval (int print) |
|
1842 { |
|
1843 tree_constant retval; |
|
1844 |
|
1845 if (error_state || ! op1 || ! op2) |
|
1846 return retval; |
|
1847 |
|
1848 Octave_object otmp = op1->eval (0); |
|
1849 tree_constant tmp = otmp(0); |
|
1850 |
|
1851 if (tmp.is_undefined ()) |
|
1852 { |
|
1853 eval_error ("invalid null value in colon expression"); |
|
1854 return retval; |
|
1855 } |
|
1856 |
|
1857 tmp = tmp.make_numeric (); |
|
1858 if (tmp.const_type () != tree_constant_rep::scalar_constant |
|
1859 && tmp.const_type () != tree_constant_rep::complex_scalar_constant) |
|
1860 { |
|
1861 eval_error ("base for colon expression must be a scalar"); |
|
1862 return retval; |
|
1863 } |
|
1864 double base = tmp.double_value (); |
|
1865 |
|
1866 otmp = op2->eval (0); |
|
1867 tmp = otmp(0); |
|
1868 |
|
1869 if (tmp.is_undefined ()) |
|
1870 { |
|
1871 eval_error ("invalid null value in colon expression"); |
|
1872 return retval; |
|
1873 } |
|
1874 |
|
1875 tmp = tmp.make_numeric (); |
|
1876 if (tmp.const_type () != tree_constant_rep::scalar_constant |
|
1877 && tmp.const_type () != tree_constant_rep::complex_scalar_constant) |
|
1878 { |
|
1879 eval_error ("limit for colon expression must be a scalar"); |
|
1880 return retval; |
|
1881 } |
|
1882 double limit = tmp.double_value (); |
|
1883 |
|
1884 double inc = 1.0; |
|
1885 if (op3) |
|
1886 { |
|
1887 otmp = op3->eval (0); |
|
1888 tmp = otmp(0); |
|
1889 |
|
1890 if (tmp.is_undefined ()) |
|
1891 { |
|
1892 eval_error ("invalid null value in colon expression"); |
|
1893 return retval; |
|
1894 } |
|
1895 |
|
1896 tmp = tmp.make_numeric (); |
|
1897 if (tmp.const_type () != tree_constant_rep::scalar_constant |
|
1898 && tmp.const_type () != tree_constant_rep::complex_scalar_constant) |
|
1899 { |
|
1900 eval_error ("increment for colon expression must be a scalar"); |
|
1901 return retval; |
|
1902 } |
|
1903 else |
|
1904 inc = tmp.double_value (); |
|
1905 } |
|
1906 |
|
1907 retval = tree_constant (base, limit, inc); |
|
1908 |
|
1909 if (error_state) |
|
1910 { |
|
1911 if (error_state) |
|
1912 eval_error ("evaluating colon expression"); |
|
1913 return tree_constant (); |
|
1914 } |
|
1915 |
|
1916 return retval; |
|
1917 } |
|
1918 |
|
1919 void |
|
1920 tree_colon_expression::eval_error (const char *s) |
|
1921 { |
|
1922 if (error_state > 0) |
|
1923 ::error ("%s near line %d column %d", s, line (), column ()); |
|
1924 } |
|
1925 |
|
1926 void |
|
1927 tree_colon_expression::print_code (ostream& os) |
|
1928 { |
|
1929 print_code_indent (os); |
|
1930 |
|
1931 if (in_parens) |
|
1932 os << "("; |
|
1933 |
|
1934 if (op1) |
|
1935 op1->print_code (os); |
|
1936 |
|
1937 // Stupid syntax. |
|
1938 |
|
1939 if (op3) |
|
1940 { |
|
1941 os << ":"; |
|
1942 op3->print_code (os); |
|
1943 } |
|
1944 |
|
1945 if (op2) |
|
1946 { |
|
1947 os << ":"; |
|
1948 op2->print_code (os); |
|
1949 } |
|
1950 |
|
1951 if (in_parens) |
|
1952 os << ")"; |
|
1953 } |
|
1954 |
|
1955 // Builtin functions. |
|
1956 |
|
1957 tree_builtin::tree_builtin (const char *nm) |
|
1958 { |
|
1959 nargin_max = -1; |
|
1960 nargout_max = -1; |
|
1961 is_mapper = 0; |
|
1962 fcn = 0; |
|
1963 if (nm) |
|
1964 my_name = strsave (nm); |
|
1965 } |
|
1966 |
|
1967 tree_builtin::tree_builtin (int i_max, int o_max, Mapper_fcn& m_fcn, |
|
1968 const char *nm) |
|
1969 { |
|
1970 nargin_max = i_max; |
|
1971 nargout_max = o_max; |
|
1972 mapper_fcn = m_fcn; |
|
1973 is_mapper = 1; |
|
1974 fcn = 0; |
|
1975 if (nm) |
|
1976 my_name = strsave (nm); |
|
1977 } |
|
1978 |
|
1979 tree_builtin::tree_builtin (int i_max, int o_max, Octave_builtin_fcn g_fcn, |
|
1980 const char *nm) |
|
1981 { |
|
1982 nargin_max = i_max; |
|
1983 nargout_max = o_max; |
|
1984 is_mapper = 0; |
|
1985 fcn = g_fcn; |
|
1986 if (nm) |
|
1987 my_name = strsave (nm); |
|
1988 } |
|
1989 |
|
1990 tree_constant |
|
1991 tree_builtin::eval (int print) |
|
1992 { |
|
1993 tree_constant retval; |
|
1994 |
|
1995 if (error_state) |
|
1996 return retval; |
|
1997 |
|
1998 if (fcn) |
|
1999 { |
|
2000 Octave_object args; |
|
2001 args(0) = tree_constant (my_name); |
|
2002 Octave_object tmp = (*fcn) (args, 1); |
|
2003 if (tmp.length () > 0) |
|
2004 retval = tmp(0); |
|
2005 } |
|
2006 else // Assume mapper function |
|
2007 ::error ("%s: argument expected", my_name); |
|
2008 |
|
2009 return retval; |
|
2010 } |
|
2011 |
|
2012 Octave_object |
|
2013 tree_builtin::eval (int print, int nargout, const Octave_object& args) |
|
2014 { |
|
2015 Octave_object retval; |
|
2016 |
|
2017 if (error_state) |
|
2018 return retval; |
|
2019 |
|
2020 int nargin = args.length (); |
|
2021 |
|
2022 if (fcn) |
|
2023 { |
|
2024 if (any_arg_is_magic_colon (args)) |
|
2025 ::error ("invalid use of colon in function argument list"); |
|
2026 else |
|
2027 retval = (*fcn) (args, nargout); |
|
2028 } |
|
2029 else if (is_mapper) |
|
2030 { |
|
2031 if (nargin > nargin_max) |
|
2032 ::error ("%s: too many arguments", my_name); |
|
2033 else if (nargin > 0 && args.length () > 0 && args(1).is_defined ()) |
|
2034 { |
|
2035 tree_constant tmp = args(1).mapper (mapper_fcn, 0); |
|
2036 retval.resize (1); |
|
2037 retval(0) = tmp; |
|
2038 } |
|
2039 } |
|
2040 else |
|
2041 panic_impossible (); |
|
2042 |
|
2043 return retval; |
|
2044 } |
|
2045 |
|
2046 int |
|
2047 tree_builtin::max_expected_args (void) |
|
2048 { |
|
2049 int ea = nargin_max; |
|
2050 if (nargin_max < 0) |
|
2051 ea = INT_MAX; |
|
2052 else |
|
2053 ea = nargin_max; |
|
2054 return ea; |
|
2055 } |
|
2056 |
578
|
2057 // User defined functions. |
494
|
2058 |
578
|
2059 #if 0 |
494
|
2060 tree_function * |
578
|
2061 tree_function::define (tree statement_list *t) |
494
|
2062 { |
|
2063 cmd_list = t; |
|
2064 return this; |
|
2065 } |
578
|
2066 #endif |
494
|
2067 |
|
2068 tree_function * |
|
2069 tree_function::define_param_list (tree_parameter_list *t) |
|
2070 { |
|
2071 param_list = t; |
508
|
2072 |
529
|
2073 if (param_list) |
508
|
2074 { |
|
2075 int len = param_list->length (); |
|
2076 int va_only = param_list->varargs_only (); |
|
2077 num_named_args = va_only ? len - 1 : len; |
|
2078 curr_va_arg_number = num_named_args; |
|
2079 } |
494
|
2080 |
|
2081 return this; |
|
2082 } |
|
2083 |
|
2084 tree_function * |
|
2085 tree_function::define_ret_list (tree_parameter_list *t) |
|
2086 { |
|
2087 ret_list = t; |
|
2088 return this; |
|
2089 } |
|
2090 |
|
2091 void |
|
2092 tree_function::stash_fcn_file_name (char *s) |
|
2093 { |
|
2094 delete [] file_name; |
|
2095 file_name = strsave (s); |
|
2096 } |
|
2097 |
|
2098 void |
|
2099 tree_function::mark_as_system_fcn_file (void) |
|
2100 { |
529
|
2101 if (file_name) |
494
|
2102 { |
|
2103 // We really should stash the whole path to the file we found, when we |
|
2104 // looked it up, to avoid possible race conditions... XXX FIXME XXX |
|
2105 // |
|
2106 // We probably also don't need to get the library directory every |
|
2107 // time, but since this function is only called when the function file |
|
2108 // is parsed, it probably doesn't matter that much. |
|
2109 |
|
2110 char *oct_lib = octave_lib_dir (); |
|
2111 int len = strlen (oct_lib); |
|
2112 |
|
2113 char *ff_name = fcn_file_in_path (file_name); |
|
2114 |
|
2115 if (strncmp (oct_lib, ff_name, len) == 0) |
|
2116 system_fcn_file = 1; |
|
2117 |
|
2118 delete [] ff_name; |
|
2119 } |
|
2120 else |
|
2121 system_fcn_file = 0; |
|
2122 } |
|
2123 |
|
2124 int |
|
2125 tree_function::takes_varargs (void) const |
|
2126 { |
529
|
2127 return (param_list && param_list->takes_varargs ()); |
494
|
2128 } |
|
2129 |
|
2130 tree_constant |
|
2131 tree_function::octave_va_arg (void) |
|
2132 { |
|
2133 tree_constant retval; |
|
2134 |
508
|
2135 if (curr_va_arg_number < num_args_passed) |
|
2136 retval = args_passed (++curr_va_arg_number); |
494
|
2137 else |
|
2138 ::error ("error getting arg number %d -- only %d provided", |
508
|
2139 curr_va_arg_number, num_args_passed-1); |
494
|
2140 |
|
2141 return retval; |
|
2142 } |
|
2143 |
|
2144 void |
|
2145 tree_function::stash_function_name (char *s) |
|
2146 { |
|
2147 delete [] fcn_name; |
|
2148 fcn_name = strsave (s); |
|
2149 } |
|
2150 |
|
2151 tree_constant |
|
2152 tree_function::eval (int print) |
|
2153 { |
|
2154 tree_constant retval; |
|
2155 |
529
|
2156 if (error_state || ! cmd_list) |
494
|
2157 return retval; |
|
2158 |
565
|
2159 Octave_object tmp_args; |
|
2160 tmp_args.resize (1); |
506
|
2161 Octave_object tmp = eval (print, 1, tmp_args); |
500
|
2162 |
|
2163 if (! error_state && tmp.length () > 0) |
|
2164 retval = tmp(0); |
494
|
2165 |
|
2166 return retval; |
|
2167 } |
|
2168 |
|
2169 // For unwind protect. |
|
2170 |
|
2171 static void |
|
2172 pop_symbol_table_context (void *table) |
|
2173 { |
|
2174 symbol_table *tmp = (symbol_table *) table; |
|
2175 tmp->pop_context (); |
|
2176 } |
|
2177 |
|
2178 static void |
|
2179 clear_symbol_table (void *table) |
|
2180 { |
|
2181 symbol_table *tmp = (symbol_table *) table; |
|
2182 tmp->clear (); |
|
2183 } |
|
2184 |
500
|
2185 Octave_object |
506
|
2186 tree_function::eval (int print, int nargout, const Octave_object& args) |
494
|
2187 { |
500
|
2188 Octave_object retval; |
494
|
2189 |
|
2190 if (error_state) |
|
2191 return retval; |
|
2192 |
529
|
2193 if (! cmd_list) |
494
|
2194 return retval; |
|
2195 |
506
|
2196 int nargin = args.length (); |
|
2197 |
494
|
2198 begin_unwind_frame ("func_eval"); |
|
2199 |
|
2200 unwind_protect_int (call_depth); |
|
2201 call_depth++; |
|
2202 |
|
2203 if (call_depth > 1) |
|
2204 { |
|
2205 sym_tab->push_context (); |
|
2206 add_unwind_protect (pop_symbol_table_context, (void *) sym_tab); |
|
2207 } |
|
2208 |
|
2209 // Force symbols to be undefined again when this function exits. |
|
2210 |
|
2211 add_unwind_protect (clear_symbol_table, (void *) sym_tab); |
|
2212 |
|
2213 // Save old and set current symbol table context, for eval_undefined_error(). |
|
2214 |
|
2215 unwind_protect_ptr (curr_sym_tab); |
|
2216 curr_sym_tab = sym_tab; |
|
2217 |
|
2218 unwind_protect_ptr (curr_function); |
|
2219 curr_function = this; |
|
2220 |
500
|
2221 // unwind_protect_ptr (args_passed); |
494
|
2222 args_passed = args; |
|
2223 |
|
2224 unwind_protect_int (num_args_passed); |
|
2225 num_args_passed = nargin; |
|
2226 |
|
2227 unwind_protect_int (num_named_args); |
508
|
2228 unwind_protect_int (curr_va_arg_number); |
494
|
2229 |
529
|
2230 if (param_list && ! param_list->varargs_only ()) |
494
|
2231 { |
506
|
2232 param_list->define_from_arg_vector (args); |
494
|
2233 if (error_state) |
|
2234 goto abort; |
|
2235 } |
|
2236 |
|
2237 // The following code is in a separate scope to avoid warnings from |
|
2238 // G++ about `goto abort' crossing the initialization of some |
|
2239 // variables. |
|
2240 |
|
2241 { |
|
2242 bind_nargin_and_nargout (sym_tab, nargin, nargout); |
|
2243 |
|
2244 // Evaluate the commands that make up the function. Always turn on |
|
2245 // printing for commands inside functions. Maybe this should be |
|
2246 // toggled by a user-leval variable? |
|
2247 |
|
2248 int pf = ! user_pref.silent_functions; |
|
2249 tree_constant last_computed_value = cmd_list->eval (pf); |
|
2250 |
|
2251 if (returning) |
|
2252 returning = 0; |
|
2253 |
|
2254 if (error_state) |
|
2255 { |
|
2256 traceback_error (); |
|
2257 goto abort; |
|
2258 } |
|
2259 |
|
2260 // Copy return values out. |
|
2261 |
529
|
2262 if (ret_list) |
494
|
2263 { |
|
2264 retval = ret_list->convert_to_const_vector (); |
|
2265 } |
|
2266 else if (user_pref.return_last_computed_value) |
|
2267 { |
500
|
2268 retval.resize (1); |
|
2269 retval(0) = last_computed_value; |
494
|
2270 } |
|
2271 } |
|
2272 |
|
2273 abort: |
|
2274 run_unwind_frame ("func_eval"); |
|
2275 |
|
2276 return retval; |
|
2277 } |
|
2278 |
|
2279 int |
|
2280 tree_function::max_expected_args (void) |
|
2281 { |
529
|
2282 if (param_list) |
494
|
2283 { |
|
2284 if (param_list->takes_varargs ()) |
|
2285 return -1; |
|
2286 else |
|
2287 return param_list->length () + 1; |
|
2288 } |
|
2289 else |
|
2290 return 1; |
|
2291 } |
|
2292 |
|
2293 void |
|
2294 tree_function::traceback_error (void) |
|
2295 { |
|
2296 if (error_state >= 0) |
|
2297 error_state = -1; |
|
2298 |
529
|
2299 if (fcn_name) |
494
|
2300 { |
529
|
2301 if (file_name) |
494
|
2302 ::error ("called from `%s' in file `%s'", fcn_name, file_name); |
|
2303 else |
|
2304 ::error ("called from `%s'", fcn_name); |
|
2305 } |
|
2306 else |
|
2307 { |
529
|
2308 if (file_name) |
494
|
2309 ::error ("called from file `%s'", file_name); |
|
2310 else |
|
2311 ::error ("called from `?unknown?'"); |
|
2312 } |
|
2313 } |
|
2314 |
581
|
2315 void |
|
2316 tree_function::print_code (ostream& os) |
|
2317 { |
|
2318 print_code_reset (); |
|
2319 |
|
2320 print_code_indent (os); |
|
2321 |
|
2322 os << "function "; |
|
2323 |
|
2324 if (ret_list) |
|
2325 { |
|
2326 int len = ret_list->length (); |
|
2327 |
|
2328 if (len > 1) |
|
2329 os << "["; |
|
2330 |
|
2331 ret_list->print_code (os); |
|
2332 |
|
2333 if (len > 1) |
|
2334 os << "]"; |
|
2335 |
|
2336 os << " = "; |
|
2337 } |
|
2338 |
|
2339 os << (fcn_name ? fcn_name : "(null)") << " "; |
|
2340 |
|
2341 if (param_list) |
|
2342 { |
|
2343 int len = param_list->length (); |
|
2344 if (len > 0) |
|
2345 os << "("; |
|
2346 |
|
2347 param_list->print_code (os); |
|
2348 |
|
2349 if (len > 0) |
|
2350 { |
|
2351 os << ")"; |
|
2352 print_code_new_line (os); |
|
2353 } |
|
2354 } |
|
2355 else |
|
2356 { |
|
2357 os << "()"; |
|
2358 print_code_new_line (os); |
|
2359 } |
|
2360 |
|
2361 if (cmd_list) |
|
2362 { |
|
2363 increment_indent_level (); |
|
2364 cmd_list->print_code (os); |
|
2365 } |
|
2366 |
|
2367 os << "endfunction"; |
|
2368 |
|
2369 print_code_new_line (os); |
|
2370 } |
|
2371 |
529
|
2372 DEFUN ("va_arg", Fva_arg, Sva_arg, 1, 1, |
|
2373 "va_arg (): return next argument in a function that takes a\n\ |
581
|
2374 variable number of parameters") |
529
|
2375 { |
|
2376 Octave_object retval; |
|
2377 |
|
2378 int nargin = args.length (); |
|
2379 |
|
2380 if (nargin == 1) |
|
2381 { |
|
2382 if (curr_function) |
|
2383 { |
|
2384 if (curr_function->takes_varargs ()) |
|
2385 retval = curr_function->octave_va_arg (); |
|
2386 else |
|
2387 { |
|
2388 error ("va_arg only valid within function taking variable"); |
|
2389 error ("number of arguments"); |
|
2390 } |
|
2391 } |
|
2392 else |
|
2393 error ("va_arg only valid within function body"); |
|
2394 } |
|
2395 else |
|
2396 print_usage ("va_arg"); |
|
2397 |
|
2398 return retval; |
|
2399 } |
|
2400 |
|
2401 DEFUN ("va_start", Fva_start, Sva_start, 1, 0, |
|
2402 "va_start (): reset the pointer to the list of optional arguments\n\ |
|
2403 to the beginning") |
|
2404 { |
|
2405 Octave_object retval; |
|
2406 |
|
2407 int nargin = args.length (); |
|
2408 |
|
2409 if (nargin == 1) |
|
2410 { |
|
2411 if (curr_function) |
|
2412 { |
|
2413 if (curr_function->takes_varargs ()) |
|
2414 curr_function->octave_va_start (); |
|
2415 else |
|
2416 { |
|
2417 error ("va_start only valid within function taking variable"); |
|
2418 error ("number of arguments"); |
|
2419 } |
|
2420 } |
|
2421 else |
|
2422 error ("va_start only valid within function body"); |
|
2423 } |
|
2424 else |
|
2425 print_usage ("va_start"); |
|
2426 |
|
2427 return retval; |
|
2428 } |
|
2429 |
494
|
2430 /* |
|
2431 ;;; Local Variables: *** |
|
2432 ;;; mode: C++ *** |
|
2433 ;;; page-delimiter: "^/\\*" *** |
|
2434 ;;; End: *** |
|
2435 */ |