2980
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1996, 1997, 2000, 2001, 2002, 2004, 2005, 2006, 2007 |
|
4 John W. Eaton |
2980
|
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 |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
2980
|
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 |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
2980
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include "error.h" |
|
29 #include "oct-obj.h" |
|
30 #include "ov.h" |
|
31 #include "pt-binop.h" |
3770
|
32 #include "pt-bp.h" |
2980
|
33 #include "pt-walk.h" |
|
34 |
|
35 // Binary expressions. |
|
36 |
|
37 octave_value_list |
|
38 tree_binary_expression::rvalue (int nargout) |
|
39 { |
|
40 octave_value_list retval; |
|
41 |
|
42 if (nargout > 1) |
|
43 error ("binary operator `%s': invalid number of output arguments", |
|
44 oper () . c_str ()); |
|
45 else |
|
46 retval = rvalue (); |
|
47 |
|
48 return retval; |
|
49 } |
|
50 |
|
51 octave_value |
|
52 tree_binary_expression::rvalue (void) |
|
53 { |
|
54 octave_value retval; |
|
55 |
3770
|
56 MAYBE_DO_BREAKPOINT; |
|
57 |
2980
|
58 if (error_state) |
|
59 return retval; |
|
60 |
|
61 if (op_lhs) |
|
62 { |
|
63 octave_value a = op_lhs->rvalue (); |
|
64 |
|
65 if (error_state) |
|
66 eval_error (); |
|
67 else if (a.is_defined () && op_rhs) |
|
68 { |
|
69 octave_value b = op_rhs->rvalue (); |
|
70 |
|
71 if (error_state) |
|
72 eval_error (); |
|
73 else if (b.is_defined ()) |
|
74 { |
|
75 retval = ::do_binary_op (etype, a, b); |
|
76 |
|
77 if (error_state) |
|
78 { |
|
79 retval = octave_value (); |
|
80 eval_error (); |
|
81 } |
|
82 } |
|
83 else |
|
84 eval_error (); |
|
85 } |
|
86 else |
|
87 eval_error (); |
|
88 } |
|
89 else |
|
90 eval_error (); |
|
91 |
|
92 return retval; |
|
93 } |
|
94 |
|
95 void |
|
96 tree_binary_expression::eval_error (void) |
|
97 { |
3965
|
98 ::error ("evaluating binary operator `%s' near line %d, column %d", |
|
99 oper () . c_str (), line (), column ()); |
2980
|
100 } |
|
101 |
3536
|
102 std::string |
2980
|
103 tree_binary_expression::oper (void) const |
|
104 { |
|
105 return octave_value::binary_op_as_string (etype); |
|
106 } |
|
107 |
5861
|
108 tree_expression * |
7336
|
109 tree_binary_expression::dup (symbol_table::scope_id scope) |
5861
|
110 { |
|
111 tree_binary_expression *new_be |
7336
|
112 = new tree_binary_expression (op_lhs ? op_lhs->dup (scope) : 0, |
|
113 op_rhs ? op_rhs->dup (scope) : 0, |
5861
|
114 line (), column (), etype); |
|
115 |
|
116 new_be->copy_base (*this); |
|
117 |
|
118 return new_be; |
|
119 } |
|
120 |
2980
|
121 void |
|
122 tree_binary_expression::accept (tree_walker& tw) |
|
123 { |
|
124 tw.visit_binary_expression (*this); |
|
125 } |
|
126 |
|
127 // Boolean expressions. |
|
128 |
|
129 octave_value_list |
|
130 tree_boolean_expression::rvalue (int nargout) |
|
131 { |
|
132 octave_value_list retval; |
|
133 |
3770
|
134 MAYBE_DO_BREAKPOINT; |
|
135 |
2980
|
136 if (nargout > 1) |
|
137 error ("binary operator `%s': invalid number of output arguments", |
|
138 oper () . c_str ()); |
|
139 else |
|
140 retval = rvalue (); |
|
141 |
|
142 return retval; |
|
143 } |
|
144 |
|
145 octave_value |
|
146 tree_boolean_expression::rvalue (void) |
|
147 { |
|
148 octave_value retval; |
|
149 |
|
150 if (error_state) |
|
151 return retval; |
|
152 |
|
153 bool result = false; |
|
154 |
|
155 if (op_lhs) |
|
156 { |
|
157 octave_value a = op_lhs->rvalue (); |
|
158 |
|
159 if (error_state) |
|
160 eval_error (); |
|
161 else |
|
162 { |
|
163 bool a_true = a.is_true (); |
|
164 |
|
165 if (error_state) |
|
166 eval_error (); |
|
167 else |
|
168 { |
|
169 if (a_true) |
|
170 { |
|
171 if (etype == bool_or) |
|
172 { |
|
173 result = true; |
|
174 goto done; |
|
175 } |
|
176 } |
|
177 else |
|
178 { |
|
179 if (etype == bool_and) |
|
180 goto done; |
|
181 } |
|
182 |
|
183 if (op_rhs) |
|
184 { |
|
185 octave_value b = op_rhs->rvalue (); |
|
186 |
|
187 if (error_state) |
|
188 eval_error (); |
|
189 else |
|
190 { |
|
191 result = b.is_true (); |
|
192 |
|
193 if (error_state) |
|
194 eval_error (); |
|
195 } |
|
196 } |
|
197 else |
|
198 eval_error (); |
|
199 |
|
200 done: |
|
201 |
|
202 if (! error_state) |
3798
|
203 retval = octave_value (result); |
2980
|
204 } |
|
205 } |
|
206 } |
|
207 else |
|
208 eval_error (); |
|
209 |
|
210 return retval; |
|
211 } |
|
212 |
3536
|
213 std::string |
2980
|
214 tree_boolean_expression::oper (void) const |
|
215 { |
3523
|
216 std::string retval = "<unknown>"; |
2980
|
217 |
|
218 switch (etype) |
|
219 { |
|
220 case bool_and: |
|
221 retval = "&&"; |
|
222 break; |
|
223 |
|
224 case bool_or: |
|
225 retval = "||"; |
|
226 break; |
|
227 |
|
228 default: |
|
229 break; |
|
230 } |
|
231 |
|
232 return retval; |
|
233 } |
|
234 |
5861
|
235 tree_expression * |
7336
|
236 tree_boolean_expression::dup (symbol_table::scope_id scope) |
5861
|
237 { |
|
238 tree_boolean_expression *new_be |
7336
|
239 = new tree_boolean_expression (op_lhs ? op_lhs->dup (scope) : 0, |
|
240 op_rhs ? op_rhs->dup (scope) : 0, |
5861
|
241 line (), column (), etype); |
|
242 |
|
243 new_be->copy_base (*this); |
|
244 |
|
245 return new_be; |
|
246 } |
|
247 |
2980
|
248 /* |
|
249 ;;; Local Variables: *** |
|
250 ;;; mode: C++ *** |
|
251 ;;; End: *** |
|
252 */ |