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