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