4343
|
1 /* |
|
2 |
|
3 Copyright (C) 2003 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 |
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
|
31 #include <iostream> |
|
32 |
|
33 #include "defun.h" |
4654
|
34 #include "error.h" |
|
35 #include "gripes.h" |
4343
|
36 #include "oct-map.h" |
|
37 #include "ov-base.h" |
|
38 #include "ov-fcn-handle.h" |
4980
|
39 #include "ov-usr-fcn.h" |
4343
|
40 #include "pr-output.h" |
4980
|
41 #include "pt-pr-code.h" |
|
42 #include "pt-misc.h" |
|
43 #include "pt-stmt.h" |
|
44 #include "pt-cmd.h" |
|
45 #include "pt-exp.h" |
|
46 #include "pt-assign.h" |
4343
|
47 #include "variables.h" |
|
48 |
|
49 DEFINE_OCTAVE_ALLOCATOR (octave_fcn_handle); |
|
50 |
4612
|
51 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_fcn_handle, |
|
52 "function handle", |
|
53 "function handle"); |
4343
|
54 |
4924
|
55 octave_value_list |
|
56 octave_fcn_handle::subsref (const std::string& type, |
|
57 const std::list<octave_value_list>& idx, |
|
58 int nargout) |
|
59 { |
|
60 octave_value_list retval; |
|
61 |
|
62 switch (type[0]) |
|
63 { |
|
64 case '(': |
|
65 { |
|
66 octave_function *f = function_value (); |
|
67 retval = f->subsref (type, idx, nargout); |
|
68 } |
|
69 break; |
|
70 |
|
71 |
|
72 case '{': |
|
73 case '.': |
|
74 { |
4930
|
75 std::string typ_nm = type_name (); |
|
76 error ("%s cannot be indexed with %c", typ_nm.c_str (), type[0]); |
4924
|
77 } |
|
78 break; |
|
79 |
|
80 default: |
|
81 panic_impossible (); |
|
82 } |
|
83 |
|
84 // XXX FIXME XXX -- perhaps there should be an |
|
85 // octave_value_list::next_subsref member function? See also |
|
86 // octave_builtin::subsref. |
|
87 |
|
88 if (idx.size () > 1) |
|
89 retval = retval(0).next_subsref (type, idx); |
|
90 |
|
91 return retval; |
|
92 } |
|
93 |
4343
|
94 void |
|
95 octave_fcn_handle::print (std::ostream& os, bool pr_as_read_syntax) const |
|
96 { |
|
97 print_raw (os, pr_as_read_syntax); |
|
98 newline (os); |
|
99 } |
|
100 |
|
101 void |
|
102 octave_fcn_handle::print_raw (std::ostream& os, bool pr_as_read_syntax) const |
|
103 { |
4980
|
104 bool printed = false; |
|
105 |
|
106 if (nm == "@<anonymous>") |
|
107 { |
|
108 tree_print_code tpc (os); |
|
109 |
|
110 // FCN is const becuase this member function is, so we can't |
|
111 // use it to call user_function_value, so we make a copy first. |
|
112 |
|
113 octave_value ftmp = fcn; |
|
114 |
|
115 octave_user_function *f = ftmp.user_function_value (); |
|
116 |
|
117 if (f) |
|
118 { |
|
119 tree_parameter_list *p = f->parameter_list (); |
|
120 |
|
121 os << "@("; |
|
122 |
|
123 if (p) |
|
124 p->accept (tpc); |
|
125 |
|
126 os << ") "; |
|
127 |
|
128 tree_statement_list *b = f->body (); |
|
129 |
|
130 if (b) |
|
131 { |
|
132 assert (b->length () == 1); |
|
133 |
|
134 tree_statement *s = b->front (); |
|
135 |
|
136 if (s) |
|
137 { |
|
138 if (s->is_expression ()) |
|
139 { |
|
140 tree_expression *e = s->expression (); |
|
141 |
|
142 if (e) |
|
143 { |
|
144 if (e->is_assignment_expression ()) |
|
145 { |
|
146 // The parser builds an assignment to |
|
147 // __retval__, and we don't want to |
|
148 // display that part. |
|
149 |
|
150 tree_simple_assignment *tsa |
|
151 = reinterpret_cast <tree_simple_assignment *> (e); |
|
152 tree_expression *rhs = tsa->right_hand_side (); |
|
153 |
|
154 if (rhs) |
|
155 rhs->accept (tpc); |
|
156 } |
|
157 else |
|
158 e->accept (tpc); |
|
159 } |
|
160 } |
|
161 else |
|
162 { |
|
163 tree_command *c = s->command (); |
|
164 |
|
165 tpc.suspend_newline (); |
|
166 c->accept (tpc); |
|
167 tpc.resume_newline (); |
|
168 } |
|
169 } |
|
170 } |
|
171 |
|
172 printed = true; |
|
173 } |
|
174 } |
|
175 |
|
176 if (! printed) |
|
177 octave_print_internal (os, nm, pr_as_read_syntax, |
|
178 current_print_indent_level ()); |
4343
|
179 } |
|
180 |
|
181 octave_value |
|
182 make_fcn_handle (const std::string& nm) |
|
183 { |
|
184 octave_value retval; |
|
185 |
4930
|
186 octave_value f = lookup_function (nm); |
4343
|
187 |
4930
|
188 if (f.is_function ()) |
|
189 retval = octave_value (new octave_fcn_handle (f, nm)); |
4343
|
190 else |
|
191 error ("error creating function handle \"@%s\"", nm.c_str ()); |
|
192 |
|
193 return retval; |
|
194 } |
|
195 |
4933
|
196 DEFUN (functions, args, , |
4343
|
197 "-*- texinfo -*-\n\ |
4933
|
198 @deftypefn {Built-in Function} {} functions (@var{fcn_handle})\n\ |
|
199 Return a struct containing information about the function handle\n\ |
|
200 @var{fcn_handle}.\n\ |
|
201 @end deftypefn") |
4343
|
202 { |
|
203 octave_value retval; |
|
204 |
4933
|
205 if (args.length () == 1) |
4343
|
206 { |
4933
|
207 octave_fcn_handle *fh = args(0).fcn_handle_value (); |
4343
|
208 |
|
209 if (! error_state) |
|
210 { |
4933
|
211 octave_function *fcn = fh ? fh->function_value (true) : 0; |
4343
|
212 |
4933
|
213 if (fcn) |
4930
|
214 { |
4933
|
215 Octave_map m; |
4649
|
216 |
4933
|
217 std::string fh_nm = fh->fcn_name (); |
|
218 |
4978
|
219 m.assign ("function", fh_nm); |
4343
|
220 |
4933
|
221 if (fcn->is_nested_function ()) |
|
222 m.assign ("type", "subfunction"); |
|
223 else |
|
224 m.assign ("type", "simple"); |
|
225 |
|
226 std::string nm = fcn->fcn_file_name (); |
4343
|
227 |
4933
|
228 if (nm.empty ()) |
4935
|
229 { |
|
230 if (fh_nm == "@<anonymous>") |
|
231 m.assign ("file", "none"); |
|
232 else |
|
233 m.assign ("file", "built-in function"); |
|
234 } |
4343
|
235 else |
4933
|
236 m.assign ("file", nm); |
|
237 |
|
238 retval = m; |
4343
|
239 } |
|
240 else |
4933
|
241 error ("functions: invalid function handle object"); |
4343
|
242 } |
|
243 else |
4933
|
244 error ("functions: argument must be a function handle object"); |
4343
|
245 } |
|
246 else |
4933
|
247 print_usage ("functions"); |
4343
|
248 |
|
249 return retval; |
|
250 } |
|
251 |
4933
|
252 DEFUN (func2str, args, , |
4343
|
253 "-*- texinfo -*-\n\ |
4933
|
254 @deftypefn {Built-in Function} {} func2str (@var{fcn_handle})\n\ |
|
255 Return a string containing the name of the function referenced by\n\ |
|
256 the function handle @var{fcn_handle}.\n\ |
|
257 @end deftypefn") |
4343
|
258 { |
|
259 octave_value retval; |
|
260 |
4933
|
261 if (args.length () == 1) |
4930
|
262 { |
4933
|
263 octave_fcn_handle *fh = args(0).fcn_handle_value (); |
4930
|
264 |
4933
|
265 if (! error_state && fh) |
|
266 { |
|
267 std::string fh_nm = fh->fcn_name (); |
4978
|
268 retval = fh_nm; |
4933
|
269 } |
4343
|
270 else |
4933
|
271 error ("func2str: expecting valid function handle as first argument"); |
4343
|
272 } |
|
273 else |
4933
|
274 print_usage ("func2str"); |
4343
|
275 |
|
276 return retval; |
|
277 } |
|
278 |
4933
|
279 DEFUN (str2func, args, , |
4343
|
280 "-*- texinfo -*-\n\ |
4933
|
281 @deftypefn {Built-in Function} {} str2func (@var{fcn_name})\n\ |
|
282 Return a function handle constructed from the string @var{fcn_name}.\n\ |
|
283 @end deftypefn") |
4343
|
284 { |
|
285 octave_value retval; |
|
286 |
4933
|
287 if (args.length () == 1) |
4343
|
288 { |
4933
|
289 std::string nm = args(0).string_value (); |
4343
|
290 |
4933
|
291 if (! error_state) |
|
292 retval = make_fcn_handle (nm); |
4343
|
293 else |
4933
|
294 error ("str2func: expecting string as first argument"); |
4343
|
295 } |
|
296 else |
4933
|
297 print_usage ("str2func"); |
4343
|
298 |
|
299 return retval; |
|
300 } |
|
301 |
|
302 /* |
|
303 ;;; Local Variables: *** |
|
304 ;;; mode: C++ *** |
|
305 ;;; End: *** |
|
306 */ |