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