Mercurial > octave-nkf
comparison src/pt-binop.cc @ 11091:5677f3f7b5fa
Matlab compatible short-circuit behavior for & and | operators
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 08 Oct 2010 15:22:47 -0400 |
parents | 57a59eae83cc |
children | fd0a3ac60b0e |
comparison
equal
deleted
inserted
replaced
11090:2adf4736dafa | 11091:5677f3f7b5fa |
---|---|
24 #ifdef HAVE_CONFIG_H | 24 #ifdef HAVE_CONFIG_H |
25 #include <config.h> | 25 #include <config.h> |
26 #endif | 26 #endif |
27 | 27 |
28 #include "error.h" | 28 #include "error.h" |
29 #include "defun.h" | |
29 #include "oct-obj.h" | 30 #include "oct-obj.h" |
30 #include "ov.h" | 31 #include "ov.h" |
31 #include "pt-binop.h" | 32 #include "pt-binop.h" |
32 #include "pt-bp.h" | 33 #include "pt-bp.h" |
33 #include "pt-walk.h" | 34 #include "pt-walk.h" |
35 #include "variables.h" | |
36 | |
37 // TRUE means we mark | and & expressions for braindead short-circuit | |
38 // behavior. | |
39 static bool Vdo_braindead_shortcircuit_evaluation; | |
34 | 40 |
35 // Binary expressions. | 41 // Binary expressions. |
36 | 42 |
37 octave_value_list | 43 octave_value_list |
38 tree_binary_expression::rvalue (int nargout) | 44 tree_binary_expression::rvalue (int nargout) |
54 octave_value retval; | 60 octave_value retval; |
55 | 61 |
56 if (error_state) | 62 if (error_state) |
57 return retval; | 63 return retval; |
58 | 64 |
65 if (Vdo_braindead_shortcircuit_evaluation | |
66 && eligible_for_braindead_shortcircuit) | |
67 { | |
68 if (op_lhs) | |
69 { | |
70 octave_value a = op_lhs->rvalue1 (); | |
71 | |
72 if (! error_state) | |
73 { | |
74 if (a.ndims () == 2 && a.rows () == 1 && a.columns () == 1) | |
75 { | |
76 bool result = false; | |
77 | |
78 bool a_true = a.is_true (); | |
79 | |
80 if (! error_state) | |
81 { | |
82 if (a_true) | |
83 { | |
84 if (etype == octave_value::op_el_or) | |
85 { | |
86 result = true; | |
87 goto done; | |
88 } | |
89 } | |
90 else | |
91 { | |
92 if (etype == octave_value::op_el_and) | |
93 goto done; | |
94 } | |
95 | |
96 if (op_rhs) | |
97 { | |
98 octave_value b = op_rhs->rvalue1 (); | |
99 | |
100 if (! error_state) | |
101 result = b.is_true (); | |
102 } | |
103 | |
104 done: | |
105 | |
106 if (! error_state) | |
107 return octave_value (result); | |
108 } | |
109 } | |
110 } | |
111 } | |
112 } | |
113 | |
59 if (op_lhs) | 114 if (op_lhs) |
60 { | 115 { |
61 octave_value a = op_lhs->rvalue1 (); | 116 octave_value a = op_lhs->rvalue1 (); |
62 | 117 |
63 if (! error_state && a.is_defined () && op_rhs) | 118 if (! error_state && a.is_defined () && op_rhs) |
205 | 260 |
206 new_be->copy_base (*this); | 261 new_be->copy_base (*this); |
207 | 262 |
208 return new_be; | 263 return new_be; |
209 } | 264 } |
265 | |
266 DEFUN (do_braindead_shortcircuit_evaluation, args, nargout, | |
267 "-*- texinfo -*-\n\ | |
268 @deftypefn {Built-in Function} {@var{val} =} do_braindead_shortcircuit_evaluation ()\n\ | |
269 @deftypefnx {Built-in Function} {@var{old_val} =} do_braindead_shortcircuit_evaluation (@var{new_val})\n\ | |
270 Query or set the internal variable that controls whether Octave will\n\ | |
271 do short-circuit evaluation of @samp{|} and @samp{&} operators inside the\n\ | |
272 conditions of if or while statements.\n\ | |
273 \n\ | |
274 This feature is only provided for compatibility with Matlab and should\n\ | |
275 not be used unless you are porting old code that relies on this feature.\n\ | |
276 \n\ | |
277 To obtain short-circuit behavior for logical expressions in new programs,\n\ | |
278 you should always use the @samp{&&} and @samp{||} operators.\n\ | |
279 @end deftypefn") | |
280 { | |
281 return SET_INTERNAL_VARIABLE (do_braindead_shortcircuit_evaluation); | |
282 } |