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