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