4933
|
1 /* |
|
2 |
|
3 Copyright (C) 2004 David Bateman |
|
4 |
|
5 This program is free software; you can redistribute it and/or modify it |
|
6 under the terms of the GNU General Public License as published by |
|
7 the Free Software Foundation; either version 2, or (at your option) |
|
8 any later version. |
|
9 |
|
10 This program is distributed in the hope that it will be useful, but |
|
11 WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 General Public License for more details. |
|
14 |
|
15 You should have received a copy of the GNU General Public License |
|
16 along with Octave; see the file COPYING. If not, write to the Free |
|
17 Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 02111-1307, USA. |
|
19 |
|
20 In addition to the terms of the GPL, you are permitted to link |
|
21 this program with any Open Source program, as defined by the |
|
22 Open Source Initiative (www.opensource.org) |
|
23 |
|
24 */ |
|
25 |
|
26 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
|
27 #pragma implementation |
|
28 #endif |
|
29 |
|
30 #ifdef HAVE_CONFIG_H |
|
31 #include <config.h> |
|
32 #endif |
|
33 |
|
34 #include <iostream> |
|
35 |
|
36 #include "defun.h" |
|
37 #include "error.h" |
|
38 #include "gripes.h" |
|
39 #include "oct-map.h" |
|
40 #include "ov-base.h" |
|
41 #include "ov-fcn-inline.h" |
|
42 #include "pr-output.h" |
|
43 #include "variables.h" |
|
44 #include "parse.h" |
|
45 |
|
46 DEFINE_OCTAVE_ALLOCATOR (octave_fcn_inline); |
|
47 |
|
48 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_fcn_inline, |
|
49 "inline function", |
|
50 "inline function"); |
|
51 |
|
52 octave_fcn_inline::octave_fcn_inline (const std::string& f, |
|
53 const string_vector& a, |
|
54 const std::string& n) |
|
55 : octave_fcn_handle (0, n), iftext (f), ifargs (a) |
|
56 { |
|
57 // Find a function name that isn't already in the symbol table. |
4954
|
58 std::string fname = unique_symbol_name ("__inline__"); |
4933
|
59 |
|
60 // Form a string representing the function. |
|
61 |
|
62 OSSTREAM buf; |
|
63 |
|
64 buf << "function __retval__ = " << fname << "("; |
|
65 |
|
66 for (int i = 0; i < ifargs.length (); i++) |
|
67 { |
|
68 if (i > 0) |
|
69 buf << ", "; |
|
70 |
|
71 buf << ifargs(i); |
|
72 } |
|
73 |
|
74 buf << ")\n __retval__ = " << iftext << ";\nendfunction" << OSSTREAM_ENDS; |
|
75 |
|
76 // Parse this function and create a user function. |
|
77 |
|
78 octave_value eval_args (OSSTREAM_STR (buf)); |
|
79 |
|
80 feval ("eval", eval_args, 0); |
|
81 |
|
82 OSSTREAM_FREEZE (buf); |
|
83 |
|
84 octave_value tmp = lookup_function (fname); |
|
85 |
|
86 if (tmp.is_function ()) |
|
87 { |
|
88 fcn = tmp; |
|
89 |
4954
|
90 clear_function (fname); |
4933
|
91 } |
|
92 else |
|
93 error ("inline: unable to define function"); |
|
94 } |
|
95 |
|
96 void |
|
97 octave_fcn_inline::print (std::ostream& os, bool pr_as_read_syntax) const |
|
98 { |
|
99 print_raw (os, pr_as_read_syntax); |
|
100 newline (os); |
|
101 } |
|
102 |
|
103 void |
|
104 octave_fcn_inline::print_raw (std::ostream& os, bool pr_as_read_syntax) const |
|
105 { |
|
106 OSSTREAM buf; |
|
107 |
|
108 if (nm.empty ()) |
|
109 buf << "f("; |
|
110 else |
|
111 buf << nm << "("; |
|
112 |
|
113 for (int i = 0; i < ifargs.length (); i++) |
|
114 { |
|
115 if (i) |
|
116 buf << ", "; |
|
117 |
|
118 buf << ifargs(i); |
|
119 } |
|
120 |
|
121 buf << ") = " << iftext << OSSTREAM_ENDS; |
|
122 |
|
123 octave_print_internal (os, OSSTREAM_STR (buf), pr_as_read_syntax, |
|
124 current_print_indent_level ()); |
|
125 OSSTREAM_FREEZE (buf); |
|
126 } |
|
127 |
|
128 octave_value |
|
129 octave_fcn_inline::convert_to_str_internal (bool, bool) const |
|
130 { |
|
131 return octave_value (fcn_text ()); |
|
132 } |
|
133 |
|
134 DEFUN (inline, args, , |
|
135 "-*- texinfo -*-\n\ |
|
136 @deftypefn {Built-in Function} {} inline (@var{str})\n\ |
|
137 @deftypefnx {Built-in Function} {} inline (@var{str}, @var{arg1}, ...)\n\ |
|
138 @deftypefnx {Built-in Function} {} inline (@var{str}, @var{n})\n\ |
|
139 Create an inline function from the character string @var{str}.\n\ |
|
140 If called with a single argument, the generated function is\n\ |
|
141 assumed to have a single argument and will be defined\n\ |
|
142 as the first isolated lower case character, except i or j.\n\ |
|
143 \n\ |
|
144 If the second and subsequent arguments are character strings,\n\ |
|
145 they are the names of the arguments of the function.\n\ |
|
146 \n\ |
|
147 If the second argument is an integer @var{n}, the arguments are\n\ |
|
148 @code{\"x\"}, @code{\"P1\"}, @dots{}, @code{\"P@var{N}\"}.\n\ |
|
149 @end deftypefn\n\ |
|
150 @seealso{argnames, formula, vectorize}") |
|
151 { |
|
152 octave_value retval; |
|
153 |
|
154 int nargin = args.length (); |
|
155 |
|
156 if (nargin > 0) |
|
157 { |
|
158 std::string fun = args(0).string_value (); |
|
159 |
|
160 if (! error_state) |
|
161 { |
|
162 string_vector fargs; |
|
163 |
|
164 if (nargin == 1) |
|
165 { |
|
166 fargs.resize (1); |
|
167 |
|
168 // Find the first isolated string as the argument of the |
|
169 // function. |
|
170 |
|
171 // XXX FIXME XXX -- use just "x" for now. |
|
172 fargs(0) = "x"; |
|
173 } |
|
174 else if (nargin == 2 && args(1).is_numeric_type ()) |
|
175 { |
|
176 int n = args(1).int_value (); |
|
177 |
|
178 if (! error_state) |
|
179 { |
|
180 if (n >= 0) |
|
181 { |
|
182 fargs.resize (n+1); |
|
183 |
|
184 fargs(0) = "x"; |
|
185 |
|
186 for (int i = 1; i < n+1; i++) |
|
187 { |
|
188 OSSTREAM buf; |
|
189 buf << "P" << i << OSSTREAM_ENDS; |
|
190 fargs(i) = OSSTREAM_STR (buf); |
|
191 OSSTREAM_FREEZE (buf); |
|
192 } |
|
193 } |
|
194 else |
|
195 { |
|
196 error ("inline: numeric argument must be nonnegative"); |
|
197 return retval; |
|
198 } |
|
199 } |
|
200 else |
|
201 { |
|
202 error ("inline: expecting second argument to be an integer"); |
|
203 return retval; |
|
204 } |
|
205 } |
|
206 else |
|
207 { |
|
208 fargs.resize (nargin - 1); |
|
209 |
|
210 for (int i = 1; i < nargin; i++) |
|
211 { |
|
212 std::string s = args(i).string_value (); |
|
213 |
|
214 if (! error_state) |
|
215 fargs(i-1) = s; |
|
216 else |
|
217 { |
|
218 error ("inline: expecting string arguments"); |
|
219 return retval; |
|
220 } |
|
221 } |
|
222 } |
|
223 |
|
224 retval = octave_value (new octave_fcn_inline (fun, fargs)); |
|
225 } |
|
226 else |
|
227 error ("inline: first argument must be a string"); |
|
228 } |
|
229 else |
|
230 print_usage ("inline"); |
|
231 |
|
232 return retval; |
|
233 } |
|
234 |
|
235 DEFUN (formula, args, , |
|
236 "-*- texinfo -*-\n\ |
|
237 @deftypefn {Built-in Function} {} formula (@var{fun})\n\ |
|
238 Return a character string representing the inline function @var{fun}.\n\ |
|
239 Note that @code{char (@var{fun})} is equivalent to\n\ |
|
240 @code{formula (@var{fun})}.\n\ |
|
241 @end deftypefn\n\ |
|
242 @seealso{argnames, inline, vectorize}") |
|
243 { |
|
244 octave_value retval; |
|
245 |
|
246 int nargin = args.length (); |
|
247 |
|
248 if (nargin == 1) |
|
249 { |
|
250 octave_fcn_inline* fn = args(0).fcn_inline_value (true); |
|
251 |
|
252 if (fn) |
|
253 retval = octave_value (fn->fcn_text ()); |
|
254 else |
|
255 error ("formula: must be an inline function"); |
|
256 } |
|
257 else |
|
258 print_usage ("formula"); |
|
259 |
|
260 return retval; |
|
261 } |
|
262 |
|
263 DEFUN (argnames, args, , |
|
264 "-*- texinfo -*-\n\ |
|
265 @deftypefn {Built-in Function} {} argnames (@var{fun})\n\ |
|
266 Return a cell array of character strings containing the names of\n\ |
|
267 the arguments of the inline function @var{fun}.\n\ |
|
268 @end deftypefn\n\ |
|
269 @seealso{argnames, inline, formula, vectorize}") |
|
270 { |
|
271 octave_value retval; |
|
272 |
|
273 int nargin = args.length (); |
|
274 |
|
275 if (nargin == 1) |
|
276 { |
|
277 octave_fcn_inline *fn = args(0).fcn_inline_value (true); |
|
278 |
|
279 if (fn) |
|
280 { |
|
281 string_vector t1 = fn->fcn_arg_names (); |
|
282 |
|
283 Cell t2 (dim_vector (t1.length (), 1)); |
|
284 |
|
285 for (int i = 0; i < t1.length (); i++) |
|
286 t2(i) = t1(i); |
|
287 |
|
288 retval = t2; |
|
289 } |
|
290 else |
|
291 error ("argnames: argument must be an inline function"); |
|
292 } |
|
293 else |
|
294 print_usage ("argnames"); |
|
295 |
|
296 return retval; |
|
297 } |
|
298 |
|
299 DEFUN (vectorize, args, , |
|
300 "-*- texinfo -*-\n\ |
|
301 @deftypefn {Built-in Function} {} argnames (@var{fun})\n\ |
|
302 Create a vectorized version of the inline function @var{fun}\n\ |
|
303 by replacing all occurrences of @code{*}, @code{/}, etc., with\n\ |
|
304 @code{.*}, @code{./}, etc.\n\ |
|
305 @end deftypefn\n\ |
|
306 @seealso{argnames, inline, formula, vectorize}") |
|
307 { |
|
308 octave_value retval; |
|
309 |
|
310 int nargin = args.length (); |
|
311 |
|
312 if (nargin == 1) |
|
313 { |
|
314 octave_fcn_inline* old = args(0).fcn_inline_value (true); |
|
315 |
|
316 if (old) |
|
317 { |
|
318 std::string old_func = old->fcn_text (); |
|
319 std::string new_func; |
|
320 |
|
321 size_t i = 0; |
|
322 |
|
323 while (i < old_func.length ()) |
|
324 { |
|
325 std::string t1 = old_func.substr (i, 1); |
|
326 |
|
327 if (t1 == "*" || t1 == "/" || t1 == "\\" || t1 == "^") |
|
328 { |
|
329 if (i && old_func.substr (i-1, 1) != ".") |
|
330 new_func.append ("."); |
|
331 |
|
332 // Special case for ** operator. |
|
333 if (t1 == "*" && i < (old_func.length () - 1) |
|
334 && old_func.substr (i+1, 1) == "*") |
|
335 { |
|
336 new_func.append ("*"); |
|
337 i++; |
|
338 } |
|
339 } |
|
340 new_func.append (t1); |
|
341 i++; |
|
342 } |
|
343 |
|
344 retval = octave_value (new octave_fcn_inline (new_func, old->fcn_arg_names ())); |
|
345 } |
|
346 else |
|
347 error ("vectorize: must be an inline function"); |
|
348 } |
|
349 else |
|
350 print_usage ("vectorize"); |
|
351 |
|
352 return retval; |
|
353 } |
|
354 |
|
355 /* |
|
356 ;;; Local Variables: *** |
|
357 ;;; mode: C++ *** |
|
358 ;;; End: *** |
|
359 */ |
|
360 |
|
361 |