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