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 |
|
31 #include <iostream.h> |
|
32 #include <strstream.h> |
|
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" |
2980
|
42 #include "pt-assign.h" |
|
43 #include "pt-walk.h" |
|
44 #include "utils.h" |
|
45 |
|
46 // TRUE means print the right hand side of an assignment instead of |
|
47 // the left. |
|
48 static bool Vprint_rhs_assign_val; |
|
49 |
|
50 // Simple assignment expressions. |
|
51 |
|
52 tree_simple_assignment::~tree_simple_assignment (void) |
|
53 { |
|
54 if (! preserve) |
|
55 delete lhs; |
|
56 |
|
57 delete rhs; |
|
58 } |
|
59 |
|
60 octave_value_list |
|
61 tree_simple_assignment::rvalue (int nargout) |
|
62 { |
|
63 octave_value_list retval; |
|
64 |
|
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 |
2984
|
73 // XXX FIXME XXX -- this works, but it would look a little better if |
|
74 // it were broken up into a couple of separate functions. |
|
75 |
2980
|
76 octave_value |
|
77 tree_simple_assignment::rvalue (void) |
|
78 { |
|
79 octave_value rhs_val; |
|
80 |
|
81 if (error_state) |
|
82 return rhs_val; |
|
83 |
|
84 if (rhs) |
|
85 { |
|
86 octave_value_list tmp = rhs->rvalue (); |
|
87 |
|
88 if (! (error_state || tmp.empty ())) |
|
89 { |
|
90 rhs_val = tmp(0); |
|
91 |
|
92 if (rhs_val.is_undefined ()) |
|
93 { |
|
94 error ("value on right hand side of assignment is undefined"); |
|
95 eval_error (); |
|
96 } |
|
97 else |
|
98 { |
|
99 octave_lvalue ult = lhs->lvalue (); |
|
100 |
|
101 if (error_state) |
|
102 eval_error (); |
|
103 else |
|
104 { |
|
105 ult.assign (etype, rhs_val); |
|
106 |
2984
|
107 // We clear any index here so that we can get the |
|
108 // new value of the referenced object below, instead |
|
109 // of the indexed value (which should be the same as |
|
110 // the right hand side value). |
|
111 |
|
112 ult.clear_index (); |
|
113 |
2980
|
114 if (error_state) |
|
115 eval_error (); |
2991
|
116 else |
2980
|
117 { |
|
118 octave_value lhs_val = ult.value (); |
|
119 |
|
120 if (! error_state && print_result ()) |
|
121 { |
|
122 if (Vprint_rhs_assign_val) |
2991
|
123 rhs_val.print_with_name (octave_stdout, |
|
124 lhs->str_print_code ()); |
2980
|
125 else |
|
126 lhs_val.print_with_name (octave_stdout, |
|
127 lhs->name ()); |
|
128 } |
|
129 } |
|
130 } |
|
131 } |
|
132 } |
|
133 else |
|
134 eval_error (); |
|
135 } |
|
136 |
|
137 return rhs_val; |
|
138 } |
|
139 |
|
140 void |
|
141 tree_simple_assignment::eval_error (void) |
|
142 { |
|
143 if (error_state > 0) |
|
144 { |
|
145 int l = line (); |
|
146 int c = column (); |
|
147 |
|
148 if (l != -1 && c != -1) |
|
149 ::error ("evaluating assignment expression near line %d, column %d", |
|
150 l, c); |
|
151 } |
|
152 } |
|
153 |
|
154 string |
|
155 tree_simple_assignment::oper (void) const |
|
156 { |
|
157 return octave_value::assign_op_as_string (etype); |
|
158 } |
|
159 |
|
160 void |
|
161 tree_simple_assignment::accept (tree_walker& tw) |
|
162 { |
|
163 tw.visit_simple_assignment (*this); |
|
164 } |
|
165 |
|
166 // Multi-valued assignment expressions. |
|
167 |
|
168 tree_multi_assignment::~tree_multi_assignment (void) |
|
169 { |
|
170 if (! preserve) |
|
171 delete lhs; |
|
172 |
|
173 delete rhs; |
|
174 } |
|
175 |
|
176 octave_value |
|
177 tree_multi_assignment::rvalue (void) |
|
178 { |
|
179 octave_value retval; |
|
180 |
|
181 octave_value_list tmp = rvalue (1); |
|
182 |
|
183 if (! tmp.empty ()) |
|
184 retval = tmp(0); |
|
185 |
|
186 return retval; |
|
187 } |
|
188 |
2984
|
189 // XXX FIXME XXX -- this works, but it would look a little better if |
|
190 // it were broken up into a couple of separate functions. |
|
191 |
2980
|
192 octave_value_list |
|
193 tree_multi_assignment::rvalue (int) |
|
194 { |
|
195 octave_value_list rhs_val; |
|
196 |
|
197 if (error_state) |
|
198 return rhs_val; |
|
199 |
|
200 if (rhs) |
|
201 { |
|
202 int n_out = lhs->length (); |
|
203 |
|
204 rhs_val = rhs->rvalue (n_out); |
|
205 |
|
206 if (! (error_state || rhs_val.empty ())) |
|
207 { |
|
208 if (rhs_val.empty ()) |
|
209 { |
|
210 error ("value on right hand side of assignment is undefined"); |
|
211 eval_error (); |
|
212 } |
|
213 else |
|
214 { |
|
215 int k = 0; |
|
216 |
|
217 int n = rhs_val.length (); |
|
218 |
|
219 for (Pix p = lhs->first (); p != 0; lhs->next (p)) |
|
220 { |
|
221 tree_expression *lhs_elt = lhs->operator () (p); |
|
222 |
|
223 if (lhs_elt) |
|
224 { |
|
225 octave_lvalue ult = lhs_elt->lvalue (); |
|
226 |
|
227 if (error_state) |
|
228 eval_error (); |
|
229 else |
|
230 { |
|
231 octave_value tmp = k < n |
|
232 ? rhs_val(k++) : octave_value (); |
|
233 |
|
234 if (tmp.is_defined ()) |
|
235 { |
|
236 // XXX FIXME XXX -- handle other assignment ops. |
|
237 ult.assign (octave_value::asn_eq, tmp); |
2984
|
238 |
|
239 // We clear any index here so that we |
|
240 // can get the new value of the |
|
241 // referenced object below, instead of |
|
242 // the indexed value (which should be |
|
243 // the same as the right hand side |
|
244 // value). |
|
245 |
|
246 ult.clear_index (); |
2980
|
247 } |
|
248 else |
|
249 error ("element number %d undefined in return list", k); |
|
250 |
|
251 if (error_state) |
|
252 eval_error (); |
|
253 else if (! Vprint_rhs_assign_val) |
|
254 { |
|
255 octave_value lhs_val = ult.value (); |
|
256 |
|
257 if (! error_state && print_result ()) |
|
258 { |
|
259 if (Vprint_rhs_assign_val) |
2991
|
260 tmp.print_with_name (octave_stdout, |
|
261 lhs_elt->str_print_code ()); |
2980
|
262 else |
|
263 lhs_val.print_with_name (octave_stdout, |
|
264 lhs_elt->name ()); |
|
265 } |
|
266 } |
|
267 } |
|
268 } |
|
269 |
|
270 if (error_state) |
|
271 break; |
|
272 } |
|
273 } |
|
274 } |
|
275 else |
|
276 eval_error (); |
|
277 } |
|
278 |
|
279 return rhs_val; |
|
280 } |
|
281 |
|
282 void |
|
283 tree_multi_assignment::eval_error (void) |
|
284 { |
|
285 if (error_state > 0) |
|
286 { |
|
287 int l = line (); |
|
288 int c = column (); |
|
289 |
|
290 if (l != -1 && c != -1) |
|
291 ::error ("evaluating assignment expression near line %d, column %d", |
|
292 l, c); |
|
293 } |
|
294 } |
|
295 |
|
296 void |
|
297 tree_multi_assignment::accept (tree_walker& tw) |
|
298 { |
|
299 tw.visit_multi_assignment (*this); |
|
300 } |
|
301 |
|
302 static int |
|
303 print_rhs_assign_val (void) |
|
304 { |
|
305 Vprint_rhs_assign_val = check_preference ("print_rhs_assign_val"); |
|
306 |
|
307 return 0; |
|
308 } |
|
309 |
|
310 void |
|
311 symbols_of_pt_assign (void) |
|
312 { |
|
313 DEFVAR (print_rhs_assign_val, 0.0, 0, print_rhs_assign_val, |
|
314 "if TRUE, print the right hand side of assignments instead of the left"); |
|
315 } |
|
316 |
|
317 /* |
|
318 ;;; Local Variables: *** |
|
319 ;;; mode: C++ *** |
|
320 ;;; End: *** |
|
321 */ |