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