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