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