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