2974
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 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 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
2974
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
|
31 #include "str-vec.h" |
|
32 |
|
33 #include <defaults.h> |
3974
|
34 #include "Cell.h" |
2974
|
35 #include "defun.h" |
|
36 #include "error.h" |
|
37 #include "input.h" |
|
38 #include "oct-obj.h" |
|
39 #include "ov-usr-fcn.h" |
|
40 #include "ov.h" |
|
41 #include "pager.h" |
2985
|
42 #include "pt-jump.h" |
2974
|
43 #include "pt-misc.h" |
|
44 #include "pt-pr-code.h" |
2982
|
45 #include "pt-stmt.h" |
2974
|
46 #include "pt-walk.h" |
|
47 #include "symtab.h" |
|
48 #include "toplev.h" |
|
49 #include "unwind-prot.h" |
|
50 #include "utils.h" |
3489
|
51 #include "parse.h" |
2974
|
52 #include "variables.h" |
|
53 |
3131
|
54 // Maximum nesting level for functions called recursively. |
|
55 static int Vmax_recursion_depth; |
|
56 |
2974
|
57 // User defined functions. |
|
58 |
3219
|
59 DEFINE_OCTAVE_ALLOCATOR (octave_user_function); |
2974
|
60 |
3219
|
61 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_user_function, |
4612
|
62 "user-defined function", |
3219
|
63 "user-defined function"); |
2974
|
64 |
|
65 // Ugh. This really needs to be simplified (code/data? |
|
66 // extrinsic/intrinsic state?). |
|
67 |
|
68 octave_user_function::octave_user_function |
|
69 (tree_parameter_list *pl, tree_parameter_list *rl, |
|
70 tree_statement_list *cl, symbol_table *st) |
3523
|
71 : octave_function (std::string (), std::string ()), |
2974
|
72 param_list (pl), ret_list (rl), cmd_list (cl), |
4748
|
73 sym_tab (st), lead_comm (), trail_comm (), file_name (), |
3255
|
74 t_parsed (static_cast<time_t> (0)), |
|
75 t_checked (static_cast<time_t> (0)), |
|
76 system_fcn_file (false), call_depth (0), |
4238
|
77 num_named_args (0), nested_function (false), |
|
78 args_passed (), num_args_passed (0), |
3165
|
79 curr_va_arg_number (0), vr_list (0), symtab_entry (0), |
3974
|
80 argn_sr (0), nargin_sr (0), nargout_sr (0), varargin_sr (0) |
2974
|
81 { |
|
82 if (param_list) |
|
83 { |
|
84 num_named_args = param_list->length (); |
|
85 curr_va_arg_number = num_named_args; |
|
86 } |
|
87 } |
|
88 |
|
89 octave_user_function::~octave_user_function (void) |
|
90 { |
|
91 delete param_list; |
|
92 delete ret_list; |
|
93 delete sym_tab; |
|
94 delete cmd_list; |
|
95 delete vr_list; |
3665
|
96 delete lead_comm; |
|
97 delete trail_comm; |
2974
|
98 } |
|
99 |
|
100 octave_user_function * |
|
101 octave_user_function::define_ret_list (tree_parameter_list *t) |
|
102 { |
|
103 ret_list = t; |
|
104 |
|
105 if (ret_list && ret_list->takes_varargs ()) |
|
106 vr_list = new tree_va_return_list; |
|
107 |
|
108 return this; |
|
109 } |
|
110 |
|
111 void |
4343
|
112 octave_user_function::stash_fcn_file_name (const std::string& nm) |
2974
|
113 { |
4343
|
114 file_name = nm; |
2974
|
115 } |
|
116 |
|
117 void |
|
118 octave_user_function::mark_as_system_fcn_file (void) |
|
119 { |
|
120 if (! file_name.empty ()) |
|
121 { |
|
122 // We really should stash the whole path to the file we found, |
|
123 // when we looked it up, to avoid possible race conditions... |
|
124 // XXX FIXME XXX |
|
125 // |
|
126 // We probably also don't need to get the library directory |
|
127 // every time, but since this function is only called when the |
|
128 // function file is parsed, it probably doesn't matter that |
|
129 // much. |
|
130 |
3523
|
131 std::string ff_name = fcn_file_in_path (file_name); |
2974
|
132 |
3565
|
133 if (Vfcn_file_dir == ff_name.substr (0, Vfcn_file_dir.length ())) |
2974
|
134 system_fcn_file = 1; |
|
135 } |
|
136 else |
|
137 system_fcn_file = 0; |
|
138 } |
|
139 |
|
140 bool |
|
141 octave_user_function::takes_varargs (void) const |
|
142 { |
|
143 return (param_list && param_list->takes_varargs ()); |
|
144 } |
|
145 |
|
146 octave_value |
|
147 octave_user_function::octave_va_arg (void) |
|
148 { |
|
149 octave_value retval; |
|
150 |
|
151 if (curr_va_arg_number < num_args_passed) |
|
152 retval = args_passed (curr_va_arg_number++); |
|
153 else |
|
154 ::error ("va_arg: error getting arg number %d -- only %d provided", |
|
155 curr_va_arg_number + 1, num_args_passed); |
|
156 |
|
157 return retval; |
|
158 } |
|
159 |
|
160 octave_value_list |
|
161 octave_user_function::octave_all_va_args (void) |
|
162 { |
|
163 octave_value_list retval; |
|
164 |
3178
|
165 int n = num_args_passed - num_named_args; |
2974
|
166 |
3178
|
167 if (n > 0) |
|
168 { |
|
169 retval.resize (n); |
|
170 |
|
171 int k = 0; |
|
172 for (int i = num_named_args; i < num_args_passed; i++) |
|
173 retval(k++) = args_passed(i); |
|
174 } |
2974
|
175 |
|
176 return retval; |
|
177 } |
|
178 |
|
179 bool |
|
180 octave_user_function::takes_var_return (void) const |
|
181 { |
|
182 return (ret_list && ret_list->takes_varargs ()); |
|
183 } |
|
184 |
|
185 void |
|
186 octave_user_function::octave_vr_val (const octave_value& val) |
|
187 { |
4280
|
188 // Use != here to avoid possible conversion to int of smaller type |
|
189 // than the vr_list pointer. |
|
190 |
|
191 assert (vr_list != 0); |
2974
|
192 |
|
193 vr_list->append (val); |
|
194 } |
|
195 |
|
196 void |
3974
|
197 octave_user_function::varargout_to_vr_val (void) |
|
198 { |
|
199 assert (vr_list && vr_list->empty ()); |
|
200 |
|
201 symbol_record *sr = sym_tab->lookup ("varargout"); |
|
202 |
|
203 if (sr && sr->is_variable ()) |
|
204 { |
|
205 octave_value v = sr->def (); |
|
206 |
|
207 Cell c = v.cell_value (); |
|
208 |
|
209 if (! error_state) |
|
210 { |
|
211 // XXX FIXME XXX -- should varargout be required to be a |
|
212 // cell array with a single row or column? If not, should |
|
213 // we have a cleaner way of doing this operation? |
|
214 |
|
215 int n = c.length (); |
|
216 |
|
217 const octave_value *d = c.data (); |
|
218 |
|
219 for (int i = 0; i < n; i++) |
|
220 vr_list->append (d[i]); |
|
221 } |
|
222 else |
|
223 error ("expecting varargout to be a cell array object"); |
|
224 } |
|
225 } |
|
226 |
|
227 bool |
|
228 octave_user_function::has_varargout (void) const |
|
229 { |
|
230 bool retval = false; |
|
231 |
|
232 if (takes_var_return ()) |
|
233 { |
|
234 symbol_record *sr = sym_tab->lookup ("varargout"); |
|
235 |
|
236 retval = (sr && sr->is_variable ()); |
|
237 } |
|
238 |
|
239 return retval; |
|
240 } |
|
241 |
2974
|
242 // For unwind protect. |
|
243 |
|
244 static void |
|
245 pop_symbol_table_context (void *table) |
|
246 { |
|
247 symbol_table *tmp = static_cast<symbol_table *> (table); |
|
248 tmp->pop_context (); |
|
249 } |
|
250 |
|
251 static void |
|
252 delete_vr_list (void *list) |
|
253 { |
|
254 tree_va_return_list *tmp = static_cast<tree_va_return_list *> (list); |
|
255 tmp->clear (); |
|
256 delete tmp; |
|
257 } |
|
258 |
|
259 static void |
|
260 clear_symbol_table (void *table) |
|
261 { |
|
262 symbol_table *tmp = static_cast<symbol_table *> (table); |
4239
|
263 tmp->clear (); |
2974
|
264 } |
|
265 |
|
266 static void |
3239
|
267 clear_param_list (void *lst) |
|
268 { |
|
269 tree_parameter_list *tmp = static_cast<tree_parameter_list *> (lst); |
|
270 |
|
271 if (tmp) |
4219
|
272 tmp->undefine (); |
3239
|
273 } |
|
274 |
|
275 static void |
3875
|
276 restore_args_passed (void *fcn) |
3239
|
277 { |
|
278 octave_user_function *tmp = static_cast<octave_user_function *> (fcn); |
|
279 |
|
280 if (tmp) |
3875
|
281 tmp->restore_args_passed (); |
3239
|
282 } |
|
283 |
|
284 static void |
2974
|
285 unprotect_function (void *sr_arg) |
|
286 { |
|
287 symbol_record *sr = static_cast<symbol_record *> (sr_arg); |
|
288 sr->unprotect (); |
|
289 } |
|
290 |
|
291 octave_value_list |
4247
|
292 octave_user_function::subsref (const std::string& type, |
4219
|
293 const std::list<octave_value_list>& idx, |
3933
|
294 int nargout) |
|
295 { |
|
296 octave_value_list retval; |
|
297 |
|
298 switch (type[0]) |
|
299 { |
|
300 case '(': |
|
301 retval = do_multi_index_op (nargout, idx.front ()); |
|
302 break; |
|
303 |
|
304 case '{': |
|
305 case '.': |
|
306 { |
|
307 std::string nm = type_name (); |
|
308 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
309 } |
|
310 break; |
|
311 |
|
312 default: |
|
313 panic_impossible (); |
|
314 } |
|
315 |
4059
|
316 // XXX FIXME XXX -- perhaps there should be an |
|
317 // octave_value_list::next_subsref member function? See also |
|
318 // octave_builtin::subsref. |
3933
|
319 |
4219
|
320 if (idx.size () > 1) |
4059
|
321 retval = retval(0).next_subsref (type, idx); |
|
322 |
|
323 return retval; |
3933
|
324 } |
|
325 |
|
326 octave_value_list |
3544
|
327 octave_user_function::do_multi_index_op (int nargout, |
|
328 const octave_value_list& args) |
2974
|
329 { |
|
330 octave_value_list retval; |
|
331 |
|
332 if (error_state) |
|
333 return retval; |
|
334 |
|
335 if (! cmd_list) |
|
336 return retval; |
|
337 |
|
338 int nargin = args.length (); |
|
339 |
4987
|
340 unwind_protect::begin_frame ("user_func_eval"); |
2974
|
341 |
|
342 unwind_protect_int (call_depth); |
|
343 call_depth++; |
|
344 |
3131
|
345 if (call_depth > Vmax_recursion_depth) |
|
346 { |
|
347 ::error ("max_recursion_limit exceeded"); |
4987
|
348 unwind_protect::run_frame ("user_func_eval"); |
3131
|
349 return retval; |
|
350 } |
|
351 |
2974
|
352 if (symtab_entry && ! symtab_entry->is_read_only ()) |
|
353 { |
|
354 symtab_entry->protect (); |
2985
|
355 unwind_protect::add (unprotect_function, symtab_entry); |
2974
|
356 } |
|
357 |
|
358 if (call_depth > 1) |
|
359 { |
|
360 sym_tab->push_context (); |
2985
|
361 unwind_protect::add (pop_symbol_table_context, sym_tab); |
2974
|
362 |
|
363 if (vr_list) |
|
364 { |
|
365 // Push new vr_list. |
|
366 |
|
367 unwind_protect_ptr (vr_list); |
|
368 vr_list = new tree_va_return_list; |
|
369 |
|
370 // Clear and delete the new one before restoring the old |
|
371 // one. |
|
372 |
2985
|
373 unwind_protect::add (delete_vr_list, vr_list); |
2974
|
374 } |
|
375 } |
|
376 |
|
377 if (vr_list) |
|
378 vr_list->clear (); |
|
379 |
4929
|
380 install_automatic_vars (); |
|
381 |
2974
|
382 // Force symbols to be undefined again when this function exits. |
|
383 |
2985
|
384 unwind_protect::add (clear_symbol_table, sym_tab); |
2974
|
385 |
|
386 // Save old and set current symbol table context, for |
|
387 // eval_undefined_error(). |
|
388 |
4245
|
389 unwind_protect_ptr (curr_caller_sym_tab); |
|
390 curr_caller_sym_tab = curr_sym_tab; |
|
391 |
2974
|
392 unwind_protect_ptr (curr_sym_tab); |
|
393 curr_sym_tab = sym_tab; |
|
394 |
|
395 unwind_protect_ptr (curr_function); |
4975
|
396 unwind_protect_ptr (curr_caller_function); |
|
397 unwind_protect_ptr (curr_caller_statement); |
|
398 |
|
399 curr_caller_statement = curr_statement; |
|
400 curr_caller_function = curr_function; |
2974
|
401 curr_function = this; |
|
402 |
4238
|
403 if (! is_nested_function ()) |
|
404 { |
|
405 unwind_protect_ptr (curr_parent_function); |
|
406 curr_parent_function = this; |
|
407 } |
|
408 |
3875
|
409 // Save and restore args passed for recursive calls. |
2974
|
410 |
3875
|
411 save_args_passed (args); |
3239
|
412 |
3875
|
413 unwind_protect::add (::restore_args_passed, this); |
3239
|
414 |
2974
|
415 string_vector arg_names = args.name_tags (); |
|
416 |
|
417 unwind_protect_int (num_args_passed); |
|
418 num_args_passed = nargin; |
|
419 |
|
420 unwind_protect_int (curr_va_arg_number); |
|
421 |
3876
|
422 curr_va_arg_number = num_named_args; |
|
423 |
2974
|
424 if (param_list && ! param_list->varargs_only ()) |
|
425 { |
|
426 param_list->define_from_arg_vector (args); |
|
427 if (error_state) |
|
428 goto abort; |
|
429 } |
|
430 |
3239
|
431 // Force parameter list to be undefined when this function exits. |
|
432 // Doing so decrements the reference counts on the values of local |
|
433 // variables that are also named function parameters. |
|
434 |
|
435 unwind_protect::add (clear_param_list, param_list); |
|
436 |
|
437 // Force return list to be undefined when this function exits. |
|
438 // Doing so decrements the reference counts on the values of local |
|
439 // variables that are also named values returned by this function. |
|
440 |
|
441 unwind_protect::add (clear_param_list, ret_list); |
|
442 |
2974
|
443 // The following code is in a separate scope to avoid warnings from |
|
444 // G++ about `goto abort' crossing the initialization of some |
|
445 // variables. |
|
446 |
|
447 { |
3974
|
448 bind_automatic_vars (arg_names, nargin, nargout, octave_all_va_args ()); |
2974
|
449 |
|
450 bool echo_commands = (Vecho_executing_commands & ECHO_FUNCTIONS); |
|
451 |
|
452 if (echo_commands) |
|
453 print_code_function_header (); |
|
454 |
|
455 // Evaluate the commands that make up the function. |
|
456 |
3489
|
457 unwind_protect_bool (evaluating_function_body); |
|
458 evaluating_function_body = true; |
|
459 |
2974
|
460 octave_value_list tmp = cmd_list->eval (); |
|
461 |
|
462 octave_value last_computed_value; |
|
463 |
|
464 if (! tmp.empty ()) |
|
465 last_computed_value = tmp(0); |
|
466 |
|
467 if (echo_commands) |
|
468 print_code_function_trailer (); |
|
469 |
4207
|
470 if (tree_return_command::returning) |
|
471 tree_return_command::returning = 0; |
2974
|
472 |
4207
|
473 if (tree_break_command::breaking) |
|
474 tree_break_command::breaking--; |
2974
|
475 |
|
476 if (error_state) |
|
477 { |
|
478 traceback_error (); |
|
479 goto abort; |
|
480 } |
|
481 |
|
482 // Copy return values out. |
|
483 |
|
484 if (ret_list) |
3871
|
485 { |
4748
|
486 ret_list->initialize_undefined_elements (my_name, nargout, Matrix ()); |
3871
|
487 |
3974
|
488 if (has_varargout ()) |
|
489 varargout_to_vr_val (); |
|
490 |
3871
|
491 retval = ret_list->convert_to_const_vector (vr_list); |
|
492 } |
2974
|
493 } |
|
494 |
|
495 abort: |
4987
|
496 unwind_protect::run_frame ("user_func_eval"); |
2974
|
497 |
|
498 return retval; |
|
499 } |
|
500 |
|
501 void |
4346
|
502 octave_user_function::traceback_error (void) const |
2974
|
503 { |
|
504 if (error_state >= 0) |
|
505 error_state = -1; |
|
506 |
4748
|
507 if (my_name.empty ()) |
2974
|
508 { |
|
509 if (file_name.empty ()) |
|
510 ::error ("called from `?unknown?'"); |
|
511 else |
|
512 ::error ("called from file `%s'", file_name.c_str ()); |
|
513 } |
|
514 else |
|
515 { |
|
516 if (file_name.empty ()) |
4748
|
517 ::error ("called from `%s'", my_name.c_str ()); |
2974
|
518 else |
|
519 ::error ("called from `%s' in file `%s'", |
4748
|
520 my_name.c_str (), file_name.c_str ()); |
2974
|
521 } |
|
522 } |
|
523 |
|
524 void |
|
525 octave_user_function::accept (tree_walker& tw) |
|
526 { |
|
527 tw.visit_octave_user_function (*this); |
|
528 } |
|
529 |
|
530 void |
3933
|
531 octave_user_function::print_symtab_info (std::ostream& os) const |
|
532 { |
|
533 if (sym_tab) |
|
534 sym_tab->print_info (os); |
|
535 else |
4748
|
536 warning ("%s: no symbol table info!", my_name.c_str ()); |
3933
|
537 } |
|
538 |
|
539 void |
2974
|
540 octave_user_function::print_code_function_header (void) |
|
541 { |
|
542 tree_print_code tpc (octave_stdout, Vps4); |
|
543 |
|
544 tpc.visit_octave_user_function_header (*this); |
|
545 } |
|
546 |
|
547 void |
|
548 octave_user_function::print_code_function_trailer (void) |
|
549 { |
|
550 tree_print_code tpc (octave_stdout, Vps4); |
|
551 |
|
552 tpc.visit_octave_user_function_trailer (*this); |
|
553 } |
|
554 |
|
555 void |
|
556 octave_user_function::install_automatic_vars (void) |
|
557 { |
4645
|
558 if (sym_tab) |
|
559 { |
|
560 argn_sr = sym_tab->lookup ("argn", true); |
4700
|
561 nargin_sr = sym_tab->lookup ("__nargin__", true); |
|
562 nargout_sr = sym_tab->lookup ("__nargout__", true); |
3974
|
563 |
4645
|
564 if (takes_varargs ()) |
4929
|
565 varargin_sr = sym_tab->lookup ("varargin", true); |
4645
|
566 } |
2974
|
567 } |
|
568 |
|
569 void |
|
570 octave_user_function::bind_automatic_vars |
3974
|
571 (const string_vector& arg_names, int nargin, int nargout, |
|
572 const octave_value_list& va_args) |
2974
|
573 { |
|
574 if (! arg_names.empty ()) |
|
575 argn_sr->define (arg_names); |
|
576 |
4233
|
577 nargin_sr->define (nargin); |
|
578 nargout_sr->define (nargout); |
3974
|
579 |
|
580 if (takes_varargs ()) |
|
581 { |
|
582 int n = va_args.length (); |
|
583 |
|
584 Cell varargin (1, n); |
|
585 |
|
586 for (int i = 0; i < n; i++) |
|
587 varargin(0,i) = va_args(i); |
|
588 |
|
589 varargin_sr->define (varargin); |
|
590 } |
2974
|
591 } |
|
592 |
4700
|
593 DEFUN (nargin, args, , |
|
594 "-*- texinfo -*-\n\ |
|
595 @deftypefn {Built-in Function} {} nargin ()\n\ |
|
596 @deftypefnx {Built-in Function} {} nargin (@var{fcn_name})\n\ |
|
597 Within a function, return the number of arguments passed to the function.\n\ |
|
598 At the top level, return the number of command line arguments passed to\n\ |
|
599 Octave. If called with the optional argument @var{fcn_name}, return the\n\ |
|
600 maximum number of arguments the named function can accept, or -1 if the\n\ |
|
601 function accepts a variable number of arguments.\n\ |
|
602 @end deftypefn") |
|
603 { |
|
604 octave_value retval; |
|
605 |
|
606 int nargin = args.length (); |
|
607 |
|
608 if (nargin == 1) |
|
609 { |
|
610 std::string fname = args(0).string_value (); |
|
611 |
|
612 if (! error_state) |
|
613 { |
4930
|
614 octave_value fcn_val = lookup_user_function (fname); |
|
615 |
|
616 octave_user_function *fcn = fcn_val.user_function_value (true); |
4700
|
617 |
|
618 if (fcn) |
|
619 { |
|
620 if (fcn->takes_varargs ()) |
|
621 retval = -1; |
|
622 else |
|
623 { |
|
624 tree_parameter_list *param_list = fcn->parameter_list (); |
|
625 |
|
626 retval = param_list ? param_list->length () : 0; |
|
627 } |
|
628 } |
|
629 else |
|
630 error ("nargin: invalid function"); |
|
631 } |
|
632 else |
|
633 error ("nargin: expecting string as first argument"); |
|
634 } |
|
635 else if (nargin == 0) |
|
636 { |
|
637 symbol_record *sr = curr_sym_tab->lookup ("__nargin__"); |
|
638 |
|
639 retval = sr ? sr->def () : 0; |
|
640 } |
|
641 else |
|
642 print_usage ("nargin"); |
|
643 |
|
644 return retval; |
|
645 } |
|
646 |
|
647 DEFUN (nargout, args, , |
|
648 "-*- texinfo -*-\n\ |
|
649 @deftypefn {Built-in Function} {} nargout ()\n\ |
|
650 @deftypefnx {Built-in Function} {} nargout (@var{fcn_name})\n\ |
|
651 Within a function, return the number of values the caller expects to\n\ |
|
652 receive. If called with the optional argument @var{fcn_name}, return the\n\ |
|
653 maximum number of values the named function can produce, or -1 if the\n\ |
|
654 function can produce a variable number of values.\n\ |
|
655 \n\ |
|
656 For example,\n\ |
|
657 \n\ |
|
658 @example\n\ |
|
659 f ()\n\ |
|
660 @end example\n\ |
|
661 \n\ |
|
662 @noindent\n\ |
4704
|
663 will cause @code{nargout} to return 0 inside the function @code{f} and\n\ |
4700
|
664 \n\ |
|
665 @example\n\ |
|
666 [s, t] = f ()\n\ |
|
667 @end example\n\ |
|
668 \n\ |
|
669 @noindent\n\ |
|
670 will cause @code{nargout} to return 2 inside the function\n\ |
|
671 @code{f}.\n\ |
|
672 \n\ |
|
673 At the top level, @code{nargout} is undefined.\n\ |
|
674 @end deftypefn") |
|
675 { |
|
676 octave_value retval; |
|
677 |
|
678 int nargin = args.length (); |
|
679 |
|
680 if (nargin == 1) |
|
681 { |
|
682 std::string fname = args(0).string_value (); |
|
683 |
|
684 if (! error_state) |
|
685 { |
4930
|
686 octave_value fcn_val = lookup_user_function (fname); |
|
687 |
|
688 octave_user_function *fcn = fcn_val.user_function_value (true); |
4700
|
689 |
|
690 if (fcn) |
|
691 { |
|
692 if (fcn->takes_var_return ()) |
|
693 retval = -1; |
|
694 else |
|
695 { |
|
696 tree_parameter_list *ret_list = fcn->return_list (); |
|
697 |
|
698 retval = ret_list ? ret_list->length () : 0; |
|
699 } |
|
700 } |
|
701 else |
|
702 error ("nargout: invalid function"); |
|
703 } |
|
704 else |
|
705 error ("nargout: expecting string as first argument"); |
|
706 } |
|
707 else if (nargin == 0) |
|
708 { |
|
709 if (! at_top_level ()) |
|
710 { |
|
711 symbol_record *sr = curr_sym_tab->lookup ("__nargout__"); |
|
712 |
|
713 retval = sr ? sr->def () : 0; |
|
714 } |
|
715 else |
|
716 error ("nargout: invalid call at top level"); |
|
717 } |
|
718 else |
|
719 print_usage ("nargout"); |
|
720 |
|
721 return retval; |
|
722 } |
|
723 |
3743
|
724 DEFUNX ("va_arg", Fva_arg, args, , |
3445
|
725 "-*- texinfo -*-\n\ |
|
726 @deftypefn {Built-in Function} {} va_arg ()\n\ |
3745
|
727 Return the value of the next available argument and move the internal\n\ |
3445
|
728 pointer to the next argument. It is an error to call @code{va_arg}\n\ |
|
729 when ther eare no more arguments available, or in a function that\n\ |
|
730 has not been declared to take a variable number of parameters.\n\ |
|
731 @end deftypefn") |
2974
|
732 { |
4124
|
733 static bool warned = false; |
|
734 |
|
735 if (! warned) |
|
736 { |
|
737 ::warning ("va_arg is deprecated; use varargin instead"); |
|
738 warned = true; |
|
739 } |
3975
|
740 |
2974
|
741 octave_value_list retval; |
|
742 |
|
743 int nargin = args.length (); |
|
744 |
|
745 if (nargin == 0) |
|
746 { |
|
747 if (curr_function) |
|
748 { |
|
749 if (curr_function->takes_varargs ()) |
|
750 retval = curr_function->octave_va_arg (); |
|
751 else |
|
752 { |
|
753 ::error ("va_arg only valid within function taking variable"); |
|
754 ::error ("number of arguments"); |
|
755 } |
|
756 } |
|
757 else |
|
758 ::error ("va_arg only valid within function body"); |
|
759 } |
|
760 else |
|
761 print_usage ("va_arg"); |
|
762 |
|
763 return retval; |
|
764 } |
|
765 |
|
766 DEFUN (va_start, args, , |
3445
|
767 "-*- texinfo -*-\n\ |
|
768 @deftypefn {Built-in Function} {} va_start ()\n\ |
|
769 Position an internal pointer to the first unnamed argument in\n\ |
|
770 functions that have been declared to accept a variable number of\n\ |
|
771 arguments. It is an error to call @code{va_start} in a function\n\ |
|
772 that has not been declared to take a variable number of parameters.\n\ |
|
773 @end deftypefn") |
2974
|
774 { |
4124
|
775 static bool warned = false; |
|
776 |
|
777 if (! warned) |
|
778 { |
|
779 ::warning ("va_start is deprecated; use varargin instead"); |
|
780 warned = true; |
|
781 } |
3975
|
782 |
2974
|
783 octave_value_list retval; |
|
784 |
|
785 int nargin = args.length (); |
|
786 |
|
787 if (nargin == 0) |
|
788 { |
|
789 if (curr_function) |
|
790 { |
|
791 if (curr_function->takes_varargs ()) |
|
792 curr_function->octave_va_start (); |
|
793 else |
|
794 { |
|
795 ::error ("va_start only valid within function taking variable"); |
|
796 ::error ("number of arguments"); |
|
797 } |
|
798 } |
|
799 else |
|
800 ::error ("va_start only valid within function body"); |
|
801 } |
|
802 else |
|
803 print_usage ("va_start"); |
|
804 |
|
805 return retval; |
|
806 } |
|
807 |
|
808 DEFUN (vr_val, args, , |
3445
|
809 "-*- texinfo -*-\n\ |
|
810 @deftypefn {Built-in Function} {} vr_val (@var{x})\n\ |
|
811 Each time this function is called, it places the value of its argument\n\ |
|
812 at the end of the list of values to return from the current\n\ |
|
813 function. Once @code{vr_val} has been called, there is no way to go\n\ |
|
814 back to the beginning of the list and rewrite any of the return\n\ |
|
815 values. This function may only be called within functions that have\n\ |
|
816 been declared to return an unspecified number of output arguments.\n\ |
|
817 @end deftypefn") |
2974
|
818 { |
4124
|
819 static bool warned = false; |
|
820 |
|
821 if (! warned) |
|
822 { |
|
823 ::warning ("vr_val is deprecated; use varargout instead"); |
|
824 warned = true; |
|
825 } |
3975
|
826 |
2974
|
827 octave_value_list retval; |
|
828 |
|
829 int nargin = args.length (); |
|
830 |
|
831 if (nargin == 1) |
|
832 { |
|
833 if (curr_function) |
|
834 { |
3974
|
835 if (curr_function->has_varargout ()) |
|
836 ::error ("vr_val and varargout cannot both be used in the same function"); |
|
837 else if (curr_function->takes_var_return ()) |
2974
|
838 curr_function->octave_vr_val (args(0)); |
|
839 else |
|
840 { |
|
841 ::error ("vr_val only valid within function declared to"); |
|
842 ::error ("produce a variable number of values"); |
|
843 } |
|
844 } |
|
845 else |
|
846 ::error ("vr_val only valid within function body"); |
|
847 } |
|
848 else |
|
849 print_usage ("vr_val"); |
|
850 |
|
851 return retval; |
|
852 } |
|
853 |
|
854 static int |
3131
|
855 max_recursion_depth (void) |
|
856 { |
|
857 Vmax_recursion_depth = check_preference ("max_recursion_depth"); |
|
858 |
|
859 return 0; |
|
860 } |
|
861 |
2974
|
862 void |
|
863 symbols_of_ov_usr_fcn (void) |
|
864 { |
3258
|
865 DEFVAR (max_recursion_depth, 256.0, max_recursion_depth, |
3371
|
866 "-*- texinfo -*-\n\ |
|
867 @defvr {Built-in Variable} max_recursion_depth\n\ |
|
868 Limit the number of times a function may be called recursively.\n\ |
|
869 If the limit is exceeded, an error message is printed and control\n\ |
|
870 returns to the top level.\n\ |
|
871 \n\ |
|
872 The default value is 256.\n\ |
|
873 @end defvr"); |
2974
|
874 } |
|
875 |
|
876 /* |
|
877 ;;; Local Variables: *** |
|
878 ;;; mode: C++ *** |
|
879 ;;; End: *** |
|
880 */ |