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 |
3933
|
28 #include "Cell.h" |
2980
|
29 #include "error.h" |
3930
|
30 #include "oct-map.h" |
2980
|
31 #include "oct-obj.h" |
|
32 #include "oct-lvalue.h" |
|
33 #include "ov.h" |
3930
|
34 #include "pager.h" |
2982
|
35 #include "pt-arg-list.h" |
3930
|
36 #include "pt-bp.h" |
2980
|
37 #include "pt-idx.h" |
|
38 #include "pt-walk.h" |
3930
|
39 #include "utils.h" |
|
40 #include "variables.h" |
2980
|
41 |
|
42 // Index expressions. |
|
43 |
5861
|
44 tree_index_expression::tree_index_expression (int l, int c) |
|
45 : tree_expression (l, c), expr (), args (), type (), |
|
46 arg_nm (), dyn_field () { } |
|
47 |
3546
|
48 tree_index_expression::tree_index_expression (tree_expression *e, |
|
49 tree_argument_list *lst, |
3933
|
50 int l, int c, char t) |
4131
|
51 : tree_expression (l, c), expr (e), args (), type (), |
|
52 arg_nm (), dyn_field () |
3933
|
53 { |
|
54 append (lst, t); |
|
55 } |
3215
|
56 |
3930
|
57 tree_index_expression::tree_index_expression (tree_expression *e, |
|
58 const std::string& n, |
3944
|
59 int l, int c) |
4131
|
60 : tree_expression (l, c), expr (e), args (), type (), |
|
61 arg_nm (), dyn_field () |
3933
|
62 { |
|
63 append (n); |
|
64 } |
|
65 |
4131
|
66 tree_index_expression::tree_index_expression (tree_expression *e, |
|
67 tree_expression *df, |
|
68 int l, int c) |
|
69 : tree_expression (l, c), expr (e), args (), type (), |
|
70 arg_nm (), dyn_field () |
|
71 { |
|
72 append (df); |
|
73 } |
|
74 |
3933
|
75 void |
|
76 tree_index_expression::append (tree_argument_list *lst, char t) |
|
77 { |
4219
|
78 args.push_back (lst); |
3933
|
79 type.append (1, t); |
4219
|
80 arg_nm.push_back (lst ? lst->get_arg_names () : string_vector ()); |
|
81 dyn_field.push_back (static_cast<tree_expression *> (0)); |
3933
|
82 } |
|
83 |
|
84 void |
|
85 tree_index_expression::append (const std::string& n) |
|
86 { |
4219
|
87 args.push_back (static_cast<tree_argument_list *> (0)); |
3933
|
88 type.append ("."); |
4219
|
89 arg_nm.push_back (n); |
|
90 dyn_field.push_back (static_cast<tree_expression *> (0)); |
4131
|
91 } |
|
92 |
|
93 void |
|
94 tree_index_expression::append (tree_expression *df) |
|
95 { |
4219
|
96 args.push_back (static_cast<tree_argument_list *> (0)); |
4131
|
97 type.append ("."); |
4219
|
98 arg_nm.push_back (""); |
|
99 dyn_field.push_back (df); |
3933
|
100 } |
3930
|
101 |
2980
|
102 tree_index_expression::~tree_index_expression (void) |
|
103 { |
|
104 delete expr; |
3933
|
105 |
|
106 while (! args.empty ()) |
|
107 { |
4219
|
108 std::list<tree_argument_list *>::iterator p = args.begin (); |
|
109 delete *p; |
|
110 args.erase (p); |
3933
|
111 } |
2980
|
112 } |
|
113 |
5099
|
114 bool |
|
115 tree_index_expression::has_magic_end (void) const |
|
116 { |
|
117 for (std::list<tree_argument_list *>::const_iterator p = args.begin (); |
|
118 p != args.end (); |
|
119 p++) |
|
120 { |
|
121 tree_argument_list *elt = *p; |
|
122 |
|
123 if (elt && elt->has_magic_end ()) |
|
124 return true; |
|
125 } |
|
126 |
|
127 return false; |
|
128 } |
|
129 |
2991
|
130 // This is useful for printing the name of the variable in an indexed |
|
131 // assignment. |
|
132 |
3536
|
133 std::string |
2991
|
134 tree_index_expression::name (void) const |
|
135 { |
4131
|
136 return expr->name (); |
3933
|
137 } |
|
138 |
|
139 static Cell |
|
140 make_subs_cell (tree_argument_list *args, const string_vector& arg_nm) |
|
141 { |
|
142 Cell retval; |
|
143 |
|
144 octave_value_list arg_values; |
|
145 |
|
146 if (args) |
|
147 arg_values = args->convert_to_const_vector (); |
|
148 |
|
149 if (! error_state) |
|
150 { |
|
151 int n = arg_values.length (); |
|
152 |
|
153 if (n > 0) |
|
154 { |
|
155 arg_values.stash_name_tags (arg_nm); |
|
156 |
4548
|
157 retval.resize (dim_vector (1, n)); |
3933
|
158 |
|
159 for (int i = 0; i < n; i++) |
|
160 retval(0,i) = arg_values(i); |
|
161 } |
|
162 } |
|
163 |
|
164 return retval; |
|
165 } |
|
166 |
|
167 static inline octave_value_list |
4234
|
168 make_value_list (tree_argument_list *args, const string_vector& arg_nm, |
|
169 const octave_value *object) |
3933
|
170 { |
|
171 octave_value_list retval; |
|
172 |
|
173 if (args) |
4234
|
174 retval = args->convert_to_const_vector (object); |
3933
|
175 |
|
176 if (! error_state) |
|
177 { |
|
178 int n = retval.length (); |
|
179 |
|
180 if (n > 0) |
|
181 retval.stash_name_tags (arg_nm); |
|
182 } |
|
183 |
|
184 return retval; |
|
185 } |
|
186 |
4131
|
187 std::string |
4219
|
188 tree_index_expression::get_struct_index |
|
189 (std::list<string_vector>::const_iterator p_arg_nm, |
|
190 std::list<tree_expression *>::const_iterator p_dyn_field) const |
4131
|
191 { |
4219
|
192 std::string fn = (*p_arg_nm)(0); |
4131
|
193 |
|
194 if (fn.empty ()) |
|
195 { |
4219
|
196 tree_expression *df = *p_dyn_field; |
4131
|
197 |
|
198 if (df) |
|
199 { |
|
200 octave_value t = df->rvalue (); |
|
201 |
|
202 if (! error_state) |
4143
|
203 { |
|
204 fn = t.string_value (); |
|
205 |
|
206 if (! valid_identifier (fn)) |
6811
|
207 ::error ("invalid structure field name `%s'", fn.c_str ()); |
4143
|
208 } |
4131
|
209 } |
|
210 else |
|
211 panic_impossible (); |
|
212 } |
|
213 |
|
214 return fn; |
|
215 } |
|
216 |
3933
|
217 Octave_map |
|
218 tree_index_expression::make_arg_struct (void) const |
|
219 { |
4219
|
220 int n = args.size (); |
3933
|
221 |
5775
|
222 // FIXME -- why not just make these Cell objects? |
3933
|
223 octave_value_list subs_list (n, octave_value ()); |
|
224 octave_value_list type_list (n, octave_value ()); |
|
225 |
4219
|
226 std::list<tree_argument_list *>::const_iterator p_args = args.begin (); |
|
227 std::list<string_vector>::const_iterator p_arg_nm = arg_nm.begin (); |
|
228 std::list<tree_expression *>::const_iterator p_dyn_field = dyn_field.begin (); |
3933
|
229 |
|
230 Octave_map m; |
|
231 |
|
232 for (int i = 0; i < n; i++) |
|
233 { |
|
234 switch (type[i]) |
|
235 { |
|
236 case '(': |
4219
|
237 subs_list(i) = make_subs_cell (*p_args, *p_arg_nm); |
3933
|
238 break; |
|
239 |
|
240 case '{': |
4219
|
241 subs_list(i) = make_subs_cell (*p_args, *p_arg_nm); |
3933
|
242 break; |
|
243 |
|
244 case '.': |
4143
|
245 { |
|
246 subs_list(i) = get_struct_index (p_arg_nm, p_dyn_field); |
|
247 |
|
248 if (error_state) |
|
249 eval_error (); |
|
250 } |
3933
|
251 break; |
|
252 |
|
253 default: |
|
254 panic_impossible (); |
|
255 } |
|
256 |
|
257 if (error_state) |
|
258 return m; |
|
259 |
4219
|
260 p_args++; |
|
261 p_arg_nm++; |
|
262 p_dyn_field++; |
3933
|
263 } |
|
264 |
4675
|
265 m.assign ("subs", Cell (subs_list)); |
|
266 m.assign ("type", Cell (type_list)); |
3933
|
267 |
|
268 return m; |
2991
|
269 } |
|
270 |
2980
|
271 octave_value_list |
|
272 tree_index_expression::rvalue (int nargout) |
|
273 { |
|
274 octave_value_list retval; |
|
275 |
|
276 if (error_state) |
|
277 return retval; |
|
278 |
4432
|
279 octave_value first_expr_val = expr->rvalue (); |
|
280 octave_value tmp = first_expr_val; |
2980
|
281 |
|
282 if (! error_state) |
|
283 { |
4219
|
284 std::list<octave_value_list> idx; |
3930
|
285 |
4219
|
286 int n = args.size (); |
3933
|
287 |
4219
|
288 std::list<tree_argument_list *>::iterator p_args = args.begin (); |
|
289 std::list<string_vector>::iterator p_arg_nm = arg_nm.begin (); |
|
290 std::list<tree_expression *>::iterator p_dyn_field = dyn_field.begin (); |
3930
|
291 |
3933
|
292 for (int i = 0; i < n; i++) |
|
293 { |
4432
|
294 if (i > 0) |
|
295 { |
|
296 tree_argument_list *al = *p_args; |
|
297 |
|
298 if (al && al->has_magic_end ()) |
|
299 { |
|
300 // We have an expression like |
|
301 // |
|
302 // x{end}.a(end) |
|
303 // |
|
304 // and we are looking at the argument list that |
|
305 // contains the second (or third, etc.) "end" token, |
|
306 // so we must evaluate everything up to the point of |
|
307 // that argument list so we pass the appropiate |
|
308 // value to the built-in __end__ function. |
|
309 |
|
310 octave_value_list tmp_list |
|
311 = first_expr_val.subsref (type, idx, nargout); |
|
312 |
|
313 tmp = tmp_list(0); |
|
314 |
|
315 if (error_state) |
|
316 break; |
|
317 } |
|
318 } |
|
319 |
3933
|
320 switch (type[i]) |
|
321 { |
|
322 case '(': |
4234
|
323 idx.push_back (make_value_list (*p_args, *p_arg_nm, &tmp)); |
3933
|
324 break; |
3930
|
325 |
3933
|
326 case '{': |
4234
|
327 idx.push_back (make_value_list (*p_args, *p_arg_nm, &tmp)); |
3933
|
328 break; |
2980
|
329 |
3933
|
330 case '.': |
4143
|
331 { |
4233
|
332 idx.push_back (octave_value (get_struct_index (p_arg_nm, p_dyn_field))); |
4143
|
333 |
|
334 if (error_state) |
|
335 eval_error (); |
|
336 } |
3933
|
337 break; |
3930
|
338 |
3933
|
339 default: |
|
340 panic_impossible (); |
|
341 } |
3930
|
342 |
3933
|
343 if (error_state) |
|
344 break; |
|
345 |
4219
|
346 p_args++; |
|
347 p_arg_nm++; |
|
348 p_dyn_field++; |
2980
|
349 } |
3933
|
350 |
|
351 if (! error_state) |
4432
|
352 retval = first_expr_val.subsref (type, idx, nargout); |
2980
|
353 } |
|
354 |
|
355 return retval; |
|
356 } |
|
357 |
|
358 octave_value |
|
359 tree_index_expression::rvalue (void) |
|
360 { |
|
361 octave_value retval; |
|
362 |
|
363 octave_value_list tmp = rvalue (1); |
|
364 |
|
365 if (! tmp.empty ()) |
|
366 retval = tmp(0); |
|
367 |
|
368 return retval; |
|
369 } |
|
370 |
|
371 octave_lvalue |
|
372 tree_index_expression::lvalue (void) |
|
373 { |
|
374 octave_lvalue retval; |
|
375 |
4219
|
376 std::list<octave_value_list> idx; |
3933
|
377 |
4219
|
378 int n = args.size (); |
3933
|
379 |
4219
|
380 std::list<tree_argument_list *>::iterator p_args = args.begin (); |
|
381 std::list<string_vector>::iterator p_arg_nm = arg_nm.begin (); |
|
382 std::list<tree_expression *>::iterator p_dyn_field = dyn_field.begin (); |
3933
|
383 |
4234
|
384 retval = expr->lvalue (); |
3933
|
385 |
2980
|
386 if (! error_state) |
|
387 { |
4432
|
388 // I think it is OK to have a copy here. |
|
389 |
|
390 const octave_value *tro = retval.object (); |
|
391 |
|
392 octave_value first_retval_object; |
|
393 |
|
394 if (tro) |
|
395 first_retval_object = *tro; |
|
396 |
|
397 octave_value tmp = first_retval_object; |
4234
|
398 |
|
399 for (int i = 0; i < n; i++) |
|
400 { |
4432
|
401 if (i > 0) |
|
402 { |
|
403 tree_argument_list *al = *p_args; |
|
404 |
|
405 if (al && al->has_magic_end ()) |
|
406 { |
|
407 // We have an expression like |
|
408 // |
|
409 // x{end}.a(end) |
|
410 // |
|
411 // and we are looking at the argument list that |
|
412 // contains the second (or third, etc.) "end" token, |
|
413 // so we must evaluate everything up to the point of |
|
414 // that argument list so we pass the appropiate |
|
415 // value to the built-in __end__ function. |
|
416 |
|
417 octave_value_list tmp_list |
|
418 = first_retval_object.subsref (type, idx, 1); |
|
419 |
|
420 tmp = tmp_list(0); |
|
421 |
|
422 if (error_state) |
|
423 break; |
|
424 } |
|
425 } |
|
426 |
4234
|
427 switch (type[i]) |
|
428 { |
|
429 case '(': |
4432
|
430 idx.push_back (make_value_list (*p_args, *p_arg_nm, &tmp)); |
4234
|
431 break; |
|
432 |
|
433 case '{': |
5846
|
434 { |
|
435 octave_value_list tidx |
|
436 = make_value_list (*p_args, *p_arg_nm, &tmp); |
|
437 |
|
438 idx.push_back (tidx); |
|
439 |
|
440 if (i == n-1) |
|
441 { |
|
442 // Last indexing element. Will this result in a |
|
443 // comma-separated list? |
|
444 |
|
445 if (tidx.has_magic_colon ()) |
|
446 { |
|
447 octave_value_list tmp_list |
|
448 = first_retval_object.subsref (type, idx, 1); |
|
449 |
|
450 if (! error_state) |
|
451 { |
|
452 octave_value val = tmp_list(0); |
|
453 |
|
454 if (val.is_cs_list ()) |
|
455 retval.numel (val.numel ()); |
|
456 } |
|
457 } |
|
458 else |
|
459 { |
|
460 octave_idx_type nel = 1; |
|
461 |
|
462 octave_idx_type nidx = tidx.length (); |
|
463 |
|
464 for (octave_idx_type j = 0; j < nidx; j++) |
|
465 { |
|
466 octave_value val = tidx(j); |
|
467 |
|
468 nel *= val.numel (); |
|
469 } |
|
470 |
|
471 retval.numel (nel); |
|
472 } |
|
473 } |
|
474 } |
4234
|
475 break; |
|
476 |
|
477 case '.': |
|
478 { |
|
479 idx.push_back (octave_value (get_struct_index (p_arg_nm, p_dyn_field))); |
|
480 |
|
481 if (error_state) |
|
482 eval_error (); |
|
483 } |
|
484 break; |
|
485 |
|
486 default: |
|
487 panic_impossible (); |
|
488 } |
|
489 |
|
490 if (error_state) |
|
491 break; |
|
492 |
|
493 p_args++; |
|
494 p_arg_nm++; |
|
495 p_dyn_field++; |
|
496 } |
3930
|
497 |
3933
|
498 if (! error_state) |
|
499 retval.set_index (type, idx); |
2980
|
500 } |
|
501 |
|
502 return retval; |
|
503 } |
|
504 |
|
505 void |
4143
|
506 tree_index_expression::eval_error (void) const |
2980
|
507 { |
3965
|
508 int l = line (); |
|
509 int c = column (); |
2980
|
510 |
3965
|
511 const char *type_str; |
3930
|
512 |
3965
|
513 if (type[0] == '.') |
|
514 type_str = "structure reference operator"; |
|
515 else if (args.front ()) |
|
516 type_str = "index expression"; |
|
517 else |
|
518 type_str = "expression"; |
3930
|
519 |
3965
|
520 if (l != -1 && c != -1) |
|
521 ::error ("evaluating %s near line %d, column %d", type_str, l, c); |
|
522 else |
|
523 ::error ("evaluating %s", type_str); |
2980
|
524 } |
|
525 |
5861
|
526 tree_index_expression * |
|
527 tree_index_expression::dup (symbol_table *sym_tab) |
|
528 { |
|
529 tree_index_expression *new_idx_expr |
|
530 = new tree_index_expression (line (), column ()); |
|
531 |
|
532 new_idx_expr->expr = expr ? expr->dup (sym_tab) : 0; |
|
533 |
|
534 std::list<tree_argument_list *> new_args; |
|
535 |
|
536 for (std::list<tree_argument_list *>::iterator p = args.begin (); |
|
537 p != args.end (); |
|
538 p++) |
|
539 { |
|
540 tree_argument_list *elt = *p; |
|
541 |
|
542 new_args.push_back (elt ? elt->dup (sym_tab) : 0); |
|
543 } |
|
544 |
|
545 new_idx_expr->args = new_args; |
|
546 |
|
547 new_idx_expr->type = type; |
|
548 |
|
549 new_idx_expr->arg_nm = arg_nm; |
|
550 |
|
551 std::list<tree_expression *> new_dyn_field; |
|
552 |
|
553 for (std::list<tree_expression *>::iterator p = dyn_field.begin (); |
|
554 p != dyn_field.end (); |
|
555 p++) |
|
556 { |
|
557 tree_expression *elt = *p; |
|
558 |
|
559 new_dyn_field.push_back (elt ? elt->dup (sym_tab) : 0); |
|
560 } |
|
561 |
|
562 new_idx_expr->dyn_field = new_dyn_field; |
|
563 |
|
564 new_idx_expr->copy_base (*this); |
|
565 |
|
566 return new_idx_expr; |
|
567 } |
|
568 |
2980
|
569 void |
|
570 tree_index_expression::accept (tree_walker& tw) |
|
571 { |
|
572 tw.visit_index_expression (*this); |
|
573 } |
|
574 |
|
575 /* |
|
576 ;;; Local Variables: *** |
|
577 ;;; mode: C++ *** |
|
578 ;;; End: *** |
|
579 */ |