Mercurial > hg > octave-lyh
comparison src/pt-binop.cc @ 2980:cd5ad3fd8049
[project @ 1997-05-16 01:12:13 by jwe]
author | jwe |
---|---|
date | Fri, 16 May 1997 01:13:19 +0000 |
parents | |
children | 20f5cec4f11c |
comparison
equal
deleted
inserted
replaced
2979:a3556d2adec9 | 2980:cd5ad3fd8049 |
---|---|
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-pr-code.h" | |
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 | |
59 if (error_state) | |
60 return retval; | |
61 | |
62 if (op_lhs) | |
63 { | |
64 octave_value a = op_lhs->rvalue (); | |
65 | |
66 if (error_state) | |
67 eval_error (); | |
68 else if (a.is_defined () && op_rhs) | |
69 { | |
70 octave_value b = op_rhs->rvalue (); | |
71 | |
72 if (error_state) | |
73 eval_error (); | |
74 else if (b.is_defined ()) | |
75 { | |
76 retval = ::do_binary_op (etype, a, b); | |
77 | |
78 if (error_state) | |
79 { | |
80 retval = octave_value (); | |
81 eval_error (); | |
82 } | |
83 } | |
84 else | |
85 eval_error (); | |
86 } | |
87 else | |
88 eval_error (); | |
89 } | |
90 else | |
91 eval_error (); | |
92 | |
93 return retval; | |
94 } | |
95 | |
96 void | |
97 tree_binary_expression::eval_error (void) | |
98 { | |
99 if (error_state > 0) | |
100 ::error ("evaluating binary operator `%s' near line %d, column %d", | |
101 oper () . c_str (), line (), column ()); | |
102 } | |
103 | |
104 string | |
105 tree_binary_expression::oper (void) const | |
106 { | |
107 return octave_value::binary_op_as_string (etype); | |
108 } | |
109 | |
110 void | |
111 tree_binary_expression::accept (tree_walker& tw) | |
112 { | |
113 tw.visit_binary_expression (*this); | |
114 } | |
115 | |
116 // Boolean expressions. | |
117 | |
118 octave_value_list | |
119 tree_boolean_expression::rvalue (int nargout) | |
120 { | |
121 octave_value_list retval; | |
122 | |
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) | |
190 retval = octave_value (static_cast<double> (result)); | |
191 } | |
192 } | |
193 } | |
194 else | |
195 eval_error (); | |
196 | |
197 return retval; | |
198 } | |
199 | |
200 string | |
201 tree_boolean_expression::oper (void) const | |
202 { | |
203 string retval = "<unknown>"; | |
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 */ |