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