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" |
|
39 #include "pr-output.h" |
|
40 #include "variables.h" |
4930
|
41 #include "parse.h" |
4343
|
42 |
|
43 DEFINE_OCTAVE_ALLOCATOR (octave_fcn_handle); |
|
44 |
4612
|
45 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_fcn_handle, |
|
46 "function handle", |
|
47 "function handle"); |
4343
|
48 |
4930
|
49 octave_fcn_handle::octave_fcn_handle (const std::string& f, |
|
50 const string_vector& a, |
|
51 const std::string& n) |
|
52 : typ (fcn_inline), nm (n), iftext (f), ifargs (a) |
|
53 { |
|
54 // Find a function name that isn't already in the symbol table. |
|
55 |
|
56 std::string fname = "__inline__"; |
|
57 |
|
58 while (symbol_exist (fname)) |
|
59 fname.append ("X"); |
|
60 |
|
61 // Form a string representing the function. |
|
62 |
|
63 OSSTREAM buf; |
|
64 |
|
65 buf << "function __retval__ = " << fname << "("; |
|
66 |
|
67 for (int i = 0; i < ifargs.length (); i++) |
|
68 { |
|
69 if (i > 0) |
|
70 buf << ", "; |
|
71 |
|
72 buf << ifargs(i); |
|
73 } |
|
74 |
|
75 buf << ")\n __retval__ = " << iftext << ";\nendfunction" << OSSTREAM_ENDS; |
|
76 |
|
77 // Parse this function and create a user function. |
|
78 |
|
79 octave_value eval_args (OSSTREAM_STR (buf)); |
|
80 |
|
81 feval ("eval", eval_args, 0); |
|
82 |
|
83 OSSTREAM_FREEZE (buf); |
|
84 |
|
85 octave_value tmp = lookup_function (fname); |
|
86 |
|
87 if (tmp.is_function ()) |
|
88 { |
|
89 fcn = tmp; |
|
90 |
|
91 // XXX FIXME XXX -- probably shouldn't be directly altering the |
|
92 // symbol table here. |
|
93 |
|
94 fbi_sym_tab->clear_function (fname); |
|
95 } |
|
96 else |
|
97 error ("inline: unable to define function"); |
|
98 } |
|
99 |
4924
|
100 octave_value_list |
|
101 octave_fcn_handle::subsref (const std::string& type, |
|
102 const std::list<octave_value_list>& idx, |
|
103 int nargout) |
|
104 { |
|
105 octave_value_list retval; |
|
106 |
|
107 switch (type[0]) |
|
108 { |
|
109 case '(': |
|
110 { |
|
111 octave_function *f = function_value (); |
|
112 retval = f->subsref (type, idx, nargout); |
|
113 } |
|
114 break; |
|
115 |
|
116 |
|
117 case '{': |
|
118 case '.': |
|
119 { |
4930
|
120 std::string typ_nm = type_name (); |
|
121 error ("%s cannot be indexed with %c", typ_nm.c_str (), type[0]); |
4924
|
122 } |
|
123 break; |
|
124 |
|
125 default: |
|
126 panic_impossible (); |
|
127 } |
|
128 |
|
129 // XXX FIXME XXX -- perhaps there should be an |
|
130 // octave_value_list::next_subsref member function? See also |
|
131 // octave_builtin::subsref. |
|
132 |
|
133 if (idx.size () > 1) |
|
134 retval = retval(0).next_subsref (type, idx); |
|
135 |
|
136 return retval; |
|
137 } |
|
138 |
4343
|
139 void |
|
140 octave_fcn_handle::print (std::ostream& os, bool pr_as_read_syntax) const |
|
141 { |
|
142 print_raw (os, pr_as_read_syntax); |
|
143 newline (os); |
|
144 } |
|
145 |
|
146 void |
|
147 octave_fcn_handle::print_raw (std::ostream& os, bool pr_as_read_syntax) const |
|
148 { |
4930
|
149 if (is_inline ()) |
|
150 { |
|
151 OSSTREAM buf; |
|
152 |
|
153 if (nm.empty ()) |
|
154 buf << "@f("; |
|
155 else |
|
156 buf << nm << "("; |
|
157 |
|
158 for (int i = 0; i < ifargs.length (); i++) |
|
159 { |
|
160 if (i) |
|
161 buf << ", "; |
|
162 |
|
163 buf << ifargs(i); |
|
164 } |
|
165 |
|
166 buf << ") = " << iftext << OSSTREAM_ENDS; |
|
167 |
|
168 octave_print_internal (os, OSSTREAM_STR (buf), pr_as_read_syntax, |
|
169 current_print_indent_level ()); |
|
170 OSSTREAM_FREEZE (buf); |
|
171 } |
|
172 else |
|
173 octave_print_internal (os, nm, pr_as_read_syntax, |
|
174 current_print_indent_level ()); |
|
175 } |
|
176 |
|
177 octave_value |
|
178 octave_fcn_handle::convert_to_str_internal (bool, bool) const |
|
179 { |
|
180 octave_value retval; |
|
181 |
|
182 if (is_inline ()) |
|
183 retval = octave_value (inline_fcn_text ()); |
|
184 else |
|
185 error ("convert_to_str_internal: must be an inline function"); |
|
186 |
|
187 return retval; |
4343
|
188 } |
|
189 |
|
190 octave_value |
|
191 make_fcn_handle (const std::string& nm) |
|
192 { |
|
193 octave_value retval; |
|
194 |
4930
|
195 octave_value f = lookup_function (nm); |
4343
|
196 |
4930
|
197 if (f.is_function ()) |
|
198 retval = octave_value (new octave_fcn_handle (f, nm)); |
4343
|
199 else |
|
200 error ("error creating function handle \"@%s\"", nm.c_str ()); |
|
201 |
|
202 return retval; |
|
203 } |
|
204 |
4930
|
205 DEFUN (inline, args, , |
4343
|
206 "-*- texinfo -*-\n\ |
4930
|
207 @deftypefn {Built-in Function} {} inline (@var{str})\n\ |
|
208 @deftypefnx {Built-in Function} {} inline (@var{str}, @var{arg1}, ...)\n\ |
|
209 @deftypefnx {Built-in Function} {} inline (@var{str}, @var{n})\n\ |
|
210 Define a function from a string @var{str}.\n\ |
|
211 \n\ |
|
212 Create an inline function. Called with a single argument, the\n\ |
|
213 function is assumed to have a single argument and will be defined\n\ |
|
214 as the first isolated lower case character, except i or j.\n\ |
|
215 \n\ |
|
216 If the second and subsequent arguments are strings, they are the names of\n\ |
|
217 the arguments of the function.\n\ |
|
218 \n\ |
|
219 If the second argument is an integer @var{n}, the arguments are\n\ |
|
220 @code{\"x\"}, @code{\"P1\"}, @dots{}, @code{\"P@var{N}\"}.\n\ |
|
221 @end deftypefn\n\ |
|
222 @seealso{argnames, formula, vectorize}") |
4343
|
223 { |
|
224 octave_value retval; |
|
225 |
4930
|
226 int nargin = args.length (); |
|
227 |
|
228 if (nargin > 0) |
4343
|
229 { |
4930
|
230 std::string fun = args(0).string_value (); |
4343
|
231 |
|
232 if (! error_state) |
|
233 { |
4930
|
234 string_vector fargs; |
4343
|
235 |
4930
|
236 if (nargin == 1) |
4343
|
237 { |
4930
|
238 fargs.resize (1); |
|
239 |
|
240 // Find the first isolated string as the argument of the |
|
241 // function. |
4343
|
242 |
4930
|
243 // XXX FIXME XXX -- use just "x" for now. |
|
244 fargs(0) = "x"; |
|
245 } |
|
246 else if (nargin == 2 && args(1).is_numeric_type ()) |
|
247 { |
|
248 int n = args(1).int_value (); |
4649
|
249 |
4930
|
250 if (! error_state) |
|
251 { |
|
252 if (n >= 0) |
|
253 { |
|
254 fargs.resize (n+1); |
4343
|
255 |
4930
|
256 fargs(0) = "x"; |
4343
|
257 |
4930
|
258 for (int i = 1; i < n+1; i++) |
|
259 { |
|
260 OSSTREAM buf; |
|
261 buf << "P" << i << OSSTREAM_ENDS; |
|
262 fargs(i) = OSSTREAM_STR (buf); |
|
263 OSSTREAM_FREEZE (buf); |
|
264 } |
|
265 } |
|
266 else |
|
267 { |
|
268 error ("inline: numeric argument must be nonnegative"); |
|
269 return retval; |
|
270 } |
|
271 } |
4343
|
272 else |
4930
|
273 { |
|
274 error ("inline: expecting second argument to be an integer"); |
|
275 return retval; |
|
276 } |
4343
|
277 } |
|
278 else |
4930
|
279 { |
|
280 fargs.resize (nargin - 1); |
|
281 |
|
282 for (int i = 1; i < nargin; i++) |
|
283 { |
|
284 std::string s = args(i).string_value (); |
|
285 |
|
286 if (! error_state) |
|
287 fargs(i-1) = s; |
|
288 else |
|
289 { |
|
290 error ("inline: expecting string arguments"); |
|
291 return retval; |
|
292 } |
|
293 } |
|
294 } |
|
295 |
|
296 retval = octave_value (new octave_fcn_handle (fun, fargs)); |
4343
|
297 } |
|
298 else |
4930
|
299 error ("inline: first argument must be a string"); |
4343
|
300 } |
|
301 else |
4930
|
302 print_usage ("inline"); |
4343
|
303 |
|
304 return retval; |
|
305 } |
|
306 |
4930
|
307 DEFUN (formula, args, , |
4343
|
308 "-*- texinfo -*-\n\ |
4930
|
309 @deftypefn {Built-in Function} {} formula (@var{fun})\n\ |
|
310 Return a string representing the inline function @var{fun}.\n\ |
|
311 @end deftypefn\n\ |
|
312 @seealso{argnames, inline, vectorize}") |
4343
|
313 { |
|
314 octave_value retval; |
|
315 |
4930
|
316 int nargin = args.length (); |
4343
|
317 |
4930
|
318 if (nargin == 1) |
|
319 { |
|
320 octave_fcn_handle* fn = args(0).fcn_handle_value (true); |
|
321 |
|
322 if (fn && fn->is_inline ()) |
|
323 retval = octave_value (fn->inline_fcn_text ()); |
4343
|
324 else |
4930
|
325 error ("formula: must be an inline function"); |
4343
|
326 } |
|
327 else |
4930
|
328 print_usage ("formula"); |
4343
|
329 |
|
330 return retval; |
|
331 } |
|
332 |
4930
|
333 DEFUN (argnames, args, , |
4343
|
334 "-*- texinfo -*-\n\ |
4930
|
335 @deftypefn {Built-in Function} {} argnames (@var{fun})\n\ |
|
336 Return a cell array of strings containing the names of the arguments\n\ |
|
337 of the inline function @var{fun}.\n\ |
|
338 @end deftypefn\n\ |
|
339 @seealso{argnames, inline, formula, vectorize}") |
4343
|
340 { |
|
341 octave_value retval; |
|
342 |
4930
|
343 int nargin = args.length (); |
|
344 |
|
345 if (nargin == 1) |
4343
|
346 { |
4930
|
347 octave_fcn_handle *fn = args(0).fcn_handle_value (true); |
|
348 |
|
349 if (fn && fn->is_inline ()) |
|
350 { |
|
351 string_vector t1 = fn->inline_fcn_arg_names (); |
4343
|
352 |
4930
|
353 Cell t2 (dim_vector (t1.length (), 1)); |
|
354 |
|
355 for (int i = 0; i < t1.length (); i++) |
|
356 t2(i) = t1(i); |
|
357 |
|
358 retval = t2; |
|
359 } |
4343
|
360 else |
4930
|
361 error ("argnames: argument must be an inline function"); |
4343
|
362 } |
|
363 else |
4930
|
364 print_usage ("argnames"); |
|
365 |
|
366 return retval; |
|
367 } |
|
368 |
|
369 DEFUN (vectorize, args, , |
|
370 "-*- texinfo -*-\n\ |
|
371 @deftypefn {Built-in Function} {} argnames (@var{fun})\n\ |
|
372 Create a vectorized version of the inline function @var{fun}\n\ |
|
373 by replacing all occurrences of @code{*}, @code{/}, etc., with\n\ |
|
374 @code{.*}, @code{./}, etc.\n\ |
|
375 @end deftypefn\n\ |
|
376 @seealso{argnames, inline, formula, vectorize}") |
|
377 { |
|
378 octave_value retval; |
|
379 |
|
380 int nargin = args.length (); |
|
381 |
|
382 if (nargin == 1) |
|
383 { |
|
384 octave_fcn_handle* old = args(0).fcn_handle_value (true); |
|
385 |
|
386 if (old && old->is_inline ()) |
|
387 { |
|
388 std::string old_func = old->inline_fcn_text (); |
|
389 std::string new_func; |
|
390 |
|
391 size_t i = 0; |
|
392 |
|
393 while (i < old_func.length ()) |
|
394 { |
|
395 std::string t1 = old_func.substr (i, 1); |
|
396 |
|
397 if (t1 == "*" || t1 == "/" || t1 == "\\" || t1 == "^") |
|
398 { |
|
399 if (i && old_func.substr (i-1, 1) != ".") |
|
400 new_func.append ("."); |
|
401 |
|
402 // Special case for ** operator. |
|
403 if (t1 == "*" && i < (old_func.length () - 1) |
|
404 && old_func.substr (i+1, 1) == "*") |
|
405 { |
|
406 new_func.append ("*"); |
|
407 i++; |
|
408 } |
|
409 } |
|
410 new_func.append (t1); |
|
411 i++; |
|
412 } |
|
413 |
|
414 retval = octave_value (new octave_fcn_handle (new_func, old->inline_fcn_arg_names ())); |
|
415 } |
|
416 else |
|
417 error ("vectorize: must be an inline function"); |
|
418 } |
|
419 else |
|
420 print_usage ("vectorize"); |
4343
|
421 |
|
422 return retval; |
|
423 } |
|
424 |
|
425 /* |
|
426 ;;; Local Variables: *** |
|
427 ;;; mode: C++ *** |
|
428 ;;; End: *** |
|
429 */ |