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 |
3503
|
28 #include <iostream> |
2980
|
29 |
|
30 #include "defun.h" |
|
31 #include "error.h" |
|
32 #include "input.h" |
|
33 #include "oct-obj.h" |
|
34 #include "oct-lvalue.h" |
|
35 #include "pager.h" |
|
36 #include "ov.h" |
2982
|
37 #include "pt-arg-list.h" |
3770
|
38 #include "pt-bp.h" |
2980
|
39 #include "pt-assign.h" |
|
40 #include "pt-walk.h" |
|
41 #include "utils.h" |
5794
|
42 #include "variables.h" |
2980
|
43 |
|
44 // Simple assignment expressions. |
|
45 |
|
46 tree_simple_assignment::~tree_simple_assignment (void) |
|
47 { |
|
48 if (! preserve) |
|
49 delete lhs; |
|
50 |
|
51 delete rhs; |
|
52 } |
|
53 |
|
54 octave_value_list |
|
55 tree_simple_assignment::rvalue (int nargout) |
|
56 { |
|
57 octave_value_list retval; |
|
58 |
3770
|
59 MAYBE_DO_BREAKPOINT; |
|
60 |
2980
|
61 if (nargout > 1) |
|
62 error ("invalid number of output arguments for expression X = RHS"); |
|
63 else |
|
64 retval = rvalue (); |
|
65 |
|
66 return retval; |
|
67 } |
|
68 |
5775
|
69 // FIXME -- this works, but it would look a little better if |
2984
|
70 // it were broken up into a couple of separate functions. |
|
71 |
2980
|
72 octave_value |
|
73 tree_simple_assignment::rvalue (void) |
|
74 { |
3208
|
75 octave_value retval; |
2980
|
76 |
|
77 if (error_state) |
3208
|
78 return retval; |
2980
|
79 |
|
80 if (rhs) |
|
81 { |
|
82 octave_value_list tmp = rhs->rvalue (); |
|
83 |
|
84 if (! (error_state || tmp.empty ())) |
|
85 { |
3208
|
86 octave_value rhs_val = tmp(0); |
2980
|
87 |
|
88 if (rhs_val.is_undefined ()) |
|
89 { |
|
90 error ("value on right hand side of assignment is undefined"); |
|
91 eval_error (); |
|
92 } |
5750
|
93 else if (rhs_val.is_cs_list ()) |
|
94 { |
|
95 error ("invalid assignment of comma-separated list"); |
|
96 eval_error (); |
|
97 } |
2980
|
98 else |
|
99 { |
|
100 octave_lvalue ult = lhs->lvalue (); |
|
101 |
|
102 if (error_state) |
|
103 eval_error (); |
|
104 else |
|
105 { |
|
106 ult.assign (etype, rhs_val); |
|
107 |
3215
|
108 if (! error_state) |
|
109 { |
3527
|
110 if (etype == octave_value::op_asn_eq) |
3215
|
111 retval = rhs_val; |
|
112 else |
|
113 retval = ult.value (); |
2984
|
114 |
3215
|
115 if (print_result ()) |
3208
|
116 { |
5841
|
117 // We clear any index here so that we can |
|
118 // get the new value of the referenced |
|
119 // object below, instead of the indexed |
|
120 // value (which should be the same as the |
|
121 // right hand side value). |
2980
|
122 |
5841
|
123 ult.clear_index (); |
3208
|
124 |
5841
|
125 octave_value lhs_val = ult.value (); |
3208
|
126 |
5841
|
127 if (! error_state) |
|
128 lhs_val.print_with_name (octave_stdout, |
|
129 lhs->name ()); |
2980
|
130 } |
|
131 } |
3215
|
132 else |
|
133 eval_error (); |
2980
|
134 } |
|
135 } |
|
136 } |
|
137 else |
|
138 eval_error (); |
|
139 } |
|
140 |
3208
|
141 return retval; |
2980
|
142 } |
|
143 |
|
144 void |
|
145 tree_simple_assignment::eval_error (void) |
|
146 { |
3965
|
147 int l = line (); |
|
148 int c = column (); |
2980
|
149 |
3965
|
150 if (l != -1 && c != -1) |
|
151 ::error ("evaluating assignment expression near line %d, column %d", |
|
152 l, c); |
2980
|
153 } |
|
154 |
3536
|
155 std::string |
2980
|
156 tree_simple_assignment::oper (void) const |
|
157 { |
|
158 return octave_value::assign_op_as_string (etype); |
|
159 } |
|
160 |
|
161 void |
|
162 tree_simple_assignment::accept (tree_walker& tw) |
|
163 { |
|
164 tw.visit_simple_assignment (*this); |
|
165 } |
|
166 |
|
167 // Multi-valued assignment expressions. |
|
168 |
|
169 tree_multi_assignment::~tree_multi_assignment (void) |
|
170 { |
|
171 if (! preserve) |
|
172 delete lhs; |
|
173 |
|
174 delete rhs; |
|
175 } |
|
176 |
|
177 octave_value |
|
178 tree_multi_assignment::rvalue (void) |
|
179 { |
|
180 octave_value retval; |
|
181 |
|
182 octave_value_list tmp = rvalue (1); |
|
183 |
|
184 if (! tmp.empty ()) |
|
185 retval = tmp(0); |
|
186 |
|
187 return retval; |
|
188 } |
|
189 |
5775
|
190 // FIXME -- this works, but it would look a little better if |
2984
|
191 // it were broken up into a couple of separate functions. |
|
192 |
2980
|
193 octave_value_list |
|
194 tree_multi_assignment::rvalue (int) |
|
195 { |
3208
|
196 octave_value_list retval; |
2980
|
197 |
|
198 if (error_state) |
3208
|
199 return retval; |
2980
|
200 |
|
201 if (rhs) |
|
202 { |
3977
|
203 int n_out = lhs->nargout_count (); |
|
204 |
|
205 if (error_state) |
|
206 return retval; |
2980
|
207 |
3208
|
208 octave_value_list rhs_val = rhs->rvalue (n_out); |
2980
|
209 |
3977
|
210 if (error_state) |
|
211 return retval; |
2980
|
212 |
3977
|
213 if (rhs_val.empty ()) |
|
214 { |
|
215 error ("value on right hand side of assignment is undefined"); |
|
216 eval_error (); |
5750
|
217 return retval; |
3977
|
218 } |
|
219 else |
|
220 { |
|
221 int k = 0; |
2980
|
222 |
3977
|
223 int n = rhs_val.length (); |
2980
|
224 |
5265
|
225 if (n == 1) |
|
226 { |
|
227 octave_value tmp = rhs_val(0); |
|
228 |
|
229 if (tmp.is_cs_list ()) |
|
230 { |
5750
|
231 error ("invalid assignment of comma-separated list"); |
|
232 eval_error (); |
|
233 return retval; |
5265
|
234 } |
|
235 } |
|
236 |
3977
|
237 retval.resize (n, octave_value ()); |
3208
|
238 |
4219
|
239 for (tree_argument_list::iterator p = lhs->begin (); |
|
240 p != lhs->end (); |
|
241 p++) |
3977
|
242 { |
4219
|
243 tree_expression *lhs_elt = *p; |
3977
|
244 |
|
245 if (lhs_elt) |
|
246 { |
|
247 octave_lvalue ult = lhs_elt->lvalue (); |
2980
|
248 |
3977
|
249 if (error_state) |
5750
|
250 { |
|
251 eval_error (); |
|
252 break; |
|
253 } |
3977
|
254 else if (k < n) |
|
255 { |
|
256 ult.assign (etype, rhs_val(k)); |
|
257 |
|
258 if (! error_state) |
3208
|
259 { |
3977
|
260 if (etype == octave_value::op_asn_eq) |
|
261 retval(k) = rhs_val(k); |
3208
|
262 else |
3977
|
263 retval(k) = ult.value (); |
2980
|
264 } |
|
265 } |
3208
|
266 else |
3977
|
267 error ("element number %d undefined in return list", k+1); |
2980
|
268 |
|
269 if (error_state) |
5750
|
270 { |
|
271 eval_error (); |
|
272 break; |
|
273 } |
3977
|
274 else if (print_result ()) |
|
275 { |
5841
|
276 // We clear any index here so that we can get |
|
277 // the new value of the referenced object below, |
|
278 // instead of the indexed value (which should be |
|
279 // the same as the right hand side value). |
3977
|
280 |
5841
|
281 ult.clear_index (); |
3208
|
282 |
5841
|
283 octave_value lhs_val = ult.value (); |
3977
|
284 |
5841
|
285 if (! error_state) |
|
286 lhs_val.print_with_name (octave_stdout, |
|
287 lhs_elt->name ()); |
3977
|
288 } |
2980
|
289 } |
3977
|
290 else |
|
291 eval_error (); |
|
292 |
|
293 if (error_state) |
|
294 break; |
|
295 |
|
296 k++; |
2980
|
297 } |
|
298 } |
|
299 } |
3208
|
300 else |
|
301 eval_error (); |
2980
|
302 |
3208
|
303 return retval; |
2980
|
304 } |
|
305 |
|
306 void |
|
307 tree_multi_assignment::eval_error (void) |
|
308 { |
3965
|
309 int l = line (); |
|
310 int c = column (); |
2980
|
311 |
3965
|
312 if (l != -1 && c != -1) |
|
313 ::error ("evaluating assignment expression near line %d, column %d", |
|
314 l, c); |
2980
|
315 } |
|
316 |
3536
|
317 std::string |
3208
|
318 tree_multi_assignment::oper (void) const |
|
319 { |
|
320 return octave_value::assign_op_as_string (etype); |
|
321 } |
|
322 |
2980
|
323 void |
|
324 tree_multi_assignment::accept (tree_walker& tw) |
|
325 { |
|
326 tw.visit_multi_assignment (*this); |
|
327 } |
|
328 |
|
329 /* |
|
330 ;;; Local Variables: *** |
|
331 ;;; mode: C++ *** |
|
332 ;;; End: *** |
|
333 */ |