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" |
5846
|
35 #include "oct-lvalue.h" |
2982
|
36 #include "oct-obj.h" |
|
37 #include "ov.h" |
|
38 #include "ov-usr-fcn.h" |
|
39 #include "pt-arg-list.h" |
|
40 #include "pt-exp.h" |
|
41 #include "pt-pr-code.h" |
|
42 #include "pt-walk.h" |
|
43 #include "toplev.h" |
4234
|
44 #include "unwind-prot.h" |
2982
|
45 |
|
46 // Argument lists. |
|
47 |
|
48 tree_argument_list::~tree_argument_list (void) |
|
49 { |
4219
|
50 while (! empty ()) |
2982
|
51 { |
4219
|
52 iterator p = begin (); |
|
53 delete *p; |
|
54 erase (p); |
2982
|
55 } |
|
56 } |
|
57 |
4267
|
58 bool |
|
59 tree_argument_list::has_magic_end (void) const |
|
60 { |
|
61 for (const_iterator p = begin (); p != end (); p++) |
|
62 { |
|
63 tree_expression *elt = *p; |
|
64 |
|
65 if (elt && elt->has_magic_end ()) |
|
66 return true; |
|
67 } |
|
68 |
|
69 return false; |
|
70 } |
|
71 |
4258
|
72 void |
|
73 tree_argument_list::append (const element_type& s) |
|
74 { |
|
75 octave_base_list<tree_expression *>::append (s); |
|
76 |
4267
|
77 if (! list_includes_magic_end && s && s->has_magic_end ()) |
|
78 list_includes_magic_end = true; |
4258
|
79 } |
|
80 |
2982
|
81 bool |
|
82 tree_argument_list::all_elements_are_constant (void) const |
|
83 { |
4219
|
84 for (const_iterator p = begin (); p != end (); p++) |
2982
|
85 { |
4219
|
86 tree_expression *elt = *p; |
2982
|
87 |
|
88 if (! elt->is_constant ()) |
|
89 return false; |
|
90 } |
|
91 |
|
92 return true; |
|
93 } |
|
94 |
4234
|
95 static const octave_value *indexed_object = 0; |
|
96 static int index_position = 0; |
|
97 |
|
98 DEFCONSTFUN (__end__, , , |
|
99 "internal function") |
|
100 { |
|
101 octave_value retval; |
|
102 |
|
103 if (indexed_object) |
|
104 { |
4671
|
105 dim_vector dv = indexed_object->dims (); |
|
106 |
4234
|
107 switch (index_position) |
|
108 { |
|
109 case -1: |
4255
|
110 { |
5275
|
111 octave_idx_type numel = dv.numel (); |
4256
|
112 |
4671
|
113 if (numel < 0) |
|
114 { |
|
115 std::string dv_str = dv.str (); |
|
116 ::error ("invalid use of end: (index 1, dims %s)", |
|
117 dv_str.c_str ()); |
|
118 } |
4256
|
119 else |
4671
|
120 retval = numel; |
4256
|
121 } |
4234
|
122 break; |
|
123 |
|
124 default: |
4671
|
125 { |
|
126 |
|
127 if (index_position < dv.length ()) |
|
128 retval = dv(index_position); |
|
129 else |
5388
|
130 retval = 1; |
4671
|
131 } |
4234
|
132 break; |
|
133 } |
|
134 } |
|
135 else |
4256
|
136 ::error ("invalid use of end"); |
4234
|
137 |
|
138 return retval; |
|
139 } |
|
140 |
2982
|
141 octave_value_list |
4234
|
142 tree_argument_list::convert_to_const_vector (const octave_value *object) |
2982
|
143 { |
4256
|
144 // END doesn't make sense for functions. Maybe we need a different |
|
145 // way of asking an octave_value object this question? |
|
146 |
4258
|
147 bool stash_object = (list_includes_magic_end |
5087
|
148 && object |
|
149 && ! (object->is_function () |
|
150 || object->is_function_handle ())); |
4234
|
151 |
4256
|
152 if (stash_object) |
|
153 { |
|
154 unwind_protect::begin_frame ("convert_to_const_vector"); |
4234
|
155 |
5760
|
156 unwind_protect_const_ptr (indexed_object); |
4256
|
157 |
|
158 indexed_object = object; |
|
159 } |
4234
|
160 |
2982
|
161 int len = length (); |
|
162 |
5775
|
163 // FIXME -- would be nice to know in advance how largs args |
2982
|
164 // needs to be even when we have a list containing an all_va_args |
|
165 // token. |
|
166 |
|
167 octave_value_list args; |
3977
|
168 int args_len = len; |
|
169 args.resize (args_len); |
2982
|
170 |
4219
|
171 iterator p = begin (); |
2982
|
172 int j = 0; |
|
173 for (int k = 0; k < len; k++) |
|
174 { |
4974
|
175 if (stash_object) |
|
176 { |
|
177 unwind_protect_int (index_position); |
|
178 |
|
179 index_position = (len == 1) ? -1 : k; |
|
180 } |
4234
|
181 |
4219
|
182 tree_expression *elt = *p++; |
2982
|
183 |
|
184 if (elt) |
|
185 { |
|
186 octave_value tmp = elt->rvalue (); |
|
187 |
|
188 if (error_state) |
|
189 { |
3162
|
190 ::error ("evaluating argument list element number %d", k+1); |
2982
|
191 args = octave_value_list (); |
|
192 break; |
|
193 } |
|
194 else |
|
195 { |
|
196 if (tmp.is_all_va_args ()) |
|
197 { |
5743
|
198 octave_function *fcn = octave_call_stack::current (); |
|
199 |
|
200 if (fcn) |
2982
|
201 { |
|
202 octave_value_list tva; |
5743
|
203 tva = fcn->octave_all_va_args (); |
2982
|
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 |
5846
|
245 std::list<octave_lvalue> |
|
246 tree_argument_list::lvalue_list (void) |
|
247 { |
|
248 std::list<octave_lvalue> retval; |
|
249 |
|
250 for (tree_argument_list::iterator p = begin (); |
|
251 p != end (); |
|
252 p++) |
|
253 { |
|
254 tree_expression *elt = *p; |
|
255 |
|
256 retval.push_back (elt->lvalue ()); |
|
257 } |
|
258 |
|
259 return retval; |
|
260 } |
|
261 |
2982
|
262 string_vector |
|
263 tree_argument_list::get_arg_names (void) const |
|
264 { |
|
265 int len = length (); |
|
266 |
|
267 string_vector retval (len); |
|
268 |
|
269 int k = 0; |
|
270 |
4219
|
271 for (const_iterator p = begin (); p != end (); p++) |
2982
|
272 { |
4219
|
273 tree_expression *elt = *p; |
2982
|
274 |
2991
|
275 retval(k++) = elt->str_print_code (); |
2982
|
276 } |
|
277 |
|
278 return retval; |
|
279 } |
|
280 |
|
281 void |
|
282 tree_argument_list::accept (tree_walker& tw) |
|
283 { |
|
284 tw.visit_argument_list (*this); |
|
285 } |
|
286 |
|
287 /* |
|
288 ;;; Local Variables: *** |
|
289 ;;; mode: C++ *** |
|
290 ;;; End: *** |
|
291 */ |