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