1741
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1741
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
|
31 #include <iostream.h> |
|
32 #include <strstream.h> |
|
33 |
|
34 #include "defun.h" |
|
35 #include "error.h" |
|
36 #include "gripes.h" |
|
37 #include "help.h" |
|
38 #include "input.h" |
|
39 #include "oct-obj.h" |
|
40 #include "pager.h" |
2388
|
41 #include "ov.h" |
1741
|
42 #include "pt-exp.h" |
2891
|
43 #include "pt-id.h" |
|
44 #include "pt-indir.h" |
1741
|
45 #include "pt-misc.h" |
|
46 #include "pt-mvr.h" |
2388
|
47 #include "pt-pr-code.h" |
2124
|
48 #include "pt-walk.h" |
1741
|
49 #include "utils.h" |
2879
|
50 #include "variables.h" |
1741
|
51 |
|
52 // Nonzero means we're returning from a function. |
|
53 extern int returning; |
|
54 |
|
55 // Nonzero means we're breaking out of a loop or function body. |
|
56 extern int breaking; |
|
57 |
|
58 // Prefix expressions. |
|
59 |
|
60 tree_prefix_expression::~tree_prefix_expression (void) |
|
61 { |
|
62 delete id; |
|
63 } |
|
64 |
2086
|
65 octave_value |
1827
|
66 tree_prefix_expression::eval (bool print) |
1741
|
67 { |
2086
|
68 octave_value retval; |
1741
|
69 |
|
70 if (error_state) |
|
71 return retval; |
|
72 |
|
73 if (id) |
|
74 { |
2388
|
75 switch (etype) |
|
76 { |
|
77 case increment: |
|
78 id->increment (); |
|
79 break; |
|
80 |
|
81 case decrement: |
|
82 id->decrement (); |
|
83 break; |
|
84 |
|
85 default: |
|
86 error ("prefix operator %d not implemented", etype); |
|
87 break; |
|
88 } |
|
89 |
|
90 |
1741
|
91 if (error_state) |
|
92 eval_error (); |
|
93 else |
|
94 { |
|
95 retval = id->eval (print); |
|
96 if (error_state) |
|
97 { |
2086
|
98 retval = octave_value (); |
1741
|
99 if (error_state) |
|
100 eval_error (); |
|
101 } |
|
102 } |
|
103 } |
|
104 return retval; |
|
105 } |
|
106 |
2805
|
107 const char * |
1741
|
108 tree_prefix_expression::oper (void) const |
|
109 { |
2805
|
110 static const char *op; |
1741
|
111 switch (etype) |
|
112 { |
2388
|
113 case increment: |
1741
|
114 op = "++"; |
|
115 break; |
|
116 |
2388
|
117 case decrement: |
1741
|
118 op = "--"; |
|
119 break; |
|
120 |
|
121 default: |
|
122 op = "<unknown>"; |
|
123 break; |
|
124 } |
|
125 return op; |
|
126 } |
|
127 |
|
128 void |
|
129 tree_prefix_expression::eval_error (void) |
|
130 { |
|
131 if (error_state > 0) |
2805
|
132 ::error ("evaluating prefix operator `%s' near line %d, column %d", |
|
133 oper (), line (), column ()); |
1741
|
134 } |
|
135 |
|
136 void |
2124
|
137 tree_prefix_expression::accept (tree_walker& tw) |
1741
|
138 { |
2124
|
139 tw.visit_prefix_expression (*this); |
1741
|
140 } |
|
141 |
|
142 // Postfix expressions. |
|
143 |
|
144 tree_postfix_expression::~tree_postfix_expression (void) |
|
145 { |
|
146 delete id; |
|
147 } |
|
148 |
2086
|
149 octave_value |
1827
|
150 tree_postfix_expression::eval (bool print) |
1741
|
151 { |
2086
|
152 octave_value retval; |
1741
|
153 |
|
154 if (error_state) |
|
155 return retval; |
|
156 |
|
157 if (id) |
|
158 { |
|
159 retval = id->eval (print); |
2388
|
160 |
|
161 switch (etype) |
|
162 { |
|
163 case increment: |
|
164 id->increment (); |
|
165 break; |
|
166 |
|
167 case decrement: |
|
168 id->decrement (); |
|
169 break; |
|
170 |
|
171 default: |
|
172 error ("postfix operator %d not implemented", etype); |
|
173 break; |
|
174 } |
|
175 |
1741
|
176 if (error_state) |
|
177 { |
2086
|
178 retval = octave_value (); |
1741
|
179 if (error_state) |
|
180 eval_error (); |
|
181 } |
|
182 } |
|
183 return retval; |
|
184 } |
|
185 |
2805
|
186 const char * |
1741
|
187 tree_postfix_expression::oper (void) const |
|
188 { |
2805
|
189 static const char *op; |
1741
|
190 switch (etype) |
|
191 { |
2388
|
192 case increment: |
1741
|
193 op = "++"; |
|
194 break; |
|
195 |
2388
|
196 case decrement: |
1741
|
197 op = "--"; |
|
198 break; |
|
199 |
|
200 default: |
|
201 op = "<unknown>"; |
|
202 break; |
|
203 } |
|
204 return op; |
|
205 } |
|
206 |
|
207 void |
|
208 tree_postfix_expression::eval_error (void) |
|
209 { |
|
210 if (error_state > 0) |
2805
|
211 ::error ("evaluating postfix operator `%s' near line %d, column %d", |
|
212 oper (), line (), column ()); |
1741
|
213 } |
|
214 |
|
215 void |
2124
|
216 tree_postfix_expression::accept (tree_walker& tw) |
1741
|
217 { |
2124
|
218 tw.visit_postfix_expression (*this); |
1741
|
219 } |
|
220 |
|
221 // Unary expressions. |
|
222 |
2086
|
223 octave_value |
1827
|
224 tree_unary_expression::eval (bool /* print */) |
1741
|
225 { |
2086
|
226 octave_value retval; |
1741
|
227 |
2388
|
228 if (error_state) |
|
229 return retval; |
|
230 |
|
231 if (op) |
1741
|
232 { |
2859
|
233 octave_value u = op->eval (); |
2388
|
234 |
|
235 if (error_state) |
|
236 eval_error (); |
|
237 else if (u.is_defined ()) |
1741
|
238 { |
2388
|
239 switch (etype) |
|
240 { |
2805
|
241 case unot: |
2388
|
242 retval = u.not (); |
|
243 break; |
|
244 |
|
245 case uminus: |
|
246 retval = u.uminus (); |
|
247 break; |
|
248 |
|
249 case transpose: |
|
250 retval = u.transpose (); |
|
251 break; |
|
252 |
|
253 case hermitian: |
|
254 retval = u.hermitian (); |
|
255 break; |
|
256 |
|
257 default: |
|
258 ::error ("unary operator %d not implemented", etype); |
|
259 break; |
|
260 } |
|
261 |
1741
|
262 if (error_state) |
|
263 { |
2388
|
264 retval = octave_value (); |
|
265 eval_error (); |
1741
|
266 } |
|
267 } |
|
268 } |
|
269 |
|
270 return retval; |
|
271 } |
|
272 |
2805
|
273 const char * |
1741
|
274 tree_unary_expression::oper (void) const |
|
275 { |
2805
|
276 static const char *op; |
1741
|
277 switch (etype) |
|
278 { |
2805
|
279 case unot: |
1741
|
280 op = "!"; |
|
281 break; |
|
282 |
2388
|
283 case uminus: |
1741
|
284 op = "-"; |
|
285 break; |
|
286 |
2388
|
287 case transpose: |
|
288 op = ".'"; |
1741
|
289 break; |
|
290 |
2388
|
291 case hermitian: |
|
292 op = "'"; |
1741
|
293 break; |
|
294 |
|
295 default: |
|
296 op = "<unknown>"; |
|
297 break; |
|
298 } |
|
299 return op; |
|
300 } |
|
301 |
|
302 void |
|
303 tree_unary_expression::eval_error (void) |
|
304 { |
|
305 if (error_state > 0) |
2805
|
306 ::error ("evaluating unary operator `%s' near line %d, column %d", |
|
307 oper (), line (), column ()); |
1741
|
308 } |
|
309 |
|
310 void |
2124
|
311 tree_unary_expression::accept (tree_walker& tw) |
1741
|
312 { |
2124
|
313 tw.visit_unary_expression (*this); |
1741
|
314 } |
|
315 |
|
316 // Binary expressions. |
|
317 |
2086
|
318 octave_value |
1827
|
319 tree_binary_expression::eval (bool /* print */) |
1741
|
320 { |
2086
|
321 octave_value retval; |
1741
|
322 |
2388
|
323 if (error_state) |
|
324 return retval; |
|
325 |
|
326 if (op_lhs) |
1741
|
327 { |
2859
|
328 octave_value a = op_lhs->eval (); |
2388
|
329 |
|
330 if (error_state) |
|
331 eval_error (); |
|
332 else if (a.is_defined () && op_rhs) |
1741
|
333 { |
2859
|
334 octave_value b = op_rhs->eval (); |
2388
|
335 |
1741
|
336 if (error_state) |
|
337 eval_error (); |
2388
|
338 else if (b.is_defined ()) |
1741
|
339 { |
2879
|
340 retval = ::do_binary_op (etype, a, b); |
2388
|
341 |
2879
|
342 if (error_state) |
2388
|
343 { |
|
344 retval = octave_value (); |
|
345 eval_error (); |
1741
|
346 } |
|
347 } |
2388
|
348 else |
|
349 eval_error (); |
1741
|
350 } |
2388
|
351 else |
|
352 eval_error (); |
1741
|
353 } |
2388
|
354 else |
|
355 eval_error (); |
1741
|
356 |
|
357 return retval; |
|
358 } |
|
359 |
2805
|
360 const char * |
1741
|
361 tree_binary_expression::oper (void) const |
|
362 { |
2879
|
363 // XXX FIXME XXX |
|
364 return octave_value::binary_op_as_string (etype) . c_str (); |
1741
|
365 } |
|
366 |
|
367 void |
|
368 tree_binary_expression::eval_error (void) |
|
369 { |
|
370 if (error_state > 0) |
2805
|
371 ::error ("evaluating binary operator `%s' near line %d, column %d", |
|
372 oper (), line (), column ()); |
1741
|
373 } |
|
374 |
|
375 void |
2124
|
376 tree_binary_expression::accept (tree_walker& tw) |
1741
|
377 { |
2124
|
378 tw.visit_binary_expression (*this); |
1741
|
379 } |
|
380 |
2388
|
381 // Boolean expressions. |
|
382 |
|
383 octave_value |
|
384 tree_boolean_expression::eval (bool /* print */) |
|
385 { |
|
386 octave_value retval; |
|
387 |
|
388 if (error_state) |
|
389 return retval; |
|
390 |
|
391 bool result = false; |
|
392 |
|
393 if (op_lhs) |
|
394 { |
2859
|
395 octave_value a = op_lhs->eval (); |
2388
|
396 |
|
397 if (error_state) |
|
398 eval_error (); |
|
399 else |
|
400 { |
|
401 bool a_true = a.is_true (); |
|
402 |
|
403 if (error_state) |
|
404 eval_error (); |
|
405 else |
|
406 { |
|
407 if (a_true) |
|
408 { |
2805
|
409 if (etype == bool_or) |
2388
|
410 { |
|
411 result = true; |
|
412 goto done; |
|
413 } |
|
414 } |
|
415 else |
|
416 { |
2805
|
417 if (etype == bool_and) |
2388
|
418 goto done; |
|
419 } |
|
420 |
|
421 if (op_rhs) |
|
422 { |
2859
|
423 octave_value b = op_rhs->eval (); |
2388
|
424 |
|
425 if (error_state) |
|
426 eval_error (); |
|
427 else |
|
428 { |
|
429 result = b.is_true (); |
|
430 |
|
431 if (error_state) |
|
432 eval_error (); |
|
433 } |
|
434 } |
|
435 else |
|
436 eval_error (); |
|
437 |
|
438 done: |
|
439 |
|
440 if (! error_state) |
2800
|
441 retval = octave_value (static_cast<double> (result)); |
2388
|
442 } |
|
443 } |
|
444 } |
|
445 else |
|
446 eval_error (); |
|
447 |
|
448 return retval; |
|
449 } |
|
450 |
2805
|
451 const char * |
2388
|
452 tree_boolean_expression::oper (void) const |
|
453 { |
2805
|
454 static const char *op; |
2388
|
455 switch (etype) |
|
456 { |
2805
|
457 case bool_and: |
2388
|
458 op = "&&"; |
|
459 break; |
|
460 |
2805
|
461 case bool_or: |
2388
|
462 op = "||"; |
|
463 break; |
|
464 |
|
465 default: |
|
466 op = "<unknown>"; |
|
467 break; |
|
468 } |
|
469 return op; |
|
470 } |
|
471 |
1741
|
472 // Simple assignment expressions. |
|
473 |
|
474 tree_simple_assignment_expression::tree_simple_assignment_expression |
1827
|
475 (tree_identifier *i, tree_expression *r, bool plhs, bool ans_assign, |
2879
|
476 int l, int c, octave_value::assign_op t) |
|
477 : tree_expression (l, c), lhs_idx_expr (0), |
|
478 lhs (new tree_indirect_ref (i)), index (0), rhs (r), |
|
479 preserve (plhs), ans_ass (ans_assign), etype (t) { } |
1741
|
480 |
|
481 tree_simple_assignment_expression::tree_simple_assignment_expression |
1827
|
482 (tree_index_expression *idx_expr, tree_expression *r, bool plhs, |
2879
|
483 bool ans_assign, int l, int c, octave_value::assign_op t) |
|
484 : tree_expression (l, c), lhs_idx_expr (idx_expr), |
|
485 lhs (idx_expr->ident ()), index (idx_expr->arg_list ()), rhs (r), |
|
486 preserve (plhs), ans_ass (ans_assign), etype (t) { } |
1741
|
487 |
|
488 tree_simple_assignment_expression::~tree_simple_assignment_expression (void) |
|
489 { |
|
490 if (! preserve) |
|
491 { |
|
492 if (lhs_idx_expr) |
|
493 delete lhs_idx_expr; |
|
494 else |
|
495 delete lhs; |
|
496 } |
|
497 |
|
498 delete rhs; |
|
499 } |
|
500 |
1827
|
501 bool |
1741
|
502 tree_simple_assignment_expression::left_hand_side_is_identifier_only (void) |
|
503 { |
|
504 return lhs->is_identifier_only (); |
|
505 } |
|
506 |
|
507 tree_identifier * |
|
508 tree_simple_assignment_expression::left_hand_side_id (void) |
|
509 { |
|
510 return lhs->ident (); |
|
511 } |
|
512 |
2388
|
513 // ??? FIXME ??? -- should octave_variable_reference::assign return |
|
514 // the right thing for us to return? |
|
515 |
2086
|
516 octave_value |
1827
|
517 tree_simple_assignment_expression::eval (bool print) |
1741
|
518 { |
2086
|
519 octave_value retval; |
1741
|
520 |
2665
|
521 octave_value lhs_val; |
|
522 |
1741
|
523 if (error_state) |
|
524 return retval; |
|
525 |
|
526 if (rhs) |
|
527 { |
2859
|
528 octave_value rhs_val = rhs->eval (); |
2665
|
529 |
1741
|
530 if (error_state) |
|
531 { |
|
532 eval_error (); |
|
533 } |
|
534 else if (rhs_val.is_undefined ()) |
|
535 { |
|
536 error ("value on right hand side of assignment is undefined"); |
|
537 eval_error (); |
|
538 } |
|
539 else |
|
540 { |
2388
|
541 octave_variable_reference ult (lhs); |
1741
|
542 |
|
543 if (error_state) |
|
544 eval_error (); |
|
545 else |
|
546 { |
2388
|
547 if (index) |
|
548 { |
|
549 // Extract the arguments into a simple vector. |
1741
|
550 |
2388
|
551 octave_value_list args = index->convert_to_const_vector (); |
|
552 |
1741
|
553 if (error_state) |
|
554 eval_error (); |
2388
|
555 else |
|
556 { |
|
557 int nargin = args.length (); |
|
558 |
|
559 if (nargin > 0) |
|
560 { |
2879
|
561 ult.assign (etype, args, rhs_val); |
2388
|
562 |
|
563 if (error_state) |
|
564 eval_error (); |
|
565 else |
2665
|
566 { |
|
567 lhs_val = ult.value (); |
|
568 |
|
569 retval = rhs_val; |
|
570 } |
2388
|
571 } |
|
572 else |
|
573 error ("??? invalid index list ???"); |
|
574 } |
|
575 } |
|
576 else |
|
577 { |
2879
|
578 ult.assign (etype, rhs_val); |
2665
|
579 |
|
580 lhs_val = ult.value (); |
|
581 |
|
582 retval = rhs_val; |
1741
|
583 } |
|
584 } |
|
585 } |
|
586 } |
|
587 |
2665
|
588 // Return value is RHS value, but the value we print is the complete |
|
589 // LHS value so that expressions like x(2) = 2 (for x previously |
|
590 // undefined) print b = [ 0; 2 ], which is more Matlab-like. |
|
591 |
|
592 if (! error_state && print && lhs_val.is_defined ()) |
|
593 lhs_val.print_with_name (lhs->name ()); |
1741
|
594 |
|
595 return retval; |
|
596 } |
|
597 |
|
598 void |
|
599 tree_simple_assignment_expression::eval_error (void) |
|
600 { |
|
601 if (error_state > 0) |
|
602 { |
|
603 int l = line (); |
|
604 int c = column (); |
1827
|
605 |
1741
|
606 if (l != -1 && c != -1) |
|
607 ::error ("evaluating assignment expression near line %d, column %d", |
|
608 l, c); |
|
609 } |
|
610 } |
|
611 |
2879
|
612 const char * |
|
613 tree_simple_assignment_expression::oper (void) const |
|
614 { |
|
615 return octave_value::assign_op_as_string (etype) . c_str (); |
|
616 } |
|
617 |
1741
|
618 void |
2124
|
619 tree_simple_assignment_expression::accept (tree_walker& tw) |
1741
|
620 { |
2124
|
621 tw.visit_simple_assignment_expression (*this); |
1741
|
622 } |
|
623 |
|
624 // Colon expressions. |
|
625 |
|
626 tree_colon_expression * |
|
627 tree_colon_expression::chain (tree_expression *t) |
|
628 { |
|
629 tree_colon_expression *retval = 0; |
2124
|
630 if (! op_base || op_increment) |
1741
|
631 ::error ("invalid colon expression"); |
|
632 else |
|
633 { |
2124
|
634 // Stupid syntax: |
|
635 // |
|
636 // base : limit |
|
637 // base : increment : limit |
|
638 |
|
639 op_increment = op_limit; |
|
640 op_limit = t; |
1741
|
641 |
|
642 retval = this; |
|
643 } |
|
644 return retval; |
|
645 } |
|
646 |
2086
|
647 octave_value |
1827
|
648 tree_colon_expression::eval (bool /* print */) |
1741
|
649 { |
2086
|
650 octave_value retval; |
1741
|
651 |
2124
|
652 if (error_state || ! op_base || ! op_limit) |
1741
|
653 return retval; |
|
654 |
2859
|
655 octave_value tmp = op_base->eval (); |
1741
|
656 |
|
657 if (tmp.is_undefined ()) |
|
658 { |
|
659 eval_error ("invalid null value in colon expression"); |
|
660 return retval; |
|
661 } |
|
662 |
|
663 double base = tmp.double_value (); |
|
664 |
|
665 if (error_state) |
|
666 { |
|
667 error ("colon expression elements must be scalars"); |
|
668 eval_error ("evaluating colon expression"); |
|
669 return retval; |
|
670 } |
|
671 |
2859
|
672 tmp = op_limit->eval (); |
1741
|
673 |
|
674 if (tmp.is_undefined ()) |
|
675 { |
|
676 eval_error ("invalid null value in colon expression"); |
|
677 return retval; |
|
678 } |
|
679 |
|
680 double limit = tmp.double_value (); |
|
681 |
|
682 if (error_state) |
|
683 { |
|
684 error ("colon expression elements must be scalars"); |
|
685 eval_error ("evaluating colon expression"); |
|
686 return retval; |
|
687 } |
|
688 |
|
689 double inc = 1.0; |
2124
|
690 |
|
691 if (op_increment) |
1741
|
692 { |
2859
|
693 tmp = op_increment->eval (); |
1741
|
694 |
|
695 if (tmp.is_undefined ()) |
|
696 { |
|
697 eval_error ("invalid null value in colon expression"); |
|
698 return retval; |
|
699 } |
|
700 |
|
701 inc = tmp.double_value (); |
|
702 |
|
703 if (error_state) |
|
704 { |
|
705 error ("colon expression elements must be scalars"); |
|
706 eval_error ("evaluating colon expression"); |
|
707 return retval; |
|
708 } |
|
709 } |
|
710 |
2086
|
711 retval = octave_value (base, limit, inc); |
1741
|
712 |
|
713 if (error_state) |
|
714 { |
|
715 if (error_state) |
|
716 eval_error ("evaluating colon expression"); |
2086
|
717 return octave_value (); |
1741
|
718 } |
|
719 |
|
720 return retval; |
|
721 } |
|
722 |
|
723 void |
|
724 tree_colon_expression::eval_error (const char *s) |
|
725 { |
|
726 if (error_state > 0) |
|
727 ::error ("%s near line %d column %d", s, line (), column ()); |
|
728 } |
|
729 |
|
730 void |
2124
|
731 tree_colon_expression::accept (tree_walker& tw) |
1741
|
732 { |
2124
|
733 tw.visit_colon_expression (*this); |
1741
|
734 } |
|
735 |
|
736 /* |
|
737 ;;; Local Variables: *** |
|
738 ;;; mode: C++ *** |
|
739 ;;; End: *** |
|
740 */ |