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 |
4258
|
60 void |
|
61 tree_argument_list::append (const element_type& s) |
|
62 { |
|
63 octave_base_list<tree_expression *>::append (s); |
|
64 |
|
65 if (s && s->is_identifier () && s->name () == "__end__") |
|
66 list_includes_magic_end = true; |
|
67 } |
|
68 |
3977
|
69 int |
|
70 tree_argument_list::nargout_count (void) const |
|
71 { |
|
72 int retval = 0; |
|
73 |
4219
|
74 for (const_iterator p = begin (); p != end (); p++) |
3977
|
75 { |
4219
|
76 tree_expression *elt = *p; |
3977
|
77 |
|
78 // XXX FIXME XXX -- need to be able to determine whether elt is |
|
79 // an expression that could evaluate to a cs-list object, and if |
|
80 // so, how many elements are in that list. Ugly! |
|
81 |
|
82 retval++; |
|
83 } |
|
84 |
|
85 return retval; |
|
86 } |
|
87 |
2982
|
88 bool |
|
89 tree_argument_list::all_elements_are_constant (void) const |
|
90 { |
4219
|
91 for (const_iterator p = begin (); p != end (); p++) |
2982
|
92 { |
4219
|
93 tree_expression *elt = *p; |
2982
|
94 |
|
95 if (! elt->is_constant ()) |
|
96 return false; |
|
97 } |
|
98 |
|
99 return true; |
|
100 } |
|
101 |
4234
|
102 static const octave_value *indexed_object = 0; |
|
103 static int index_position = 0; |
|
104 |
|
105 DEFCONSTFUN (__end__, , , |
|
106 "internal function") |
|
107 { |
|
108 octave_value retval; |
|
109 |
|
110 if (indexed_object) |
|
111 { |
|
112 switch (index_position) |
|
113 { |
|
114 case -1: |
|
115 // XXX FIXME XXX -- we really want "numel" here. |
4255
|
116 { |
|
117 int nr = indexed_object->rows (); |
|
118 int nc = indexed_object->columns (); |
|
119 |
|
120 if (nr < 0 || nc < 0) |
|
121 ::error ("invalid use of end"); |
|
122 else |
|
123 retval = nr * nc; |
|
124 } |
4234
|
125 break; |
|
126 |
|
127 case 0: |
4256
|
128 { |
|
129 int nr = indexed_object->rows (); |
|
130 |
|
131 if (nr < 0) |
|
132 ::error ("invalid use of end"); |
|
133 else |
|
134 retval = nr; |
|
135 } |
4234
|
136 break; |
|
137 |
|
138 case 1: |
4256
|
139 { |
|
140 int nc = indexed_object->columns (); |
|
141 |
|
142 if (nc < 0) |
|
143 ::error ("invalid use of end"); |
|
144 else |
|
145 retval = nc; |
|
146 } |
4234
|
147 break; |
|
148 |
|
149 default: |
|
150 ::error ("__end__: internal error"); |
|
151 break; |
|
152 } |
|
153 } |
|
154 else |
4256
|
155 ::error ("invalid use of end"); |
4234
|
156 |
|
157 return retval; |
|
158 } |
|
159 |
2982
|
160 octave_value_list |
4234
|
161 tree_argument_list::convert_to_const_vector (const octave_value *object) |
2982
|
162 { |
4256
|
163 // END doesn't make sense for functions. Maybe we need a different |
|
164 // way of asking an octave_value object this question? |
|
165 |
4258
|
166 bool stash_object = (list_includes_magic_end |
|
167 && object && object->is_constant ()); |
4234
|
168 |
4256
|
169 if (stash_object) |
|
170 { |
|
171 unwind_protect::begin_frame ("convert_to_const_vector"); |
4234
|
172 |
4256
|
173 unwind_protect_ptr (indexed_object); |
|
174 |
|
175 indexed_object = object; |
|
176 } |
4234
|
177 |
2982
|
178 int len = length (); |
|
179 |
|
180 // XXX FIXME XXX -- would be nice to know in advance how largs args |
|
181 // needs to be even when we have a list containing an all_va_args |
|
182 // token. |
|
183 |
|
184 octave_value_list args; |
3977
|
185 int args_len = len; |
|
186 args.resize (args_len); |
2982
|
187 |
4219
|
188 iterator p = begin (); |
2982
|
189 int j = 0; |
|
190 for (int k = 0; k < len; k++) |
|
191 { |
4234
|
192 index_position = (len == 1) ? -1 : k; |
|
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 { |
|
208 if (tmp.is_all_va_args ()) |
|
209 { |
|
210 if (curr_function) |
|
211 { |
|
212 octave_value_list tva; |
|
213 tva = curr_function->octave_all_va_args (); |
|
214 int n = tva.length (); |
3977
|
215 args_len += n - 1; |
|
216 args.resize (args_len); |
2982
|
217 for (int i = 0; i < n; i++) |
|
218 args(j++) = tva(i); |
|
219 } |
|
220 else |
|
221 { |
|
222 ::error ("all_va_args is only valid inside functions"); |
|
223 args = octave_value_list (); |
|
224 break; |
|
225 } |
|
226 } |
3977
|
227 else if (tmp.is_cs_list ()) |
|
228 { |
|
229 octave_value_list tl = tmp.list_value (); |
|
230 int n = tl.length (); |
|
231 args_len += n - 1; |
|
232 args.resize (args_len); |
|
233 for (int i = 0; i < n; i++) |
|
234 args(j++) = tl(i); |
|
235 } |
2982
|
236 else |
|
237 args(j++) = tmp; |
|
238 } |
|
239 } |
|
240 else |
|
241 { |
|
242 args(j++) = octave_value (); |
|
243 break; |
|
244 } |
|
245 } |
|
246 |
|
247 args.resize (j); |
|
248 |
4256
|
249 if (stash_object) |
|
250 unwind_protect::run_frame ("convert_to_const_vector"); |
4234
|
251 |
2982
|
252 return args; |
|
253 } |
|
254 |
|
255 string_vector |
|
256 tree_argument_list::get_arg_names (void) const |
|
257 { |
|
258 int len = length (); |
|
259 |
|
260 string_vector retval (len); |
|
261 |
|
262 int k = 0; |
|
263 |
4219
|
264 for (const_iterator p = begin (); p != end (); p++) |
2982
|
265 { |
4219
|
266 tree_expression *elt = *p; |
2982
|
267 |
2991
|
268 retval(k++) = elt->str_print_code (); |
2982
|
269 } |
|
270 |
|
271 return retval; |
|
272 } |
|
273 |
|
274 void |
|
275 tree_argument_list::accept (tree_walker& tw) |
|
276 { |
|
277 tw.visit_argument_list (*this); |
|
278 } |
|
279 |
|
280 /* |
|
281 ;;; Local Variables: *** |
|
282 ;;; mode: C++ *** |
|
283 ;;; End: *** |
|
284 */ |