2982
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
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" |
|
35 #include "oct-obj.h" |
|
36 #include "ov.h" |
|
37 #include "ov-usr-fcn.h" |
|
38 #include "pt-arg-list.h" |
|
39 #include "pt-exp.h" |
|
40 #include "pt-pr-code.h" |
|
41 #include "pt-walk.h" |
|
42 #include "toplev.h" |
4234
|
43 #include "unwind-prot.h" |
2982
|
44 |
|
45 // Argument lists. |
|
46 |
|
47 tree_argument_list::~tree_argument_list (void) |
|
48 { |
4219
|
49 while (! empty ()) |
2982
|
50 { |
4219
|
51 iterator p = begin (); |
|
52 delete *p; |
|
53 erase (p); |
2982
|
54 } |
|
55 } |
|
56 |
4267
|
57 bool |
|
58 tree_argument_list::has_magic_end (void) const |
|
59 { |
|
60 for (const_iterator p = begin (); p != end (); p++) |
|
61 { |
|
62 tree_expression *elt = *p; |
|
63 |
|
64 if (elt && elt->has_magic_end ()) |
|
65 return true; |
|
66 } |
|
67 |
|
68 return false; |
|
69 } |
|
70 |
4258
|
71 void |
|
72 tree_argument_list::append (const element_type& s) |
|
73 { |
|
74 octave_base_list<tree_expression *>::append (s); |
|
75 |
4267
|
76 if (! list_includes_magic_end && s && s->has_magic_end ()) |
|
77 list_includes_magic_end = true; |
4258
|
78 } |
|
79 |
3977
|
80 int |
|
81 tree_argument_list::nargout_count (void) const |
|
82 { |
|
83 int retval = 0; |
|
84 |
4219
|
85 for (const_iterator p = begin (); p != end (); p++) |
3977
|
86 { |
4219
|
87 tree_expression *elt = *p; |
3977
|
88 |
|
89 // XXX FIXME XXX -- need to be able to determine whether elt is |
|
90 // an expression that could evaluate to a cs-list object, and if |
|
91 // so, how many elements are in that list. Ugly! |
|
92 |
|
93 retval++; |
|
94 } |
|
95 |
|
96 return retval; |
|
97 } |
|
98 |
2982
|
99 bool |
|
100 tree_argument_list::all_elements_are_constant (void) const |
|
101 { |
4219
|
102 for (const_iterator p = begin (); p != end (); p++) |
2982
|
103 { |
4219
|
104 tree_expression *elt = *p; |
2982
|
105 |
|
106 if (! elt->is_constant ()) |
|
107 return false; |
|
108 } |
|
109 |
|
110 return true; |
|
111 } |
|
112 |
4234
|
113 static const octave_value *indexed_object = 0; |
|
114 static int index_position = 0; |
|
115 |
|
116 DEFCONSTFUN (__end__, , , |
|
117 "internal function") |
|
118 { |
|
119 octave_value retval; |
|
120 |
|
121 if (indexed_object) |
|
122 { |
4671
|
123 dim_vector dv = indexed_object->dims (); |
|
124 |
4234
|
125 switch (index_position) |
|
126 { |
|
127 case -1: |
4255
|
128 { |
5275
|
129 octave_idx_type numel = dv.numel (); |
4256
|
130 |
4671
|
131 if (numel < 0) |
|
132 { |
|
133 std::string dv_str = dv.str (); |
|
134 ::error ("invalid use of end: (index 1, dims %s)", |
|
135 dv_str.c_str ()); |
|
136 } |
4256
|
137 else |
4671
|
138 retval = numel; |
4256
|
139 } |
4234
|
140 break; |
|
141 |
|
142 default: |
4671
|
143 { |
|
144 |
|
145 if (index_position < dv.length ()) |
|
146 retval = dv(index_position); |
|
147 else |
5388
|
148 retval = 1; |
4671
|
149 } |
4234
|
150 break; |
|
151 } |
|
152 } |
|
153 else |
4256
|
154 ::error ("invalid use of end"); |
4234
|
155 |
|
156 return retval; |
|
157 } |
|
158 |
2982
|
159 octave_value_list |
4234
|
160 tree_argument_list::convert_to_const_vector (const octave_value *object) |
2982
|
161 { |
4256
|
162 // END doesn't make sense for functions. Maybe we need a different |
|
163 // way of asking an octave_value object this question? |
|
164 |
4258
|
165 bool stash_object = (list_includes_magic_end |
5087
|
166 && object |
|
167 && ! (object->is_function () |
|
168 || object->is_function_handle ())); |
4234
|
169 |
4256
|
170 if (stash_object) |
|
171 { |
|
172 unwind_protect::begin_frame ("convert_to_const_vector"); |
4234
|
173 |
5760
|
174 unwind_protect_const_ptr (indexed_object); |
4256
|
175 |
|
176 indexed_object = object; |
|
177 } |
4234
|
178 |
2982
|
179 int len = length (); |
|
180 |
|
181 // XXX FIXME XXX -- would be nice to know in advance how largs args |
|
182 // needs to be even when we have a list containing an all_va_args |
|
183 // token. |
|
184 |
|
185 octave_value_list args; |
3977
|
186 int args_len = len; |
|
187 args.resize (args_len); |
2982
|
188 |
4219
|
189 iterator p = begin (); |
2982
|
190 int j = 0; |
|
191 for (int k = 0; k < len; k++) |
|
192 { |
4974
|
193 if (stash_object) |
|
194 { |
|
195 unwind_protect_int (index_position); |
|
196 |
|
197 index_position = (len == 1) ? -1 : k; |
|
198 } |
4234
|
199 |
4219
|
200 tree_expression *elt = *p++; |
2982
|
201 |
|
202 if (elt) |
|
203 { |
|
204 octave_value tmp = elt->rvalue (); |
|
205 |
|
206 if (error_state) |
|
207 { |
3162
|
208 ::error ("evaluating argument list element number %d", k+1); |
2982
|
209 args = octave_value_list (); |
|
210 break; |
|
211 } |
|
212 else |
|
213 { |
|
214 if (tmp.is_all_va_args ()) |
|
215 { |
5743
|
216 octave_function *fcn = octave_call_stack::current (); |
|
217 |
|
218 if (fcn) |
2982
|
219 { |
|
220 octave_value_list tva; |
5743
|
221 tva = fcn->octave_all_va_args (); |
2982
|
222 int n = tva.length (); |
3977
|
223 args_len += n - 1; |
|
224 args.resize (args_len); |
2982
|
225 for (int i = 0; i < n; i++) |
|
226 args(j++) = tva(i); |
|
227 } |
|
228 else |
|
229 { |
|
230 ::error ("all_va_args is only valid inside functions"); |
|
231 args = octave_value_list (); |
|
232 break; |
|
233 } |
|
234 } |
3977
|
235 else if (tmp.is_cs_list ()) |
|
236 { |
|
237 octave_value_list tl = tmp.list_value (); |
|
238 int n = tl.length (); |
|
239 args_len += n - 1; |
|
240 args.resize (args_len); |
|
241 for (int i = 0; i < n; i++) |
|
242 args(j++) = tl(i); |
|
243 } |
2982
|
244 else |
|
245 args(j++) = tmp; |
|
246 } |
|
247 } |
|
248 else |
|
249 { |
|
250 args(j++) = octave_value (); |
|
251 break; |
|
252 } |
|
253 } |
|
254 |
|
255 args.resize (j); |
|
256 |
4256
|
257 if (stash_object) |
|
258 unwind_protect::run_frame ("convert_to_const_vector"); |
4234
|
259 |
2982
|
260 return args; |
|
261 } |
|
262 |
|
263 string_vector |
|
264 tree_argument_list::get_arg_names (void) const |
|
265 { |
|
266 int len = length (); |
|
267 |
|
268 string_vector retval (len); |
|
269 |
|
270 int k = 0; |
|
271 |
4219
|
272 for (const_iterator p = begin (); p != end (); p++) |
2982
|
273 { |
4219
|
274 tree_expression *elt = *p; |
2982
|
275 |
2991
|
276 retval(k++) = elt->str_print_code (); |
2982
|
277 } |
|
278 |
|
279 return retval; |
|
280 } |
|
281 |
|
282 void |
|
283 tree_argument_list::accept (tree_walker& tw) |
|
284 { |
|
285 tw.visit_argument_list (*this); |
|
286 } |
|
287 |
|
288 /* |
|
289 ;;; Local Variables: *** |
|
290 ;;; mode: C++ *** |
|
291 ;;; End: *** |
|
292 */ |