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 { |
|
201 assert (vr_list); |
|
202 |
|
203 vr_list->append (val); |
|
204 } |
|
205 |
|
206 void |
3974
|
207 octave_user_function::varargout_to_vr_val (void) |
|
208 { |
|
209 assert (vr_list && vr_list->empty ()); |
|
210 |
|
211 symbol_record *sr = sym_tab->lookup ("varargout"); |
|
212 |
|
213 if (sr && sr->is_variable ()) |
|
214 { |
|
215 octave_value v = sr->def (); |
|
216 |
|
217 Cell c = v.cell_value (); |
|
218 |
|
219 if (! error_state) |
|
220 { |
|
221 // XXX FIXME XXX -- should varargout be required to be a |
|
222 // cell array with a single row or column? If not, should |
|
223 // we have a cleaner way of doing this operation? |
|
224 |
|
225 int n = c.length (); |
|
226 |
|
227 const octave_value *d = c.data (); |
|
228 |
|
229 for (int i = 0; i < n; i++) |
|
230 vr_list->append (d[i]); |
|
231 } |
|
232 else |
|
233 error ("expecting varargout to be a cell array object"); |
|
234 } |
|
235 } |
|
236 |
|
237 bool |
|
238 octave_user_function::has_varargout (void) const |
|
239 { |
|
240 bool retval = false; |
|
241 |
|
242 if (takes_var_return ()) |
|
243 { |
|
244 symbol_record *sr = sym_tab->lookup ("varargout"); |
|
245 |
|
246 retval = (sr && sr->is_variable ()); |
|
247 } |
|
248 |
|
249 return retval; |
|
250 } |
|
251 |
|
252 void |
3523
|
253 octave_user_function::stash_function_name (const std::string& s) |
2974
|
254 { |
|
255 fcn_name = s; |
|
256 } |
|
257 |
|
258 // For unwind protect. |
|
259 |
|
260 static void |
|
261 pop_symbol_table_context (void *table) |
|
262 { |
|
263 symbol_table *tmp = static_cast<symbol_table *> (table); |
|
264 tmp->pop_context (); |
|
265 } |
|
266 |
|
267 static void |
|
268 delete_vr_list (void *list) |
|
269 { |
|
270 tree_va_return_list *tmp = static_cast<tree_va_return_list *> (list); |
|
271 tmp->clear (); |
|
272 delete tmp; |
|
273 } |
|
274 |
|
275 static void |
|
276 clear_symbol_table (void *table) |
|
277 { |
|
278 symbol_table *tmp = static_cast<symbol_table *> (table); |
4239
|
279 tmp->clear (); |
2974
|
280 } |
|
281 |
|
282 static void |
3239
|
283 clear_param_list (void *lst) |
|
284 { |
|
285 tree_parameter_list *tmp = static_cast<tree_parameter_list *> (lst); |
|
286 |
|
287 if (tmp) |
4219
|
288 tmp->undefine (); |
3239
|
289 } |
|
290 |
|
291 static void |
3875
|
292 restore_args_passed (void *fcn) |
3239
|
293 { |
|
294 octave_user_function *tmp = static_cast<octave_user_function *> (fcn); |
|
295 |
|
296 if (tmp) |
3875
|
297 tmp->restore_args_passed (); |
3239
|
298 } |
|
299 |
|
300 static void |
2974
|
301 unprotect_function (void *sr_arg) |
|
302 { |
|
303 symbol_record *sr = static_cast<symbol_record *> (sr_arg); |
|
304 sr->unprotect (); |
|
305 } |
|
306 |
|
307 octave_value_list |
3933
|
308 octave_user_function::subsref (const std::string type, |
4219
|
309 const std::list<octave_value_list>& idx, |
3933
|
310 int nargout) |
|
311 { |
|
312 octave_value_list retval; |
|
313 |
|
314 switch (type[0]) |
|
315 { |
|
316 case '(': |
|
317 retval = do_multi_index_op (nargout, idx.front ()); |
|
318 break; |
|
319 |
|
320 case '{': |
|
321 case '.': |
|
322 { |
|
323 std::string nm = type_name (); |
|
324 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
325 } |
|
326 break; |
|
327 |
|
328 default: |
|
329 panic_impossible (); |
|
330 } |
|
331 |
4059
|
332 // XXX FIXME XXX -- perhaps there should be an |
|
333 // octave_value_list::next_subsref member function? See also |
|
334 // octave_builtin::subsref. |
3933
|
335 |
4219
|
336 if (idx.size () > 1) |
4059
|
337 retval = retval(0).next_subsref (type, idx); |
|
338 |
|
339 return retval; |
3933
|
340 } |
|
341 |
|
342 octave_value_list |
3544
|
343 octave_user_function::do_multi_index_op (int nargout, |
|
344 const octave_value_list& args) |
2974
|
345 { |
|
346 octave_value_list retval; |
|
347 |
|
348 if (error_state) |
|
349 return retval; |
|
350 |
|
351 if (! cmd_list) |
|
352 return retval; |
|
353 |
|
354 int nargin = args.length (); |
|
355 |
2985
|
356 unwind_protect::begin_frame ("func_eval"); |
2974
|
357 |
|
358 unwind_protect_int (call_depth); |
|
359 call_depth++; |
|
360 |
3131
|
361 if (call_depth > Vmax_recursion_depth) |
|
362 { |
|
363 ::error ("max_recursion_limit exceeded"); |
3321
|
364 unwind_protect::run_frame ("func_eval"); |
3131
|
365 return retval; |
|
366 } |
|
367 |
2974
|
368 if (symtab_entry && ! symtab_entry->is_read_only ()) |
|
369 { |
|
370 symtab_entry->protect (); |
2985
|
371 unwind_protect::add (unprotect_function, symtab_entry); |
2974
|
372 } |
|
373 |
|
374 if (call_depth > 1) |
|
375 { |
|
376 sym_tab->push_context (); |
2985
|
377 unwind_protect::add (pop_symbol_table_context, sym_tab); |
2974
|
378 |
|
379 if (vr_list) |
|
380 { |
|
381 // Push new vr_list. |
|
382 |
|
383 unwind_protect_ptr (vr_list); |
|
384 vr_list = new tree_va_return_list; |
|
385 |
|
386 // Clear and delete the new one before restoring the old |
|
387 // one. |
|
388 |
2985
|
389 unwind_protect::add (delete_vr_list, vr_list); |
2974
|
390 } |
|
391 } |
|
392 |
|
393 if (vr_list) |
|
394 vr_list->clear (); |
|
395 |
|
396 // Force symbols to be undefined again when this function exits. |
|
397 |
2985
|
398 unwind_protect::add (clear_symbol_table, sym_tab); |
2974
|
399 |
|
400 // Save old and set current symbol table context, for |
|
401 // eval_undefined_error(). |
|
402 |
|
403 unwind_protect_ptr (curr_sym_tab); |
|
404 curr_sym_tab = sym_tab; |
|
405 |
|
406 unwind_protect_ptr (curr_function); |
|
407 curr_function = this; |
|
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 { |
|
492 if (Vdefine_all_return_values) |
|
493 { |
|
494 octave_value tmp = builtin_any_variable ("default_return_value"); |
|
495 |
|
496 if (tmp.is_defined ()) |
|
497 ret_list->initialize_undefined_elements (tmp); |
|
498 } |
|
499 |
3974
|
500 if (has_varargout ()) |
|
501 varargout_to_vr_val (); |
|
502 |
3871
|
503 retval = ret_list->convert_to_const_vector (vr_list); |
|
504 } |
2974
|
505 else if (Vreturn_last_computed_value) |
|
506 retval(0) = last_computed_value; |
|
507 } |
|
508 |
|
509 abort: |
2985
|
510 unwind_protect::run_frame ("func_eval"); |
2974
|
511 |
|
512 return retval; |
|
513 } |
|
514 |
|
515 void |
|
516 octave_user_function::traceback_error (void) |
|
517 { |
|
518 if (error_state >= 0) |
|
519 error_state = -1; |
|
520 |
|
521 if (fcn_name.empty ()) |
|
522 { |
|
523 if (file_name.empty ()) |
|
524 ::error ("called from `?unknown?'"); |
|
525 else |
|
526 ::error ("called from file `%s'", file_name.c_str ()); |
|
527 } |
|
528 else |
|
529 { |
|
530 if (file_name.empty ()) |
|
531 ::error ("called from `%s'", fcn_name.c_str ()); |
|
532 else |
|
533 ::error ("called from `%s' in file `%s'", |
|
534 fcn_name.c_str (), file_name.c_str ()); |
|
535 } |
|
536 } |
|
537 |
|
538 void |
|
539 octave_user_function::accept (tree_walker& tw) |
|
540 { |
|
541 tw.visit_octave_user_function (*this); |
|
542 } |
|
543 |
|
544 void |
3933
|
545 octave_user_function::print_symtab_info (std::ostream& os) const |
|
546 { |
|
547 if (sym_tab) |
|
548 sym_tab->print_info (os); |
|
549 else |
|
550 warning ("%s: no symbol table info!", fcn_name.c_str ()); |
|
551 } |
|
552 |
|
553 void |
2974
|
554 octave_user_function::print_code_function_header (void) |
|
555 { |
|
556 tree_print_code tpc (octave_stdout, Vps4); |
|
557 |
|
558 tpc.visit_octave_user_function_header (*this); |
|
559 } |
|
560 |
|
561 void |
|
562 octave_user_function::print_code_function_trailer (void) |
|
563 { |
|
564 tree_print_code tpc (octave_stdout, Vps4); |
|
565 |
|
566 tpc.visit_octave_user_function_trailer (*this); |
|
567 } |
|
568 |
|
569 void |
|
570 octave_user_function::install_automatic_vars (void) |
|
571 { |
|
572 argn_sr = sym_tab->lookup ("argn", true); |
|
573 nargin_sr = sym_tab->lookup ("nargin", true); |
|
574 nargout_sr = sym_tab->lookup ("nargout", true); |
3974
|
575 |
|
576 if (takes_varargs ()) |
|
577 varargin_sr = sym_tab->lookup ("varargin", true); |
2974
|
578 } |
|
579 |
|
580 void |
|
581 octave_user_function::bind_automatic_vars |
3974
|
582 (const string_vector& arg_names, int nargin, int nargout, |
|
583 const octave_value_list& va_args) |
2974
|
584 { |
|
585 if (! arg_names.empty ()) |
|
586 argn_sr->define (arg_names); |
|
587 |
4233
|
588 nargin_sr->define (nargin); |
|
589 nargout_sr->define (nargout); |
3974
|
590 |
|
591 if (takes_varargs ()) |
|
592 { |
|
593 int n = va_args.length (); |
|
594 |
|
595 Cell varargin (1, n); |
|
596 |
|
597 for (int i = 0; i < n; i++) |
|
598 varargin(0,i) = va_args(i); |
|
599 |
|
600 varargin_sr->define (varargin); |
|
601 } |
2974
|
602 } |
|
603 |
3743
|
604 DEFUNX ("va_arg", Fva_arg, args, , |
3445
|
605 "-*- texinfo -*-\n\ |
|
606 @deftypefn {Built-in Function} {} va_arg ()\n\ |
3745
|
607 Return the value of the next available argument and move the internal\n\ |
3445
|
608 pointer to the next argument. It is an error to call @code{va_arg}\n\ |
|
609 when ther eare no more arguments available, or in a function that\n\ |
|
610 has not been declared to take a variable number of parameters.\n\ |
|
611 @end deftypefn") |
2974
|
612 { |
4124
|
613 static bool warned = false; |
|
614 |
|
615 if (! warned) |
|
616 { |
|
617 ::warning ("va_arg is deprecated; use varargin instead"); |
|
618 warned = true; |
|
619 } |
3975
|
620 |
2974
|
621 octave_value_list retval; |
|
622 |
|
623 int nargin = args.length (); |
|
624 |
|
625 if (nargin == 0) |
|
626 { |
|
627 if (curr_function) |
|
628 { |
|
629 if (curr_function->takes_varargs ()) |
|
630 retval = curr_function->octave_va_arg (); |
|
631 else |
|
632 { |
|
633 ::error ("va_arg only valid within function taking variable"); |
|
634 ::error ("number of arguments"); |
|
635 } |
|
636 } |
|
637 else |
|
638 ::error ("va_arg only valid within function body"); |
|
639 } |
|
640 else |
|
641 print_usage ("va_arg"); |
|
642 |
|
643 return retval; |
|
644 } |
|
645 |
|
646 DEFUN (va_start, args, , |
3445
|
647 "-*- texinfo -*-\n\ |
|
648 @deftypefn {Built-in Function} {} va_start ()\n\ |
|
649 Position an internal pointer to the first unnamed argument in\n\ |
|
650 functions that have been declared to accept a variable number of\n\ |
|
651 arguments. It is an error to call @code{va_start} in a function\n\ |
|
652 that has not been declared to take a variable number of parameters.\n\ |
|
653 @end deftypefn") |
2974
|
654 { |
4124
|
655 static bool warned = false; |
|
656 |
|
657 if (! warned) |
|
658 { |
|
659 ::warning ("va_start is deprecated; use varargin instead"); |
|
660 warned = true; |
|
661 } |
3975
|
662 |
2974
|
663 octave_value_list retval; |
|
664 |
|
665 int nargin = args.length (); |
|
666 |
|
667 if (nargin == 0) |
|
668 { |
|
669 if (curr_function) |
|
670 { |
|
671 if (curr_function->takes_varargs ()) |
|
672 curr_function->octave_va_start (); |
|
673 else |
|
674 { |
|
675 ::error ("va_start only valid within function taking variable"); |
|
676 ::error ("number of arguments"); |
|
677 } |
|
678 } |
|
679 else |
|
680 ::error ("va_start only valid within function body"); |
|
681 } |
|
682 else |
|
683 print_usage ("va_start"); |
|
684 |
|
685 return retval; |
|
686 } |
|
687 |
|
688 DEFUN (vr_val, args, , |
3445
|
689 "-*- texinfo -*-\n\ |
|
690 @deftypefn {Built-in Function} {} vr_val (@var{x})\n\ |
|
691 Each time this function is called, it places the value of its argument\n\ |
|
692 at the end of the list of values to return from the current\n\ |
|
693 function. Once @code{vr_val} has been called, there is no way to go\n\ |
|
694 back to the beginning of the list and rewrite any of the return\n\ |
|
695 values. This function may only be called within functions that have\n\ |
|
696 been declared to return an unspecified number of output arguments.\n\ |
|
697 @end deftypefn") |
2974
|
698 { |
4124
|
699 static bool warned = false; |
|
700 |
|
701 if (! warned) |
|
702 { |
|
703 ::warning ("vr_val is deprecated; use varargout instead"); |
|
704 warned = true; |
|
705 } |
3975
|
706 |
2974
|
707 octave_value_list retval; |
|
708 |
|
709 int nargin = args.length (); |
|
710 |
|
711 if (nargin == 1) |
|
712 { |
|
713 if (curr_function) |
|
714 { |
3974
|
715 if (curr_function->has_varargout ()) |
|
716 ::error ("vr_val and varargout cannot both be used in the same function"); |
|
717 else if (curr_function->takes_var_return ()) |
2974
|
718 curr_function->octave_vr_val (args(0)); |
|
719 else |
|
720 { |
|
721 ::error ("vr_val only valid within function declared to"); |
|
722 ::error ("produce a variable number of values"); |
|
723 } |
|
724 } |
|
725 else |
|
726 ::error ("vr_val only valid within function body"); |
|
727 } |
|
728 else |
|
729 print_usage ("vr_val"); |
|
730 |
|
731 return retval; |
|
732 } |
|
733 |
|
734 static int |
|
735 define_all_return_values (void) |
|
736 { |
|
737 Vdefine_all_return_values = check_preference ("define_all_return_values"); |
|
738 |
|
739 return 0; |
|
740 } |
|
741 |
|
742 static int |
3131
|
743 max_recursion_depth (void) |
|
744 { |
|
745 Vmax_recursion_depth = check_preference ("max_recursion_depth"); |
|
746 |
|
747 return 0; |
|
748 } |
|
749 |
|
750 static int |
2974
|
751 return_last_computed_value (void) |
|
752 { |
|
753 Vreturn_last_computed_value |
|
754 = check_preference ("return_last_computed_value"); |
|
755 |
|
756 return 0; |
|
757 } |
|
758 |
|
759 void |
|
760 symbols_of_ov_usr_fcn (void) |
|
761 { |
3258
|
762 DEFVAR (default_return_value, Matrix (), 0, |
3371
|
763 "-*- texinfo -*-\n\ |
|
764 @defvr {Built-in Variable} default_return_value\n\ |
|
765 The value given to otherwise uninitialized return values if\n\ |
|
766 @code{define_all_return_values} is nonzero. The default value is\n\ |
|
767 @code{[]}.\n\ |
|
768 @end defvr"); |
2974
|
769 |
4233
|
770 DEFVAR (define_all_return_values, false, define_all_return_values, |
3371
|
771 "-*- texinfo -*-\n\ |
|
772 @defvr {Built-in Variable} define_all_return_values\n\ |
|
773 If the value of @code{define_all_return_values} is nonzero, Octave\n\ |
|
774 will substitute the value specified by @code{default_return_value} for\n\ |
|
775 any return values that remain undefined when a function returns. The\n\ |
|
776 default value is 0.\n\ |
|
777 @end defvr"); |
2974
|
778 |
3258
|
779 DEFVAR (max_recursion_depth, 256.0, max_recursion_depth, |
3371
|
780 "-*- texinfo -*-\n\ |
|
781 @defvr {Built-in Variable} max_recursion_depth\n\ |
|
782 Limit the number of times a function may be called recursively.\n\ |
|
783 If the limit is exceeded, an error message is printed and control\n\ |
|
784 returns to the top level.\n\ |
|
785 \n\ |
|
786 The default value is 256.\n\ |
|
787 @end defvr"); |
3131
|
788 |
4233
|
789 DEFVAR (return_last_computed_value, false, return_last_computed_value, |
3371
|
790 "-*- texinfo -*-\n\ |
|
791 @defvr {Built-in Variable} return_last_computed_value\n\ |
|
792 If the value of @code{return_last_computed_value} is true, and a\n\ |
|
793 function is defined without explicitly specifying a return value, the\n\ |
|
794 function will return the value of the last expression. Otherwise, no\n\ |
|
795 value will be returned. The default value is 0.\n\ |
|
796 \n\ |
|
797 For example, the function\n\ |
|
798 \n\ |
|
799 @example\n\ |
|
800 function f ()\n\ |
|
801 2 + 2;\n\ |
|
802 endfunction\n\ |
|
803 @end example\n\ |
|
804 \n\ |
|
805 @noindent\n\ |
|
806 will either return nothing, if the value of\n\ |
|
807 @code{return_last_computed_value} is 0, or 4, if the value of\n\ |
|
808 @code{return_last_computed_value} is nonzero.\n\ |
|
809 @end defvr"); |
2974
|
810 } |
|
811 |
|
812 /* |
|
813 ;;; Local Variables: *** |
|
814 ;;; mode: C++ *** |
|
815 ;;; End: *** |
|
816 */ |