Mercurial > hg > octave-nkf
annotate src/pt-arg-list.cc @ 10884:cc2bc3f46cd4
fix assignment bug with lazy indices
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Wed, 11 Aug 2010 10:55:31 +0200 |
parents | 57a59eae83cc |
children | fd0a3ac60b0e |
rev | line source |
---|---|
2982 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 2000, 2002, 2003, 2004, 2005, 2006, |
8920 | 4 2007, 2008, 2009 John W. Eaton |
2982 | 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. | |
2982 | 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/>. | |
2982 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
3503 | 28 #include <iostream> |
2982 | 29 #include <string> |
30 | |
31 #include "str-vec.h" | |
32 | |
4234 | 33 #include "defun.h" |
2982 | 34 #include "error.h" |
5846 | 35 #include "oct-lvalue.h" |
2982 | 36 #include "oct-obj.h" |
37 #include "ov.h" | |
38 #include "ov-usr-fcn.h" | |
8136
2b2ca62f8ab6
dispatch to user-defined end function for classes if one is defined
John W. Eaton <jwe@octave.org>
parents:
7767
diff
changeset
|
39 #include "parse.h" |
2982 | 40 #include "pt-arg-list.h" |
41 #include "pt-exp.h" | |
10206
37a08e0ce2dc
support Matlab-style empty output/input arguments
Jaroslav Hajek <highegg@gmail.com>
parents:
10160
diff
changeset
|
42 #include "pt-id.h" |
2982 | 43 #include "pt-pr-code.h" |
44 #include "pt-walk.h" | |
45 #include "toplev.h" | |
4234 | 46 #include "unwind-prot.h" |
2982 | 47 |
48 // Argument lists. | |
49 | |
50 tree_argument_list::~tree_argument_list (void) | |
51 { | |
4219 | 52 while (! empty ()) |
2982 | 53 { |
4219 | 54 iterator p = begin (); |
55 delete *p; | |
56 erase (p); | |
2982 | 57 } |
58 } | |
59 | |
4267 | 60 bool |
61 tree_argument_list::has_magic_end (void) const | |
62 { | |
63 for (const_iterator p = begin (); p != end (); p++) | |
64 { | |
65 tree_expression *elt = *p; | |
66 | |
67 if (elt && elt->has_magic_end ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
68 return true; |
4267 | 69 } |
70 | |
71 return false; | |
72 } | |
73 | |
4258 | 74 void |
75 tree_argument_list::append (const element_type& s) | |
76 { | |
77 octave_base_list<tree_expression *>::append (s); | |
78 | |
4267 | 79 if (! list_includes_magic_end && s && s->has_magic_end ()) |
80 list_includes_magic_end = true; | |
10206
37a08e0ce2dc
support Matlab-style empty output/input arguments
Jaroslav Hajek <highegg@gmail.com>
parents:
10160
diff
changeset
|
81 |
37a08e0ce2dc
support Matlab-style empty output/input arguments
Jaroslav Hajek <highegg@gmail.com>
parents:
10160
diff
changeset
|
82 if (! list_includes_magic_tilde && s && s->is_identifier ()) |
37a08e0ce2dc
support Matlab-style empty output/input arguments
Jaroslav Hajek <highegg@gmail.com>
parents:
10160
diff
changeset
|
83 { |
37a08e0ce2dc
support Matlab-style empty output/input arguments
Jaroslav Hajek <highegg@gmail.com>
parents:
10160
diff
changeset
|
84 tree_identifier *id = dynamic_cast<tree_identifier *> (s); |
37a08e0ce2dc
support Matlab-style empty output/input arguments
Jaroslav Hajek <highegg@gmail.com>
parents:
10160
diff
changeset
|
85 list_includes_magic_tilde = id && id->is_black_hole (); |
37a08e0ce2dc
support Matlab-style empty output/input arguments
Jaroslav Hajek <highegg@gmail.com>
parents:
10160
diff
changeset
|
86 } |
4258 | 87 } |
88 | |
2982 | 89 bool |
90 tree_argument_list::all_elements_are_constant (void) const | |
91 { | |
4219 | 92 for (const_iterator p = begin (); p != end (); p++) |
2982 | 93 { |
4219 | 94 tree_expression *elt = *p; |
2982 | 95 |
96 if (! elt->is_constant ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
97 return false; |
2982 | 98 } |
99 | |
100 return true; | |
101 } | |
102 | |
4234 | 103 static const octave_value *indexed_object = 0; |
104 static int index_position = 0; | |
7751
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
105 static int num_indices = 0; |
4234 | 106 |
107 DEFCONSTFUN (__end__, , , | |
108 "internal function") | |
109 { | |
110 octave_value retval; | |
111 | |
112 if (indexed_object) | |
113 { | |
8136
2b2ca62f8ab6
dispatch to user-defined end function for classes if one is defined
John W. Eaton <jwe@octave.org>
parents:
7767
diff
changeset
|
114 if (indexed_object->is_object ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
115 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
116 octave_value_list args; |
8136
2b2ca62f8ab6
dispatch to user-defined end function for classes if one is defined
John W. Eaton <jwe@octave.org>
parents:
7767
diff
changeset
|
117 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
118 args(2) = num_indices; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
119 args(1) = index_position + 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
120 args(0) = *indexed_object; |
8136
2b2ca62f8ab6
dispatch to user-defined end function for classes if one is defined
John W. Eaton <jwe@octave.org>
parents:
7767
diff
changeset
|
121 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
122 std::string class_name = indexed_object->class_name (); |
8136
2b2ca62f8ab6
dispatch to user-defined end function for classes if one is defined
John W. Eaton <jwe@octave.org>
parents:
7767
diff
changeset
|
123 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
124 octave_value meth = symbol_table::find_method ("end", class_name); |
8136
2b2ca62f8ab6
dispatch to user-defined end function for classes if one is defined
John W. Eaton <jwe@octave.org>
parents:
7767
diff
changeset
|
125 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
126 if (meth.is_defined ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
127 return feval (meth.function_value (), args, 1); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
128 } |
8136
2b2ca62f8ab6
dispatch to user-defined end function for classes if one is defined
John W. Eaton <jwe@octave.org>
parents:
7767
diff
changeset
|
129 |
4671 | 130 dim_vector dv = indexed_object->dims (); |
7751
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
131 int ndims = dv.length (); |
4671 | 132 |
7751
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
133 if (num_indices < ndims) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
134 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
135 for (int i = num_indices; i < ndims; i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
136 dv(num_indices-1) *= dv(i); |
4256 | 137 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
138 if (num_indices == 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
139 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
140 ndims = 2; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
141 dv.resize (ndims); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
142 dv(1) = 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
143 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
144 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
145 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
146 ndims = num_indices; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
147 dv.resize (ndims); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
148 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
149 } |
4234 | 150 |
7751
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
151 if (index_position < ndims) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
152 retval = dv(index_position); |
7751
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
153 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
154 retval = 1; |
4234 | 155 } |
156 else | |
4256 | 157 ::error ("invalid use of end"); |
4234 | 158 |
159 return retval; | |
160 } | |
161 | |
2982 | 162 octave_value_list |
4234 | 163 tree_argument_list::convert_to_const_vector (const octave_value *object) |
2982 | 164 { |
4256 | 165 // END doesn't make sense for functions. Maybe we need a different |
166 // way of asking an octave_value object this question? | |
167 | |
4258 | 168 bool stash_object = (list_includes_magic_end |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
169 && object |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
170 && ! (object->is_function () |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
171 || object->is_function_handle ())); |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
172 |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9702
diff
changeset
|
173 unwind_protect frame; |
4234 | 174 |
4256 | 175 if (stash_object) |
176 { | |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9702
diff
changeset
|
177 frame.protect_var (indexed_object); |
4256 | 178 |
179 indexed_object = object; | |
180 } | |
4234 | 181 |
2982 | 182 int len = length (); |
183 | |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8173
diff
changeset
|
184 std::list<octave_value_list> args; |
2982 | 185 |
4219 | 186 iterator p = begin (); |
2982 | 187 for (int k = 0; k < len; k++) |
188 { | |
4974 | 189 if (stash_object) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
190 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
191 frame.protect_var (index_position); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
192 frame.protect_var (num_indices); |
4974 | 193 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
194 index_position = k; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
195 num_indices = len; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
196 } |
4234 | 197 |
4219 | 198 tree_expression *elt = *p++; |
2982 | 199 |
200 if (elt) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
201 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
202 octave_value tmp = elt->rvalue1 (); |
2982 | 203 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
204 if (error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
205 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
206 ::error ("evaluating argument list element number %d", k+1); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
207 args.clear (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
208 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
209 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
210 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
211 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
212 if (tmp.is_cs_list ()) |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8173
diff
changeset
|
213 args.push_back (tmp.list_value ()); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
214 else if (tmp.is_defined ()) |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8173
diff
changeset
|
215 args.push_back (tmp); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
216 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
217 } |
2982 | 218 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
219 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
220 args.push_back (octave_value ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
221 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
222 } |
2982 | 223 } |
224 | |
225 return args; | |
226 } | |
227 | |
5846 | 228 std::list<octave_lvalue> |
229 tree_argument_list::lvalue_list (void) | |
230 { | |
231 std::list<octave_lvalue> retval; | |
232 | |
233 for (tree_argument_list::iterator p = begin (); | |
234 p != end (); | |
235 p++) | |
236 { | |
237 tree_expression *elt = *p; | |
238 | |
239 retval.push_back (elt->lvalue ()); | |
240 } | |
241 | |
242 return retval; | |
243 } | |
244 | |
2982 | 245 string_vector |
246 tree_argument_list::get_arg_names (void) const | |
247 { | |
248 int len = length (); | |
249 | |
250 string_vector retval (len); | |
251 | |
252 int k = 0; | |
253 | |
4219 | 254 for (const_iterator p = begin (); p != end (); p++) |
2982 | 255 { |
4219 | 256 tree_expression *elt = *p; |
2982 | 257 |
2991 | 258 retval(k++) = elt->str_print_code (); |
2982 | 259 } |
260 | |
261 return retval; | |
262 } | |
263 | |
5861 | 264 tree_argument_list * |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7751
diff
changeset
|
265 tree_argument_list::dup (symbol_table::scope_id scope, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10206
diff
changeset
|
266 symbol_table::context_id context) const |
5861 | 267 { |
268 tree_argument_list *new_list = new tree_argument_list (); | |
269 | |
270 new_list->list_includes_magic_end = list_includes_magic_end; | |
271 new_list->simple_assign_lhs = simple_assign_lhs; | |
272 | |
8913
35cd375d4bb3
make tree::dup functions const
John W. Eaton <jwe@octave.org>
parents:
8658
diff
changeset
|
273 for (const_iterator p = begin (); p != end (); p++) |
5861 | 274 { |
8913
35cd375d4bb3
make tree::dup functions const
John W. Eaton <jwe@octave.org>
parents:
8658
diff
changeset
|
275 const tree_expression *elt = *p; |
5861 | 276 |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7751
diff
changeset
|
277 new_list->append (elt ? elt->dup (scope, context) : 0); |
5861 | 278 } |
279 | |
280 return new_list; | |
281 } | |
282 | |
2982 | 283 void |
284 tree_argument_list::accept (tree_walker& tw) | |
285 { | |
286 tw.visit_argument_list (*this); | |
287 } |