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. |
|
58 |
|
59 std::string fname = "__inline__"; |
|
60 |
|
61 while (symbol_exist (fname)) |
|
62 fname.append ("X"); |
|
63 |
|
64 // Form a string representing the function. |
|
65 |
|
66 OSSTREAM buf; |
|
67 |
|
68 buf << "function __retval__ = " << fname << "("; |
|
69 |
|
70 for (int i = 0; i < ifargs.length (); i++) |
|
71 { |
|
72 if (i > 0) |
|
73 buf << ", "; |
|
74 |
|
75 buf << ifargs(i); |
|
76 } |
|
77 |
|
78 buf << ")\n __retval__ = " << iftext << ";\nendfunction" << OSSTREAM_ENDS; |
|
79 |
|
80 // Parse this function and create a user function. |
|
81 |
|
82 octave_value eval_args (OSSTREAM_STR (buf)); |
|
83 |
|
84 feval ("eval", eval_args, 0); |
|
85 |
|
86 OSSTREAM_FREEZE (buf); |
|
87 |
|
88 octave_value tmp = lookup_function (fname); |
|
89 |
|
90 if (tmp.is_function ()) |
|
91 { |
|
92 fcn = tmp; |
|
93 |
|
94 // XXX FIXME XXX -- probably shouldn't be directly altering the |
|
95 // symbol table here. |
|
96 |
|
97 fbi_sym_tab->clear_function (fname); |
|
98 } |
|
99 else |
|
100 error ("inline: unable to define function"); |
|
101 } |
|
102 |
|
103 void |
|
104 octave_fcn_inline::print (std::ostream& os, bool pr_as_read_syntax) const |
|
105 { |
|
106 print_raw (os, pr_as_read_syntax); |
|
107 newline (os); |
|
108 } |
|
109 |
|
110 void |
|
111 octave_fcn_inline::print_raw (std::ostream& os, bool pr_as_read_syntax) const |
|
112 { |
|
113 OSSTREAM buf; |
|
114 |
|
115 if (nm.empty ()) |
|
116 buf << "f("; |
|
117 else |
|
118 buf << nm << "("; |
|
119 |
|
120 for (int i = 0; i < ifargs.length (); i++) |
|
121 { |
|
122 if (i) |
|
123 buf << ", "; |
|
124 |
|
125 buf << ifargs(i); |
|
126 } |
|
127 |
|
128 buf << ") = " << iftext << OSSTREAM_ENDS; |
|
129 |
|
130 octave_print_internal (os, OSSTREAM_STR (buf), pr_as_read_syntax, |
|
131 current_print_indent_level ()); |
|
132 OSSTREAM_FREEZE (buf); |
|
133 } |
|
134 |
|
135 octave_value |
|
136 octave_fcn_inline::convert_to_str_internal (bool, bool) const |
|
137 { |
|
138 return octave_value (fcn_text ()); |
|
139 } |
|
140 |
|
141 DEFUN (inline, args, , |
|
142 "-*- texinfo -*-\n\ |
|
143 @deftypefn {Built-in Function} {} inline (@var{str})\n\ |
|
144 @deftypefnx {Built-in Function} {} inline (@var{str}, @var{arg1}, ...)\n\ |
|
145 @deftypefnx {Built-in Function} {} inline (@var{str}, @var{n})\n\ |
|
146 Create an inline function from the character string @var{str}.\n\ |
|
147 If called with a single argument, the generated function is\n\ |
|
148 assumed to have a single argument and will be defined\n\ |
|
149 as the first isolated lower case character, except i or j.\n\ |
|
150 \n\ |
|
151 If the second and subsequent arguments are character strings,\n\ |
|
152 they are the names of the arguments of the function.\n\ |
|
153 \n\ |
|
154 If the second argument is an integer @var{n}, the arguments are\n\ |
|
155 @code{\"x\"}, @code{\"P1\"}, @dots{}, @code{\"P@var{N}\"}.\n\ |
|
156 @end deftypefn\n\ |
|
157 @seealso{argnames, formula, vectorize}") |
|
158 { |
|
159 octave_value retval; |
|
160 |
|
161 int nargin = args.length (); |
|
162 |
|
163 if (nargin > 0) |
|
164 { |
|
165 std::string fun = args(0).string_value (); |
|
166 |
|
167 if (! error_state) |
|
168 { |
|
169 string_vector fargs; |
|
170 |
|
171 if (nargin == 1) |
|
172 { |
|
173 fargs.resize (1); |
|
174 |
|
175 // Find the first isolated string as the argument of the |
|
176 // function. |
|
177 |
|
178 // XXX FIXME XXX -- use just "x" for now. |
|
179 fargs(0) = "x"; |
|
180 } |
|
181 else if (nargin == 2 && args(1).is_numeric_type ()) |
|
182 { |
|
183 int n = args(1).int_value (); |
|
184 |
|
185 if (! error_state) |
|
186 { |
|
187 if (n >= 0) |
|
188 { |
|
189 fargs.resize (n+1); |
|
190 |
|
191 fargs(0) = "x"; |
|
192 |
|
193 for (int i = 1; i < n+1; i++) |
|
194 { |
|
195 OSSTREAM buf; |
|
196 buf << "P" << i << OSSTREAM_ENDS; |
|
197 fargs(i) = OSSTREAM_STR (buf); |
|
198 OSSTREAM_FREEZE (buf); |
|
199 } |
|
200 } |
|
201 else |
|
202 { |
|
203 error ("inline: numeric argument must be nonnegative"); |
|
204 return retval; |
|
205 } |
|
206 } |
|
207 else |
|
208 { |
|
209 error ("inline: expecting second argument to be an integer"); |
|
210 return retval; |
|
211 } |
|
212 } |
|
213 else |
|
214 { |
|
215 fargs.resize (nargin - 1); |
|
216 |
|
217 for (int i = 1; i < nargin; i++) |
|
218 { |
|
219 std::string s = args(i).string_value (); |
|
220 |
|
221 if (! error_state) |
|
222 fargs(i-1) = s; |
|
223 else |
|
224 { |
|
225 error ("inline: expecting string arguments"); |
|
226 return retval; |
|
227 } |
|
228 } |
|
229 } |
|
230 |
|
231 retval = octave_value (new octave_fcn_inline (fun, fargs)); |
|
232 } |
|
233 else |
|
234 error ("inline: first argument must be a string"); |
|
235 } |
|
236 else |
|
237 print_usage ("inline"); |
|
238 |
|
239 return retval; |
|
240 } |
|
241 |
|
242 DEFUN (formula, args, , |
|
243 "-*- texinfo -*-\n\ |
|
244 @deftypefn {Built-in Function} {} formula (@var{fun})\n\ |
|
245 Return a character string representing the inline function @var{fun}.\n\ |
|
246 Note that @code{char (@var{fun})} is equivalent to\n\ |
|
247 @code{formula (@var{fun})}.\n\ |
|
248 @end deftypefn\n\ |
|
249 @seealso{argnames, inline, vectorize}") |
|
250 { |
|
251 octave_value retval; |
|
252 |
|
253 int nargin = args.length (); |
|
254 |
|
255 if (nargin == 1) |
|
256 { |
|
257 octave_fcn_inline* fn = args(0).fcn_inline_value (true); |
|
258 |
|
259 if (fn) |
|
260 retval = octave_value (fn->fcn_text ()); |
|
261 else |
|
262 error ("formula: must be an inline function"); |
|
263 } |
|
264 else |
|
265 print_usage ("formula"); |
|
266 |
|
267 return retval; |
|
268 } |
|
269 |
|
270 DEFUN (argnames, args, , |
|
271 "-*- texinfo -*-\n\ |
|
272 @deftypefn {Built-in Function} {} argnames (@var{fun})\n\ |
|
273 Return a cell array of character strings containing the names of\n\ |
|
274 the arguments of the inline function @var{fun}.\n\ |
|
275 @end deftypefn\n\ |
|
276 @seealso{argnames, inline, formula, vectorize}") |
|
277 { |
|
278 octave_value retval; |
|
279 |
|
280 int nargin = args.length (); |
|
281 |
|
282 if (nargin == 1) |
|
283 { |
|
284 octave_fcn_inline *fn = args(0).fcn_inline_value (true); |
|
285 |
|
286 if (fn) |
|
287 { |
|
288 string_vector t1 = fn->fcn_arg_names (); |
|
289 |
|
290 Cell t2 (dim_vector (t1.length (), 1)); |
|
291 |
|
292 for (int i = 0; i < t1.length (); i++) |
|
293 t2(i) = t1(i); |
|
294 |
|
295 retval = t2; |
|
296 } |
|
297 else |
|
298 error ("argnames: argument must be an inline function"); |
|
299 } |
|
300 else |
|
301 print_usage ("argnames"); |
|
302 |
|
303 return retval; |
|
304 } |
|
305 |
|
306 DEFUN (vectorize, args, , |
|
307 "-*- texinfo -*-\n\ |
|
308 @deftypefn {Built-in Function} {} argnames (@var{fun})\n\ |
|
309 Create a vectorized version of the inline function @var{fun}\n\ |
|
310 by replacing all occurrences of @code{*}, @code{/}, etc., with\n\ |
|
311 @code{.*}, @code{./}, etc.\n\ |
|
312 @end deftypefn\n\ |
|
313 @seealso{argnames, inline, formula, vectorize}") |
|
314 { |
|
315 octave_value retval; |
|
316 |
|
317 int nargin = args.length (); |
|
318 |
|
319 if (nargin == 1) |
|
320 { |
|
321 octave_fcn_inline* old = args(0).fcn_inline_value (true); |
|
322 |
|
323 if (old) |
|
324 { |
|
325 std::string old_func = old->fcn_text (); |
|
326 std::string new_func; |
|
327 |
|
328 size_t i = 0; |
|
329 |
|
330 while (i < old_func.length ()) |
|
331 { |
|
332 std::string t1 = old_func.substr (i, 1); |
|
333 |
|
334 if (t1 == "*" || t1 == "/" || t1 == "\\" || t1 == "^") |
|
335 { |
|
336 if (i && old_func.substr (i-1, 1) != ".") |
|
337 new_func.append ("."); |
|
338 |
|
339 // Special case for ** operator. |
|
340 if (t1 == "*" && i < (old_func.length () - 1) |
|
341 && old_func.substr (i+1, 1) == "*") |
|
342 { |
|
343 new_func.append ("*"); |
|
344 i++; |
|
345 } |
|
346 } |
|
347 new_func.append (t1); |
|
348 i++; |
|
349 } |
|
350 |
|
351 retval = octave_value (new octave_fcn_inline (new_func, old->fcn_arg_names ())); |
|
352 } |
|
353 else |
|
354 error ("vectorize: must be an inline function"); |
|
355 } |
|
356 else |
|
357 print_usage ("vectorize"); |
|
358 |
|
359 return retval; |
|
360 } |
|
361 |
|
362 /* |
|
363 ;;; Local Variables: *** |
|
364 ;;; mode: C++ *** |
|
365 ;;; End: *** |
|
366 */ |
|
367 |
|
368 |