2980
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1996, 1997, 1998, 2000, 2002, 2003, 2004, 2005, 2006, |
|
4 2007 John W. Eaton |
2980
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
2980
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
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 { |
7099
|
388 bool have_new_struct_field = false; |
|
389 |
|
390 octave_idx_type new_struct_field_nel = 0; |
|
391 |
4432
|
392 // I think it is OK to have a copy here. |
|
393 |
|
394 const octave_value *tro = retval.object (); |
|
395 |
|
396 octave_value first_retval_object; |
|
397 |
|
398 if (tro) |
|
399 first_retval_object = *tro; |
|
400 |
|
401 octave_value tmp = first_retval_object; |
4234
|
402 |
|
403 for (int i = 0; i < n; i++) |
|
404 { |
4432
|
405 if (i > 0) |
|
406 { |
|
407 tree_argument_list *al = *p_args; |
|
408 |
|
409 if (al && al->has_magic_end ()) |
|
410 { |
|
411 // We have an expression like |
|
412 // |
|
413 // x{end}.a(end) |
|
414 // |
|
415 // and we are looking at the argument list that |
|
416 // contains the second (or third, etc.) "end" token, |
|
417 // so we must evaluate everything up to the point of |
|
418 // that argument list so we pass the appropiate |
|
419 // value to the built-in __end__ function. |
|
420 |
|
421 octave_value_list tmp_list |
|
422 = first_retval_object.subsref (type, idx, 1); |
|
423 |
|
424 tmp = tmp_list(0); |
|
425 |
|
426 if (error_state) |
|
427 break; |
|
428 } |
|
429 } |
|
430 |
4234
|
431 switch (type[i]) |
|
432 { |
|
433 case '(': |
4432
|
434 idx.push_back (make_value_list (*p_args, *p_arg_nm, &tmp)); |
4234
|
435 break; |
|
436 |
|
437 case '{': |
5846
|
438 { |
|
439 octave_value_list tidx |
|
440 = make_value_list (*p_args, *p_arg_nm, &tmp); |
|
441 |
|
442 idx.push_back (tidx); |
|
443 |
|
444 if (i == n-1) |
|
445 { |
|
446 // Last indexing element. Will this result in a |
|
447 // comma-separated list? |
|
448 |
|
449 if (tidx.has_magic_colon ()) |
|
450 { |
|
451 octave_value_list tmp_list |
|
452 = first_retval_object.subsref (type, idx, 1); |
|
453 |
|
454 if (! error_state) |
|
455 { |
|
456 octave_value val = tmp_list(0); |
|
457 |
|
458 if (val.is_cs_list ()) |
|
459 retval.numel (val.numel ()); |
|
460 } |
|
461 } |
|
462 else |
|
463 { |
|
464 octave_idx_type nel = 1; |
|
465 |
|
466 octave_idx_type nidx = tidx.length (); |
|
467 |
|
468 for (octave_idx_type j = 0; j < nidx; j++) |
|
469 { |
|
470 octave_value val = tidx(j); |
|
471 |
|
472 nel *= val.numel (); |
|
473 } |
|
474 |
|
475 retval.numel (nel); |
|
476 } |
|
477 } |
|
478 } |
4234
|
479 break; |
|
480 |
|
481 case '.': |
|
482 { |
6833
|
483 octave_value tidx = get_struct_index (p_arg_nm, p_dyn_field); |
|
484 |
|
485 if (! error_state) |
|
486 { |
|
487 if (i == n-1) |
|
488 { |
|
489 // Last indexing element. Will this result in a |
|
490 // comma-separated list? |
|
491 |
7099
|
492 if (have_new_struct_field) |
|
493 retval.numel (new_struct_field_nel); |
|
494 else if (i > 0) |
7057
|
495 { |
7099
|
496 std::string ttype = type.substr (0, i); |
|
497 |
|
498 if (ttype[ttype.length()-1] == '(') |
7057
|
499 { |
7099
|
500 octave_idx_type nel = 1; |
|
501 |
7057
|
502 octave_value_list xidx = idx.back (); |
|
503 |
7099
|
504 octave_idx_type nidx = xidx.length (); |
|
505 |
|
506 for (octave_idx_type j = 0; j < nidx; j++) |
|
507 { |
|
508 octave_value val = xidx(j); |
|
509 |
|
510 nel *= val.numel (); |
|
511 } |
|
512 |
|
513 retval.numel (nel); |
|
514 } |
|
515 else if (first_retval_object.is_defined ()) |
|
516 { |
|
517 octave_value_list tmp_list |
|
518 = first_retval_object.subsref (ttype, idx, 1); |
|
519 |
|
520 if (! error_state) |
7057
|
521 { |
7099
|
522 octave_value val = tmp_list(0); |
7057
|
523 |
7099
|
524 retval.numel (val.numel ()); |
|
525 } |
|
526 } |
|
527 else |
|
528 retval.numel (1); |
|
529 } |
|
530 else |
|
531 { |
|
532 if (first_retval_object.is_defined ()) |
|
533 retval.numel (first_retval_object.numel ()); |
|
534 else |
|
535 retval.numel (1); |
|
536 } |
|
537 } |
|
538 else |
|
539 { |
|
540 octave_value tobj = first_retval_object; |
|
541 |
|
542 if (! have_new_struct_field) |
|
543 { |
|
544 if (i > 0 && first_retval_object.is_defined ()) |
|
545 { |
|
546 std::string ttype = type.substr (0, i); |
|
547 |
|
548 if (ttype[ttype.length()-1] != '(') |
|
549 { |
7057
|
550 octave_value_list tmp_list |
|
551 = first_retval_object.subsref (ttype, idx, 1); |
|
552 |
|
553 if (! error_state) |
7099
|
554 tobj = tmp_list(0); |
|
555 } |
|
556 } |
|
557 |
|
558 if (! error_state && tobj.is_map ()) |
|
559 { |
|
560 if (tidx.is_string ()) |
|
561 { |
|
562 Octave_map m = tobj.map_value (); |
|
563 |
|
564 std::string s = tidx.string_value (); |
|
565 |
|
566 if (! m.contains (s)) |
7057
|
567 { |
7099
|
568 have_new_struct_field = true; |
7057
|
569 |
7099
|
570 new_struct_field_nel = m.numel (); |
7057
|
571 } |
|
572 } |
|
573 } |
|
574 } |
6833
|
575 } |
7057
|
576 |
|
577 idx.push_back (octave_value (tidx)); |
6833
|
578 } |
|
579 else |
4234
|
580 eval_error (); |
|
581 } |
|
582 break; |
|
583 |
|
584 default: |
|
585 panic_impossible (); |
|
586 } |
|
587 |
|
588 if (error_state) |
|
589 break; |
|
590 |
|
591 p_args++; |
|
592 p_arg_nm++; |
|
593 p_dyn_field++; |
|
594 } |
3930
|
595 |
3933
|
596 if (! error_state) |
|
597 retval.set_index (type, idx); |
2980
|
598 } |
|
599 |
|
600 return retval; |
|
601 } |
|
602 |
7099
|
603 /* |
|
604 %!test |
|
605 %! x = {1, 2, 3}; |
|
606 %! [x{:}] = deal (4, 5, 6); |
|
607 %! assert (x, {4, 5, 6}); |
|
608 |
|
609 %!test |
|
610 %! [x.a, x.b.c] = deal (1, 2); |
|
611 %! assert (x.a == 1 && x.b.c == 2); |
|
612 |
|
613 %!test |
|
614 %! [x.a, x(2).b] = deal (1, 2); |
|
615 %! assert (x(1).a == 1 && isempty (x(2).a) && isempty (x(1).b) && x(2).b == 2); |
|
616 |
|
617 %!test |
|
618 %! x = struct (zeros (0, 1), {"a", "b"}); |
|
619 %! x(2).b = 1; |
|
620 %! assert (x(2).b == 1); |
|
621 |
|
622 %!test |
|
623 %! x = struct (zeros (0, 1), {"a", "b"}); |
|
624 %! x(2).b = 1; |
|
625 %! assert (x(2).b == 1); |
|
626 */ |
|
627 |
2980
|
628 void |
4143
|
629 tree_index_expression::eval_error (void) const |
2980
|
630 { |
3965
|
631 int l = line (); |
|
632 int c = column (); |
2980
|
633 |
3965
|
634 const char *type_str; |
3930
|
635 |
3965
|
636 if (type[0] == '.') |
|
637 type_str = "structure reference operator"; |
|
638 else if (args.front ()) |
|
639 type_str = "index expression"; |
|
640 else |
|
641 type_str = "expression"; |
3930
|
642 |
3965
|
643 if (l != -1 && c != -1) |
|
644 ::error ("evaluating %s near line %d, column %d", type_str, l, c); |
|
645 else |
|
646 ::error ("evaluating %s", type_str); |
2980
|
647 } |
|
648 |
5861
|
649 tree_index_expression * |
|
650 tree_index_expression::dup (symbol_table *sym_tab) |
|
651 { |
|
652 tree_index_expression *new_idx_expr |
|
653 = new tree_index_expression (line (), column ()); |
|
654 |
|
655 new_idx_expr->expr = expr ? expr->dup (sym_tab) : 0; |
|
656 |
|
657 std::list<tree_argument_list *> new_args; |
|
658 |
|
659 for (std::list<tree_argument_list *>::iterator p = args.begin (); |
|
660 p != args.end (); |
|
661 p++) |
|
662 { |
|
663 tree_argument_list *elt = *p; |
|
664 |
|
665 new_args.push_back (elt ? elt->dup (sym_tab) : 0); |
|
666 } |
|
667 |
|
668 new_idx_expr->args = new_args; |
|
669 |
|
670 new_idx_expr->type = type; |
|
671 |
|
672 new_idx_expr->arg_nm = arg_nm; |
|
673 |
|
674 std::list<tree_expression *> new_dyn_field; |
|
675 |
|
676 for (std::list<tree_expression *>::iterator p = dyn_field.begin (); |
|
677 p != dyn_field.end (); |
|
678 p++) |
|
679 { |
|
680 tree_expression *elt = *p; |
|
681 |
|
682 new_dyn_field.push_back (elt ? elt->dup (sym_tab) : 0); |
|
683 } |
|
684 |
|
685 new_idx_expr->dyn_field = new_dyn_field; |
|
686 |
|
687 new_idx_expr->copy_base (*this); |
|
688 |
|
689 return new_idx_expr; |
|
690 } |
|
691 |
2980
|
692 void |
|
693 tree_index_expression::accept (tree_walker& tw) |
|
694 { |
|
695 tw.visit_index_expression (*this); |
|
696 } |
|
697 |
|
698 /* |
|
699 ;;; Local Variables: *** |
|
700 ;;; mode: C++ *** |
|
701 ;;; End: *** |
|
702 */ |