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