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