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 { |
|
126 switch (index_position) |
|
127 { |
|
128 case -1: |
|
129 // XXX FIXME XXX -- we really want "numel" here. |
4255
|
130 { |
|
131 int nr = indexed_object->rows (); |
|
132 int nc = indexed_object->columns (); |
|
133 |
|
134 if (nr < 0 || nc < 0) |
4432
|
135 ::error ("invalid use of end: (nr=%d, nc=%d)", nr, nc); |
4255
|
136 else |
|
137 retval = nr * nc; |
|
138 } |
4234
|
139 break; |
|
140 |
|
141 case 0: |
4256
|
142 { |
|
143 int nr = indexed_object->rows (); |
|
144 |
|
145 if (nr < 0) |
4432
|
146 ::error ("invalid use of end: (nr=%d)", nr); |
4256
|
147 else |
|
148 retval = nr; |
|
149 } |
4234
|
150 break; |
|
151 |
|
152 case 1: |
4256
|
153 { |
|
154 int nc = indexed_object->columns (); |
|
155 |
|
156 if (nc < 0) |
4432
|
157 ::error ("invalid use of end: (nc=%d)", nc); |
4256
|
158 else |
|
159 retval = nc; |
|
160 } |
4234
|
161 break; |
|
162 |
|
163 default: |
|
164 ::error ("__end__: internal error"); |
|
165 break; |
|
166 } |
|
167 } |
|
168 else |
4256
|
169 ::error ("invalid use of end"); |
4234
|
170 |
|
171 return retval; |
|
172 } |
|
173 |
2982
|
174 octave_value_list |
4234
|
175 tree_argument_list::convert_to_const_vector (const octave_value *object) |
2982
|
176 { |
4256
|
177 // END doesn't make sense for functions. Maybe we need a different |
|
178 // way of asking an octave_value object this question? |
|
179 |
4258
|
180 bool stash_object = (list_includes_magic_end |
|
181 && object && object->is_constant ()); |
4234
|
182 |
4256
|
183 if (stash_object) |
|
184 { |
|
185 unwind_protect::begin_frame ("convert_to_const_vector"); |
4234
|
186 |
4256
|
187 unwind_protect_ptr (indexed_object); |
|
188 |
|
189 indexed_object = object; |
|
190 } |
4234
|
191 |
2982
|
192 int len = length (); |
|
193 |
|
194 // XXX FIXME XXX -- would be nice to know in advance how largs args |
|
195 // needs to be even when we have a list containing an all_va_args |
|
196 // token. |
|
197 |
|
198 octave_value_list args; |
3977
|
199 int args_len = len; |
|
200 args.resize (args_len); |
2982
|
201 |
4219
|
202 iterator p = begin (); |
2982
|
203 int j = 0; |
|
204 for (int k = 0; k < len; k++) |
|
205 { |
4234
|
206 index_position = (len == 1) ? -1 : k; |
|
207 |
4219
|
208 tree_expression *elt = *p++; |
2982
|
209 |
|
210 if (elt) |
|
211 { |
|
212 octave_value tmp = elt->rvalue (); |
|
213 |
|
214 if (error_state) |
|
215 { |
3162
|
216 ::error ("evaluating argument list element number %d", k+1); |
2982
|
217 args = octave_value_list (); |
|
218 break; |
|
219 } |
|
220 else |
|
221 { |
|
222 if (tmp.is_all_va_args ()) |
|
223 { |
|
224 if (curr_function) |
|
225 { |
|
226 octave_value_list tva; |
|
227 tva = curr_function->octave_all_va_args (); |
|
228 int n = tva.length (); |
3977
|
229 args_len += n - 1; |
|
230 args.resize (args_len); |
2982
|
231 for (int i = 0; i < n; i++) |
|
232 args(j++) = tva(i); |
|
233 } |
|
234 else |
|
235 { |
|
236 ::error ("all_va_args is only valid inside functions"); |
|
237 args = octave_value_list (); |
|
238 break; |
|
239 } |
|
240 } |
3977
|
241 else if (tmp.is_cs_list ()) |
|
242 { |
|
243 octave_value_list tl = tmp.list_value (); |
|
244 int n = tl.length (); |
|
245 args_len += n - 1; |
|
246 args.resize (args_len); |
|
247 for (int i = 0; i < n; i++) |
|
248 args(j++) = tl(i); |
|
249 } |
2982
|
250 else |
|
251 args(j++) = tmp; |
|
252 } |
|
253 } |
|
254 else |
|
255 { |
|
256 args(j++) = octave_value (); |
|
257 break; |
|
258 } |
|
259 } |
|
260 |
|
261 args.resize (j); |
|
262 |
4256
|
263 if (stash_object) |
|
264 unwind_protect::run_frame ("convert_to_const_vector"); |
4234
|
265 |
2982
|
266 return args; |
|
267 } |
|
268 |
|
269 string_vector |
|
270 tree_argument_list::get_arg_names (void) const |
|
271 { |
|
272 int len = length (); |
|
273 |
|
274 string_vector retval (len); |
|
275 |
|
276 int k = 0; |
|
277 |
4219
|
278 for (const_iterator p = begin (); p != end (); p++) |
2982
|
279 { |
4219
|
280 tree_expression *elt = *p; |
2982
|
281 |
2991
|
282 retval(k++) = elt->str_print_code (); |
2982
|
283 } |
|
284 |
|
285 return retval; |
|
286 } |
|
287 |
|
288 void |
|
289 tree_argument_list::accept (tree_walker& tw) |
|
290 { |
|
291 tw.visit_argument_list (*this); |
|
292 } |
|
293 |
|
294 /* |
|
295 ;;; Local Variables: *** |
|
296 ;;; mode: C++ *** |
|
297 ;;; End: *** |
|
298 */ |