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