Mercurial > hg > octave-nkf
annotate src/pt-arg-list.cc @ 7767:71f068b22fcc
scope and context fixes for function handles
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 07 May 2008 13:45:30 -0400 |
parents | 7c020c067a60 |
children | 2b2ca62f8ab6 |
rev | line source |
---|---|
2982 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 2000, 2002, 2003, 2004, 2005, 2006, |
4 2007 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" | |
39 #include "pt-arg-list.h" | |
40 #include "pt-exp.h" | |
41 #include "pt-pr-code.h" | |
42 #include "pt-walk.h" | |
43 #include "toplev.h" | |
4234 | 44 #include "unwind-prot.h" |
2982 | 45 |
46 // Argument lists. | |
47 | |
48 tree_argument_list::~tree_argument_list (void) | |
49 { | |
4219 | 50 while (! empty ()) |
2982 | 51 { |
4219 | 52 iterator p = begin (); |
53 delete *p; | |
54 erase (p); | |
2982 | 55 } |
56 } | |
57 | |
4267 | 58 bool |
59 tree_argument_list::has_magic_end (void) const | |
60 { | |
61 for (const_iterator p = begin (); p != end (); p++) | |
62 { | |
63 tree_expression *elt = *p; | |
64 | |
65 if (elt && elt->has_magic_end ()) | |
66 return true; | |
67 } | |
68 | |
69 return false; | |
70 } | |
71 | |
4258 | 72 void |
73 tree_argument_list::append (const element_type& s) | |
74 { | |
75 octave_base_list<tree_expression *>::append (s); | |
76 | |
4267 | 77 if (! list_includes_magic_end && s && s->has_magic_end ()) |
78 list_includes_magic_end = true; | |
4258 | 79 } |
80 | |
2982 | 81 bool |
82 tree_argument_list::all_elements_are_constant (void) const | |
83 { | |
4219 | 84 for (const_iterator p = begin (); p != end (); p++) |
2982 | 85 { |
4219 | 86 tree_expression *elt = *p; |
2982 | 87 |
88 if (! elt->is_constant ()) | |
89 return false; | |
90 } | |
91 | |
92 return true; | |
93 } | |
94 | |
4234 | 95 static const octave_value *indexed_object = 0; |
96 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
|
97 static int num_indices = 0; |
4234 | 98 |
99 DEFCONSTFUN (__end__, , , | |
100 "internal function") | |
101 { | |
102 octave_value retval; | |
103 | |
104 if (indexed_object) | |
105 { | |
4671 | 106 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
|
107 int ndims = dv.length (); |
4671 | 108 |
7751
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
109 if (num_indices < ndims) |
4234 | 110 { |
7751
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
111 for (int i = num_indices; i < ndims; i++) |
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
112 dv(num_indices-1) *= dv(i); |
4256 | 113 |
7751
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
114 if (num_indices == 1) |
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
115 { |
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
116 ndims = 2; |
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
117 dv.resize (ndims); |
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
118 dv(1) = 1; |
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
119 } |
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
120 else |
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
121 { |
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
122 ndims = num_indices; |
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
123 dv.resize (ndims); |
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
124 } |
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
125 } |
4234 | 126 |
7751
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
127 if (index_position < ndims) |
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
128 retval = dv(index_position); |
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
129 else |
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
130 retval = 1; |
4234 | 131 } |
132 else | |
4256 | 133 ::error ("invalid use of end"); |
4234 | 134 |
135 return retval; | |
136 } | |
137 | |
2982 | 138 octave_value_list |
4234 | 139 tree_argument_list::convert_to_const_vector (const octave_value *object) |
2982 | 140 { |
4256 | 141 // END doesn't make sense for functions. Maybe we need a different |
142 // way of asking an octave_value object this question? | |
143 | |
4258 | 144 bool stash_object = (list_includes_magic_end |
5087 | 145 && object |
146 && ! (object->is_function () | |
147 || object->is_function_handle ())); | |
4234 | 148 |
4256 | 149 if (stash_object) |
150 { | |
151 unwind_protect::begin_frame ("convert_to_const_vector"); | |
4234 | 152 |
5760 | 153 unwind_protect_const_ptr (indexed_object); |
4256 | 154 |
155 indexed_object = object; | |
156 } | |
4234 | 157 |
2982 | 158 int len = length (); |
159 | |
160 octave_value_list args; | |
3977 | 161 int args_len = len; |
162 args.resize (args_len); | |
2982 | 163 |
4219 | 164 iterator p = begin (); |
2982 | 165 int j = 0; |
166 for (int k = 0; k < len; k++) | |
167 { | |
4974 | 168 if (stash_object) |
169 { | |
170 unwind_protect_int (index_position); | |
7751
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
171 unwind_protect_int (num_indices); |
4974 | 172 |
7751
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
173 index_position = k; |
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
174 num_indices = len; |
4974 | 175 } |
4234 | 176 |
4219 | 177 tree_expression *elt = *p++; |
2982 | 178 |
179 if (elt) | |
180 { | |
181 octave_value tmp = elt->rvalue (); | |
182 | |
183 if (error_state) | |
184 { | |
3162 | 185 ::error ("evaluating argument list element number %d", k+1); |
2982 | 186 args = octave_value_list (); |
187 break; | |
188 } | |
189 else | |
190 { | |
5848 | 191 if (tmp.is_cs_list ()) |
3977 | 192 { |
193 octave_value_list tl = tmp.list_value (); | |
194 int n = tl.length (); | |
195 args_len += n - 1; | |
196 args.resize (args_len); | |
197 for (int i = 0; i < n; i++) | |
198 args(j++) = tl(i); | |
199 } | |
2982 | 200 else |
201 args(j++) = tmp; | |
202 } | |
203 } | |
204 else | |
205 { | |
206 args(j++) = octave_value (); | |
207 break; | |
208 } | |
209 } | |
210 | |
211 args.resize (j); | |
212 | |
4256 | 213 if (stash_object) |
214 unwind_protect::run_frame ("convert_to_const_vector"); | |
4234 | 215 |
2982 | 216 return args; |
217 } | |
218 | |
5846 | 219 std::list<octave_lvalue> |
220 tree_argument_list::lvalue_list (void) | |
221 { | |
222 std::list<octave_lvalue> retval; | |
223 | |
224 for (tree_argument_list::iterator p = begin (); | |
225 p != end (); | |
226 p++) | |
227 { | |
228 tree_expression *elt = *p; | |
229 | |
230 retval.push_back (elt->lvalue ()); | |
231 } | |
232 | |
233 return retval; | |
234 } | |
235 | |
2982 | 236 string_vector |
237 tree_argument_list::get_arg_names (void) const | |
238 { | |
239 int len = length (); | |
240 | |
241 string_vector retval (len); | |
242 | |
243 int k = 0; | |
244 | |
4219 | 245 for (const_iterator p = begin (); p != end (); p++) |
2982 | 246 { |
4219 | 247 tree_expression *elt = *p; |
2982 | 248 |
2991 | 249 retval(k++) = elt->str_print_code (); |
2982 | 250 } |
251 | |
252 return retval; | |
253 } | |
254 | |
5861 | 255 tree_argument_list * |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7751
diff
changeset
|
256 tree_argument_list::dup (symbol_table::scope_id scope, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7751
diff
changeset
|
257 symbol_table::context_id context) |
5861 | 258 { |
259 tree_argument_list *new_list = new tree_argument_list (); | |
260 | |
261 new_list->list_includes_magic_end = list_includes_magic_end; | |
262 new_list->simple_assign_lhs = simple_assign_lhs; | |
263 | |
264 for (iterator p = begin (); p != end (); p++) | |
265 { | |
266 tree_expression *elt = *p; | |
267 | |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7751
diff
changeset
|
268 new_list->append (elt ? elt->dup (scope, context) : 0); |
5861 | 269 } |
270 | |
271 return new_list; | |
272 } | |
273 | |
2982 | 274 void |
275 tree_argument_list::accept (tree_walker& tw) | |
276 { | |
277 tw.visit_argument_list (*this); | |
278 } | |
279 | |
280 /* | |
281 ;;; Local Variables: *** | |
282 ;;; mode: C++ *** | |
283 ;;; End: *** | |
284 */ |