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