Mercurial > hg > octave-lyh
annotate src/pt-arg-list.cc @ 9428:b317debeb828
data.cc (Fones, Fzeros, Ftrue, Ffalse): update docstring
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 09 Jul 2009 15:41:27 -0400 |
parents | 610bf90fce2a |
children | 9ecd35a606e3 |
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" | |
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 ())); | |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
165 |
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
166 unwind_protect::frame_id_t uwp_frame; |
4234 | 167 |
4256 | 168 if (stash_object) |
169 { | |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
170 uwp_frame = unwind_protect::begin_frame (); |
4234 | 171 |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
172 unwind_protect::protect_var (indexed_object); |
4256 | 173 |
174 indexed_object = object; | |
175 } | |
4234 | 176 |
2982 | 177 int len = length (); |
178 | |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8173
diff
changeset
|
179 std::list<octave_value_list> args; |
2982 | 180 |
4219 | 181 iterator p = begin (); |
2982 | 182 for (int k = 0; k < len; k++) |
183 { | |
4974 | 184 if (stash_object) |
185 { | |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
186 unwind_protect::protect_var (index_position); |
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
187 unwind_protect::protect_var (num_indices); |
4974 | 188 |
7751
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
189 index_position = k; |
7c020c067a60
F__end__: correctly handle fewer indices than dimensions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
190 num_indices = len; |
4974 | 191 } |
4234 | 192 |
4219 | 193 tree_expression *elt = *p++; |
2982 | 194 |
195 if (elt) | |
196 { | |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
197 octave_value tmp = elt->rvalue1 (); |
2982 | 198 |
199 if (error_state) | |
200 { | |
3162 | 201 ::error ("evaluating argument list element number %d", k+1); |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8173
diff
changeset
|
202 args.clear (); |
2982 | 203 break; |
204 } | |
205 else | |
206 { | |
5848 | 207 if (tmp.is_cs_list ()) |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8173
diff
changeset
|
208 args.push_back (tmp.list_value ()); |
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
|
209 else if (tmp.is_defined ()) |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8173
diff
changeset
|
210 args.push_back (tmp); |
2982 | 211 } |
212 } | |
213 else | |
214 { | |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8173
diff
changeset
|
215 args.push_back (octave_value ()); |
2982 | 216 break; |
217 } | |
218 } | |
219 | |
4256 | 220 if (stash_object) |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
221 unwind_protect::run_frame (uwp_frame); |
4234 | 222 |
2982 | 223 return args; |
224 } | |
225 | |
5846 | 226 std::list<octave_lvalue> |
227 tree_argument_list::lvalue_list (void) | |
228 { | |
229 std::list<octave_lvalue> retval; | |
230 | |
231 for (tree_argument_list::iterator p = begin (); | |
232 p != end (); | |
233 p++) | |
234 { | |
235 tree_expression *elt = *p; | |
236 | |
237 retval.push_back (elt->lvalue ()); | |
238 } | |
239 | |
240 return retval; | |
241 } | |
242 | |
2982 | 243 string_vector |
244 tree_argument_list::get_arg_names (void) const | |
245 { | |
246 int len = length (); | |
247 | |
248 string_vector retval (len); | |
249 | |
250 int k = 0; | |
251 | |
4219 | 252 for (const_iterator p = begin (); p != end (); p++) |
2982 | 253 { |
4219 | 254 tree_expression *elt = *p; |
2982 | 255 |
2991 | 256 retval(k++) = elt->str_print_code (); |
2982 | 257 } |
258 | |
259 return retval; | |
260 } | |
261 | |
5861 | 262 tree_argument_list * |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7751
diff
changeset
|
263 tree_argument_list::dup (symbol_table::scope_id scope, |
8913
35cd375d4bb3
make tree::dup functions const
John W. Eaton <jwe@octave.org>
parents:
8658
diff
changeset
|
264 symbol_table::context_id context) const |
5861 | 265 { |
266 tree_argument_list *new_list = new tree_argument_list (); | |
267 | |
268 new_list->list_includes_magic_end = list_includes_magic_end; | |
269 new_list->simple_assign_lhs = simple_assign_lhs; | |
270 | |
8913
35cd375d4bb3
make tree::dup functions const
John W. Eaton <jwe@octave.org>
parents:
8658
diff
changeset
|
271 for (const_iterator p = begin (); p != end (); p++) |
5861 | 272 { |
8913
35cd375d4bb3
make tree::dup functions const
John W. Eaton <jwe@octave.org>
parents:
8658
diff
changeset
|
273 const tree_expression *elt = *p; |
5861 | 274 |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7751
diff
changeset
|
275 new_list->append (elt ? elt->dup (scope, context) : 0); |
5861 | 276 } |
277 | |
278 return new_list; | |
279 } | |
280 | |
2982 | 281 void |
282 tree_argument_list::accept (tree_walker& tw) | |
283 { | |
284 tw.visit_argument_list (*this); | |
285 } | |
286 | |
287 /* | |
288 ;;; Local Variables: *** | |
289 ;;; mode: C++ *** | |
290 ;;; End: *** | |
291 */ |