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 |
4059
|
331 // XXX FIXME XXX -- perhaps there should be an |
|
332 // octave_value_list::next_subsref member function? See also |
|
333 // octave_builtin::subsref. |
3933
|
334 |
4059
|
335 if (idx.length () > 1) |
|
336 retval = retval(0).next_subsref (type, idx); |
|
337 |
|
338 return retval; |
3933
|
339 } |
|
340 |
|
341 octave_value_list |
3544
|
342 octave_user_function::do_multi_index_op (int nargout, |
|
343 const octave_value_list& args) |
2974
|
344 { |
|
345 octave_value_list retval; |
|
346 |
|
347 if (error_state) |
|
348 return retval; |
|
349 |
|
350 if (! cmd_list) |
|
351 return retval; |
|
352 |
|
353 int nargin = args.length (); |
|
354 |
2985
|
355 unwind_protect::begin_frame ("func_eval"); |
2974
|
356 |
|
357 unwind_protect_int (call_depth); |
|
358 call_depth++; |
|
359 |
3131
|
360 if (call_depth > Vmax_recursion_depth) |
|
361 { |
|
362 ::error ("max_recursion_limit exceeded"); |
3321
|
363 unwind_protect::run_frame ("func_eval"); |
3131
|
364 return retval; |
|
365 } |
|
366 |
2974
|
367 if (symtab_entry && ! symtab_entry->is_read_only ()) |
|
368 { |
|
369 symtab_entry->protect (); |
2985
|
370 unwind_protect::add (unprotect_function, symtab_entry); |
2974
|
371 } |
|
372 |
|
373 if (call_depth > 1) |
|
374 { |
|
375 sym_tab->push_context (); |
2985
|
376 unwind_protect::add (pop_symbol_table_context, sym_tab); |
2974
|
377 |
|
378 if (vr_list) |
|
379 { |
|
380 // Push new vr_list. |
|
381 |
|
382 unwind_protect_ptr (vr_list); |
|
383 vr_list = new tree_va_return_list; |
|
384 |
|
385 // Clear and delete the new one before restoring the old |
|
386 // one. |
|
387 |
2985
|
388 unwind_protect::add (delete_vr_list, vr_list); |
2974
|
389 } |
|
390 } |
|
391 |
|
392 if (vr_list) |
|
393 vr_list->clear (); |
|
394 |
|
395 // Force symbols to be undefined again when this function exits. |
|
396 |
2985
|
397 unwind_protect::add (clear_symbol_table, sym_tab); |
2974
|
398 |
|
399 // Save old and set current symbol table context, for |
|
400 // eval_undefined_error(). |
|
401 |
|
402 unwind_protect_ptr (curr_sym_tab); |
|
403 curr_sym_tab = sym_tab; |
|
404 |
|
405 unwind_protect_ptr (curr_function); |
|
406 curr_function = this; |
|
407 |
3875
|
408 // Save and restore args passed for recursive calls. |
2974
|
409 |
3875
|
410 save_args_passed (args); |
3239
|
411 |
3875
|
412 unwind_protect::add (::restore_args_passed, this); |
3239
|
413 |
2974
|
414 string_vector arg_names = args.name_tags (); |
|
415 |
|
416 unwind_protect_int (num_args_passed); |
|
417 num_args_passed = nargin; |
|
418 |
|
419 unwind_protect_int (curr_va_arg_number); |
|
420 |
3876
|
421 curr_va_arg_number = num_named_args; |
|
422 |
2974
|
423 if (param_list && ! param_list->varargs_only ()) |
|
424 { |
|
425 param_list->define_from_arg_vector (args); |
|
426 if (error_state) |
|
427 goto abort; |
|
428 } |
|
429 |
3239
|
430 // Force parameter list to be undefined when this function exits. |
|
431 // Doing so decrements the reference counts on the values of local |
|
432 // variables that are also named function parameters. |
|
433 |
|
434 unwind_protect::add (clear_param_list, param_list); |
|
435 |
|
436 // Force return list to be undefined when this function exits. |
|
437 // Doing so decrements the reference counts on the values of local |
|
438 // variables that are also named values returned by this function. |
|
439 |
|
440 unwind_protect::add (clear_param_list, ret_list); |
|
441 |
2974
|
442 // The following code is in a separate scope to avoid warnings from |
|
443 // G++ about `goto abort' crossing the initialization of some |
|
444 // variables. |
|
445 |
|
446 { |
3974
|
447 bind_automatic_vars (arg_names, nargin, nargout, octave_all_va_args ()); |
2974
|
448 |
|
449 bool echo_commands = (Vecho_executing_commands & ECHO_FUNCTIONS); |
|
450 |
|
451 if (echo_commands) |
|
452 print_code_function_header (); |
|
453 |
|
454 // Evaluate the commands that make up the function. |
|
455 |
3489
|
456 unwind_protect_bool (evaluating_function_body); |
|
457 evaluating_function_body = true; |
|
458 |
2974
|
459 octave_value_list tmp = cmd_list->eval (); |
|
460 |
|
461 octave_value last_computed_value; |
|
462 |
|
463 if (! tmp.empty ()) |
|
464 last_computed_value = tmp(0); |
|
465 |
|
466 if (echo_commands) |
|
467 print_code_function_trailer (); |
|
468 |
2985
|
469 if (tree_return_command::returning) |
|
470 tree_return_command::returning = 0; |
2974
|
471 |
2985
|
472 if (tree_break_command::breaking) |
|
473 tree_break_command::breaking--; |
2974
|
474 |
|
475 if (error_state) |
|
476 { |
|
477 traceback_error (); |
|
478 goto abort; |
|
479 } |
|
480 |
|
481 // Copy return values out. |
|
482 |
|
483 if (ret_list) |
3871
|
484 { |
|
485 if (Vdefine_all_return_values) |
|
486 { |
|
487 octave_value tmp = builtin_any_variable ("default_return_value"); |
|
488 |
|
489 if (tmp.is_defined ()) |
|
490 ret_list->initialize_undefined_elements (tmp); |
|
491 } |
|
492 |
3974
|
493 if (has_varargout ()) |
|
494 varargout_to_vr_val (); |
|
495 |
3871
|
496 retval = ret_list->convert_to_const_vector (vr_list); |
|
497 } |
2974
|
498 else if (Vreturn_last_computed_value) |
|
499 retval(0) = last_computed_value; |
|
500 } |
|
501 |
|
502 abort: |
2985
|
503 unwind_protect::run_frame ("func_eval"); |
2974
|
504 |
|
505 return retval; |
|
506 } |
|
507 |
|
508 void |
|
509 octave_user_function::traceback_error (void) |
|
510 { |
|
511 if (error_state >= 0) |
|
512 error_state = -1; |
|
513 |
|
514 if (fcn_name.empty ()) |
|
515 { |
|
516 if (file_name.empty ()) |
|
517 ::error ("called from `?unknown?'"); |
|
518 else |
|
519 ::error ("called from file `%s'", file_name.c_str ()); |
|
520 } |
|
521 else |
|
522 { |
|
523 if (file_name.empty ()) |
|
524 ::error ("called from `%s'", fcn_name.c_str ()); |
|
525 else |
|
526 ::error ("called from `%s' in file `%s'", |
|
527 fcn_name.c_str (), file_name.c_str ()); |
|
528 } |
|
529 } |
|
530 |
|
531 void |
|
532 octave_user_function::accept (tree_walker& tw) |
|
533 { |
|
534 tw.visit_octave_user_function (*this); |
|
535 } |
|
536 |
|
537 void |
3933
|
538 octave_user_function::print_symtab_info (std::ostream& os) const |
|
539 { |
|
540 if (sym_tab) |
|
541 sym_tab->print_info (os); |
|
542 else |
|
543 warning ("%s: no symbol table info!", fcn_name.c_str ()); |
|
544 } |
|
545 |
|
546 void |
2974
|
547 octave_user_function::print_code_function_header (void) |
|
548 { |
|
549 tree_print_code tpc (octave_stdout, Vps4); |
|
550 |
|
551 tpc.visit_octave_user_function_header (*this); |
|
552 } |
|
553 |
|
554 void |
|
555 octave_user_function::print_code_function_trailer (void) |
|
556 { |
|
557 tree_print_code tpc (octave_stdout, Vps4); |
|
558 |
|
559 tpc.visit_octave_user_function_trailer (*this); |
|
560 } |
|
561 |
|
562 void |
|
563 octave_user_function::install_automatic_vars (void) |
|
564 { |
|
565 argn_sr = sym_tab->lookup ("argn", true); |
|
566 nargin_sr = sym_tab->lookup ("nargin", true); |
|
567 nargout_sr = sym_tab->lookup ("nargout", true); |
3974
|
568 |
|
569 if (takes_varargs ()) |
|
570 varargin_sr = sym_tab->lookup ("varargin", true); |
2974
|
571 } |
|
572 |
|
573 void |
|
574 octave_user_function::bind_automatic_vars |
3974
|
575 (const string_vector& arg_names, int nargin, int nargout, |
|
576 const octave_value_list& va_args) |
2974
|
577 { |
|
578 if (! arg_names.empty ()) |
|
579 argn_sr->define (arg_names); |
|
580 |
|
581 nargin_sr->define (static_cast<double> (nargin)); |
|
582 nargout_sr->define (static_cast<double> (nargout)); |
3974
|
583 |
|
584 if (takes_varargs ()) |
|
585 { |
|
586 int n = va_args.length (); |
|
587 |
|
588 Cell varargin (1, n); |
|
589 |
|
590 for (int i = 0; i < n; i++) |
|
591 varargin(0,i) = va_args(i); |
|
592 |
|
593 varargin_sr->define (varargin); |
|
594 } |
2974
|
595 } |
|
596 |
3743
|
597 DEFUNX ("va_arg", Fva_arg, args, , |
3445
|
598 "-*- texinfo -*-\n\ |
|
599 @deftypefn {Built-in Function} {} va_arg ()\n\ |
3745
|
600 Return the value of the next available argument and move the internal\n\ |
3445
|
601 pointer to the next argument. It is an error to call @code{va_arg}\n\ |
|
602 when ther eare no more arguments available, or in a function that\n\ |
|
603 has not been declared to take a variable number of parameters.\n\ |
|
604 @end deftypefn") |
2974
|
605 { |
3975
|
606 ::warning ("va_arg is deprecated; use varargin instead"); |
|
607 |
2974
|
608 octave_value_list retval; |
|
609 |
|
610 int nargin = args.length (); |
|
611 |
|
612 if (nargin == 0) |
|
613 { |
|
614 if (curr_function) |
|
615 { |
|
616 if (curr_function->takes_varargs ()) |
|
617 retval = curr_function->octave_va_arg (); |
|
618 else |
|
619 { |
|
620 ::error ("va_arg only valid within function taking variable"); |
|
621 ::error ("number of arguments"); |
|
622 } |
|
623 } |
|
624 else |
|
625 ::error ("va_arg only valid within function body"); |
|
626 } |
|
627 else |
|
628 print_usage ("va_arg"); |
|
629 |
|
630 return retval; |
|
631 } |
|
632 |
|
633 DEFUN (va_start, args, , |
3445
|
634 "-*- texinfo -*-\n\ |
|
635 @deftypefn {Built-in Function} {} va_start ()\n\ |
|
636 Position an internal pointer to the first unnamed argument in\n\ |
|
637 functions that have been declared to accept a variable number of\n\ |
|
638 arguments. It is an error to call @code{va_start} in a function\n\ |
|
639 that has not been declared to take a variable number of parameters.\n\ |
|
640 @end deftypefn") |
2974
|
641 { |
3975
|
642 ::warning ("va_start is deprecated; use varargin instead"); |
|
643 |
2974
|
644 octave_value_list retval; |
|
645 |
|
646 int nargin = args.length (); |
|
647 |
|
648 if (nargin == 0) |
|
649 { |
|
650 if (curr_function) |
|
651 { |
|
652 if (curr_function->takes_varargs ()) |
|
653 curr_function->octave_va_start (); |
|
654 else |
|
655 { |
|
656 ::error ("va_start only valid within function taking variable"); |
|
657 ::error ("number of arguments"); |
|
658 } |
|
659 } |
|
660 else |
|
661 ::error ("va_start only valid within function body"); |
|
662 } |
|
663 else |
|
664 print_usage ("va_start"); |
|
665 |
|
666 return retval; |
|
667 } |
|
668 |
|
669 DEFUN (vr_val, args, , |
3445
|
670 "-*- texinfo -*-\n\ |
|
671 @deftypefn {Built-in Function} {} vr_val (@var{x})\n\ |
|
672 Each time this function is called, it places the value of its argument\n\ |
|
673 at the end of the list of values to return from the current\n\ |
|
674 function. Once @code{vr_val} has been called, there is no way to go\n\ |
|
675 back to the beginning of the list and rewrite any of the return\n\ |
|
676 values. This function may only be called within functions that have\n\ |
|
677 been declared to return an unspecified number of output arguments.\n\ |
|
678 @end deftypefn") |
2974
|
679 { |
3975
|
680 ::warning ("vr_val is deprecated; use varargout instead"); |
|
681 |
2974
|
682 octave_value_list retval; |
|
683 |
|
684 int nargin = args.length (); |
|
685 |
|
686 if (nargin == 1) |
|
687 { |
|
688 if (curr_function) |
|
689 { |
3974
|
690 if (curr_function->has_varargout ()) |
|
691 ::error ("vr_val and varargout cannot both be used in the same function"); |
|
692 else if (curr_function->takes_var_return ()) |
2974
|
693 curr_function->octave_vr_val (args(0)); |
|
694 else |
|
695 { |
|
696 ::error ("vr_val only valid within function declared to"); |
|
697 ::error ("produce a variable number of values"); |
|
698 } |
|
699 } |
|
700 else |
|
701 ::error ("vr_val only valid within function body"); |
|
702 } |
|
703 else |
|
704 print_usage ("vr_val"); |
|
705 |
|
706 return retval; |
|
707 } |
|
708 |
|
709 static int |
|
710 define_all_return_values (void) |
|
711 { |
|
712 Vdefine_all_return_values = check_preference ("define_all_return_values"); |
|
713 |
|
714 return 0; |
|
715 } |
|
716 |
|
717 static int |
3131
|
718 max_recursion_depth (void) |
|
719 { |
|
720 Vmax_recursion_depth = check_preference ("max_recursion_depth"); |
|
721 |
|
722 return 0; |
|
723 } |
|
724 |
|
725 static int |
2974
|
726 return_last_computed_value (void) |
|
727 { |
|
728 Vreturn_last_computed_value |
|
729 = check_preference ("return_last_computed_value"); |
|
730 |
|
731 return 0; |
|
732 } |
|
733 |
|
734 void |
|
735 symbols_of_ov_usr_fcn (void) |
|
736 { |
3258
|
737 DEFVAR (default_return_value, Matrix (), 0, |
3371
|
738 "-*- texinfo -*-\n\ |
|
739 @defvr {Built-in Variable} default_return_value\n\ |
|
740 The value given to otherwise uninitialized return values if\n\ |
|
741 @code{define_all_return_values} is nonzero. The default value is\n\ |
|
742 @code{[]}.\n\ |
|
743 @end defvr"); |
2974
|
744 |
3258
|
745 DEFVAR (define_all_return_values, 0.0, define_all_return_values, |
3371
|
746 "-*- texinfo -*-\n\ |
|
747 @defvr {Built-in Variable} define_all_return_values\n\ |
|
748 If the value of @code{define_all_return_values} is nonzero, Octave\n\ |
|
749 will substitute the value specified by @code{default_return_value} for\n\ |
|
750 any return values that remain undefined when a function returns. The\n\ |
|
751 default value is 0.\n\ |
|
752 @end defvr"); |
2974
|
753 |
3258
|
754 DEFVAR (max_recursion_depth, 256.0, max_recursion_depth, |
3371
|
755 "-*- texinfo -*-\n\ |
|
756 @defvr {Built-in Variable} max_recursion_depth\n\ |
|
757 Limit the number of times a function may be called recursively.\n\ |
|
758 If the limit is exceeded, an error message is printed and control\n\ |
|
759 returns to the top level.\n\ |
|
760 \n\ |
|
761 The default value is 256.\n\ |
|
762 @end defvr"); |
3131
|
763 |
3258
|
764 DEFVAR (return_last_computed_value, 0.0, return_last_computed_value, |
3371
|
765 "-*- texinfo -*-\n\ |
|
766 @defvr {Built-in Variable} return_last_computed_value\n\ |
|
767 If the value of @code{return_last_computed_value} is true, and a\n\ |
|
768 function is defined without explicitly specifying a return value, the\n\ |
|
769 function will return the value of the last expression. Otherwise, no\n\ |
|
770 value will be returned. The default value is 0.\n\ |
|
771 \n\ |
|
772 For example, the function\n\ |
|
773 \n\ |
|
774 @example\n\ |
|
775 function f ()\n\ |
|
776 2 + 2;\n\ |
|
777 endfunction\n\ |
|
778 @end example\n\ |
|
779 \n\ |
|
780 @noindent\n\ |
|
781 will either return nothing, if the value of\n\ |
|
782 @code{return_last_computed_value} is 0, or 4, if the value of\n\ |
|
783 @code{return_last_computed_value} is nonzero.\n\ |
|
784 @end defvr"); |
2974
|
785 } |
|
786 |
|
787 /* |
|
788 ;;; Local Variables: *** |
|
789 ;;; mode: C++ *** |
|
790 ;;; End: *** |
|
791 */ |