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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
2980
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
|
31 #include "error.h" |
|
32 #include "oct-obj.h" |
|
33 #include "ov.h" |
|
34 #include "pt-binop.h" |
3770
|
35 #include "pt-bp.h" |
2980
|
36 #include "pt-walk.h" |
|
37 |
|
38 // Binary expressions. |
|
39 |
|
40 octave_value_list |
|
41 tree_binary_expression::rvalue (int nargout) |
|
42 { |
|
43 octave_value_list retval; |
|
44 |
|
45 if (nargout > 1) |
|
46 error ("binary operator `%s': invalid number of output arguments", |
|
47 oper () . c_str ()); |
|
48 else |
|
49 retval = rvalue (); |
|
50 |
|
51 return retval; |
|
52 } |
|
53 |
|
54 octave_value |
|
55 tree_binary_expression::rvalue (void) |
|
56 { |
|
57 octave_value retval; |
|
58 |
3770
|
59 MAYBE_DO_BREAKPOINT; |
|
60 |
2980
|
61 if (error_state) |
|
62 return retval; |
|
63 |
|
64 if (op_lhs) |
|
65 { |
|
66 octave_value a = op_lhs->rvalue (); |
|
67 |
|
68 if (error_state) |
|
69 eval_error (); |
|
70 else if (a.is_defined () && op_rhs) |
|
71 { |
|
72 octave_value b = op_rhs->rvalue (); |
|
73 |
|
74 if (error_state) |
|
75 eval_error (); |
|
76 else if (b.is_defined ()) |
|
77 { |
|
78 retval = ::do_binary_op (etype, a, b); |
|
79 |
|
80 if (error_state) |
|
81 { |
|
82 retval = octave_value (); |
|
83 eval_error (); |
|
84 } |
|
85 } |
|
86 else |
|
87 eval_error (); |
|
88 } |
|
89 else |
|
90 eval_error (); |
|
91 } |
|
92 else |
|
93 eval_error (); |
|
94 |
|
95 return retval; |
|
96 } |
|
97 |
|
98 void |
|
99 tree_binary_expression::eval_error (void) |
|
100 { |
3965
|
101 ::error ("evaluating binary operator `%s' near line %d, column %d", |
|
102 oper () . c_str (), line (), column ()); |
2980
|
103 } |
|
104 |
3536
|
105 std::string |
2980
|
106 tree_binary_expression::oper (void) const |
|
107 { |
|
108 return octave_value::binary_op_as_string (etype); |
|
109 } |
|
110 |
|
111 void |
|
112 tree_binary_expression::accept (tree_walker& tw) |
|
113 { |
|
114 tw.visit_binary_expression (*this); |
|
115 } |
|
116 |
|
117 // Boolean expressions. |
|
118 |
|
119 octave_value_list |
|
120 tree_boolean_expression::rvalue (int nargout) |
|
121 { |
|
122 octave_value_list retval; |
|
123 |
3770
|
124 MAYBE_DO_BREAKPOINT; |
|
125 |
2980
|
126 if (nargout > 1) |
|
127 error ("binary operator `%s': invalid number of output arguments", |
|
128 oper () . c_str ()); |
|
129 else |
|
130 retval = rvalue (); |
|
131 |
|
132 return retval; |
|
133 } |
|
134 |
|
135 octave_value |
|
136 tree_boolean_expression::rvalue (void) |
|
137 { |
|
138 octave_value retval; |
|
139 |
|
140 if (error_state) |
|
141 return retval; |
|
142 |
|
143 bool result = false; |
|
144 |
|
145 if (op_lhs) |
|
146 { |
|
147 octave_value a = op_lhs->rvalue (); |
|
148 |
|
149 if (error_state) |
|
150 eval_error (); |
|
151 else |
|
152 { |
|
153 bool a_true = a.is_true (); |
|
154 |
|
155 if (error_state) |
|
156 eval_error (); |
|
157 else |
|
158 { |
|
159 if (a_true) |
|
160 { |
|
161 if (etype == bool_or) |
|
162 { |
|
163 result = true; |
|
164 goto done; |
|
165 } |
|
166 } |
|
167 else |
|
168 { |
|
169 if (etype == bool_and) |
|
170 goto done; |
|
171 } |
|
172 |
|
173 if (op_rhs) |
|
174 { |
|
175 octave_value b = op_rhs->rvalue (); |
|
176 |
|
177 if (error_state) |
|
178 eval_error (); |
|
179 else |
|
180 { |
|
181 result = b.is_true (); |
|
182 |
|
183 if (error_state) |
|
184 eval_error (); |
|
185 } |
|
186 } |
|
187 else |
|
188 eval_error (); |
|
189 |
|
190 done: |
|
191 |
|
192 if (! error_state) |
3798
|
193 retval = octave_value (result); |
2980
|
194 } |
|
195 } |
|
196 } |
|
197 else |
|
198 eval_error (); |
|
199 |
|
200 return retval; |
|
201 } |
|
202 |
3536
|
203 std::string |
2980
|
204 tree_boolean_expression::oper (void) const |
|
205 { |
3523
|
206 std::string retval = "<unknown>"; |
2980
|
207 |
|
208 switch (etype) |
|
209 { |
|
210 case bool_and: |
|
211 retval = "&&"; |
|
212 break; |
|
213 |
|
214 case bool_or: |
|
215 retval = "||"; |
|
216 break; |
|
217 |
|
218 default: |
|
219 break; |
|
220 } |
|
221 |
|
222 return retval; |
|
223 } |
|
224 |
|
225 /* |
|
226 ;;; Local Variables: *** |
|
227 ;;; mode: C++ *** |
|
228 ;;; End: *** |
|
229 */ |