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