1741
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1741
|
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 |
2942
|
33 #include "str-vec.h" |
|
34 |
1741
|
35 #include "error.h" |
|
36 #include "oct-obj.h" |
|
37 #include "pager.h" |
|
38 #include "pt-const.h" |
|
39 #include "pt-exp.h" |
2891
|
40 #include "pt-id.h" |
|
41 #include "pt-indir.h" |
1741
|
42 #include "pt-misc.h" |
|
43 #include "pt-mvr.h" |
2124
|
44 #include "pt-walk.h" |
1741
|
45 |
|
46 // But first, some extra functions used by the tree classes. |
|
47 |
|
48 // Make sure that all arguments have values. |
|
49 |
1827
|
50 static bool |
2086
|
51 all_args_defined (const octave_value_list& args) |
1741
|
52 { |
|
53 int nargin = args.length (); |
|
54 |
|
55 for (int i = 0; i < nargin; i++) |
|
56 if (args(i).is_undefined ()) |
1827
|
57 return false; |
1741
|
58 |
1827
|
59 return true; |
1741
|
60 } |
|
61 |
|
62 // Used internally. |
|
63 |
2086
|
64 octave_value |
1827
|
65 tree_oct_obj::eval (bool /* print */) |
1741
|
66 { |
|
67 return values(0); |
|
68 } |
|
69 |
2086
|
70 octave_value_list |
2942
|
71 tree_oct_obj::eval (bool, int, const octave_value_list&) |
1741
|
72 { |
|
73 return values; |
|
74 } |
|
75 |
2124
|
76 void |
|
77 tree_oct_obj::accept (tree_walker& tw) |
|
78 { |
|
79 tw.visit_oct_obj (*this); |
|
80 } |
|
81 |
1741
|
82 // Index expressions. |
|
83 |
|
84 tree_index_expression::tree_index_expression |
|
85 (tree_identifier *i, int l = -1, int c = -1) |
2942
|
86 : tree_multi_val_ret (l, c), id (new tree_indirect_ref (i)), |
|
87 list (0), arg_nm () |
|
88 { |
|
89 } |
1741
|
90 |
|
91 tree_index_expression::tree_index_expression |
|
92 (tree_identifier *i, tree_argument_list *lst, int l = -1, int c = -1) |
2942
|
93 : tree_multi_val_ret (l, c), id (new tree_indirect_ref (i)), |
|
94 list (lst), arg_nm () |
|
95 { |
|
96 if (list) |
|
97 arg_nm = list->get_arg_names (); |
|
98 } |
1741
|
99 |
|
100 tree_index_expression::~tree_index_expression (void) |
|
101 { |
|
102 delete id; |
|
103 delete list; |
|
104 } |
|
105 |
1755
|
106 string |
2942
|
107 tree_index_expression::name (void) const |
1741
|
108 { |
|
109 return id->name (); |
|
110 } |
|
111 |
|
112 void |
|
113 tree_index_expression::mark_for_possible_ans_assign (void) |
|
114 { |
|
115 if (id) |
|
116 id->mark_for_possible_ans_assign (); |
|
117 } |
|
118 |
2086
|
119 octave_value |
1827
|
120 tree_index_expression::eval (bool print) |
1741
|
121 { |
2086
|
122 octave_value retval; |
1741
|
123 |
|
124 if (error_state) |
|
125 return retval; |
|
126 |
|
127 if (list) |
|
128 { |
|
129 // Extract the arguments into a simple vector. Don't pass null |
|
130 // args. |
|
131 |
2086
|
132 octave_value_list args = list->convert_to_const_vector (); |
1741
|
133 |
2942
|
134 args.stash_name_tags (arg_nm); |
|
135 |
1741
|
136 if (error_state) |
|
137 eval_error (); |
|
138 else |
|
139 { |
2946
|
140 if (all_args_defined (args)) |
|
141 { |
|
142 octave_value_list tmp = id->eval (print, 1, args); |
|
143 |
|
144 if (error_state) |
|
145 eval_error (); |
|
146 else if (tmp.length () > 0) |
|
147 retval = tmp(0); |
|
148 } |
1741
|
149 else |
|
150 { |
2946
|
151 ::error ("undefined arguments found in index expression"); |
|
152 eval_error (); |
1741
|
153 } |
|
154 } |
|
155 } |
|
156 else |
|
157 { |
|
158 retval = id->eval (print); |
|
159 |
|
160 if (error_state) |
|
161 eval_error (); |
|
162 } |
|
163 |
|
164 return retval; |
|
165 } |
|
166 |
2086
|
167 octave_value_list |
2942
|
168 tree_index_expression::eval (bool print, int nargout, const octave_value_list&) |
1741
|
169 { |
2086
|
170 octave_value_list retval; |
1741
|
171 |
|
172 if (error_state) |
|
173 return retval; |
|
174 |
|
175 if (list) |
|
176 { |
|
177 // Extract the arguments into a simple vector. Don't pass null |
|
178 // args. |
|
179 |
2086
|
180 octave_value_list tmp_args = list->convert_to_const_vector (); |
1741
|
181 |
|
182 if (error_state) |
|
183 eval_error (); |
|
184 else |
|
185 { |
2946
|
186 if (all_args_defined (tmp_args)) |
|
187 { |
|
188 retval = id->eval (print, nargout, tmp_args); |
|
189 |
|
190 if (error_state) |
|
191 eval_error (); |
|
192 } |
1741
|
193 else |
|
194 { |
2946
|
195 ::error ("undefined arguments found in index expression"); |
|
196 eval_error (); |
1741
|
197 } |
|
198 } |
|
199 } |
|
200 else |
|
201 { |
2086
|
202 octave_value_list tmp_args; |
1741
|
203 |
|
204 retval = id->eval (print, nargout, tmp_args); |
|
205 |
|
206 if (error_state) |
|
207 eval_error (); |
|
208 } |
|
209 |
|
210 return retval; |
|
211 } |
|
212 |
|
213 void |
|
214 tree_index_expression::eval_error (void) |
|
215 { |
|
216 if (error_state > 0) |
|
217 { |
|
218 int l = line (); |
|
219 int c = column (); |
2805
|
220 const char *fmt; |
1741
|
221 if (l != -1 && c != -1) |
|
222 { |
|
223 if (list) |
|
224 fmt = "evaluating index expression near line %d, column %d"; |
|
225 else |
|
226 fmt = "evaluating expression near line %d, column %d"; |
|
227 |
|
228 ::error (fmt, l, c); |
|
229 } |
|
230 else |
|
231 { |
|
232 if (list) |
|
233 ::error ("evaluating index expression"); |
|
234 else |
|
235 ::error ("evaluating expression"); |
|
236 } |
|
237 } |
|
238 } |
|
239 |
|
240 void |
2124
|
241 tree_index_expression::accept (tree_walker& tw) |
1741
|
242 { |
2124
|
243 tw.visit_index_expression (*this); |
1741
|
244 } |
|
245 |
|
246 // Multi-valued assignmnt expressions. |
|
247 |
|
248 tree_multi_assignment_expression::~tree_multi_assignment_expression (void) |
|
249 { |
|
250 if (! preserve) |
|
251 delete lhs; |
|
252 |
|
253 delete rhs; |
|
254 } |
|
255 |
2086
|
256 octave_value |
1827
|
257 tree_multi_assignment_expression::eval (bool print) |
1741
|
258 { |
2086
|
259 octave_value retval; |
1741
|
260 |
|
261 if (error_state) |
|
262 return retval; |
|
263 |
2086
|
264 octave_value_list tmp_args; |
|
265 octave_value_list result = eval (print, 1, tmp_args); |
1741
|
266 |
|
267 if (result.length () > 0) |
|
268 retval = result(0); |
|
269 |
|
270 return retval; |
|
271 } |
|
272 |
2086
|
273 octave_value_list |
1827
|
274 tree_multi_assignment_expression::eval (bool print, int nargout, |
2942
|
275 const octave_value_list&) |
1741
|
276 { |
|
277 assert (etype == tree_expression::multi_assignment); |
|
278 |
|
279 if (error_state || ! rhs) |
2086
|
280 return octave_value_list (); |
1741
|
281 |
|
282 nargout = lhs->length (); |
2086
|
283 octave_value_list tmp_args; |
2859
|
284 octave_value_list results = rhs->eval (false, nargout, tmp_args); |
1741
|
285 |
|
286 if (error_state) |
|
287 eval_error (); |
|
288 |
|
289 int ma_line = line (); |
|
290 int ma_column = column (); |
|
291 |
|
292 if (results.length () > 0) |
|
293 { |
|
294 int i = 0; |
1827
|
295 |
|
296 bool pad_after = false; |
|
297 |
1741
|
298 for (Pix p = lhs->first (); p != 0; lhs->next (p)) |
|
299 { |
|
300 tree_index_expression *lhs_expr = lhs->operator () (p); |
|
301 |
|
302 if (i < nargout) |
|
303 { |
|
304 // XXX FIXME? XXX -- this is apparently the way Matlab |
|
305 // works, but maybe we should have the option of |
|
306 // skipping the assignment instead. |
|
307 |
2389
|
308 tree_constant *tmp = 0; |
1741
|
309 if (results(i).is_undefined ()) |
|
310 { |
|
311 error ("element number %d undefined in return list", i+1); |
|
312 eval_error (); |
|
313 break; |
|
314 } |
|
315 else |
2389
|
316 tmp = new tree_constant (results(i)); |
1741
|
317 |
|
318 tree_simple_assignment_expression tmp_expr |
|
319 (lhs_expr, tmp, 1, 0, ma_line, ma_column); |
|
320 |
2859
|
321 results(i) = tmp_expr.eval (); // May change |
1741
|
322 |
|
323 if (error_state) |
|
324 break; |
|
325 |
|
326 if (print && pad_after) |
2095
|
327 octave_stdout << "\n"; |
1741
|
328 |
|
329 if (print) |
2900
|
330 results(i).print_with_name (octave_stdout, |
|
331 lhs_expr->name (), 0); |
1741
|
332 |
1827
|
333 pad_after = true; |
|
334 |
1741
|
335 i++; |
|
336 } |
|
337 else |
|
338 { |
|
339 tree_simple_assignment_expression tmp_expr |
|
340 (lhs_expr, 0, 1, 0, ma_line, ma_column); |
|
341 |
2859
|
342 tmp_expr.eval (); |
1741
|
343 } |
|
344 } |
|
345 |
|
346 if (print && pad_after) |
2095
|
347 octave_stdout << "\n"; |
1741
|
348 } |
|
349 |
|
350 return results; |
|
351 } |
|
352 |
|
353 void |
|
354 tree_multi_assignment_expression::eval_error (void) |
|
355 { |
|
356 if (error_state > 0) |
|
357 ::error ("evaluating assignment expression near line %d, column %d", |
|
358 line (), column ()); |
|
359 } |
|
360 |
|
361 void |
2124
|
362 tree_multi_assignment_expression::accept (tree_walker& tw) |
1741
|
363 { |
2124
|
364 tw.visit_multi_assignment_expression (*this); |
1741
|
365 } |
|
366 |
|
367 /* |
|
368 ;;; Local Variables: *** |
|
369 ;;; mode: C++ *** |
|
370 ;;; End: *** |
|
371 */ |