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