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