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