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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
2974
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include "str-vec.h" |
|
29 |
|
30 #include <defaults.h> |
3974
|
31 #include "Cell.h" |
2974
|
32 #include "defun.h" |
|
33 #include "error.h" |
|
34 #include "input.h" |
|
35 #include "oct-obj.h" |
|
36 #include "ov-usr-fcn.h" |
|
37 #include "ov.h" |
|
38 #include "pager.h" |
2985
|
39 #include "pt-jump.h" |
2974
|
40 #include "pt-misc.h" |
|
41 #include "pt-pr-code.h" |
2982
|
42 #include "pt-stmt.h" |
2974
|
43 #include "pt-walk.h" |
|
44 #include "symtab.h" |
|
45 #include "toplev.h" |
|
46 #include "unwind-prot.h" |
|
47 #include "utils.h" |
3489
|
48 #include "parse.h" |
2974
|
49 #include "variables.h" |
|
50 |
3131
|
51 // Maximum nesting level for functions called recursively. |
|
52 static int Vmax_recursion_depth; |
|
53 |
2974
|
54 // User defined functions. |
|
55 |
3219
|
56 DEFINE_OCTAVE_ALLOCATOR (octave_user_function); |
2974
|
57 |
3219
|
58 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_user_function, |
4612
|
59 "user-defined function", |
3219
|
60 "user-defined function"); |
2974
|
61 |
|
62 // Ugh. This really needs to be simplified (code/data? |
|
63 // extrinsic/intrinsic state?). |
|
64 |
|
65 octave_user_function::octave_user_function |
|
66 (tree_parameter_list *pl, tree_parameter_list *rl, |
|
67 tree_statement_list *cl, symbol_table *st) |
3523
|
68 : octave_function (std::string (), std::string ()), |
2974
|
69 param_list (pl), ret_list (rl), cmd_list (cl), |
4748
|
70 sym_tab (st), lead_comm (), trail_comm (), file_name (), |
3255
|
71 t_parsed (static_cast<time_t> (0)), |
|
72 t_checked (static_cast<time_t> (0)), |
|
73 system_fcn_file (false), call_depth (0), |
4238
|
74 num_named_args (0), nested_function (false), |
|
75 args_passed (), num_args_passed (0), |
3165
|
76 curr_va_arg_number (0), vr_list (0), symtab_entry (0), |
3974
|
77 argn_sr (0), nargin_sr (0), nargout_sr (0), varargin_sr (0) |
2974
|
78 { |
|
79 if (param_list) |
|
80 { |
|
81 num_named_args = param_list->length (); |
|
82 curr_va_arg_number = num_named_args; |
|
83 } |
|
84 } |
|
85 |
|
86 octave_user_function::~octave_user_function (void) |
|
87 { |
|
88 delete param_list; |
|
89 delete ret_list; |
|
90 delete sym_tab; |
|
91 delete cmd_list; |
|
92 delete vr_list; |
3665
|
93 delete lead_comm; |
|
94 delete trail_comm; |
2974
|
95 } |
|
96 |
|
97 octave_user_function * |
|
98 octave_user_function::define_ret_list (tree_parameter_list *t) |
|
99 { |
|
100 ret_list = t; |
|
101 |
|
102 if (ret_list && ret_list->takes_varargs ()) |
|
103 vr_list = new tree_va_return_list; |
|
104 |
|
105 return this; |
|
106 } |
|
107 |
|
108 void |
4343
|
109 octave_user_function::stash_fcn_file_name (const std::string& nm) |
2974
|
110 { |
4343
|
111 file_name = nm; |
2974
|
112 } |
|
113 |
|
114 void |
|
115 octave_user_function::mark_as_system_fcn_file (void) |
|
116 { |
|
117 if (! file_name.empty ()) |
|
118 { |
|
119 // We really should stash the whole path to the file we found, |
|
120 // when we looked it up, to avoid possible race conditions... |
|
121 // XXX FIXME XXX |
|
122 // |
|
123 // We probably also don't need to get the library directory |
|
124 // every time, but since this function is only called when the |
|
125 // function file is parsed, it probably doesn't matter that |
|
126 // much. |
|
127 |
3523
|
128 std::string ff_name = fcn_file_in_path (file_name); |
2974
|
129 |
3565
|
130 if (Vfcn_file_dir == ff_name.substr (0, Vfcn_file_dir.length ())) |
2974
|
131 system_fcn_file = 1; |
|
132 } |
|
133 else |
|
134 system_fcn_file = 0; |
|
135 } |
|
136 |
|
137 bool |
|
138 octave_user_function::takes_varargs (void) const |
|
139 { |
|
140 return (param_list && param_list->takes_varargs ()); |
|
141 } |
|
142 |
|
143 octave_value |
|
144 octave_user_function::octave_va_arg (void) |
|
145 { |
|
146 octave_value retval; |
|
147 |
|
148 if (curr_va_arg_number < num_args_passed) |
|
149 retval = args_passed (curr_va_arg_number++); |
|
150 else |
|
151 ::error ("va_arg: error getting arg number %d -- only %d provided", |
|
152 curr_va_arg_number + 1, num_args_passed); |
|
153 |
|
154 return retval; |
|
155 } |
|
156 |
|
157 octave_value_list |
|
158 octave_user_function::octave_all_va_args (void) |
|
159 { |
|
160 octave_value_list retval; |
|
161 |
3178
|
162 int n = num_args_passed - num_named_args; |
2974
|
163 |
3178
|
164 if (n > 0) |
|
165 { |
|
166 retval.resize (n); |
|
167 |
|
168 int k = 0; |
|
169 for (int i = num_named_args; i < num_args_passed; i++) |
|
170 retval(k++) = args_passed(i); |
|
171 } |
2974
|
172 |
|
173 return retval; |
|
174 } |
|
175 |
|
176 bool |
|
177 octave_user_function::takes_var_return (void) const |
|
178 { |
|
179 return (ret_list && ret_list->takes_varargs ()); |
|
180 } |
|
181 |
|
182 void |
|
183 octave_user_function::octave_vr_val (const octave_value& val) |
|
184 { |
4280
|
185 // Use != here to avoid possible conversion to int of smaller type |
|
186 // than the vr_list pointer. |
|
187 |
|
188 assert (vr_list != 0); |
2974
|
189 |
|
190 vr_list->append (val); |
|
191 } |
|
192 |
|
193 void |
3974
|
194 octave_user_function::varargout_to_vr_val (void) |
|
195 { |
|
196 assert (vr_list && vr_list->empty ()); |
|
197 |
|
198 symbol_record *sr = sym_tab->lookup ("varargout"); |
|
199 |
|
200 if (sr && sr->is_variable ()) |
|
201 { |
|
202 octave_value v = sr->def (); |
|
203 |
|
204 Cell c = v.cell_value (); |
|
205 |
|
206 if (! error_state) |
|
207 { |
|
208 // XXX FIXME XXX -- should varargout be required to be a |
|
209 // cell array with a single row or column? If not, should |
|
210 // we have a cleaner way of doing this operation? |
|
211 |
|
212 int n = c.length (); |
|
213 |
|
214 const octave_value *d = c.data (); |
|
215 |
|
216 for (int i = 0; i < n; i++) |
|
217 vr_list->append (d[i]); |
|
218 } |
|
219 else |
|
220 error ("expecting varargout to be a cell array object"); |
|
221 } |
|
222 } |
|
223 |
|
224 bool |
|
225 octave_user_function::has_varargout (void) const |
|
226 { |
|
227 bool retval = false; |
|
228 |
|
229 if (takes_var_return ()) |
|
230 { |
|
231 symbol_record *sr = sym_tab->lookup ("varargout"); |
|
232 |
|
233 retval = (sr && sr->is_variable ()); |
|
234 } |
|
235 |
|
236 return retval; |
|
237 } |
|
238 |
2974
|
239 // For unwind protect. |
|
240 |
|
241 static void |
|
242 pop_symbol_table_context (void *table) |
|
243 { |
|
244 symbol_table *tmp = static_cast<symbol_table *> (table); |
|
245 tmp->pop_context (); |
|
246 } |
|
247 |
|
248 static void |
|
249 delete_vr_list (void *list) |
|
250 { |
|
251 tree_va_return_list *tmp = static_cast<tree_va_return_list *> (list); |
|
252 tmp->clear (); |
|
253 delete tmp; |
|
254 } |
|
255 |
|
256 static void |
|
257 clear_symbol_table (void *table) |
|
258 { |
|
259 symbol_table *tmp = static_cast<symbol_table *> (table); |
4239
|
260 tmp->clear (); |
2974
|
261 } |
|
262 |
|
263 static void |
3239
|
264 clear_param_list (void *lst) |
|
265 { |
|
266 tree_parameter_list *tmp = static_cast<tree_parameter_list *> (lst); |
|
267 |
|
268 if (tmp) |
4219
|
269 tmp->undefine (); |
3239
|
270 } |
|
271 |
|
272 static void |
3875
|
273 restore_args_passed (void *fcn) |
3239
|
274 { |
|
275 octave_user_function *tmp = static_cast<octave_user_function *> (fcn); |
|
276 |
|
277 if (tmp) |
3875
|
278 tmp->restore_args_passed (); |
3239
|
279 } |
|
280 |
|
281 static void |
2974
|
282 unprotect_function (void *sr_arg) |
|
283 { |
|
284 symbol_record *sr = static_cast<symbol_record *> (sr_arg); |
|
285 sr->unprotect (); |
|
286 } |
|
287 |
|
288 octave_value_list |
4247
|
289 octave_user_function::subsref (const std::string& type, |
4219
|
290 const std::list<octave_value_list>& idx, |
3933
|
291 int nargout) |
|
292 { |
|
293 octave_value_list retval; |
|
294 |
|
295 switch (type[0]) |
|
296 { |
|
297 case '(': |
5154
|
298 { |
5155
|
299 int tmp_nargout = (type.length () > 1 && nargout == 0) ? 1 : nargout; |
5154
|
300 |
|
301 retval = do_multi_index_op (tmp_nargout, idx.front ()); |
|
302 } |
3933
|
303 break; |
|
304 |
|
305 case '{': |
|
306 case '.': |
|
307 { |
|
308 std::string nm = type_name (); |
|
309 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
310 } |
|
311 break; |
|
312 |
|
313 default: |
|
314 panic_impossible (); |
|
315 } |
|
316 |
4059
|
317 // XXX FIXME XXX -- perhaps there should be an |
|
318 // octave_value_list::next_subsref member function? See also |
|
319 // octave_builtin::subsref. |
3933
|
320 |
4219
|
321 if (idx.size () > 1) |
4994
|
322 retval = retval(0).next_subsref (nargout, type, idx); |
4059
|
323 |
|
324 return retval; |
3933
|
325 } |
|
326 |
|
327 octave_value_list |
3544
|
328 octave_user_function::do_multi_index_op (int nargout, |
|
329 const octave_value_list& args) |
2974
|
330 { |
|
331 octave_value_list retval; |
|
332 |
|
333 if (error_state) |
|
334 return retval; |
|
335 |
|
336 if (! cmd_list) |
|
337 return retval; |
|
338 |
|
339 int nargin = args.length (); |
|
340 |
4987
|
341 unwind_protect::begin_frame ("user_func_eval"); |
2974
|
342 |
|
343 unwind_protect_int (call_depth); |
|
344 call_depth++; |
|
345 |
3131
|
346 if (call_depth > Vmax_recursion_depth) |
|
347 { |
|
348 ::error ("max_recursion_limit exceeded"); |
4987
|
349 unwind_protect::run_frame ("user_func_eval"); |
3131
|
350 return retval; |
|
351 } |
|
352 |
2974
|
353 if (symtab_entry && ! symtab_entry->is_read_only ()) |
|
354 { |
|
355 symtab_entry->protect (); |
2985
|
356 unwind_protect::add (unprotect_function, symtab_entry); |
2974
|
357 } |
|
358 |
|
359 if (call_depth > 1) |
|
360 { |
|
361 sym_tab->push_context (); |
2985
|
362 unwind_protect::add (pop_symbol_table_context, sym_tab); |
2974
|
363 |
|
364 if (vr_list) |
|
365 { |
|
366 // Push new vr_list. |
|
367 |
|
368 unwind_protect_ptr (vr_list); |
|
369 vr_list = new tree_va_return_list; |
|
370 |
|
371 // Clear and delete the new one before restoring the old |
|
372 // one. |
|
373 |
2985
|
374 unwind_protect::add (delete_vr_list, vr_list); |
2974
|
375 } |
|
376 } |
|
377 |
|
378 if (vr_list) |
|
379 vr_list->clear (); |
|
380 |
4929
|
381 install_automatic_vars (); |
|
382 |
2974
|
383 // Force symbols to be undefined again when this function exits. |
|
384 |
2985
|
385 unwind_protect::add (clear_symbol_table, sym_tab); |
2974
|
386 |
|
387 // Save old and set current symbol table context, for |
|
388 // eval_undefined_error(). |
|
389 |
4245
|
390 unwind_protect_ptr (curr_caller_sym_tab); |
|
391 curr_caller_sym_tab = curr_sym_tab; |
|
392 |
2974
|
393 unwind_protect_ptr (curr_sym_tab); |
|
394 curr_sym_tab = sym_tab; |
|
395 |
4975
|
396 unwind_protect_ptr (curr_caller_statement); |
5743
|
397 curr_caller_statement = curr_statement; |
4975
|
398 |
5743
|
399 octave_call_stack::push (this); |
|
400 |
|
401 unwind_protect::add (octave_call_stack::unwind_pop, 0); |
2974
|
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\ |
5642
|
602 @seealso{nargout, varargin, varargout}\n\ |
4700
|
603 @end deftypefn") |
|
604 { |
|
605 octave_value retval; |
|
606 |
|
607 int nargin = args.length (); |
|
608 |
|
609 if (nargin == 1) |
|
610 { |
|
611 std::string fname = args(0).string_value (); |
|
612 |
|
613 if (! error_state) |
|
614 { |
4930
|
615 octave_value fcn_val = lookup_user_function (fname); |
|
616 |
|
617 octave_user_function *fcn = fcn_val.user_function_value (true); |
4700
|
618 |
|
619 if (fcn) |
|
620 { |
|
621 if (fcn->takes_varargs ()) |
|
622 retval = -1; |
|
623 else |
|
624 { |
|
625 tree_parameter_list *param_list = fcn->parameter_list (); |
|
626 |
|
627 retval = param_list ? param_list->length () : 0; |
|
628 } |
|
629 } |
|
630 else |
|
631 error ("nargin: invalid function"); |
|
632 } |
|
633 else |
|
634 error ("nargin: expecting string as first argument"); |
|
635 } |
|
636 else if (nargin == 0) |
|
637 { |
|
638 symbol_record *sr = curr_sym_tab->lookup ("__nargin__"); |
|
639 |
|
640 retval = sr ? sr->def () : 0; |
|
641 } |
|
642 else |
|
643 print_usage ("nargin"); |
|
644 |
|
645 return retval; |
|
646 } |
|
647 |
|
648 DEFUN (nargout, args, , |
|
649 "-*- texinfo -*-\n\ |
|
650 @deftypefn {Built-in Function} {} nargout ()\n\ |
|
651 @deftypefnx {Built-in Function} {} nargout (@var{fcn_name})\n\ |
|
652 Within a function, return the number of values the caller expects to\n\ |
|
653 receive. If called with the optional argument @var{fcn_name}, return the\n\ |
|
654 maximum number of values the named function can produce, or -1 if the\n\ |
|
655 function can produce a variable number of values.\n\ |
|
656 \n\ |
|
657 For example,\n\ |
|
658 \n\ |
|
659 @example\n\ |
|
660 f ()\n\ |
|
661 @end example\n\ |
|
662 \n\ |
|
663 @noindent\n\ |
4704
|
664 will cause @code{nargout} to return 0 inside the function @code{f} and\n\ |
4700
|
665 \n\ |
|
666 @example\n\ |
|
667 [s, t] = f ()\n\ |
|
668 @end example\n\ |
|
669 \n\ |
|
670 @noindent\n\ |
|
671 will cause @code{nargout} to return 2 inside the function\n\ |
|
672 @code{f}.\n\ |
|
673 \n\ |
|
674 At the top level, @code{nargout} is undefined.\n\ |
5642
|
675 @seealso{nargin, varargin, varargout}\n\ |
4700
|
676 @end deftypefn") |
|
677 { |
|
678 octave_value retval; |
|
679 |
|
680 int nargin = args.length (); |
|
681 |
|
682 if (nargin == 1) |
|
683 { |
|
684 std::string fname = args(0).string_value (); |
|
685 |
|
686 if (! error_state) |
|
687 { |
4930
|
688 octave_value fcn_val = lookup_user_function (fname); |
|
689 |
|
690 octave_user_function *fcn = fcn_val.user_function_value (true); |
4700
|
691 |
|
692 if (fcn) |
|
693 { |
|
694 if (fcn->takes_var_return ()) |
|
695 retval = -1; |
|
696 else |
|
697 { |
|
698 tree_parameter_list *ret_list = fcn->return_list (); |
|
699 |
|
700 retval = ret_list ? ret_list->length () : 0; |
|
701 } |
|
702 } |
|
703 else |
|
704 error ("nargout: invalid function"); |
|
705 } |
|
706 else |
|
707 error ("nargout: expecting string as first argument"); |
|
708 } |
|
709 else if (nargin == 0) |
|
710 { |
|
711 if (! at_top_level ()) |
|
712 { |
|
713 symbol_record *sr = curr_sym_tab->lookup ("__nargout__"); |
|
714 |
|
715 retval = sr ? sr->def () : 0; |
|
716 } |
|
717 else |
|
718 error ("nargout: invalid call at top level"); |
|
719 } |
|
720 else |
|
721 print_usage ("nargout"); |
|
722 |
|
723 return retval; |
|
724 } |
|
725 |
3743
|
726 DEFUNX ("va_arg", Fva_arg, args, , |
3445
|
727 "-*- texinfo -*-\n\ |
|
728 @deftypefn {Built-in Function} {} va_arg ()\n\ |
3745
|
729 Return the value of the next available argument and move the internal\n\ |
3445
|
730 pointer to the next argument. It is an error to call @code{va_arg}\n\ |
|
731 when ther eare no more arguments available, or in a function that\n\ |
|
732 has not been declared to take a variable number of parameters.\n\ |
|
733 @end deftypefn") |
2974
|
734 { |
4124
|
735 static bool warned = false; |
|
736 |
|
737 if (! warned) |
|
738 { |
|
739 ::warning ("va_arg is deprecated; use varargin instead"); |
|
740 warned = true; |
|
741 } |
3975
|
742 |
2974
|
743 octave_value_list retval; |
|
744 |
|
745 int nargin = args.length (); |
|
746 |
|
747 if (nargin == 0) |
|
748 { |
5743
|
749 octave_function *fcn = octave_call_stack::caller_script (); |
|
750 |
|
751 if (fcn) |
2974
|
752 { |
5743
|
753 if (fcn->takes_varargs ()) |
|
754 retval = fcn->octave_va_arg (); |
2974
|
755 else |
|
756 { |
|
757 ::error ("va_arg only valid within function taking variable"); |
|
758 ::error ("number of arguments"); |
|
759 } |
|
760 } |
|
761 else |
|
762 ::error ("va_arg only valid within function body"); |
|
763 } |
|
764 else |
|
765 print_usage ("va_arg"); |
|
766 |
|
767 return retval; |
|
768 } |
|
769 |
|
770 DEFUN (va_start, args, , |
3445
|
771 "-*- texinfo -*-\n\ |
|
772 @deftypefn {Built-in Function} {} va_start ()\n\ |
|
773 Position an internal pointer to the first unnamed argument in\n\ |
|
774 functions that have been declared to accept a variable number of\n\ |
|
775 arguments. It is an error to call @code{va_start} in a function\n\ |
|
776 that has not been declared to take a variable number of parameters.\n\ |
|
777 @end deftypefn") |
2974
|
778 { |
4124
|
779 static bool warned = false; |
|
780 |
|
781 if (! warned) |
|
782 { |
|
783 ::warning ("va_start is deprecated; use varargin instead"); |
|
784 warned = true; |
|
785 } |
3975
|
786 |
2974
|
787 octave_value_list retval; |
|
788 |
|
789 int nargin = args.length (); |
|
790 |
|
791 if (nargin == 0) |
|
792 { |
5743
|
793 octave_function *fcn = octave_call_stack::caller_script (); |
|
794 |
|
795 if (fcn) |
2974
|
796 { |
5743
|
797 if (fcn->takes_varargs ()) |
|
798 fcn->octave_va_start (); |
2974
|
799 else |
|
800 { |
|
801 ::error ("va_start only valid within function taking variable"); |
|
802 ::error ("number of arguments"); |
|
803 } |
|
804 } |
|
805 else |
|
806 ::error ("va_start only valid within function body"); |
|
807 } |
|
808 else |
|
809 print_usage ("va_start"); |
|
810 |
|
811 return retval; |
|
812 } |
|
813 |
|
814 DEFUN (vr_val, args, , |
3445
|
815 "-*- texinfo -*-\n\ |
|
816 @deftypefn {Built-in Function} {} vr_val (@var{x})\n\ |
|
817 Each time this function is called, it places the value of its argument\n\ |
|
818 at the end of the list of values to return from the current\n\ |
|
819 function. Once @code{vr_val} has been called, there is no way to go\n\ |
|
820 back to the beginning of the list and rewrite any of the return\n\ |
|
821 values. This function may only be called within functions that have\n\ |
|
822 been declared to return an unspecified number of output arguments.\n\ |
|
823 @end deftypefn") |
2974
|
824 { |
4124
|
825 static bool warned = false; |
|
826 |
|
827 if (! warned) |
|
828 { |
|
829 ::warning ("vr_val is deprecated; use varargout instead"); |
|
830 warned = true; |
|
831 } |
3975
|
832 |
2974
|
833 octave_value_list retval; |
|
834 |
|
835 int nargin = args.length (); |
|
836 |
|
837 if (nargin == 1) |
|
838 { |
5743
|
839 octave_function *fcn = octave_call_stack::caller_script (); |
|
840 |
|
841 if (fcn) |
2974
|
842 { |
5743
|
843 if (fcn->has_varargout ()) |
3974
|
844 ::error ("vr_val and varargout cannot both be used in the same function"); |
5743
|
845 else if (fcn->takes_var_return ()) |
|
846 fcn->octave_vr_val (args(0)); |
2974
|
847 else |
|
848 { |
|
849 ::error ("vr_val only valid within function declared to"); |
|
850 ::error ("produce a variable number of values"); |
|
851 } |
|
852 } |
|
853 else |
|
854 ::error ("vr_val only valid within function body"); |
|
855 } |
|
856 else |
|
857 print_usage ("vr_val"); |
|
858 |
|
859 return retval; |
|
860 } |
|
861 |
|
862 static int |
3131
|
863 max_recursion_depth (void) |
|
864 { |
|
865 Vmax_recursion_depth = check_preference ("max_recursion_depth"); |
|
866 |
|
867 return 0; |
|
868 } |
|
869 |
2974
|
870 void |
|
871 symbols_of_ov_usr_fcn (void) |
|
872 { |
3258
|
873 DEFVAR (max_recursion_depth, 256.0, max_recursion_depth, |
3371
|
874 "-*- texinfo -*-\n\ |
|
875 @defvr {Built-in Variable} max_recursion_depth\n\ |
|
876 Limit the number of times a function may be called recursively.\n\ |
|
877 If the limit is exceeded, an error message is printed and control\n\ |
|
878 returns to the top level.\n\ |
|
879 \n\ |
|
880 The default value is 256.\n\ |
|
881 @end defvr"); |
2974
|
882 } |
|
883 |
|
884 /* |
|
885 ;;; Local Variables: *** |
|
886 ;;; mode: C++ *** |
|
887 ;;; End: *** |
|
888 */ |