2982
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1996, 1997, 1998, 2000, 2002, 2003, 2004, 2005, 2006, |
|
4 2007 John W. Eaton |
2982
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
2982
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
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 |
|
163 octave_value_list args; |
3977
|
164 int args_len = len; |
|
165 args.resize (args_len); |
2982
|
166 |
4219
|
167 iterator p = begin (); |
2982
|
168 int j = 0; |
|
169 for (int k = 0; k < len; k++) |
|
170 { |
4974
|
171 if (stash_object) |
|
172 { |
|
173 unwind_protect_int (index_position); |
|
174 |
|
175 index_position = (len == 1) ? -1 : k; |
|
176 } |
4234
|
177 |
4219
|
178 tree_expression *elt = *p++; |
2982
|
179 |
|
180 if (elt) |
|
181 { |
|
182 octave_value tmp = elt->rvalue (); |
|
183 |
|
184 if (error_state) |
|
185 { |
3162
|
186 ::error ("evaluating argument list element number %d", k+1); |
2982
|
187 args = octave_value_list (); |
|
188 break; |
|
189 } |
|
190 else |
|
191 { |
5848
|
192 if (tmp.is_cs_list ()) |
3977
|
193 { |
|
194 octave_value_list tl = tmp.list_value (); |
|
195 int n = tl.length (); |
|
196 args_len += n - 1; |
|
197 args.resize (args_len); |
|
198 for (int i = 0; i < n; i++) |
|
199 args(j++) = tl(i); |
|
200 } |
2982
|
201 else |
|
202 args(j++) = tmp; |
|
203 } |
|
204 } |
|
205 else |
|
206 { |
|
207 args(j++) = octave_value (); |
|
208 break; |
|
209 } |
|
210 } |
|
211 |
|
212 args.resize (j); |
|
213 |
4256
|
214 if (stash_object) |
|
215 unwind_protect::run_frame ("convert_to_const_vector"); |
4234
|
216 |
2982
|
217 return args; |
|
218 } |
|
219 |
5846
|
220 std::list<octave_lvalue> |
|
221 tree_argument_list::lvalue_list (void) |
|
222 { |
|
223 std::list<octave_lvalue> retval; |
|
224 |
|
225 for (tree_argument_list::iterator p = begin (); |
|
226 p != end (); |
|
227 p++) |
|
228 { |
|
229 tree_expression *elt = *p; |
|
230 |
|
231 retval.push_back (elt->lvalue ()); |
|
232 } |
|
233 |
|
234 return retval; |
|
235 } |
|
236 |
2982
|
237 string_vector |
|
238 tree_argument_list::get_arg_names (void) const |
|
239 { |
|
240 int len = length (); |
|
241 |
|
242 string_vector retval (len); |
|
243 |
|
244 int k = 0; |
|
245 |
4219
|
246 for (const_iterator p = begin (); p != end (); p++) |
2982
|
247 { |
4219
|
248 tree_expression *elt = *p; |
2982
|
249 |
2991
|
250 retval(k++) = elt->str_print_code (); |
2982
|
251 } |
|
252 |
|
253 return retval; |
|
254 } |
|
255 |
5861
|
256 tree_argument_list * |
7336
|
257 tree_argument_list::dup (symbol_table::scope_id scope) |
5861
|
258 { |
|
259 tree_argument_list *new_list = new tree_argument_list (); |
|
260 |
|
261 new_list->list_includes_magic_end = list_includes_magic_end; |
|
262 new_list->simple_assign_lhs = simple_assign_lhs; |
|
263 |
|
264 for (iterator p = begin (); p != end (); p++) |
|
265 { |
|
266 tree_expression *elt = *p; |
|
267 |
7336
|
268 new_list->append (elt ? elt->dup (scope) : 0); |
5861
|
269 } |
|
270 |
|
271 return new_list; |
|
272 } |
|
273 |
2982
|
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 */ |