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: |
|
119 retval = indexed_object->rows (); |
|
120 break; |
|
121 |
|
122 case 1: |
|
123 retval = indexed_object->columns (); |
|
124 break; |
|
125 |
|
126 default: |
|
127 ::error ("__end__: internal error"); |
|
128 break; |
|
129 } |
|
130 } |
|
131 else |
|
132 ::error ("__end__: internal error"); |
|
133 |
|
134 return retval; |
|
135 } |
|
136 |
2982
|
137 octave_value_list |
4234
|
138 tree_argument_list::convert_to_const_vector (const octave_value *object) |
2982
|
139 { |
4234
|
140 unwind_protect::begin_frame ("convert_to_const_vector"); |
|
141 |
|
142 unwind_protect_ptr (indexed_object); |
|
143 |
|
144 indexed_object = object; |
|
145 |
2982
|
146 int len = length (); |
|
147 |
|
148 // XXX FIXME XXX -- would be nice to know in advance how largs args |
|
149 // needs to be even when we have a list containing an all_va_args |
|
150 // token. |
|
151 |
|
152 octave_value_list args; |
3977
|
153 int args_len = len; |
|
154 args.resize (args_len); |
2982
|
155 |
4219
|
156 iterator p = begin (); |
2982
|
157 int j = 0; |
|
158 for (int k = 0; k < len; k++) |
|
159 { |
4234
|
160 index_position = (len == 1) ? -1 : k; |
|
161 |
4219
|
162 tree_expression *elt = *p++; |
2982
|
163 |
|
164 if (elt) |
|
165 { |
|
166 octave_value tmp = elt->rvalue (); |
|
167 |
|
168 if (error_state) |
|
169 { |
3162
|
170 ::error ("evaluating argument list element number %d", k+1); |
2982
|
171 args = octave_value_list (); |
|
172 break; |
|
173 } |
|
174 else |
|
175 { |
|
176 if (tmp.is_all_va_args ()) |
|
177 { |
|
178 if (curr_function) |
|
179 { |
|
180 octave_value_list tva; |
|
181 tva = curr_function->octave_all_va_args (); |
|
182 int n = tva.length (); |
3977
|
183 args_len += n - 1; |
|
184 args.resize (args_len); |
2982
|
185 for (int i = 0; i < n; i++) |
|
186 args(j++) = tva(i); |
|
187 } |
|
188 else |
|
189 { |
|
190 ::error ("all_va_args is only valid inside functions"); |
|
191 args = octave_value_list (); |
|
192 break; |
|
193 } |
|
194 } |
3977
|
195 else if (tmp.is_cs_list ()) |
|
196 { |
|
197 octave_value_list tl = tmp.list_value (); |
|
198 int n = tl.length (); |
|
199 args_len += n - 1; |
|
200 args.resize (args_len); |
|
201 for (int i = 0; i < n; i++) |
|
202 args(j++) = tl(i); |
|
203 } |
2982
|
204 else |
|
205 args(j++) = tmp; |
|
206 } |
|
207 } |
|
208 else |
|
209 { |
|
210 args(j++) = octave_value (); |
|
211 break; |
|
212 } |
|
213 } |
|
214 |
|
215 args.resize (j); |
|
216 |
4234
|
217 unwind_protect::run_frame ("convert_to_const_vector"); |
|
218 |
2982
|
219 return args; |
|
220 } |
|
221 |
|
222 string_vector |
|
223 tree_argument_list::get_arg_names (void) const |
|
224 { |
|
225 int len = length (); |
|
226 |
|
227 string_vector retval (len); |
|
228 |
|
229 int k = 0; |
|
230 |
4219
|
231 for (const_iterator p = begin (); p != end (); p++) |
2982
|
232 { |
4219
|
233 tree_expression *elt = *p; |
2982
|
234 |
2991
|
235 retval(k++) = elt->str_print_code (); |
2982
|
236 } |
|
237 |
|
238 return retval; |
|
239 } |
|
240 |
|
241 void |
|
242 tree_argument_list::accept (tree_walker& tw) |
|
243 { |
|
244 tw.visit_argument_list (*this); |
|
245 } |
|
246 |
|
247 /* |
|
248 ;;; Local Variables: *** |
|
249 ;;; mode: C++ *** |
|
250 ;;; End: *** |
|
251 */ |