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