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 |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
2982
|
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 |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
2982
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
3503
|
27 #include <iostream> |
2982
|
28 #include <string> |
|
29 |
|
30 #include "str-vec.h" |
|
31 |
4234
|
32 #include "defun.h" |
2982
|
33 #include "error.h" |
5846
|
34 #include "oct-lvalue.h" |
2982
|
35 #include "oct-obj.h" |
|
36 #include "ov.h" |
|
37 #include "ov-usr-fcn.h" |
|
38 #include "pt-arg-list.h" |
|
39 #include "pt-exp.h" |
|
40 #include "pt-pr-code.h" |
|
41 #include "pt-walk.h" |
|
42 #include "toplev.h" |
4234
|
43 #include "unwind-prot.h" |
2982
|
44 |
|
45 // Argument lists. |
|
46 |
|
47 tree_argument_list::~tree_argument_list (void) |
|
48 { |
4219
|
49 while (! empty ()) |
2982
|
50 { |
4219
|
51 iterator p = begin (); |
|
52 delete *p; |
|
53 erase (p); |
2982
|
54 } |
|
55 } |
|
56 |
4267
|
57 bool |
|
58 tree_argument_list::has_magic_end (void) const |
|
59 { |
|
60 for (const_iterator p = begin (); p != end (); p++) |
|
61 { |
|
62 tree_expression *elt = *p; |
|
63 |
|
64 if (elt && elt->has_magic_end ()) |
|
65 return true; |
|
66 } |
|
67 |
|
68 return false; |
|
69 } |
|
70 |
4258
|
71 void |
|
72 tree_argument_list::append (const element_type& s) |
|
73 { |
|
74 octave_base_list<tree_expression *>::append (s); |
|
75 |
4267
|
76 if (! list_includes_magic_end && s && s->has_magic_end ()) |
|
77 list_includes_magic_end = true; |
4258
|
78 } |
|
79 |
2982
|
80 bool |
|
81 tree_argument_list::all_elements_are_constant (void) const |
|
82 { |
4219
|
83 for (const_iterator p = begin (); p != end (); p++) |
2982
|
84 { |
4219
|
85 tree_expression *elt = *p; |
2982
|
86 |
|
87 if (! elt->is_constant ()) |
|
88 return false; |
|
89 } |
|
90 |
|
91 return true; |
|
92 } |
|
93 |
4234
|
94 static const octave_value *indexed_object = 0; |
|
95 static int index_position = 0; |
|
96 |
|
97 DEFCONSTFUN (__end__, , , |
|
98 "internal function") |
|
99 { |
|
100 octave_value retval; |
|
101 |
|
102 if (indexed_object) |
|
103 { |
4671
|
104 dim_vector dv = indexed_object->dims (); |
|
105 |
4234
|
106 switch (index_position) |
|
107 { |
|
108 case -1: |
4255
|
109 { |
5275
|
110 octave_idx_type numel = dv.numel (); |
4256
|
111 |
4671
|
112 if (numel < 0) |
|
113 { |
|
114 std::string dv_str = dv.str (); |
|
115 ::error ("invalid use of end: (index 1, dims %s)", |
|
116 dv_str.c_str ()); |
|
117 } |
4256
|
118 else |
4671
|
119 retval = numel; |
4256
|
120 } |
4234
|
121 break; |
|
122 |
|
123 default: |
4671
|
124 { |
|
125 |
|
126 if (index_position < dv.length ()) |
|
127 retval = dv(index_position); |
|
128 else |
5388
|
129 retval = 1; |
4671
|
130 } |
4234
|
131 break; |
|
132 } |
|
133 } |
|
134 else |
4256
|
135 ::error ("invalid use of end"); |
4234
|
136 |
|
137 return retval; |
|
138 } |
|
139 |
2982
|
140 octave_value_list |
4234
|
141 tree_argument_list::convert_to_const_vector (const octave_value *object) |
2982
|
142 { |
4256
|
143 // END doesn't make sense for functions. Maybe we need a different |
|
144 // way of asking an octave_value object this question? |
|
145 |
4258
|
146 bool stash_object = (list_includes_magic_end |
5087
|
147 && object |
|
148 && ! (object->is_function () |
|
149 || object->is_function_handle ())); |
4234
|
150 |
4256
|
151 if (stash_object) |
|
152 { |
|
153 unwind_protect::begin_frame ("convert_to_const_vector"); |
4234
|
154 |
5760
|
155 unwind_protect_const_ptr (indexed_object); |
4256
|
156 |
|
157 indexed_object = object; |
|
158 } |
4234
|
159 |
2982
|
160 int len = length (); |
|
161 |
|
162 octave_value_list args; |
3977
|
163 int args_len = len; |
|
164 args.resize (args_len); |
2982
|
165 |
4219
|
166 iterator p = begin (); |
2982
|
167 int j = 0; |
|
168 for (int k = 0; k < len; k++) |
|
169 { |
4974
|
170 if (stash_object) |
|
171 { |
|
172 unwind_protect_int (index_position); |
|
173 |
|
174 index_position = (len == 1) ? -1 : k; |
|
175 } |
4234
|
176 |
4219
|
177 tree_expression *elt = *p++; |
2982
|
178 |
|
179 if (elt) |
|
180 { |
|
181 octave_value tmp = elt->rvalue (); |
|
182 |
|
183 if (error_state) |
|
184 { |
3162
|
185 ::error ("evaluating argument list element number %d", k+1); |
2982
|
186 args = octave_value_list (); |
|
187 break; |
|
188 } |
|
189 else |
|
190 { |
5848
|
191 if (tmp.is_cs_list ()) |
3977
|
192 { |
|
193 octave_value_list tl = tmp.list_value (); |
|
194 int n = tl.length (); |
|
195 args_len += n - 1; |
|
196 args.resize (args_len); |
|
197 for (int i = 0; i < n; i++) |
|
198 args(j++) = tl(i); |
|
199 } |
2982
|
200 else |
|
201 args(j++) = tmp; |
|
202 } |
|
203 } |
|
204 else |
|
205 { |
|
206 args(j++) = octave_value (); |
|
207 break; |
|
208 } |
|
209 } |
|
210 |
|
211 args.resize (j); |
|
212 |
4256
|
213 if (stash_object) |
|
214 unwind_protect::run_frame ("convert_to_const_vector"); |
4234
|
215 |
2982
|
216 return args; |
|
217 } |
|
218 |
5846
|
219 std::list<octave_lvalue> |
|
220 tree_argument_list::lvalue_list (void) |
|
221 { |
|
222 std::list<octave_lvalue> retval; |
|
223 |
|
224 for (tree_argument_list::iterator p = begin (); |
|
225 p != end (); |
|
226 p++) |
|
227 { |
|
228 tree_expression *elt = *p; |
|
229 |
|
230 retval.push_back (elt->lvalue ()); |
|
231 } |
|
232 |
|
233 return retval; |
|
234 } |
|
235 |
2982
|
236 string_vector |
|
237 tree_argument_list::get_arg_names (void) const |
|
238 { |
|
239 int len = length (); |
|
240 |
|
241 string_vector retval (len); |
|
242 |
|
243 int k = 0; |
|
244 |
4219
|
245 for (const_iterator p = begin (); p != end (); p++) |
2982
|
246 { |
4219
|
247 tree_expression *elt = *p; |
2982
|
248 |
2991
|
249 retval(k++) = elt->str_print_code (); |
2982
|
250 } |
|
251 |
|
252 return retval; |
|
253 } |
|
254 |
5861
|
255 tree_argument_list * |
|
256 tree_argument_list::dup (symbol_table *sym_tab) |
|
257 { |
|
258 tree_argument_list *new_list = new tree_argument_list (); |
|
259 |
|
260 new_list->list_includes_magic_end = list_includes_magic_end; |
|
261 new_list->simple_assign_lhs = simple_assign_lhs; |
|
262 |
|
263 for (iterator p = begin (); p != end (); p++) |
|
264 { |
|
265 tree_expression *elt = *p; |
|
266 |
|
267 new_list->append (elt ? elt->dup (sym_tab) : 0); |
|
268 } |
|
269 |
|
270 return new_list; |
|
271 } |
|
272 |
2982
|
273 void |
|
274 tree_argument_list::accept (tree_walker& tw) |
|
275 { |
|
276 tw.visit_argument_list (*this); |
|
277 } |
|
278 |
|
279 /* |
|
280 ;;; Local Variables: *** |
|
281 ;;; mode: C++ *** |
|
282 ;;; End: *** |
|
283 */ |