Mercurial > hg > octave-nkf
annotate src/ov-fcn-inline.cc @ 8032:2fd4a5ef6b59
stash parent function scope for inline functions and anonymous function handles.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 12 Aug 2008 16:26:53 -0400 |
parents | 47a18b8c9000 |
children | 24701aa75ecb |
rev | line source |
---|---|
4933 | 1 /* |
2 | |
7017 | 3 Copyright (C) 2004, 2005, 2006, 2007 David Bateman |
4933 | 4 |
7016 | 5 This file is part of Octave. |
4933 | 6 |
7016 | 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 3 of the License, or (at your | |
10 option) any 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. | |
4933 | 16 |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
4933 | 20 |
21 In addition to the terms of the GPL, you are permitted to link | |
22 this program with any Open Source program, as defined by the | |
23 Open Source Initiative (www.opensource.org) | |
24 | |
25 */ | |
26 | |
27 #ifdef HAVE_CONFIG_H | |
28 #include <config.h> | |
29 #endif | |
30 | |
4988 | 31 #include <istream> |
4933 | 32 #include <iostream> |
5765 | 33 #include <sstream> |
5164 | 34 #include <vector> |
4933 | 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" | |
8032
2fd4a5ef6b59
stash parent function scope for inline functions and anonymous function handles.
John W. Eaton <jwe@octave.org>
parents:
7908
diff
changeset
|
42 #include "ov-usr-fcn.h" |
4933 | 43 #include "pr-output.h" |
44 #include "variables.h" | |
45 #include "parse.h" | |
8032
2fd4a5ef6b59
stash parent function scope for inline functions and anonymous function handles.
John W. Eaton <jwe@octave.org>
parents:
7908
diff
changeset
|
46 #include "toplev.h" |
4933 | 47 |
4972 | 48 #include "byte-swap.h" |
49 #include "ls-oct-ascii.h" | |
50 #include "ls-hdf5.h" | |
51 #include "ls-utils.h" | |
52 | |
4933 | 53 DEFINE_OCTAVE_ALLOCATOR (octave_fcn_inline); |
54 | |
55 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_fcn_inline, | |
56 "inline function", | |
6220 | 57 "function_handle"); |
4933 | 58 |
59 octave_fcn_inline::octave_fcn_inline (const std::string& f, | |
4973 | 60 const string_vector& a, |
4933 | 61 const std::string& n) |
5007 | 62 : octave_fcn_handle (n), iftext (f), ifargs (a) |
4933 | 63 { |
4973 | 64 // Form a string representing the function. |
4933 | 65 |
5765 | 66 std::ostringstream buf; |
4933 | 67 |
5007 | 68 buf << "@("; |
4933 | 69 |
70 for (int i = 0; i < ifargs.length (); i++) | |
71 { | |
72 if (i > 0) | |
73 buf << ", "; | |
74 | |
75 buf << ifargs(i); | |
76 } | |
77 | |
5765 | 78 buf << ") " << iftext; |
4933 | 79 |
5007 | 80 int parse_status; |
5765 | 81 octave_value anon_fcn_handle = eval_string (buf.str (), true, parse_status); |
4933 | 82 |
5007 | 83 if (parse_status == 0) |
4933 | 84 { |
5007 | 85 octave_fcn_handle *fh = anon_fcn_handle.fcn_handle_value (); |
4933 | 86 |
5007 | 87 if (fh) |
8032
2fd4a5ef6b59
stash parent function scope for inline functions and anonymous function handles.
John W. Eaton <jwe@octave.org>
parents:
7908
diff
changeset
|
88 { |
2fd4a5ef6b59
stash parent function scope for inline functions and anonymous function handles.
John W. Eaton <jwe@octave.org>
parents:
7908
diff
changeset
|
89 fcn = fh->fcn_val (); |
2fd4a5ef6b59
stash parent function scope for inline functions and anonymous function handles.
John W. Eaton <jwe@octave.org>
parents:
7908
diff
changeset
|
90 |
2fd4a5ef6b59
stash parent function scope for inline functions and anonymous function handles.
John W. Eaton <jwe@octave.org>
parents:
7908
diff
changeset
|
91 octave_user_function *uf = fcn.user_function_value (); |
2fd4a5ef6b59
stash parent function scope for inline functions and anonymous function handles.
John W. Eaton <jwe@octave.org>
parents:
7908
diff
changeset
|
92 |
2fd4a5ef6b59
stash parent function scope for inline functions and anonymous function handles.
John W. Eaton <jwe@octave.org>
parents:
7908
diff
changeset
|
93 if (uf) |
2fd4a5ef6b59
stash parent function scope for inline functions and anonymous function handles.
John W. Eaton <jwe@octave.org>
parents:
7908
diff
changeset
|
94 uf->stash_parent_fcn_scope (octave_call_stack::current_scope ()); |
2fd4a5ef6b59
stash parent function scope for inline functions and anonymous function handles.
John W. Eaton <jwe@octave.org>
parents:
7908
diff
changeset
|
95 } |
4933 | 96 } |
5007 | 97 |
98 if (fcn.is_undefined ()) | |
4933 | 99 error ("inline: unable to define function"); |
100 } | |
101 | |
6625 | 102 // This function is supplied to allow a Matlab style class structure |
103 // to be returned.. | |
104 Octave_map | |
105 octave_fcn_inline::map_value (void) const | |
106 { | |
107 Octave_map m; | |
108 string_vector args = fcn_arg_names (); | |
109 m.assign ("version", octave_value (1.0)); | |
110 m.assign ("isEmpty", octave_value (0.0)); | |
111 m.assign ("expr", octave_value (fcn_text ())); | |
112 m.assign ("numArgs", octave_value (args.length ())); | |
113 m.assign ("args", octave_value (args)); | |
114 std::ostringstream buf; | |
115 for (int i = 0; i < args.length (); i++) | |
116 buf << args(i) << " = INLINE_INPUTS_{" << i + 1 << "}; "; | |
117 m.assign ("inputExpr", octave_value (buf.str ())); | |
118 | |
119 return m; | |
120 } | |
121 | |
4972 | 122 bool |
6974 | 123 octave_fcn_inline::save_ascii (std::ostream& os) |
4972 | 124 { |
4973 | 125 os << "# nargs: " << ifargs.length () << "\n"; |
4972 | 126 for (int i = 0; i < ifargs.length (); i++) |
4973 | 127 os << ifargs(i) << "\n"; |
128 if (nm.length () < 1) | |
129 // Write an invalid value to flag empty fcn handle name. | |
4972 | 130 os << "0\n"; |
131 else | |
132 os << nm << "\n"; | |
133 os << iftext << "\n"; | |
134 return true; | |
135 } | |
136 | |
137 bool | |
138 octave_fcn_inline::load_ascii (std::istream& is) | |
139 { | |
140 int nargs; | |
141 if (extract_keyword (is, "nargs", nargs, true)) | |
142 { | |
143 ifargs.resize (nargs); | |
144 for (int i = 0; i < nargs; i++) | |
4973 | 145 is >> ifargs(i); |
4972 | 146 is >> nm; |
147 if (nm == "0") | |
148 nm = ""; | |
4988 | 149 |
150 char c; | |
5765 | 151 std::ostringstream buf; |
4988 | 152 |
153 // Skip preceeding newline(s) | |
7908
47a18b8c9000
ov-fcn-inline.cc (octave_fcn_inline::load_ascii): Avoid GCC warning
John W. Eaton <jwe@octave.org>
parents:
7764
diff
changeset
|
154 while (is.get (c) && c == '\n') |
47a18b8c9000
ov-fcn-inline.cc (octave_fcn_inline::load_ascii): Avoid GCC warning
John W. Eaton <jwe@octave.org>
parents:
7764
diff
changeset
|
155 /* do nothing */; |
4988 | 156 |
157 if (is) | |
158 { | |
159 buf << c; | |
160 | |
161 // Get a line of text whitespace characters included, leaving | |
162 // newline in the stream | |
163 while (is.peek () != '\n') | |
164 { | |
165 is.get (c); | |
166 if (! is) | |
167 break; | |
168 buf << c; | |
169 } | |
170 } | |
171 | |
5765 | 172 iftext = buf.str (); |
4972 | 173 |
174 octave_fcn_inline tmp (iftext, ifargs, nm); | |
175 fcn = tmp.fcn; | |
176 | |
177 return true; | |
178 } | |
179 else | |
180 return false; | |
181 } | |
182 | |
4973 | 183 bool |
4972 | 184 octave_fcn_inline::save_binary (std::ostream& os, bool&) |
185 { | |
5828 | 186 int32_t tmp = ifargs.length (); |
5760 | 187 os.write (reinterpret_cast<char *> (&tmp), 4); |
4973 | 188 for (int i = 0; i < ifargs.length (); i++) |
4972 | 189 { |
4973 | 190 tmp = ifargs(i).length (); |
5760 | 191 os.write (reinterpret_cast<char *> (&tmp), 4); |
4973 | 192 os.write (ifargs(i).c_str (), ifargs(i).length ()); |
4972 | 193 } |
4973 | 194 tmp = nm.length (); |
5760 | 195 os.write (reinterpret_cast<char *> (&tmp), 4); |
4973 | 196 os.write (nm.c_str (), nm.length ()); |
197 tmp = iftext.length (); | |
5760 | 198 os.write (reinterpret_cast<char *> (&tmp), 4); |
4973 | 199 os.write (iftext.c_str (), iftext.length ()); |
4972 | 200 return true; |
201 } | |
202 | |
4973 | 203 bool |
4972 | 204 octave_fcn_inline::load_binary (std::istream& is, bool swap, |
205 oct_mach_info::float_format) | |
206 { | |
5828 | 207 int32_t nargs; |
5760 | 208 if (! is.read (reinterpret_cast<char *> (&nargs), 4)) |
4972 | 209 return false; |
210 if (swap) | |
211 swap_bytes<4> (&nargs); | |
212 | |
213 if (nargs < 1) | |
214 return false; | |
215 else | |
216 { | |
5828 | 217 int32_t tmp; |
4973 | 218 ifargs.resize (nargs); |
4972 | 219 for (int i = 0; i < nargs; i++) |
220 { | |
5760 | 221 if (! is.read (reinterpret_cast<char *> (&tmp), 4)) |
4972 | 222 return false; |
223 if (swap) | |
224 swap_bytes<4> (&tmp); | |
225 | |
226 OCTAVE_LOCAL_BUFFER (char, ctmp, tmp+1); | |
227 is.read (ctmp, tmp); | |
228 ifargs(i) = std::string (ctmp); | |
229 | |
230 if (! is) | |
231 return false; | |
4973 | 232 } |
4972 | 233 |
5760 | 234 if (! is.read (reinterpret_cast<char *> (&tmp), 4)) |
4972 | 235 return false; |
236 if (swap) | |
237 swap_bytes<4> (&tmp); | |
238 | |
239 OCTAVE_LOCAL_BUFFER (char, ctmp1, tmp+1); | |
240 is.read (ctmp1, tmp); | |
241 nm = std::string (ctmp1); | |
242 | |
243 if (! is) | |
244 return false; | |
245 | |
5760 | 246 if (! is.read (reinterpret_cast<char *> (&tmp), 4)) |
4972 | 247 return false; |
248 if (swap) | |
249 swap_bytes<4> (&tmp); | |
250 | |
251 OCTAVE_LOCAL_BUFFER (char, ctmp2, tmp+1); | |
252 is.read (ctmp2, tmp); | |
253 iftext = std::string (ctmp2); | |
254 | |
255 if (! is) | |
256 return false; | |
257 | |
258 octave_fcn_inline ftmp (iftext, ifargs, nm); | |
259 fcn = ftmp.fcn; | |
260 } | |
261 return true; | |
262 } | |
263 | |
264 #if defined (HAVE_HDF5) | |
265 bool | |
266 octave_fcn_inline::save_hdf5 (hid_t loc_id, const char *name, | |
267 bool /* save_as_floats */) | |
268 { | |
269 hid_t group_hid = -1; | |
270 group_hid = H5Gcreate (loc_id, name, 0); | |
271 if (group_hid < 0 ) return false; | |
272 | |
273 size_t len = 0; | |
4973 | 274 for (int i = 0; i < ifargs.length (); i++) |
275 if (len < ifargs(i).length ()) | |
276 len = ifargs(i).length (); | |
4972 | 277 |
278 hid_t space_hid = -1, data_hid = -1, type_hid = -1;; | |
279 bool retval = true; | |
280 | |
5775 | 281 // FIXME Is there a better way of saving string vectors, than a |
4972 | 282 // null padded matrix? |
283 | |
284 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, 2); | |
285 | |
286 // Octave uses column-major, while HDF5 uses row-major ordering | |
4973 | 287 hdims[1] = ifargs.length (); |
4972 | 288 hdims[0] = len + 1; |
289 | |
290 space_hid = H5Screate_simple (2, hdims, 0); | |
291 if (space_hid < 0) | |
292 { | |
293 H5Gclose (group_hid); | |
294 return false; | |
295 } | |
296 | |
4973 | 297 data_hid = H5Dcreate (group_hid, "args", H5T_NATIVE_CHAR, space_hid, |
4972 | 298 H5P_DEFAULT); |
299 if (data_hid < 0) | |
300 { | |
301 H5Sclose (space_hid); | |
302 H5Gclose (group_hid); | |
303 return false; | |
304 } | |
305 | |
4973 | 306 OCTAVE_LOCAL_BUFFER (char, s, ifargs.length () * (len + 1)); |
4972 | 307 |
308 // Save the args as a null teminated list | |
4973 | 309 for (int i = 0; i < ifargs.length (); i++) |
4972 | 310 { |
4973 | 311 const char * cptr = ifargs(i).c_str (); |
312 for (size_t j = 0; j < ifargs(i).length (); j++) | |
4972 | 313 s[i*(len+1)+j] = *cptr++; |
4973 | 314 s[ifargs(i).length ()] = '\0'; |
4972 | 315 } |
316 | |
4973 | 317 retval = H5Dwrite (data_hid, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, |
4972 | 318 H5P_DEFAULT, s) >= 0; |
319 | |
320 H5Dclose (data_hid); | |
321 H5Sclose (space_hid); | |
322 | |
323 if (!retval) | |
324 { | |
325 H5Gclose (group_hid); | |
326 return false; | |
4973 | 327 } |
4972 | 328 |
329 // attach the type of the variable | |
4973 | 330 type_hid = H5Tcopy (H5T_C_S1); |
4972 | 331 H5Tset_size (type_hid, nm.length () + 1); |
332 if (type_hid < 0) | |
333 { | |
334 H5Gclose (group_hid); | |
335 return false; | |
4973 | 336 } |
4972 | 337 |
338 hdims[0] = 0; | |
5760 | 339 space_hid = H5Screate_simple (0 , hdims, 0); |
4972 | 340 if (space_hid < 0) |
341 { | |
342 H5Tclose (type_hid); | |
343 H5Gclose (group_hid); | |
344 return false; | |
4973 | 345 } |
4972 | 346 |
347 data_hid = H5Dcreate (group_hid, "nm", type_hid, space_hid, H5P_DEFAULT); | |
4973 | 348 if (data_hid < 0 || H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL, |
5760 | 349 H5P_DEFAULT, nm.c_str ()) < 0) |
4972 | 350 { |
351 H5Sclose (space_hid); | |
352 H5Tclose (type_hid); | |
353 H5Gclose (group_hid); | |
354 return false; | |
4973 | 355 } |
4972 | 356 H5Dclose (data_hid); |
357 | |
358 // attach the type of the variable | |
359 H5Tset_size (type_hid, iftext.length () + 1); | |
360 if (type_hid < 0) | |
361 { | |
362 H5Gclose (group_hid); | |
363 return false; | |
4973 | 364 } |
4972 | 365 |
4973 | 366 data_hid = H5Dcreate (group_hid, "iftext", type_hid, space_hid, |
4972 | 367 H5P_DEFAULT); |
4973 | 368 if (data_hid < 0 || H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL, |
5760 | 369 H5P_DEFAULT, iftext.c_str ()) < 0) |
4972 | 370 { |
371 H5Sclose (space_hid); | |
372 H5Tclose (type_hid); | |
373 H5Gclose (group_hid); | |
374 return false; | |
4973 | 375 } |
4972 | 376 |
377 H5Dclose (data_hid); | |
4988 | 378 H5Sclose (space_hid); |
379 H5Tclose (type_hid); | |
380 H5Gclose (group_hid); | |
4972 | 381 |
382 return retval; | |
383 } | |
384 | |
385 bool | |
386 octave_fcn_inline::load_hdf5 (hid_t loc_id, const char *name, | |
4973 | 387 bool /* have_h5giterate_bug */) |
4972 | 388 { |
389 hid_t group_hid, data_hid, space_hid, type_hid, type_class_hid, st_id; | |
390 hsize_t rank; | |
391 int slen; | |
392 | |
393 group_hid = H5Gopen (loc_id, name); | |
394 if (group_hid < 0 ) return false; | |
395 | |
396 data_hid = H5Dopen (group_hid, "args"); | |
397 space_hid = H5Dget_space (data_hid); | |
398 rank = H5Sget_simple_extent_ndims (space_hid); | |
399 | |
400 if (rank != 2) | |
4973 | 401 { |
4972 | 402 H5Dclose (data_hid); |
403 H5Sclose (space_hid); | |
404 H5Gclose (group_hid); | |
405 return false; | |
406 } | |
407 | |
408 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); | |
409 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); | |
410 | |
411 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); | |
412 | |
4973 | 413 ifargs.resize (hdims[1]); |
4972 | 414 |
415 OCTAVE_LOCAL_BUFFER (char, s1, hdims[0] * hdims[1]); | |
416 | |
4973 | 417 if (H5Dread (data_hid, H5T_NATIVE_UCHAR, H5S_ALL, H5S_ALL, |
4972 | 418 H5P_DEFAULT, s1) < 0) |
4973 | 419 { |
4972 | 420 H5Dclose (data_hid); |
421 H5Sclose (space_hid); | |
422 H5Gclose (group_hid); | |
423 return false; | |
424 } | |
425 | |
426 H5Dclose (data_hid); | |
427 H5Sclose (space_hid); | |
428 | |
429 for (size_t i = 0; i < hdims[1]; i++) | |
430 ifargs(i) = std::string (s1 + i*hdims[0]); | |
431 | |
432 data_hid = H5Dopen (group_hid, "nm"); | |
433 | |
434 if (data_hid < 0) | |
435 { | |
436 H5Gclose (group_hid); | |
437 return false; | |
438 } | |
439 | |
440 type_hid = H5Dget_type (data_hid); | |
441 type_class_hid = H5Tget_class (type_hid); | |
442 | |
443 if (type_class_hid != H5T_STRING) | |
444 { | |
445 H5Tclose (type_hid); | |
446 H5Dclose (data_hid); | |
447 H5Gclose (group_hid); | |
448 return false; | |
449 } | |
4973 | 450 |
4972 | 451 space_hid = H5Dget_space (data_hid); |
452 rank = H5Sget_simple_extent_ndims (space_hid); | |
453 | |
454 if (rank != 0) | |
455 { | |
456 H5Sclose (space_hid); | |
457 H5Tclose (type_hid); | |
458 H5Dclose (data_hid); | |
459 H5Gclose (group_hid); | |
460 return false; | |
461 } | |
462 | |
463 slen = H5Tget_size (type_hid); | |
464 if (slen < 0) | |
465 { | |
466 H5Sclose (space_hid); | |
467 H5Tclose (type_hid); | |
468 H5Dclose (data_hid); | |
469 H5Gclose (group_hid); | |
470 return false; | |
471 } | |
472 | |
473 OCTAVE_LOCAL_BUFFER (char, nm_tmp, slen); | |
474 | |
475 // create datatype for (null-terminated) string to read into: | |
476 st_id = H5Tcopy (H5T_C_S1); | |
477 H5Tset_size (st_id, slen); | |
478 | |
5760 | 479 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, nm_tmp) < 0) |
4972 | 480 { |
481 H5Sclose (space_hid); | |
482 H5Tclose (type_hid); | |
483 H5Gclose (group_hid); | |
484 return false; | |
485 } | |
486 H5Tclose (st_id); | |
487 H5Dclose (data_hid); | |
488 nm = nm_tmp; | |
489 | |
490 data_hid = H5Dopen (group_hid, "iftext"); | |
491 | |
492 if (data_hid < 0) | |
493 { | |
494 H5Gclose (group_hid); | |
495 return false; | |
496 } | |
497 | |
498 type_hid = H5Dget_type (data_hid); | |
499 type_class_hid = H5Tget_class (type_hid); | |
500 | |
501 if (type_class_hid != H5T_STRING) | |
502 { | |
503 H5Tclose (type_hid); | |
504 H5Dclose (data_hid); | |
505 H5Gclose (group_hid); | |
506 return false; | |
507 } | |
4973 | 508 |
4972 | 509 space_hid = H5Dget_space (data_hid); |
510 rank = H5Sget_simple_extent_ndims (space_hid); | |
511 | |
512 if (rank != 0) | |
513 { | |
514 H5Sclose (space_hid); | |
515 H5Tclose (type_hid); | |
516 H5Dclose (data_hid); | |
517 H5Gclose (group_hid); | |
518 return false; | |
519 } | |
520 | |
521 slen = H5Tget_size (type_hid); | |
522 if (slen < 0) | |
523 { | |
524 H5Sclose (space_hid); | |
525 H5Tclose (type_hid); | |
526 H5Dclose (data_hid); | |
527 H5Gclose (group_hid); | |
528 return false; | |
529 } | |
530 | |
531 OCTAVE_LOCAL_BUFFER (char, iftext_tmp, slen); | |
532 | |
533 // create datatype for (null-terminated) string to read into: | |
534 st_id = H5Tcopy (H5T_C_S1); | |
535 H5Tset_size (st_id, slen); | |
536 | |
5760 | 537 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, iftext_tmp) < 0) |
4972 | 538 { |
539 H5Sclose (space_hid); | |
540 H5Tclose (type_hid); | |
541 H5Gclose (group_hid); | |
542 return false; | |
543 } | |
544 H5Tclose (st_id); | |
545 H5Dclose (data_hid); | |
546 iftext = iftext_tmp; | |
547 | |
548 octave_fcn_inline ftmp (iftext, ifargs, nm); | |
549 fcn = ftmp.fcn; | |
550 | |
551 return true; | |
552 } | |
553 #endif | |
554 | |
4933 | 555 void |
556 octave_fcn_inline::print (std::ostream& os, bool pr_as_read_syntax) const | |
557 { | |
558 print_raw (os, pr_as_read_syntax); | |
559 newline (os); | |
560 } | |
561 | |
562 void | |
563 octave_fcn_inline::print_raw (std::ostream& os, bool pr_as_read_syntax) const | |
564 { | |
5765 | 565 std::ostringstream buf; |
4933 | 566 |
567 if (nm.empty ()) | |
568 buf << "f("; | |
569 else | |
570 buf << nm << "("; | |
571 | |
572 for (int i = 0; i < ifargs.length (); i++) | |
573 { | |
574 if (i) | |
575 buf << ", "; | |
576 | |
577 buf << ifargs(i); | |
578 } | |
579 | |
5765 | 580 buf << ") = " << iftext; |
4933 | 581 |
5765 | 582 octave_print_internal (os, buf.str (), pr_as_read_syntax, |
4933 | 583 current_print_indent_level ()); |
584 } | |
585 | |
586 octave_value | |
5279 | 587 octave_fcn_inline::convert_to_str_internal (bool, bool, char type) const |
4933 | 588 { |
5279 | 589 return octave_value (fcn_text (), type); |
4933 | 590 } |
591 | |
6973 | 592 DEFUNX ("inline", Finline, args, , |
4933 | 593 "-*- texinfo -*-\n\ |
594 @deftypefn {Built-in Function} {} inline (@var{str})\n\ | |
6547 | 595 @deftypefnx {Built-in Function} {} inline (@var{str}, @var{arg1}, @dots{})\n\ |
4933 | 596 @deftypefnx {Built-in Function} {} inline (@var{str}, @var{n})\n\ |
597 Create an inline function from the character string @var{str}.\n\ | |
5034 | 598 If called with a single argument, the arguments of the generated\n\ |
599 function are extracted from the function itself. The generated\n\ | |
600 function arguments will then be in alphabetical order. It should\n\ | |
601 be noted that i, and j are ignored as arguments due to the\n\ | |
602 ambiguity between their use as a variable or their use as an inbuilt\n\ | |
7001 | 603 constant. All arguments followed by a parenthesis are considered\n\ |
5034 | 604 to be functions.\n\ |
4933 | 605 \n\ |
606 If the second and subsequent arguments are character strings,\n\ | |
607 they are the names of the arguments of the function.\n\ | |
608 \n\ | |
609 If the second argument is an integer @var{n}, the arguments are\n\ | |
610 @code{\"x\"}, @code{\"P1\"}, @dots{}, @code{\"P@var{N}\"}.\n\ | |
5642 | 611 @seealso{argnames, formula, vectorize}\n\ |
612 @end deftypefn") | |
4933 | 613 { |
614 octave_value retval; | |
615 | |
616 int nargin = args.length (); | |
617 | |
618 if (nargin > 0) | |
619 { | |
620 std::string fun = args(0).string_value (); | |
621 | |
622 if (! error_state) | |
623 { | |
624 string_vector fargs; | |
625 | |
626 if (nargin == 1) | |
627 { | |
5034 | 628 bool is_arg = false; |
5037 | 629 bool in_string = false; |
5034 | 630 std::string tmp_arg; |
631 size_t i = 0; | |
632 | |
633 while (i < fun.length ()) | |
634 { | |
5037 | 635 bool terminate_arg = false; |
5034 | 636 char c = fun[i++]; |
4933 | 637 |
5037 | 638 if (in_string) |
639 { | |
640 if (c == '\'' || c == '\"') | |
641 in_string = false; | |
642 } | |
643 else if (c == '\'' || c == '\"') | |
644 { | |
645 in_string = true; | |
646 if (is_arg) | |
647 terminate_arg = true; | |
648 } | |
649 else if (! isalpha (c) && c != '_') | |
5034 | 650 if (! is_arg) |
651 continue; | |
652 else if (isdigit (c)) | |
5037 | 653 tmp_arg.append (1, c); |
5034 | 654 else |
655 { | |
5037 | 656 // Before we do anything remove trailing whitespaces. |
5034 | 657 while (i < fun.length () && isspace (c)) |
658 c = fun[i++]; | |
5037 | 659 |
5034 | 660 // Do we have a variable or a function? |
661 if (c != '(') | |
5037 | 662 terminate_arg = true; |
663 else | |
5034 | 664 { |
5037 | 665 tmp_arg = std::string (); |
666 is_arg = false; | |
5034 | 667 } |
668 } | |
669 else | |
670 { | |
671 tmp_arg.append (1, c); | |
672 is_arg = true; | |
5037 | 673 } |
5034 | 674 |
5037 | 675 if (terminate_arg || (i == fun.length () && is_arg)) |
676 { | |
677 bool have_arg = false; | |
678 | |
679 for (int j = 0; j < fargs.length (); j++) | |
680 if (tmp_arg == fargs (j)) | |
681 { | |
682 have_arg = true; | |
683 break; | |
684 } | |
5034 | 685 |
7764
aaa808ed5d53
Also ignore other constants in Finline
David Bateman <dbateman@free.fr>
parents:
7394
diff
changeset
|
686 if (! have_arg && tmp_arg != "i" && tmp_arg != "j" && |
aaa808ed5d53
Also ignore other constants in Finline
David Bateman <dbateman@free.fr>
parents:
7394
diff
changeset
|
687 tmp_arg != "NaN" && tmp_arg != "nan" && |
aaa808ed5d53
Also ignore other constants in Finline
David Bateman <dbateman@free.fr>
parents:
7394
diff
changeset
|
688 tmp_arg != "Inf" && tmp_arg != "inf" && |
aaa808ed5d53
Also ignore other constants in Finline
David Bateman <dbateman@free.fr>
parents:
7394
diff
changeset
|
689 tmp_arg != "NA" && tmp_arg != "pi" && |
aaa808ed5d53
Also ignore other constants in Finline
David Bateman <dbateman@free.fr>
parents:
7394
diff
changeset
|
690 tmp_arg != "eps") |
5037 | 691 fargs.append (tmp_arg); |
692 | |
693 tmp_arg = std::string (); | |
694 is_arg = false; | |
5022 | 695 } |
696 } | |
5034 | 697 |
5037 | 698 // Sort the arguments into ascii order. |
5034 | 699 fargs.qsort (); |
4933 | 700 } |
701 else if (nargin == 2 && args(1).is_numeric_type ()) | |
702 { | |
703 int n = args(1).int_value (); | |
704 | |
705 if (! error_state) | |
706 { | |
707 if (n >= 0) | |
708 { | |
709 fargs.resize (n+1); | |
710 | |
711 fargs(0) = "x"; | |
712 | |
713 for (int i = 1; i < n+1; i++) | |
714 { | |
5765 | 715 std::ostringstream buf; |
716 buf << "P" << i; | |
717 fargs(i) = buf.str (); | |
4933 | 718 } |
719 } | |
720 else | |
721 { | |
722 error ("inline: numeric argument must be nonnegative"); | |
723 return retval; | |
724 } | |
725 } | |
726 else | |
727 { | |
728 error ("inline: expecting second argument to be an integer"); | |
729 return retval; | |
730 } | |
731 } | |
732 else | |
733 { | |
734 fargs.resize (nargin - 1); | |
735 | |
736 for (int i = 1; i < nargin; i++) | |
737 { | |
738 std::string s = args(i).string_value (); | |
739 | |
740 if (! error_state) | |
741 fargs(i-1) = s; | |
742 else | |
743 { | |
744 error ("inline: expecting string arguments"); | |
745 return retval; | |
746 } | |
747 } | |
748 } | |
749 | |
750 retval = octave_value (new octave_fcn_inline (fun, fargs)); | |
751 } | |
752 else | |
753 error ("inline: first argument must be a string"); | |
754 } | |
755 else | |
5823 | 756 print_usage (); |
4933 | 757 |
758 return retval; | |
759 } | |
760 | |
7394 | 761 /* |
762 %!shared fn | |
763 %! fn = inline ("x.^2 + 1","x"); | |
764 %!assert (feval (fn, 6), 37) | |
765 %!assert (fn (6), 37) | |
766 */ | |
767 | |
4933 | 768 DEFUN (formula, args, , |
769 "-*- texinfo -*-\n\ | |
770 @deftypefn {Built-in Function} {} formula (@var{fun})\n\ | |
771 Return a character string representing the inline function @var{fun}.\n\ | |
772 Note that @code{char (@var{fun})} is equivalent to\n\ | |
773 @code{formula (@var{fun})}.\n\ | |
5642 | 774 @seealso{argnames, inline, vectorize}\n\ |
775 @end deftypefn") | |
4933 | 776 { |
777 octave_value retval; | |
778 | |
779 int nargin = args.length (); | |
780 | |
781 if (nargin == 1) | |
782 { | |
783 octave_fcn_inline* fn = args(0).fcn_inline_value (true); | |
784 | |
785 if (fn) | |
786 retval = octave_value (fn->fcn_text ()); | |
787 else | |
788 error ("formula: must be an inline function"); | |
789 } | |
790 else | |
5823 | 791 print_usage (); |
4933 | 792 |
793 return retval; | |
794 } | |
795 | |
796 DEFUN (argnames, args, , | |
797 "-*- texinfo -*-\n\ | |
798 @deftypefn {Built-in Function} {} argnames (@var{fun})\n\ | |
799 Return a cell array of character strings containing the names of\n\ | |
800 the arguments of the inline function @var{fun}.\n\ | |
6529 | 801 @seealso{inline, formula, vectorize}\n\ |
5642 | 802 @end deftypefn") |
4933 | 803 { |
804 octave_value retval; | |
805 | |
806 int nargin = args.length (); | |
807 | |
808 if (nargin == 1) | |
809 { | |
810 octave_fcn_inline *fn = args(0).fcn_inline_value (true); | |
811 | |
812 if (fn) | |
813 { | |
814 string_vector t1 = fn->fcn_arg_names (); | |
815 | |
816 Cell t2 (dim_vector (t1.length (), 1)); | |
817 | |
818 for (int i = 0; i < t1.length (); i++) | |
819 t2(i) = t1(i); | |
820 | |
821 retval = t2; | |
822 } | |
823 else | |
824 error ("argnames: argument must be an inline function"); | |
825 } | |
826 else | |
5823 | 827 print_usage (); |
4933 | 828 |
829 return retval; | |
830 } | |
831 | |
832 DEFUN (vectorize, args, , | |
833 "-*- texinfo -*-\n\ | |
6635 | 834 @deftypefn {Built-in Function} {} vectorize (@var{fun})\n\ |
4933 | 835 Create a vectorized version of the inline function @var{fun}\n\ |
836 by replacing all occurrences of @code{*}, @code{/}, etc., with\n\ | |
837 @code{.*}, @code{./}, etc.\n\ | |
5642 | 838 @end deftypefn") |
4933 | 839 { |
840 octave_value retval; | |
841 | |
842 int nargin = args.length (); | |
843 | |
844 if (nargin == 1) | |
845 { | |
5409 | 846 std::string old_func; |
847 octave_fcn_inline* old = 0; | |
848 bool func_is_string = true; | |
4933 | 849 |
5409 | 850 if (args(0).is_string ()) |
851 old_func = args(0).string_value (); | |
852 else | |
4933 | 853 { |
5409 | 854 old = args(0).fcn_inline_value (true); |
855 func_is_string = false; | |
856 | |
857 if (old) | |
858 old_func = old->fcn_text (); | |
859 else | |
860 error ("vectorize: must be a string or inline function"); | |
861 } | |
862 | |
863 if (! error_state) | |
864 { | |
4933 | 865 std::string new_func; |
866 size_t i = 0; | |
867 | |
868 while (i < old_func.length ()) | |
869 { | |
870 std::string t1 = old_func.substr (i, 1); | |
871 | |
872 if (t1 == "*" || t1 == "/" || t1 == "\\" || t1 == "^") | |
873 { | |
874 if (i && old_func.substr (i-1, 1) != ".") | |
875 new_func.append ("."); | |
876 | |
877 // Special case for ** operator. | |
4973 | 878 if (t1 == "*" && i < (old_func.length () - 1) |
4933 | 879 && old_func.substr (i+1, 1) == "*") |
880 { | |
881 new_func.append ("*"); | |
882 i++; | |
883 } | |
884 } | |
885 new_func.append (t1); | |
886 i++; | |
887 } | |
888 | |
5409 | 889 if (func_is_string) |
890 retval = octave_value (new_func); | |
891 else | |
892 retval = octave_value (new octave_fcn_inline | |
893 (new_func, old->fcn_arg_names ())); | |
4933 | 894 } |
895 } | |
896 else | |
5823 | 897 print_usage (); |
4933 | 898 |
899 return retval; | |
900 } | |
901 | |
902 /* | |
903 ;;; Local Variables: *** | |
904 ;;; mode: C++ *** | |
905 ;;; End: *** | |
906 */ |