Mercurial > octave-nkf
annotate src/pt-binop.cc @ 7948:af10baa63915 ss-3-1-50
3.1.50 snapshot
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 18 Jul 2008 17:42:48 -0400 |
parents | 71f068b22fcc |
children | 3100283874d7 |
rev | line source |
---|---|
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 * |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
109 tree_binary_expression::dup (symbol_table::scope_id scope, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
110 symbol_table::context_id context) |
5861 | 111 { |
112 tree_binary_expression *new_be | |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
113 = new tree_binary_expression (op_lhs ? op_lhs->dup (scope, context) : 0, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
114 op_rhs ? op_rhs->dup (scope, context) : 0, |
5861 | 115 line (), column (), etype); |
116 | |
117 new_be->copy_base (*this); | |
118 | |
119 return new_be; | |
120 } | |
121 | |
2980 | 122 void |
123 tree_binary_expression::accept (tree_walker& tw) | |
124 { | |
125 tw.visit_binary_expression (*this); | |
126 } | |
127 | |
128 // Boolean expressions. | |
129 | |
130 octave_value_list | |
131 tree_boolean_expression::rvalue (int nargout) | |
132 { | |
133 octave_value_list retval; | |
134 | |
3770 | 135 MAYBE_DO_BREAKPOINT; |
136 | |
2980 | 137 if (nargout > 1) |
138 error ("binary operator `%s': invalid number of output arguments", | |
139 oper () . c_str ()); | |
140 else | |
141 retval = rvalue (); | |
142 | |
143 return retval; | |
144 } | |
145 | |
146 octave_value | |
147 tree_boolean_expression::rvalue (void) | |
148 { | |
149 octave_value retval; | |
150 | |
151 if (error_state) | |
152 return retval; | |
153 | |
154 bool result = false; | |
155 | |
156 if (op_lhs) | |
157 { | |
158 octave_value a = op_lhs->rvalue (); | |
159 | |
160 if (error_state) | |
161 eval_error (); | |
162 else | |
163 { | |
164 bool a_true = a.is_true (); | |
165 | |
166 if (error_state) | |
167 eval_error (); | |
168 else | |
169 { | |
170 if (a_true) | |
171 { | |
172 if (etype == bool_or) | |
173 { | |
174 result = true; | |
175 goto done; | |
176 } | |
177 } | |
178 else | |
179 { | |
180 if (etype == bool_and) | |
181 goto done; | |
182 } | |
183 | |
184 if (op_rhs) | |
185 { | |
186 octave_value b = op_rhs->rvalue (); | |
187 | |
188 if (error_state) | |
189 eval_error (); | |
190 else | |
191 { | |
192 result = b.is_true (); | |
193 | |
194 if (error_state) | |
195 eval_error (); | |
196 } | |
197 } | |
198 else | |
199 eval_error (); | |
200 | |
201 done: | |
202 | |
203 if (! error_state) | |
3798 | 204 retval = octave_value (result); |
2980 | 205 } |
206 } | |
207 } | |
208 else | |
209 eval_error (); | |
210 | |
211 return retval; | |
212 } | |
213 | |
3536 | 214 std::string |
2980 | 215 tree_boolean_expression::oper (void) const |
216 { | |
3523 | 217 std::string retval = "<unknown>"; |
2980 | 218 |
219 switch (etype) | |
220 { | |
221 case bool_and: | |
222 retval = "&&"; | |
223 break; | |
224 | |
225 case bool_or: | |
226 retval = "||"; | |
227 break; | |
228 | |
229 default: | |
230 break; | |
231 } | |
232 | |
233 return retval; | |
234 } | |
235 | |
5861 | 236 tree_expression * |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
237 tree_boolean_expression::dup (symbol_table::scope_id scope, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
238 symbol_table::context_id context) |
5861 | 239 { |
240 tree_boolean_expression *new_be | |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
241 = new tree_boolean_expression (op_lhs ? op_lhs->dup (scope, context) : 0, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
242 op_rhs ? op_rhs->dup (scope, context) : 0, |
5861 | 243 line (), column (), etype); |
244 | |
245 new_be->copy_base (*this); | |
246 | |
247 return new_be; | |
248 } | |
249 | |
2980 | 250 /* |
251 ;;; Local Variables: *** | |
252 ;;; mode: C++ *** | |
253 ;;; End: *** | |
254 */ |