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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
2982
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
3503
|
31 #include <iostream> |
2982
|
32 #include <string> |
|
33 |
|
34 #include "str-vec.h" |
|
35 |
4234
|
36 #include "defun.h" |
2982
|
37 #include "error.h" |
|
38 #include "oct-obj.h" |
|
39 #include "ov.h" |
|
40 #include "ov-usr-fcn.h" |
|
41 #include "pt-arg-list.h" |
|
42 #include "pt-exp.h" |
|
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 ()) |
|
68 return true; |
|
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; |
4258
|
81 } |
|
82 |
3977
|
83 int |
|
84 tree_argument_list::nargout_count (void) const |
|
85 { |
|
86 int retval = 0; |
|
87 |
4219
|
88 for (const_iterator p = begin (); p != end (); p++) |
3977
|
89 { |
4219
|
90 tree_expression *elt = *p; |
3977
|
91 |
|
92 // XXX FIXME XXX -- need to be able to determine whether elt is |
|
93 // an expression that could evaluate to a cs-list object, and if |
|
94 // so, how many elements are in that list. Ugly! |
|
95 |
|
96 retval++; |
|
97 } |
|
98 |
|
99 return retval; |
|
100 } |
|
101 |
2982
|
102 bool |
|
103 tree_argument_list::all_elements_are_constant (void) const |
|
104 { |
4219
|
105 for (const_iterator p = begin (); p != end (); p++) |
2982
|
106 { |
4219
|
107 tree_expression *elt = *p; |
2982
|
108 |
|
109 if (! elt->is_constant ()) |
|
110 return false; |
|
111 } |
|
112 |
|
113 return true; |
|
114 } |
|
115 |
4234
|
116 static const octave_value *indexed_object = 0; |
|
117 static int index_position = 0; |
|
118 |
|
119 DEFCONSTFUN (__end__, , , |
|
120 "internal function") |
|
121 { |
|
122 octave_value retval; |
|
123 |
|
124 if (indexed_object) |
|
125 { |
4671
|
126 dim_vector dv = indexed_object->dims (); |
|
127 |
4234
|
128 switch (index_position) |
|
129 { |
|
130 case -1: |
4255
|
131 { |
5082
|
132 // We want numel = prod (size ()) here, so don't use |
5081
|
133 // index_object->numel () as that may be different (it is |
|
134 // the number of nonzero elements for sparse arrays). |
|
135 |
|
136 int numel = dv.numel (); |
4256
|
137 |
4671
|
138 if (numel < 0) |
|
139 { |
|
140 std::string dv_str = dv.str (); |
|
141 ::error ("invalid use of end: (index 1, dims %s)", |
|
142 dv_str.c_str ()); |
|
143 } |
4256
|
144 else |
4671
|
145 retval = numel; |
4256
|
146 } |
4234
|
147 break; |
|
148 |
|
149 default: |
4671
|
150 { |
|
151 |
|
152 if (index_position < dv.length ()) |
|
153 retval = dv(index_position); |
|
154 else |
|
155 { |
|
156 std::string dv_str = dv.str (); |
|
157 ::error ("invalid use of end: (index %d, dims %s)", |
|
158 index_position+1, dv_str.c_str ()); |
|
159 } |
|
160 } |
4234
|
161 break; |
|
162 } |
|
163 } |
|
164 else |
4256
|
165 ::error ("invalid use of end"); |
4234
|
166 |
|
167 return retval; |
|
168 } |
|
169 |
2982
|
170 octave_value_list |
4234
|
171 tree_argument_list::convert_to_const_vector (const octave_value *object) |
2982
|
172 { |
4256
|
173 // END doesn't make sense for functions. Maybe we need a different |
|
174 // way of asking an octave_value object this question? |
|
175 |
4258
|
176 bool stash_object = (list_includes_magic_end |
|
177 && object && object->is_constant ()); |
4234
|
178 |
4256
|
179 if (stash_object) |
|
180 { |
|
181 unwind_protect::begin_frame ("convert_to_const_vector"); |
4234
|
182 |
4256
|
183 unwind_protect_ptr (indexed_object); |
|
184 |
|
185 indexed_object = object; |
|
186 } |
4234
|
187 |
2982
|
188 int len = length (); |
|
189 |
|
190 // XXX FIXME XXX -- would be nice to know in advance how largs args |
|
191 // needs to be even when we have a list containing an all_va_args |
|
192 // token. |
|
193 |
|
194 octave_value_list args; |
3977
|
195 int args_len = len; |
|
196 args.resize (args_len); |
2982
|
197 |
4219
|
198 iterator p = begin (); |
2982
|
199 int j = 0; |
|
200 for (int k = 0; k < len; k++) |
|
201 { |
4974
|
202 if (stash_object) |
|
203 { |
|
204 unwind_protect_int (index_position); |
|
205 |
|
206 index_position = (len == 1) ? -1 : k; |
|
207 } |
4234
|
208 |
4219
|
209 tree_expression *elt = *p++; |
2982
|
210 |
|
211 if (elt) |
|
212 { |
|
213 octave_value tmp = elt->rvalue (); |
|
214 |
|
215 if (error_state) |
|
216 { |
3162
|
217 ::error ("evaluating argument list element number %d", k+1); |
2982
|
218 args = octave_value_list (); |
|
219 break; |
|
220 } |
|
221 else |
|
222 { |
|
223 if (tmp.is_all_va_args ()) |
|
224 { |
|
225 if (curr_function) |
|
226 { |
|
227 octave_value_list tva; |
|
228 tva = curr_function->octave_all_va_args (); |
|
229 int n = tva.length (); |
3977
|
230 args_len += n - 1; |
|
231 args.resize (args_len); |
2982
|
232 for (int i = 0; i < n; i++) |
|
233 args(j++) = tva(i); |
|
234 } |
|
235 else |
|
236 { |
|
237 ::error ("all_va_args is only valid inside functions"); |
|
238 args = octave_value_list (); |
|
239 break; |
|
240 } |
|
241 } |
3977
|
242 else if (tmp.is_cs_list ()) |
|
243 { |
|
244 octave_value_list tl = tmp.list_value (); |
|
245 int n = tl.length (); |
|
246 args_len += n - 1; |
|
247 args.resize (args_len); |
|
248 for (int i = 0; i < n; i++) |
|
249 args(j++) = tl(i); |
|
250 } |
2982
|
251 else |
|
252 args(j++) = tmp; |
|
253 } |
|
254 } |
|
255 else |
|
256 { |
|
257 args(j++) = octave_value (); |
|
258 break; |
|
259 } |
|
260 } |
|
261 |
|
262 args.resize (j); |
|
263 |
4256
|
264 if (stash_object) |
|
265 unwind_protect::run_frame ("convert_to_const_vector"); |
4234
|
266 |
2982
|
267 return args; |
|
268 } |
|
269 |
|
270 string_vector |
|
271 tree_argument_list::get_arg_names (void) const |
|
272 { |
|
273 int len = length (); |
|
274 |
|
275 string_vector retval (len); |
|
276 |
|
277 int k = 0; |
|
278 |
4219
|
279 for (const_iterator p = begin (); p != end (); p++) |
2982
|
280 { |
4219
|
281 tree_expression *elt = *p; |
2982
|
282 |
2991
|
283 retval(k++) = elt->str_print_code (); |
2982
|
284 } |
|
285 |
|
286 return retval; |
|
287 } |
|
288 |
|
289 void |
|
290 tree_argument_list::accept (tree_walker& tw) |
|
291 { |
|
292 tw.visit_argument_list (*this); |
|
293 } |
|
294 |
|
295 /* |
|
296 ;;; Local Variables: *** |
|
297 ;;; mode: C++ *** |
|
298 ;;; End: *** |
|
299 */ |