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 |
|
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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
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 |
|
108 void |
|
109 tree_binary_expression::accept (tree_walker& tw) |
|
110 { |
|
111 tw.visit_binary_expression (*this); |
|
112 } |
|
113 |
|
114 // Boolean expressions. |
|
115 |
|
116 octave_value_list |
|
117 tree_boolean_expression::rvalue (int nargout) |
|
118 { |
|
119 octave_value_list retval; |
|
120 |
3770
|
121 MAYBE_DO_BREAKPOINT; |
|
122 |
2980
|
123 if (nargout > 1) |
|
124 error ("binary operator `%s': invalid number of output arguments", |
|
125 oper () . c_str ()); |
|
126 else |
|
127 retval = rvalue (); |
|
128 |
|
129 return retval; |
|
130 } |
|
131 |
|
132 octave_value |
|
133 tree_boolean_expression::rvalue (void) |
|
134 { |
|
135 octave_value retval; |
|
136 |
|
137 if (error_state) |
|
138 return retval; |
|
139 |
|
140 bool result = false; |
|
141 |
|
142 if (op_lhs) |
|
143 { |
|
144 octave_value a = op_lhs->rvalue (); |
|
145 |
|
146 if (error_state) |
|
147 eval_error (); |
|
148 else |
|
149 { |
|
150 bool a_true = a.is_true (); |
|
151 |
|
152 if (error_state) |
|
153 eval_error (); |
|
154 else |
|
155 { |
|
156 if (a_true) |
|
157 { |
|
158 if (etype == bool_or) |
|
159 { |
|
160 result = true; |
|
161 goto done; |
|
162 } |
|
163 } |
|
164 else |
|
165 { |
|
166 if (etype == bool_and) |
|
167 goto done; |
|
168 } |
|
169 |
|
170 if (op_rhs) |
|
171 { |
|
172 octave_value b = op_rhs->rvalue (); |
|
173 |
|
174 if (error_state) |
|
175 eval_error (); |
|
176 else |
|
177 { |
|
178 result = b.is_true (); |
|
179 |
|
180 if (error_state) |
|
181 eval_error (); |
|
182 } |
|
183 } |
|
184 else |
|
185 eval_error (); |
|
186 |
|
187 done: |
|
188 |
|
189 if (! error_state) |
3798
|
190 retval = octave_value (result); |
2980
|
191 } |
|
192 } |
|
193 } |
|
194 else |
|
195 eval_error (); |
|
196 |
|
197 return retval; |
|
198 } |
|
199 |
3536
|
200 std::string |
2980
|
201 tree_boolean_expression::oper (void) const |
|
202 { |
3523
|
203 std::string retval = "<unknown>"; |
2980
|
204 |
|
205 switch (etype) |
|
206 { |
|
207 case bool_and: |
|
208 retval = "&&"; |
|
209 break; |
|
210 |
|
211 case bool_or: |
|
212 retval = "||"; |
|
213 break; |
|
214 |
|
215 default: |
|
216 break; |
|
217 } |
|
218 |
|
219 return retval; |
|
220 } |
|
221 |
|
222 /* |
|
223 ;;; Local Variables: *** |
|
224 ;;; mode: C++ *** |
|
225 ;;; End: *** |
|
226 */ |