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 { |
615
|
780 script_file_executed = lookup (sym); |
494
|
781 |
529
|
782 tree_fvc *ans = 0; |
494
|
783 |
|
784 if (! script_file_executed) |
|
785 ans = sym->def (); |
|
786 |
|
787 return ans; |
|
788 } |
|
789 |
|
790 void |
|
791 tree_identifier::mark_as_formal_parameter (void) |
|
792 { |
529
|
793 if (sym) |
494
|
794 sym->mark_as_formal_parameter (); |
|
795 } |
|
796 |
|
797 tree_constant |
|
798 tree_identifier::eval (int print) |
|
799 { |
|
800 tree_constant retval; |
|
801 |
|
802 if (error_state) |
|
803 return retval; |
|
804 |
|
805 int script_file_executed = 0; |
|
806 |
|
807 tree_fvc *ans = do_lookup (script_file_executed); |
|
808 |
|
809 if (! script_file_executed) |
|
810 { |
529
|
811 if (ans) |
494
|
812 { |
|
813 int nargout = maybe_do_ans_assign ? 0 : 1; |
|
814 |
508
|
815 int nargin = (ans->is_constant ()) ? 0 : 1; |
565
|
816 Octave_object tmp_args; |
|
817 tmp_args.resize (nargin); |
506
|
818 Octave_object tmp = ans->eval (0, nargout, tmp_args); |
500
|
819 |
|
820 if (tmp.length () > 0) |
|
821 retval = tmp(0); |
494
|
822 } |
529
|
823 else |
|
824 eval_undefined_error (); |
494
|
825 } |
|
826 |
|
827 if (! error_state && retval.is_defined ()) |
|
828 { |
|
829 if (maybe_do_ans_assign && ! ans->is_constant ()) |
|
830 { |
581
|
831 |
|
832 // XXX FIXME XXX -- need a procedure to do this, probably in |
|
833 // variables.cc, to isolate the code that does lookups... |
|
834 |
494
|
835 symbol_record *sr = global_sym_tab->lookup ("ans", 1, 0); |
|
836 |
529
|
837 assert (sr); |
|
838 |
494
|
839 tree_identifier *ans_id = new tree_identifier (sr); |
|
840 |
|
841 tree_constant *tmp = new tree_constant (retval); |
|
842 |
581
|
843 tree_simple_assignment_expression tmp_ass (ans_id, tmp, 0, 1); |
494
|
844 |
|
845 tmp_ass.eval (print); |
|
846 |
|
847 delete ans_id; // XXX FIXME XXX |
|
848 } |
|
849 else |
|
850 { |
|
851 if (print) |
|
852 { |
|
853 int pad_after = 0; |
|
854 if (user_pref.print_answer_id_name) |
|
855 { |
|
856 char *result_tag = name (); |
|
857 |
|
858 if (print_as_scalar (retval)) |
|
859 { |
|
860 ostrstream output_buf; |
|
861 output_buf << result_tag << " = " << ends; |
|
862 maybe_page_output (output_buf); |
|
863 } |
|
864 else |
|
865 { |
|
866 pad_after = 1; |
|
867 ostrstream output_buf; |
|
868 output_buf << result_tag << " =\n\n" << ends; |
|
869 maybe_page_output (output_buf); |
|
870 } |
|
871 } |
|
872 |
|
873 retval.eval (print); |
|
874 |
|
875 if (pad_after) |
|
876 { |
|
877 ostrstream output_buf; |
|
878 output_buf << "\n" << ends; |
|
879 maybe_page_output (output_buf); |
|
880 } |
|
881 } |
|
882 } |
|
883 } |
|
884 return retval; |
|
885 } |
|
886 |
500
|
887 Octave_object |
506
|
888 tree_identifier::eval (int print, int nargout, const Octave_object& args) |
494
|
889 { |
500
|
890 Octave_object retval; |
494
|
891 |
|
892 if (error_state) |
|
893 return retval; |
|
894 |
|
895 int script_file_executed = 0; |
|
896 |
|
897 tree_fvc *ans = do_lookup (script_file_executed); |
|
898 |
|
899 if (! script_file_executed) |
|
900 { |
529
|
901 if (ans) |
494
|
902 { |
|
903 if (maybe_do_ans_assign && nargout == 1) |
|
904 { |
|
905 |
|
906 // Don't count the output arguments that we create automatically. |
|
907 |
|
908 nargout = 0; |
|
909 |
506
|
910 retval = ans->eval (0, nargout, args); |
494
|
911 |
500
|
912 if (retval.length () > 0 && retval(0).is_defined ()) |
494
|
913 { |
581
|
914 |
|
915 // XXX FIXME XXX -- need a procedure to do this, probably in |
|
916 // variables.cc, to isolate the code that does lookups... |
|
917 |
494
|
918 symbol_record *sr = global_sym_tab->lookup ("ans", 1, 0); |
|
919 |
529
|
920 assert (sr); |
494
|
921 |
|
922 tree_identifier *ans_id = new tree_identifier (sr); |
|
923 |
500
|
924 tree_constant *tmp = new tree_constant (retval(0)); |
494
|
925 |
581
|
926 tree_simple_assignment_expression tmp_ass (ans_id, |
|
927 tmp, 0, 1); |
494
|
928 |
|
929 tmp_ass.eval (print); |
|
930 |
|
931 delete ans_id; // XXX FIXME XXX |
|
932 } |
|
933 } |
|
934 else |
506
|
935 retval = ans->eval (print, nargout, args); |
494
|
936 } |
529
|
937 else |
|
938 eval_undefined_error (); |
494
|
939 } |
|
940 |
|
941 return retval; |
|
942 } |
|
943 |
581
|
944 void |
|
945 tree_identifier::print_code (ostream& os) |
|
946 { |
|
947 print_code_indent (os); |
|
948 |
|
949 if (in_parens) |
|
950 os << "("; |
|
951 |
|
952 char *nm = name (); |
|
953 os << (nm) ? nm : "(null)"; |
|
954 |
|
955 if (in_parens) |
|
956 os << ")"; |
|
957 } |
|
958 |
|
959 // Index expressions. |
|
960 |
|
961 tree_index_expression::~tree_index_expression (void) |
|
962 { |
|
963 delete id; |
|
964 delete list; |
|
965 } |
|
966 |
|
967 tree_constant |
|
968 tree_index_expression::eval (int print) |
|
969 { |
|
970 tree_constant retval; |
|
971 |
|
972 if (error_state) |
|
973 return retval; |
|
974 |
|
975 if (list) |
|
976 { |
|
977 // Extract the arguments into a simple vector. |
|
978 Octave_object args = list->convert_to_const_vector (); |
|
979 // Don't pass null arguments. |
|
980 int nargin = args.length (); |
|
981 if (error_state) |
|
982 eval_error (); |
|
983 else if (nargin > 1 && all_args_defined (args)) |
|
984 { |
|
985 Octave_object tmp = id->eval (print, 1, args); |
|
986 |
|
987 if (error_state) |
|
988 eval_error (); |
|
989 |
|
990 if (tmp.length () > 0) |
|
991 retval = tmp(0); |
|
992 } |
|
993 } |
|
994 else |
|
995 { |
|
996 retval = id->eval (print); |
|
997 if (error_state) |
|
998 eval_error (); |
|
999 } |
|
1000 |
|
1001 return retval; |
|
1002 } |
|
1003 |
|
1004 Octave_object |
|
1005 tree_index_expression::eval (int print, int nargout, const Octave_object& args) |
|
1006 { |
|
1007 Octave_object retval; |
|
1008 |
|
1009 if (error_state) |
|
1010 return retval; |
|
1011 |
|
1012 if (list) |
|
1013 { |
|
1014 // Extract the arguments into a simple vector. |
|
1015 Octave_object args = list->convert_to_const_vector (); |
|
1016 // Don't pass null arguments. |
|
1017 if (error_state) |
|
1018 eval_error (); |
|
1019 else if (args.length () > 1 && all_args_defined (args)) |
|
1020 { |
|
1021 retval = id->eval (print, nargout, args); |
|
1022 if (error_state) |
|
1023 eval_error (); |
|
1024 } |
|
1025 } |
|
1026 else |
|
1027 { |
|
1028 Octave_object tmp_args; |
|
1029 retval = id->eval (print, nargout, tmp_args); |
|
1030 if (error_state) |
|
1031 eval_error (); |
|
1032 } |
|
1033 |
|
1034 return retval; |
|
1035 } |
|
1036 |
|
1037 void |
|
1038 tree_index_expression::eval_error (void) |
|
1039 { |
|
1040 if (error_state > 0) |
|
1041 { |
|
1042 int l = line (); |
|
1043 int c = column (); |
|
1044 char *fmt; |
|
1045 if (l != -1 && c != -1) |
|
1046 { |
|
1047 if (list) |
|
1048 fmt = "evaluating index expression near line %d, column %d"; |
|
1049 else |
|
1050 fmt = "evaluating expression near line %d, column %d"; |
|
1051 |
|
1052 ::error (fmt, l, c); |
|
1053 } |
|
1054 else |
|
1055 { |
|
1056 if (list) |
|
1057 ::error ("evaluating index expression"); |
|
1058 else |
|
1059 ::error ("evaluating expression"); |
|
1060 } |
|
1061 } |
|
1062 } |
|
1063 |
|
1064 void |
|
1065 tree_index_expression::print_code (ostream& os) |
|
1066 { |
|
1067 print_code_indent (os); |
|
1068 |
|
1069 if (in_parens) |
|
1070 os << "("; |
|
1071 |
|
1072 if (id) |
|
1073 id->print_code (os); |
|
1074 |
|
1075 if (list) |
|
1076 { |
|
1077 os << " ("; |
|
1078 list->print_code (os); |
|
1079 os << ")"; |
|
1080 } |
|
1081 |
|
1082 if (in_parens) |
|
1083 os << ")"; |
|
1084 } |
|
1085 |
|
1086 // Prefix expressions. |
|
1087 |
|
1088 tree_constant |
|
1089 tree_prefix_expression::eval (int print) |
|
1090 { |
|
1091 tree_constant retval; |
|
1092 |
|
1093 if (error_state) |
|
1094 return retval; |
|
1095 |
|
1096 if (id) |
|
1097 { |
|
1098 id->bump_value (etype); |
|
1099 retval = id->eval (print); |
|
1100 if (error_state) |
|
1101 { |
|
1102 retval = tree_constant (); |
|
1103 if (error_state) |
|
1104 eval_error (); |
|
1105 } |
|
1106 } |
|
1107 return retval; |
|
1108 } |
|
1109 |
|
1110 char * |
|
1111 tree_prefix_expression::oper (void) const |
|
1112 { |
|
1113 static char *op; |
|
1114 switch (etype) |
|
1115 { |
|
1116 case tree_expression::increment: op = "++"; break; |
|
1117 case tree_expression::decrement: op = "--"; break; |
|
1118 default: op = "unknown"; break; |
|
1119 } |
|
1120 return op; |
|
1121 } |
|
1122 |
|
1123 void |
|
1124 tree_prefix_expression::eval_error (void) |
|
1125 { |
|
1126 if (error_state > 0) |
|
1127 { |
|
1128 char *op = oper (); |
|
1129 |
|
1130 ::error ("evaluating prefix operator `%s' near line %d, column %d", |
|
1131 op, line (), column ()); |
|
1132 } |
|
1133 } |
|
1134 |
|
1135 void |
|
1136 tree_prefix_expression::print_code (ostream& os) |
|
1137 { |
|
1138 print_code_indent (os); |
|
1139 |
|
1140 if (in_parens) |
|
1141 os << "("; |
|
1142 |
|
1143 os << oper (); |
|
1144 |
|
1145 if (id) |
|
1146 id->print_code (os); |
|
1147 |
|
1148 if (in_parens) |
|
1149 os << ")"; |
|
1150 } |
|
1151 |
|
1152 // Postfix expressions. |
|
1153 |
|
1154 tree_constant |
|
1155 tree_postfix_expression::eval (int print) |
|
1156 { |
|
1157 tree_constant retval; |
|
1158 |
|
1159 if (error_state) |
|
1160 return retval; |
|
1161 |
|
1162 if (id) |
|
1163 { |
|
1164 retval = id->eval (print); |
|
1165 id->bump_value (etype); |
|
1166 if (error_state) |
|
1167 { |
|
1168 retval = tree_constant (); |
|
1169 if (error_state) |
|
1170 eval_error (); |
|
1171 } |
|
1172 } |
|
1173 return retval; |
|
1174 } |
|
1175 |
|
1176 char * |
|
1177 tree_postfix_expression::oper (void) const |
|
1178 { |
|
1179 static char *op; |
|
1180 switch (etype) |
|
1181 { |
|
1182 case tree_expression::increment: op = "++"; break; |
|
1183 case tree_expression::decrement: op = "--"; break; |
|
1184 default: op = "unknown"; break; |
|
1185 } |
|
1186 return op; |
|
1187 } |
|
1188 |
|
1189 void |
|
1190 tree_postfix_expression::eval_error (void) |
|
1191 { |
|
1192 if (error_state > 0) |
|
1193 { |
|
1194 char *op = oper (); |
|
1195 |
|
1196 ::error ("evaluating postfix operator `%s' near line %d, column %d", |
|
1197 op, line (), column ()); |
|
1198 } |
|
1199 } |
|
1200 |
|
1201 void |
|
1202 tree_postfix_expression::print_code (ostream& os) |
|
1203 { |
|
1204 print_code_indent (os); |
|
1205 |
|
1206 if (in_parens) |
|
1207 os << "("; |
|
1208 |
|
1209 if (id) |
|
1210 id->print_code (os); |
|
1211 |
|
1212 os << oper (); |
|
1213 |
|
1214 if (in_parens) |
|
1215 os << ")"; |
|
1216 } |
|
1217 |
|
1218 // Unary expressions. |
|
1219 |
|
1220 tree_constant |
|
1221 tree_unary_expression::eval (int print) |
|
1222 { |
|
1223 if (error_state) |
|
1224 return tree_constant (); |
|
1225 |
|
1226 tree_constant ans; |
|
1227 |
|
1228 switch (etype) |
|
1229 { |
|
1230 case tree_expression::not: |
|
1231 case tree_expression::uminus: |
|
1232 case tree_expression::hermitian: |
|
1233 case tree_expression::transpose: |
|
1234 if (op) |
|
1235 { |
|
1236 Octave_object tmp = op->eval (0); |
|
1237 tree_constant u = tmp(0); |
|
1238 if (error_state) |
|
1239 eval_error (); |
|
1240 else if (u.is_defined ()) |
|
1241 { |
|
1242 ans = do_unary_op (u, etype); |
|
1243 if (error_state) |
|
1244 { |
|
1245 ans = tree_constant (); |
|
1246 if (error_state) |
|
1247 eval_error (); |
|
1248 } |
|
1249 } |
|
1250 } |
|
1251 break; |
|
1252 default: |
|
1253 ::error ("unary operator %d not implemented", etype); |
|
1254 break; |
|
1255 } |
|
1256 |
|
1257 return ans; |
|
1258 } |
|
1259 |
|
1260 char * |
|
1261 tree_unary_expression::oper (void) const |
|
1262 { |
|
1263 static char *op; |
|
1264 switch (etype) |
|
1265 { |
|
1266 case tree_expression::not: op = "!"; break; |
|
1267 case tree_expression::uminus: op = "-"; break; |
|
1268 case tree_expression::hermitian: op = "'"; break; |
|
1269 case tree_expression::transpose: op = ".'"; break; |
|
1270 default: op = "unknown"; break; |
|
1271 } |
|
1272 return op; |
|
1273 } |
|
1274 |
|
1275 void |
|
1276 tree_unary_expression::eval_error (void) |
|
1277 { |
|
1278 if (error_state > 0) |
|
1279 { |
|
1280 char *op = oper (); |
|
1281 |
|
1282 ::error ("evaluating unary operator `%s' near line %d, column %d", |
|
1283 op, line (), column ()); |
|
1284 } |
|
1285 } |
|
1286 |
|
1287 void |
|
1288 tree_unary_expression::print_code (ostream& os) |
|
1289 { |
|
1290 print_code_indent (os); |
|
1291 |
|
1292 if (in_parens) |
|
1293 os << "("; |
|
1294 |
|
1295 switch (etype) |
|
1296 { |
|
1297 case tree_expression::not: |
|
1298 case tree_expression::uminus: |
|
1299 os << oper (); |
|
1300 if (op) |
|
1301 op->print_code (os); |
|
1302 break; |
|
1303 case tree_expression::hermitian: |
|
1304 case tree_expression::transpose: |
|
1305 if (op) |
|
1306 op->print_code (os); |
|
1307 os << oper (); |
|
1308 break; |
|
1309 default: |
|
1310 panic_impossible (); |
|
1311 break; |
|
1312 } |
|
1313 |
|
1314 if (in_parens) |
|
1315 os << ")"; |
|
1316 } |
|
1317 |
|
1318 // Binary expressions. |
|
1319 |
|
1320 tree_constant |
|
1321 tree_binary_expression::eval (int print) |
|
1322 { |
|
1323 if (error_state) |
|
1324 return tree_constant (); |
|
1325 |
|
1326 tree_constant ans; |
|
1327 switch (etype) |
|
1328 { |
|
1329 case tree_expression::add: |
|
1330 case tree_expression::subtract: |
|
1331 case tree_expression::multiply: |
|
1332 case tree_expression::el_mul: |
|
1333 case tree_expression::divide: |
|
1334 case tree_expression::el_div: |
|
1335 case tree_expression::leftdiv: |
|
1336 case tree_expression::el_leftdiv: |
|
1337 case tree_expression::power: |
|
1338 case tree_expression::elem_pow: |
|
1339 case tree_expression::cmp_lt: |
|
1340 case tree_expression::cmp_le: |
|
1341 case tree_expression::cmp_eq: |
|
1342 case tree_expression::cmp_ge: |
|
1343 case tree_expression::cmp_gt: |
|
1344 case tree_expression::cmp_ne: |
|
1345 case tree_expression::and: |
|
1346 case tree_expression::or: |
|
1347 if (op1) |
|
1348 { |
|
1349 Octave_object tmp = op1->eval (0); |
|
1350 tree_constant a = tmp(0); |
|
1351 if (error_state) |
|
1352 eval_error (); |
|
1353 else if (a.is_defined () && op2) |
|
1354 { |
|
1355 tmp = op2->eval (0); |
|
1356 tree_constant b = tmp (0); |
|
1357 if (error_state) |
|
1358 eval_error (); |
|
1359 else if (b.is_defined ()) |
|
1360 { |
|
1361 ans = do_binary_op (a, b, etype); |
|
1362 if (error_state) |
|
1363 { |
|
1364 ans = tree_constant (); |
|
1365 if (error_state) |
|
1366 eval_error (); |
|
1367 } |
|
1368 } |
|
1369 } |
|
1370 } |
|
1371 break; |
|
1372 case tree_expression::and_and: |
|
1373 case tree_expression::or_or: |
|
1374 { |
|
1375 int result = 0; |
|
1376 if (op1) |
|
1377 { |
|
1378 Octave_object tmp = op1->eval (0); |
|
1379 tree_constant a = tmp(0); |
|
1380 if (error_state) |
|
1381 { |
|
1382 eval_error (); |
|
1383 break; |
|
1384 } |
|
1385 |
|
1386 int a_true = a.is_true (); |
|
1387 if (error_state) |
|
1388 { |
|
1389 eval_error (); |
|
1390 break; |
|
1391 } |
|
1392 |
|
1393 if (a_true) |
|
1394 { |
|
1395 if (etype == tree_expression::or_or) |
|
1396 { |
|
1397 result = 1; |
|
1398 goto done; |
|
1399 } |
|
1400 } |
|
1401 else |
|
1402 { |
|
1403 if (etype == tree_expression::and_and) |
|
1404 { |
|
1405 result = 0; |
|
1406 goto done; |
|
1407 } |
|
1408 } |
|
1409 |
|
1410 if (op2) |
|
1411 { |
|
1412 tmp = op2->eval (0); |
|
1413 tree_constant b = tmp(0); |
|
1414 if (error_state) |
|
1415 { |
|
1416 eval_error (); |
|
1417 break; |
|
1418 } |
|
1419 |
|
1420 result = b.is_true (); |
|
1421 if (error_state) |
|
1422 { |
|
1423 eval_error (); |
|
1424 break; |
|
1425 } |
|
1426 } |
|
1427 } |
|
1428 done: |
|
1429 ans = tree_constant ((double) result); |
|
1430 } |
|
1431 break; |
|
1432 default: |
|
1433 ::error ("binary operator %d not implemented", etype); |
|
1434 break; |
|
1435 } |
|
1436 |
|
1437 return ans; |
|
1438 } |
|
1439 |
|
1440 char * |
|
1441 tree_binary_expression::oper (void) const |
|
1442 { |
|
1443 static char *op; |
|
1444 switch (etype) |
|
1445 { |
|
1446 case tree_expression::add: op = "+"; break; |
|
1447 case tree_expression::subtract: op = "-"; break; |
|
1448 case tree_expression::multiply: op = "*"; break; |
|
1449 case tree_expression::el_mul: op = ".*"; break; |
|
1450 case tree_expression::divide: op = "/"; break; |
|
1451 case tree_expression::el_div: op = "./"; break; |
|
1452 case tree_expression::leftdiv: op = "\\"; break; |
|
1453 case tree_expression::el_leftdiv: op = ".\\"; break; |
|
1454 case tree_expression::power: op = "^"; break; |
|
1455 case tree_expression::elem_pow: op = ".^"; break; |
|
1456 case tree_expression::cmp_lt: op = "<"; break; |
|
1457 case tree_expression::cmp_le: op = "<="; break; |
|
1458 case tree_expression::cmp_eq: op = "=="; break; |
|
1459 case tree_expression::cmp_ge: op = ">="; break; |
|
1460 case tree_expression::cmp_gt: op = ">"; break; |
|
1461 case tree_expression::cmp_ne: op = "!="; break; |
|
1462 case tree_expression::and_and: op = "&&"; break; |
|
1463 case tree_expression::or_or: op = "||"; break; |
|
1464 case tree_expression::and: op = "&"; break; |
|
1465 case tree_expression::or: op = "|"; break; |
|
1466 default: op = "unknown"; break; |
|
1467 } |
|
1468 return op; |
|
1469 } |
|
1470 |
|
1471 void |
|
1472 tree_binary_expression::eval_error (void) |
|
1473 { |
|
1474 if (error_state > 0) |
|
1475 { |
|
1476 char *op = oper (); |
|
1477 |
|
1478 ::error ("evaluating binary operator `%s' near line %d, column %d", |
|
1479 op, line (), column ()); |
|
1480 } |
|
1481 } |
|
1482 |
|
1483 void |
|
1484 tree_binary_expression::print_code (ostream& os) |
|
1485 { |
|
1486 print_code_indent (os); |
|
1487 |
|
1488 if (in_parens) |
|
1489 os << "("; |
|
1490 |
|
1491 if (op1) |
|
1492 op1->print_code (os); |
|
1493 |
|
1494 os << " " << oper () << " "; |
|
1495 |
|
1496 if (op2) |
|
1497 op2->print_code (os); |
|
1498 |
|
1499 if (in_parens) |
|
1500 os << ")"; |
|
1501 } |
|
1502 |
|
1503 // Assignment expressions. |
|
1504 |
|
1505 tree_constant |
|
1506 tree_assignment_expression::eval (int print) |
|
1507 { |
|
1508 panic ("invalid evaluation of generic expression"); |
|
1509 return tree_constant (); |
|
1510 } |
|
1511 |
|
1512 // Simple assignment expressions. |
|
1513 |
|
1514 tree_simple_assignment_expression::~tree_simple_assignment_expression (void) |
|
1515 { |
|
1516 if (! preserve) |
|
1517 { |
|
1518 delete lhs; |
|
1519 delete index; |
|
1520 } |
|
1521 delete rhs; |
|
1522 } |
|
1523 |
|
1524 tree_constant |
|
1525 tree_simple_assignment_expression::eval (int print) |
|
1526 { |
|
1527 assert (etype == tree_expression::assignment); |
|
1528 |
|
1529 tree_constant ans; |
|
1530 tree_constant retval; |
|
1531 |
|
1532 if (error_state) |
|
1533 return retval; |
|
1534 |
|
1535 if (rhs) |
|
1536 { |
|
1537 Octave_object tmp = rhs->eval (0); |
|
1538 tree_constant rhs_val = tmp(0); |
|
1539 if (error_state) |
|
1540 { |
|
1541 if (error_state) |
|
1542 eval_error (); |
|
1543 } |
|
1544 else if (! index) |
|
1545 { |
|
1546 ans = lhs->assign (rhs_val); |
|
1547 if (error_state) |
|
1548 eval_error (); |
|
1549 } |
|
1550 else |
|
1551 { |
|
1552 // Extract the arguments into a simple vector. |
|
1553 Octave_object args = index->convert_to_const_vector (); |
|
1554 |
|
1555 int nargin = args.length (); |
|
1556 |
|
1557 if (error_state) |
|
1558 eval_error (); |
|
1559 else if (nargin > 1) |
|
1560 { |
|
1561 ans = lhs->assign (rhs_val, args); |
|
1562 if (error_state) |
|
1563 eval_error (); |
|
1564 } |
|
1565 } |
|
1566 } |
|
1567 |
|
1568 if (! error_state && ans.is_defined ()) |
|
1569 { |
|
1570 int pad_after = 0; |
|
1571 if (print && user_pref.print_answer_id_name) |
|
1572 { |
|
1573 if (print_as_scalar (ans)) |
|
1574 { |
|
1575 ostrstream output_buf; |
|
1576 output_buf << lhs->name () << " = " << ends; |
|
1577 maybe_page_output (output_buf); |
|
1578 } |
|
1579 else |
|
1580 { |
|
1581 pad_after = 1; |
|
1582 ostrstream output_buf; |
|
1583 output_buf << lhs->name () << " =\n\n" << ends; |
|
1584 maybe_page_output (output_buf); |
|
1585 } |
|
1586 } |
|
1587 |
|
1588 retval = ans.eval (print); |
|
1589 |
|
1590 if (print && pad_after) |
|
1591 { |
|
1592 ostrstream output_buf; |
|
1593 output_buf << "\n" << ends; |
|
1594 maybe_page_output (output_buf); |
|
1595 } |
|
1596 } |
|
1597 |
|
1598 return retval; |
|
1599 } |
|
1600 |
|
1601 void |
|
1602 tree_simple_assignment_expression::eval_error (void) |
|
1603 { |
|
1604 if (error_state > 0) |
|
1605 { |
|
1606 int l = line (); |
|
1607 int c = column (); |
|
1608 if (l != -1 && c != -1) |
|
1609 ::error ("evaluating assignment expression near line %d, column %d", |
|
1610 l, c); |
|
1611 // else |
|
1612 // error ("evaluating assignment expression"); |
|
1613 } |
|
1614 } |
|
1615 |
|
1616 void |
|
1617 tree_simple_assignment_expression::print_code (ostream& os) |
|
1618 { |
|
1619 print_code_indent (os); |
|
1620 |
|
1621 if (in_parens) |
|
1622 os << "("; |
|
1623 |
|
1624 if (! is_ans_assign ()) |
|
1625 { |
|
1626 if (lhs) |
|
1627 lhs->print_code (os); |
|
1628 |
|
1629 if (index) |
|
1630 { |
|
1631 os << " ("; |
|
1632 index->print_code (os); |
|
1633 os << ")"; |
|
1634 } |
|
1635 |
|
1636 os << " = "; |
|
1637 } |
|
1638 |
|
1639 if (rhs) |
|
1640 rhs->print_code (os); |
|
1641 |
|
1642 if (in_parens) |
|
1643 os << ")"; |
|
1644 } |
|
1645 |
|
1646 // Multi-valued assignmnt expressions. |
|
1647 |
|
1648 tree_multi_assignment_expression::~tree_multi_assignment_expression (void) |
|
1649 { |
|
1650 delete lhs; |
|
1651 delete rhs; |
|
1652 } |
|
1653 |
|
1654 tree_constant |
|
1655 tree_multi_assignment_expression::eval (int print) |
|
1656 { |
|
1657 tree_constant retval; |
|
1658 |
|
1659 if (error_state) |
|
1660 return retval; |
|
1661 |
|
1662 Octave_object tmp_args; |
|
1663 Octave_object result = eval (print, 1, tmp_args); |
|
1664 |
|
1665 if (result.length () > 0) |
|
1666 retval = result(0); |
|
1667 |
|
1668 return retval; |
|
1669 } |
|
1670 |
|
1671 Octave_object |
|
1672 tree_multi_assignment_expression::eval (int print, int nargout, |
|
1673 const Octave_object& args) |
|
1674 { |
|
1675 assert (etype == tree_expression::multi_assignment); |
|
1676 |
|
1677 if (error_state || ! rhs) |
|
1678 return Octave_object (); |
|
1679 |
|
1680 nargout = lhs->length (); |
|
1681 Octave_object tmp_args; |
|
1682 Octave_object results = rhs->eval (0, nargout, tmp_args); |
|
1683 |
|
1684 if (error_state) |
|
1685 eval_error (); |
|
1686 |
|
1687 int ma_line = line (); |
|
1688 int ma_column = column (); |
|
1689 |
|
1690 if (results.length () > 0) |
|
1691 { |
|
1692 int i = 0; |
|
1693 int pad_after = 0; |
|
1694 int last_was_scalar_type = 0; |
|
1695 for (Pix p = lhs->first (); p != 0; lhs->next (p)) |
|
1696 { |
|
1697 tree_index_expression *lhs_expr = (*lhs) (p); |
|
1698 |
|
1699 if (i < nargout) |
|
1700 { |
|
1701 // XXX FIXME? XXX -- this is apparently the way Matlab works, but |
|
1702 // maybe we should have the option of skipping the assignment instead. |
|
1703 |
|
1704 tree_constant *tmp = 0; |
|
1705 if (results(i).is_undefined ()) |
|
1706 { |
|
1707 Matrix m; |
|
1708 tmp = new tree_constant (m); |
|
1709 } |
|
1710 else |
|
1711 tmp = new tree_constant (results(i)); |
|
1712 |
|
1713 tree_simple_assignment_expression tmp_expr |
|
1714 (lhs_expr, tmp, 1, 0, ma_line, ma_column); |
|
1715 |
|
1716 results(i) = tmp_expr.eval (0); // May change |
|
1717 |
|
1718 if (error_state) |
|
1719 break; |
|
1720 |
|
1721 if (print && pad_after) |
|
1722 { |
|
1723 ostrstream output_buf; |
|
1724 output_buf << "\n" << '\0'; |
|
1725 maybe_page_output (output_buf); |
|
1726 } |
|
1727 |
|
1728 if (print && user_pref.print_answer_id_name) |
|
1729 { |
|
1730 tree_identifier *tmp_id = lhs_expr->ident (); |
|
1731 char *tmp_nm = tmp_id->name (); |
|
1732 |
|
1733 if (print_as_scalar (results(i))) |
|
1734 { |
|
1735 ostrstream output_buf; |
|
1736 output_buf << tmp_nm << " = " << '\0'; |
|
1737 maybe_page_output (output_buf); |
|
1738 last_was_scalar_type = 1; |
|
1739 } |
|
1740 else |
|
1741 { |
|
1742 ostrstream output_buf; |
|
1743 output_buf << tmp_nm << " =\n\n" << '\0'; |
|
1744 maybe_page_output (output_buf); |
|
1745 last_was_scalar_type = 0; |
|
1746 } |
|
1747 } |
|
1748 |
|
1749 results(i).eval (print); |
|
1750 |
|
1751 pad_after++; |
|
1752 i++; |
|
1753 } |
|
1754 else |
|
1755 { |
|
1756 tree_simple_assignment_expression tmp_expr |
|
1757 (lhs_expr, 0, 1, 0, ma_line, ma_column); |
|
1758 |
|
1759 tmp_expr.eval (0); |
|
1760 |
|
1761 if (error_state) |
|
1762 break; |
|
1763 |
|
1764 if (last_was_scalar_type && i == 1) |
|
1765 pad_after = 0; |
|
1766 |
|
1767 break; |
|
1768 } |
|
1769 } |
|
1770 |
|
1771 if (print && pad_after) |
|
1772 { |
|
1773 ostrstream output_buf; |
|
1774 output_buf << "\n" << '\0'; |
|
1775 maybe_page_output (output_buf); |
|
1776 } |
|
1777 } |
|
1778 |
|
1779 return results; |
|
1780 } |
|
1781 |
|
1782 void |
|
1783 tree_multi_assignment_expression::eval_error (void) |
|
1784 { |
|
1785 if (error_state > 0) |
|
1786 ::error ("evaluating assignment expression near line %d, column %d", |
|
1787 line (), column ()); |
|
1788 } |
|
1789 |
|
1790 void |
|
1791 tree_multi_assignment_expression::print_code (ostream& os) |
|
1792 { |
|
1793 print_code_indent (os); |
|
1794 |
|
1795 if (in_parens) |
|
1796 os << "("; |
|
1797 |
|
1798 if (lhs) |
|
1799 { |
|
1800 int len = lhs->length (); |
|
1801 |
|
1802 if (len > 1) |
|
1803 os << "["; |
|
1804 |
|
1805 lhs->print_code (os); |
|
1806 |
|
1807 if (len > 1) |
|
1808 os << "]"; |
|
1809 } |
|
1810 |
|
1811 os << " = "; |
|
1812 |
|
1813 if (rhs) |
|
1814 rhs->print_code (os); |
|
1815 |
|
1816 if (in_parens) |
|
1817 os << ")"; |
|
1818 } |
|
1819 |
|
1820 // Colon expressions. |
|
1821 |
|
1822 tree_colon_expression * |
|
1823 tree_colon_expression::chain (tree_expression *t) |
|
1824 { |
|
1825 tree_colon_expression *retval = 0; |
|
1826 if (! op1 || op3) |
|
1827 ::error ("invalid colon expression"); |
|
1828 else |
|
1829 { |
|
1830 op3 = op2; // Stupid syntax. |
|
1831 op2 = t; |
|
1832 |
|
1833 retval = this; |
|
1834 } |
|
1835 return retval; |
|
1836 } |
|
1837 |
|
1838 tree_constant |
|
1839 tree_colon_expression::eval (int print) |
|
1840 { |
|
1841 tree_constant retval; |
|
1842 |
|
1843 if (error_state || ! op1 || ! op2) |
|
1844 return retval; |
|
1845 |
|
1846 Octave_object otmp = op1->eval (0); |
|
1847 tree_constant tmp = otmp(0); |
|
1848 |
|
1849 if (tmp.is_undefined ()) |
|
1850 { |
|
1851 eval_error ("invalid null value in colon expression"); |
|
1852 return retval; |
|
1853 } |
|
1854 |
|
1855 tmp = tmp.make_numeric (); |
|
1856 if (tmp.const_type () != tree_constant_rep::scalar_constant |
|
1857 && tmp.const_type () != tree_constant_rep::complex_scalar_constant) |
|
1858 { |
|
1859 eval_error ("base for colon expression must be a scalar"); |
|
1860 return retval; |
|
1861 } |
|
1862 double base = tmp.double_value (); |
|
1863 |
|
1864 otmp = op2->eval (0); |
|
1865 tmp = otmp(0); |
|
1866 |
|
1867 if (tmp.is_undefined ()) |
|
1868 { |
|
1869 eval_error ("invalid null value in colon expression"); |
|
1870 return retval; |
|
1871 } |
|
1872 |
|
1873 tmp = tmp.make_numeric (); |
|
1874 if (tmp.const_type () != tree_constant_rep::scalar_constant |
|
1875 && tmp.const_type () != tree_constant_rep::complex_scalar_constant) |
|
1876 { |
|
1877 eval_error ("limit for colon expression must be a scalar"); |
|
1878 return retval; |
|
1879 } |
|
1880 double limit = tmp.double_value (); |
|
1881 |
|
1882 double inc = 1.0; |
|
1883 if (op3) |
|
1884 { |
|
1885 otmp = op3->eval (0); |
|
1886 tmp = otmp(0); |
|
1887 |
|
1888 if (tmp.is_undefined ()) |
|
1889 { |
|
1890 eval_error ("invalid null value in colon expression"); |
|
1891 return retval; |
|
1892 } |
|
1893 |
|
1894 tmp = tmp.make_numeric (); |
|
1895 if (tmp.const_type () != tree_constant_rep::scalar_constant |
|
1896 && tmp.const_type () != tree_constant_rep::complex_scalar_constant) |
|
1897 { |
|
1898 eval_error ("increment for colon expression must be a scalar"); |
|
1899 return retval; |
|
1900 } |
|
1901 else |
|
1902 inc = tmp.double_value (); |
|
1903 } |
|
1904 |
|
1905 retval = tree_constant (base, limit, inc); |
|
1906 |
|
1907 if (error_state) |
|
1908 { |
|
1909 if (error_state) |
|
1910 eval_error ("evaluating colon expression"); |
|
1911 return tree_constant (); |
|
1912 } |
|
1913 |
|
1914 return retval; |
|
1915 } |
|
1916 |
|
1917 void |
|
1918 tree_colon_expression::eval_error (const char *s) |
|
1919 { |
|
1920 if (error_state > 0) |
|
1921 ::error ("%s near line %d column %d", s, line (), column ()); |
|
1922 } |
|
1923 |
|
1924 void |
|
1925 tree_colon_expression::print_code (ostream& os) |
|
1926 { |
|
1927 print_code_indent (os); |
|
1928 |
|
1929 if (in_parens) |
|
1930 os << "("; |
|
1931 |
|
1932 if (op1) |
|
1933 op1->print_code (os); |
|
1934 |
|
1935 // Stupid syntax. |
|
1936 |
|
1937 if (op3) |
|
1938 { |
|
1939 os << ":"; |
|
1940 op3->print_code (os); |
|
1941 } |
|
1942 |
|
1943 if (op2) |
|
1944 { |
|
1945 os << ":"; |
|
1946 op2->print_code (os); |
|
1947 } |
|
1948 |
|
1949 if (in_parens) |
|
1950 os << ")"; |
|
1951 } |
|
1952 |
|
1953 // Builtin functions. |
|
1954 |
|
1955 tree_builtin::tree_builtin (const char *nm) |
|
1956 { |
|
1957 nargin_max = -1; |
|
1958 nargout_max = -1; |
|
1959 is_mapper = 0; |
|
1960 fcn = 0; |
|
1961 if (nm) |
|
1962 my_name = strsave (nm); |
|
1963 } |
|
1964 |
|
1965 tree_builtin::tree_builtin (int i_max, int o_max, Mapper_fcn& m_fcn, |
|
1966 const char *nm) |
|
1967 { |
|
1968 nargin_max = i_max; |
|
1969 nargout_max = o_max; |
|
1970 mapper_fcn = m_fcn; |
|
1971 is_mapper = 1; |
|
1972 fcn = 0; |
|
1973 if (nm) |
|
1974 my_name = strsave (nm); |
|
1975 } |
|
1976 |
|
1977 tree_builtin::tree_builtin (int i_max, int o_max, Octave_builtin_fcn g_fcn, |
|
1978 const char *nm) |
|
1979 { |
|
1980 nargin_max = i_max; |
|
1981 nargout_max = o_max; |
|
1982 is_mapper = 0; |
|
1983 fcn = g_fcn; |
|
1984 if (nm) |
|
1985 my_name = strsave (nm); |
|
1986 } |
|
1987 |
|
1988 tree_constant |
|
1989 tree_builtin::eval (int print) |
|
1990 { |
|
1991 tree_constant retval; |
|
1992 |
|
1993 if (error_state) |
|
1994 return retval; |
|
1995 |
|
1996 if (fcn) |
|
1997 { |
|
1998 Octave_object args; |
|
1999 args(0) = tree_constant (my_name); |
|
2000 Octave_object tmp = (*fcn) (args, 1); |
|
2001 if (tmp.length () > 0) |
|
2002 retval = tmp(0); |
|
2003 } |
|
2004 else // Assume mapper function |
|
2005 ::error ("%s: argument expected", my_name); |
|
2006 |
|
2007 return retval; |
|
2008 } |
|
2009 |
|
2010 Octave_object |
|
2011 tree_builtin::eval (int print, int nargout, const Octave_object& args) |
|
2012 { |
|
2013 Octave_object retval; |
|
2014 |
|
2015 if (error_state) |
|
2016 return retval; |
|
2017 |
|
2018 int nargin = args.length (); |
|
2019 |
|
2020 if (fcn) |
|
2021 { |
|
2022 if (any_arg_is_magic_colon (args)) |
|
2023 ::error ("invalid use of colon in function argument list"); |
|
2024 else |
|
2025 retval = (*fcn) (args, nargout); |
|
2026 } |
|
2027 else if (is_mapper) |
|
2028 { |
|
2029 if (nargin > nargin_max) |
|
2030 ::error ("%s: too many arguments", my_name); |
|
2031 else if (nargin > 0 && args.length () > 0 && args(1).is_defined ()) |
|
2032 { |
|
2033 tree_constant tmp = args(1).mapper (mapper_fcn, 0); |
|
2034 retval.resize (1); |
|
2035 retval(0) = tmp; |
|
2036 } |
|
2037 } |
|
2038 else |
|
2039 panic_impossible (); |
|
2040 |
|
2041 return retval; |
|
2042 } |
|
2043 |
|
2044 int |
|
2045 tree_builtin::max_expected_args (void) |
|
2046 { |
|
2047 int ea = nargin_max; |
|
2048 if (nargin_max < 0) |
|
2049 ea = INT_MAX; |
|
2050 else |
|
2051 ea = nargin_max; |
|
2052 return ea; |
|
2053 } |
|
2054 |
578
|
2055 // User defined functions. |
494
|
2056 |
578
|
2057 #if 0 |
494
|
2058 tree_function * |
578
|
2059 tree_function::define (tree statement_list *t) |
494
|
2060 { |
|
2061 cmd_list = t; |
|
2062 return this; |
|
2063 } |
578
|
2064 #endif |
494
|
2065 |
|
2066 tree_function * |
|
2067 tree_function::define_param_list (tree_parameter_list *t) |
|
2068 { |
|
2069 param_list = t; |
508
|
2070 |
529
|
2071 if (param_list) |
508
|
2072 { |
|
2073 int len = param_list->length (); |
|
2074 int va_only = param_list->varargs_only (); |
|
2075 num_named_args = va_only ? len - 1 : len; |
|
2076 curr_va_arg_number = num_named_args; |
|
2077 } |
494
|
2078 |
|
2079 return this; |
|
2080 } |
|
2081 |
|
2082 tree_function * |
|
2083 tree_function::define_ret_list (tree_parameter_list *t) |
|
2084 { |
|
2085 ret_list = t; |
|
2086 return this; |
|
2087 } |
|
2088 |
|
2089 void |
|
2090 tree_function::stash_fcn_file_name (char *s) |
|
2091 { |
|
2092 delete [] file_name; |
|
2093 file_name = strsave (s); |
|
2094 } |
|
2095 |
|
2096 void |
|
2097 tree_function::mark_as_system_fcn_file (void) |
|
2098 { |
529
|
2099 if (file_name) |
494
|
2100 { |
|
2101 // We really should stash the whole path to the file we found, when we |
|
2102 // looked it up, to avoid possible race conditions... XXX FIXME XXX |
|
2103 // |
|
2104 // We probably also don't need to get the library directory every |
|
2105 // time, but since this function is only called when the function file |
|
2106 // is parsed, it probably doesn't matter that much. |
|
2107 |
|
2108 char *oct_lib = octave_lib_dir (); |
|
2109 int len = strlen (oct_lib); |
|
2110 |
|
2111 char *ff_name = fcn_file_in_path (file_name); |
|
2112 |
|
2113 if (strncmp (oct_lib, ff_name, len) == 0) |
|
2114 system_fcn_file = 1; |
|
2115 |
|
2116 delete [] ff_name; |
|
2117 } |
|
2118 else |
|
2119 system_fcn_file = 0; |
|
2120 } |
|
2121 |
|
2122 int |
|
2123 tree_function::takes_varargs (void) const |
|
2124 { |
529
|
2125 return (param_list && param_list->takes_varargs ()); |
494
|
2126 } |
|
2127 |
|
2128 tree_constant |
|
2129 tree_function::octave_va_arg (void) |
|
2130 { |
|
2131 tree_constant retval; |
|
2132 |
508
|
2133 if (curr_va_arg_number < num_args_passed) |
|
2134 retval = args_passed (++curr_va_arg_number); |
494
|
2135 else |
|
2136 ::error ("error getting arg number %d -- only %d provided", |
508
|
2137 curr_va_arg_number, num_args_passed-1); |
494
|
2138 |
|
2139 return retval; |
|
2140 } |
|
2141 |
|
2142 void |
|
2143 tree_function::stash_function_name (char *s) |
|
2144 { |
|
2145 delete [] fcn_name; |
|
2146 fcn_name = strsave (s); |
|
2147 } |
|
2148 |
|
2149 tree_constant |
|
2150 tree_function::eval (int print) |
|
2151 { |
|
2152 tree_constant retval; |
|
2153 |
529
|
2154 if (error_state || ! cmd_list) |
494
|
2155 return retval; |
|
2156 |
565
|
2157 Octave_object tmp_args; |
|
2158 tmp_args.resize (1); |
506
|
2159 Octave_object tmp = eval (print, 1, tmp_args); |
500
|
2160 |
|
2161 if (! error_state && tmp.length () > 0) |
|
2162 retval = tmp(0); |
494
|
2163 |
|
2164 return retval; |
|
2165 } |
|
2166 |
|
2167 // For unwind protect. |
|
2168 |
|
2169 static void |
|
2170 pop_symbol_table_context (void *table) |
|
2171 { |
|
2172 symbol_table *tmp = (symbol_table *) table; |
|
2173 tmp->pop_context (); |
|
2174 } |
|
2175 |
|
2176 static void |
|
2177 clear_symbol_table (void *table) |
|
2178 { |
|
2179 symbol_table *tmp = (symbol_table *) table; |
|
2180 tmp->clear (); |
|
2181 } |
|
2182 |
500
|
2183 Octave_object |
506
|
2184 tree_function::eval (int print, int nargout, const Octave_object& args) |
494
|
2185 { |
500
|
2186 Octave_object retval; |
494
|
2187 |
|
2188 if (error_state) |
|
2189 return retval; |
|
2190 |
529
|
2191 if (! cmd_list) |
494
|
2192 return retval; |
|
2193 |
506
|
2194 int nargin = args.length (); |
|
2195 |
494
|
2196 begin_unwind_frame ("func_eval"); |
|
2197 |
|
2198 unwind_protect_int (call_depth); |
|
2199 call_depth++; |
|
2200 |
|
2201 if (call_depth > 1) |
|
2202 { |
|
2203 sym_tab->push_context (); |
|
2204 add_unwind_protect (pop_symbol_table_context, (void *) sym_tab); |
|
2205 } |
|
2206 |
|
2207 // Force symbols to be undefined again when this function exits. |
|
2208 |
|
2209 add_unwind_protect (clear_symbol_table, (void *) sym_tab); |
|
2210 |
|
2211 // Save old and set current symbol table context, for eval_undefined_error(). |
|
2212 |
|
2213 unwind_protect_ptr (curr_sym_tab); |
|
2214 curr_sym_tab = sym_tab; |
|
2215 |
|
2216 unwind_protect_ptr (curr_function); |
|
2217 curr_function = this; |
|
2218 |
500
|
2219 // unwind_protect_ptr (args_passed); |
494
|
2220 args_passed = args; |
|
2221 |
|
2222 unwind_protect_int (num_args_passed); |
|
2223 num_args_passed = nargin; |
|
2224 |
|
2225 unwind_protect_int (num_named_args); |
508
|
2226 unwind_protect_int (curr_va_arg_number); |
494
|
2227 |
529
|
2228 if (param_list && ! param_list->varargs_only ()) |
494
|
2229 { |
506
|
2230 param_list->define_from_arg_vector (args); |
494
|
2231 if (error_state) |
|
2232 goto abort; |
|
2233 } |
|
2234 |
|
2235 // The following code is in a separate scope to avoid warnings from |
|
2236 // G++ about `goto abort' crossing the initialization of some |
|
2237 // variables. |
|
2238 |
|
2239 { |
|
2240 bind_nargin_and_nargout (sym_tab, nargin, nargout); |
|
2241 |
|
2242 // Evaluate the commands that make up the function. Always turn on |
|
2243 // printing for commands inside functions. Maybe this should be |
|
2244 // toggled by a user-leval variable? |
|
2245 |
|
2246 int pf = ! user_pref.silent_functions; |
|
2247 tree_constant last_computed_value = cmd_list->eval (pf); |
|
2248 |
|
2249 if (returning) |
|
2250 returning = 0; |
|
2251 |
|
2252 if (error_state) |
|
2253 { |
|
2254 traceback_error (); |
|
2255 goto abort; |
|
2256 } |
|
2257 |
|
2258 // Copy return values out. |
|
2259 |
529
|
2260 if (ret_list) |
494
|
2261 { |
|
2262 retval = ret_list->convert_to_const_vector (); |
|
2263 } |
|
2264 else if (user_pref.return_last_computed_value) |
|
2265 { |
500
|
2266 retval.resize (1); |
|
2267 retval(0) = last_computed_value; |
494
|
2268 } |
|
2269 } |
|
2270 |
|
2271 abort: |
|
2272 run_unwind_frame ("func_eval"); |
|
2273 |
|
2274 return retval; |
|
2275 } |
|
2276 |
|
2277 int |
|
2278 tree_function::max_expected_args (void) |
|
2279 { |
529
|
2280 if (param_list) |
494
|
2281 { |
|
2282 if (param_list->takes_varargs ()) |
|
2283 return -1; |
|
2284 else |
|
2285 return param_list->length () + 1; |
|
2286 } |
|
2287 else |
|
2288 return 1; |
|
2289 } |
|
2290 |
|
2291 void |
|
2292 tree_function::traceback_error (void) |
|
2293 { |
|
2294 if (error_state >= 0) |
|
2295 error_state = -1; |
|
2296 |
529
|
2297 if (fcn_name) |
494
|
2298 { |
529
|
2299 if (file_name) |
494
|
2300 ::error ("called from `%s' in file `%s'", fcn_name, file_name); |
|
2301 else |
|
2302 ::error ("called from `%s'", fcn_name); |
|
2303 } |
|
2304 else |
|
2305 { |
529
|
2306 if (file_name) |
494
|
2307 ::error ("called from file `%s'", file_name); |
|
2308 else |
|
2309 ::error ("called from `?unknown?'"); |
|
2310 } |
|
2311 } |
|
2312 |
581
|
2313 void |
|
2314 tree_function::print_code (ostream& os) |
|
2315 { |
|
2316 print_code_reset (); |
|
2317 |
|
2318 print_code_indent (os); |
|
2319 |
|
2320 os << "function "; |
|
2321 |
|
2322 if (ret_list) |
|
2323 { |
|
2324 int len = ret_list->length (); |
|
2325 |
|
2326 if (len > 1) |
|
2327 os << "["; |
|
2328 |
|
2329 ret_list->print_code (os); |
|
2330 |
|
2331 if (len > 1) |
|
2332 os << "]"; |
|
2333 |
|
2334 os << " = "; |
|
2335 } |
|
2336 |
|
2337 os << (fcn_name ? fcn_name : "(null)") << " "; |
|
2338 |
|
2339 if (param_list) |
|
2340 { |
|
2341 int len = param_list->length (); |
|
2342 if (len > 0) |
|
2343 os << "("; |
|
2344 |
|
2345 param_list->print_code (os); |
|
2346 |
|
2347 if (len > 0) |
|
2348 { |
|
2349 os << ")"; |
|
2350 print_code_new_line (os); |
|
2351 } |
|
2352 } |
|
2353 else |
|
2354 { |
|
2355 os << "()"; |
|
2356 print_code_new_line (os); |
|
2357 } |
|
2358 |
|
2359 if (cmd_list) |
|
2360 { |
|
2361 increment_indent_level (); |
|
2362 cmd_list->print_code (os); |
|
2363 } |
|
2364 |
|
2365 os << "endfunction"; |
|
2366 |
|
2367 print_code_new_line (os); |
|
2368 } |
|
2369 |
529
|
2370 DEFUN ("va_arg", Fva_arg, Sva_arg, 1, 1, |
|
2371 "va_arg (): return next argument in a function that takes a\n\ |
581
|
2372 variable number of parameters") |
529
|
2373 { |
|
2374 Octave_object retval; |
|
2375 |
|
2376 int nargin = args.length (); |
|
2377 |
|
2378 if (nargin == 1) |
|
2379 { |
|
2380 if (curr_function) |
|
2381 { |
|
2382 if (curr_function->takes_varargs ()) |
|
2383 retval = curr_function->octave_va_arg (); |
|
2384 else |
|
2385 { |
|
2386 error ("va_arg only valid within function taking variable"); |
|
2387 error ("number of arguments"); |
|
2388 } |
|
2389 } |
|
2390 else |
|
2391 error ("va_arg only valid within function body"); |
|
2392 } |
|
2393 else |
|
2394 print_usage ("va_arg"); |
|
2395 |
|
2396 return retval; |
|
2397 } |
|
2398 |
|
2399 DEFUN ("va_start", Fva_start, Sva_start, 1, 0, |
|
2400 "va_start (): reset the pointer to the list of optional arguments\n\ |
|
2401 to the beginning") |
|
2402 { |
|
2403 Octave_object retval; |
|
2404 |
|
2405 int nargin = args.length (); |
|
2406 |
|
2407 if (nargin == 1) |
|
2408 { |
|
2409 if (curr_function) |
|
2410 { |
|
2411 if (curr_function->takes_varargs ()) |
|
2412 curr_function->octave_va_start (); |
|
2413 else |
|
2414 { |
|
2415 error ("va_start only valid within function taking variable"); |
|
2416 error ("number of arguments"); |
|
2417 } |
|
2418 } |
|
2419 else |
|
2420 error ("va_start only valid within function body"); |
|
2421 } |
|
2422 else |
|
2423 print_usage ("va_start"); |
|
2424 |
|
2425 return retval; |
|
2426 } |
|
2427 |
494
|
2428 /* |
|
2429 ;;; Local Variables: *** |
|
2430 ;;; mode: C++ *** |
|
2431 ;;; page-delimiter: "^/\\*" *** |
|
2432 ;;; End: *** |
|
2433 */ |