2974
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, |
|
4 2006, 2007 John W. Eaton |
2974
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
2974
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
2974
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include "str-vec.h" |
|
29 |
|
30 #include <defaults.h> |
3974
|
31 #include "Cell.h" |
2974
|
32 #include "defun.h" |
|
33 #include "error.h" |
|
34 #include "input.h" |
|
35 #include "oct-obj.h" |
|
36 #include "ov-usr-fcn.h" |
|
37 #include "ov.h" |
|
38 #include "pager.h" |
2985
|
39 #include "pt-jump.h" |
2974
|
40 #include "pt-misc.h" |
|
41 #include "pt-pr-code.h" |
2982
|
42 #include "pt-stmt.h" |
2974
|
43 #include "pt-walk.h" |
|
44 #include "symtab.h" |
|
45 #include "toplev.h" |
|
46 #include "unwind-prot.h" |
|
47 #include "utils.h" |
3489
|
48 #include "parse.h" |
2974
|
49 #include "variables.h" |
|
50 |
3131
|
51 // Maximum nesting level for functions called recursively. |
5794
|
52 static int Vmax_recursion_depth = 256; |
3131
|
53 |
7336
|
54 // Scripts. |
|
55 |
|
56 octave_value_list |
|
57 octave_user_script::do_multi_index_op (int nargout, |
|
58 const octave_value_list& args) |
|
59 { |
|
60 octave_value_list retval; |
|
61 |
|
62 if (! error_state) |
|
63 { |
|
64 if (args.length () == 0) |
|
65 { |
|
66 // FIXME -- I think we need a way to protect against |
|
67 // recursion, but we can't use the same method as we use for |
|
68 // functions. |
|
69 |
|
70 source_file (file_name); |
|
71 } |
|
72 else |
|
73 error ("invalid call to script"); |
|
74 } |
|
75 |
|
76 return retval; |
|
77 } |
|
78 |
2974
|
79 // User defined functions. |
|
80 |
3219
|
81 DEFINE_OCTAVE_ALLOCATOR (octave_user_function); |
2974
|
82 |
3219
|
83 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_user_function, |
4612
|
84 "user-defined function", |
3219
|
85 "user-defined function"); |
2974
|
86 |
5744
|
87 DEFINE_OCTAVE_ALLOCATOR (octave_user_script); |
|
88 |
|
89 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_user_script, |
|
90 "user-defined script", |
|
91 "user-defined script"); |
|
92 |
2974
|
93 // Ugh. This really needs to be simplified (code/data? |
|
94 // extrinsic/intrinsic state?). |
|
95 |
|
96 octave_user_function::octave_user_function |
7336
|
97 (symbol_table::scope_id sid, tree_parameter_list *pl, |
|
98 tree_parameter_list *rl, tree_statement_list *cl) |
3523
|
99 : octave_function (std::string (), std::string ()), |
2974
|
100 param_list (pl), ret_list (rl), cmd_list (cl), |
7336
|
101 lead_comm (), trail_comm (), file_name (), |
6323
|
102 parent_name (), t_parsed (static_cast<time_t> (0)), |
3255
|
103 t_checked (static_cast<time_t> (0)), |
6149
|
104 system_fcn_file (false), call_depth (0), num_named_args (0), |
7336
|
105 nested_function (false), inline_function (false), |
|
106 class_constructor (false), class_method (false), xdispatch_class (), |
|
107 args_passed (), num_args_passed (0), local_scope (sid) |
2974
|
108 { |
|
109 if (param_list) |
5848
|
110 num_named_args = param_list->length (); |
2974
|
111 } |
|
112 |
|
113 octave_user_function::~octave_user_function (void) |
|
114 { |
|
115 delete param_list; |
|
116 delete ret_list; |
|
117 delete cmd_list; |
3665
|
118 delete lead_comm; |
|
119 delete trail_comm; |
7336
|
120 |
|
121 symbol_table::erase_scope (local_scope); |
2974
|
122 } |
|
123 |
|
124 octave_user_function * |
|
125 octave_user_function::define_ret_list (tree_parameter_list *t) |
|
126 { |
|
127 ret_list = t; |
|
128 |
|
129 return this; |
|
130 } |
|
131 |
|
132 void |
4343
|
133 octave_user_function::stash_fcn_file_name (const std::string& nm) |
2974
|
134 { |
4343
|
135 file_name = nm; |
2974
|
136 } |
|
137 |
|
138 void |
|
139 octave_user_function::mark_as_system_fcn_file (void) |
|
140 { |
|
141 if (! file_name.empty ()) |
|
142 { |
|
143 // We really should stash the whole path to the file we found, |
|
144 // when we looked it up, to avoid possible race conditions... |
5775
|
145 // FIXME |
2974
|
146 // |
|
147 // We probably also don't need to get the library directory |
|
148 // every time, but since this function is only called when the |
|
149 // function file is parsed, it probably doesn't matter that |
|
150 // much. |
|
151 |
3523
|
152 std::string ff_name = fcn_file_in_path (file_name); |
2974
|
153 |
3565
|
154 if (Vfcn_file_dir == ff_name.substr (0, Vfcn_file_dir.length ())) |
2974
|
155 system_fcn_file = 1; |
|
156 } |
|
157 else |
|
158 system_fcn_file = 0; |
|
159 } |
|
160 |
|
161 bool |
|
162 octave_user_function::takes_varargs (void) const |
|
163 { |
|
164 return (param_list && param_list->takes_varargs ()); |
|
165 } |
|
166 |
5848
|
167 bool |
|
168 octave_user_function::takes_var_return (void) const |
2974
|
169 { |
5848
|
170 return (ret_list && ret_list->takes_varargs ()); |
2974
|
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 static void |
3239
|
193 clear_param_list (void *lst) |
|
194 { |
|
195 tree_parameter_list *tmp = static_cast<tree_parameter_list *> (lst); |
|
196 |
|
197 if (tmp) |
4219
|
198 tmp->undefine (); |
3239
|
199 } |
|
200 |
|
201 static void |
3875
|
202 restore_args_passed (void *fcn) |
3239
|
203 { |
|
204 octave_user_function *tmp = static_cast<octave_user_function *> (fcn); |
|
205 |
|
206 if (tmp) |
3875
|
207 tmp->restore_args_passed (); |
3239
|
208 } |
|
209 |
2974
|
210 octave_value_list |
4247
|
211 octave_user_function::subsref (const std::string& type, |
4219
|
212 const std::list<octave_value_list>& idx, |
3933
|
213 int nargout) |
|
214 { |
|
215 octave_value_list retval; |
|
216 |
|
217 switch (type[0]) |
|
218 { |
|
219 case '(': |
5154
|
220 { |
5155
|
221 int tmp_nargout = (type.length () > 1 && nargout == 0) ? 1 : nargout; |
5154
|
222 |
|
223 retval = do_multi_index_op (tmp_nargout, idx.front ()); |
|
224 } |
3933
|
225 break; |
|
226 |
|
227 case '{': |
|
228 case '.': |
|
229 { |
|
230 std::string nm = type_name (); |
|
231 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
232 } |
|
233 break; |
|
234 |
|
235 default: |
|
236 panic_impossible (); |
|
237 } |
|
238 |
5775
|
239 // FIXME -- perhaps there should be an |
4059
|
240 // octave_value_list::next_subsref member function? See also |
|
241 // octave_builtin::subsref. |
3933
|
242 |
4219
|
243 if (idx.size () > 1) |
4994
|
244 retval = retval(0).next_subsref (nargout, type, idx); |
4059
|
245 |
|
246 return retval; |
3933
|
247 } |
|
248 |
|
249 octave_value_list |
3544
|
250 octave_user_function::do_multi_index_op (int nargout, |
|
251 const octave_value_list& args) |
2974
|
252 { |
|
253 octave_value_list retval; |
|
254 |
|
255 if (error_state) |
|
256 return retval; |
|
257 |
|
258 if (! cmd_list) |
|
259 return retval; |
|
260 |
|
261 int nargin = args.length (); |
|
262 |
4987
|
263 unwind_protect::begin_frame ("user_func_eval"); |
2974
|
264 |
|
265 unwind_protect_int (call_depth); |
|
266 call_depth++; |
|
267 |
3131
|
268 if (call_depth > Vmax_recursion_depth) |
|
269 { |
|
270 ::error ("max_recursion_limit exceeded"); |
4987
|
271 unwind_protect::run_frame ("user_func_eval"); |
3131
|
272 return retval; |
|
273 } |
|
274 |
7336
|
275 // Save old and set current symbol table context, for |
|
276 // eval_undefined_error(). |
|
277 |
|
278 symbol_table::push_scope (local_scope); |
|
279 unwind_protect::add (symbol_table::pop_scope); |
2974
|
280 |
|
281 if (call_depth > 1) |
|
282 { |
7336
|
283 symbol_table::push_context (); |
2974
|
284 |
7336
|
285 unwind_protect::add (symbol_table::pop_context); |
|
286 } |
|
287 else |
|
288 { |
|
289 // Force symbols to be undefined again when this function exits. |
|
290 unwind_protect::add (symbol_table::clear_variables); |
|
291 } |
2974
|
292 |
4975
|
293 unwind_protect_ptr (curr_caller_statement); |
5743
|
294 curr_caller_statement = curr_statement; |
4975
|
295 |
5743
|
296 octave_call_stack::push (this); |
|
297 |
|
298 unwind_protect::add (octave_call_stack::unwind_pop, 0); |
2974
|
299 |
6149
|
300 if (! (is_nested_function () || is_inline_function ())) |
4238
|
301 { |
|
302 unwind_protect_ptr (curr_parent_function); |
|
303 curr_parent_function = this; |
|
304 } |
|
305 |
3875
|
306 // Save and restore args passed for recursive calls. |
2974
|
307 |
3875
|
308 save_args_passed (args); |
3239
|
309 |
3875
|
310 unwind_protect::add (::restore_args_passed, this); |
3239
|
311 |
2974
|
312 string_vector arg_names = args.name_tags (); |
|
313 |
|
314 unwind_protect_int (num_args_passed); |
|
315 num_args_passed = nargin; |
|
316 |
|
317 if (param_list && ! param_list->varargs_only ()) |
|
318 { |
|
319 param_list->define_from_arg_vector (args); |
|
320 if (error_state) |
|
321 goto abort; |
|
322 } |
|
323 |
3239
|
324 // Force parameter list to be undefined when this function exits. |
|
325 // Doing so decrements the reference counts on the values of local |
|
326 // variables that are also named function parameters. |
|
327 |
|
328 unwind_protect::add (clear_param_list, param_list); |
|
329 |
|
330 // Force return list to be undefined when this function exits. |
|
331 // Doing so decrements the reference counts on the values of local |
|
332 // variables that are also named values returned by this function. |
|
333 |
|
334 unwind_protect::add (clear_param_list, ret_list); |
|
335 |
2974
|
336 // The following code is in a separate scope to avoid warnings from |
|
337 // G++ about `goto abort' crossing the initialization of some |
|
338 // variables. |
|
339 |
|
340 { |
3974
|
341 bind_automatic_vars (arg_names, nargin, nargout, octave_all_va_args ()); |
2974
|
342 |
|
343 bool echo_commands = (Vecho_executing_commands & ECHO_FUNCTIONS); |
|
344 |
|
345 if (echo_commands) |
|
346 print_code_function_header (); |
|
347 |
|
348 // Evaluate the commands that make up the function. |
|
349 |
3489
|
350 unwind_protect_bool (evaluating_function_body); |
|
351 evaluating_function_body = true; |
|
352 |
6657
|
353 if (is_inline_function ()) |
|
354 { |
|
355 assert (cmd_list->length () == 1); |
|
356 |
|
357 retval = cmd_list->eval (false, nargout); |
|
358 } |
|
359 else |
|
360 cmd_list->eval (); |
2974
|
361 |
|
362 if (echo_commands) |
|
363 print_code_function_trailer (); |
|
364 |
4207
|
365 if (tree_return_command::returning) |
|
366 tree_return_command::returning = 0; |
2974
|
367 |
4207
|
368 if (tree_break_command::breaking) |
|
369 tree_break_command::breaking--; |
2974
|
370 |
|
371 if (error_state) |
|
372 { |
|
373 traceback_error (); |
|
374 goto abort; |
|
375 } |
|
376 |
|
377 // Copy return values out. |
|
378 |
6657
|
379 if (ret_list && ! is_inline_function ()) |
3871
|
380 { |
4748
|
381 ret_list->initialize_undefined_elements (my_name, nargout, Matrix ()); |
3871
|
382 |
5848
|
383 Cell varargout; |
|
384 |
6591
|
385 if (ret_list->takes_varargs ()) |
5848
|
386 { |
7336
|
387 octave_value varargout_varval = symbol_table::varval ("varargout"); |
6591
|
388 |
7336
|
389 if (varargout_varval.is_defined ()) |
6591
|
390 { |
7336
|
391 varargout = varargout_varval.cell_value (); |
5848
|
392 |
6591
|
393 if (error_state) |
|
394 error ("expecting varargout to be a cell array object"); |
|
395 } |
5848
|
396 } |
|
397 |
|
398 if (! error_state) |
|
399 retval = ret_list->convert_to_const_vector (varargout); |
3871
|
400 } |
2974
|
401 } |
|
402 |
|
403 abort: |
4987
|
404 unwind_protect::run_frame ("user_func_eval"); |
2974
|
405 |
|
406 return retval; |
|
407 } |
|
408 |
|
409 void |
4346
|
410 octave_user_function::traceback_error (void) const |
2974
|
411 { |
|
412 if (error_state >= 0) |
|
413 error_state = -1; |
|
414 |
4748
|
415 if (my_name.empty ()) |
2974
|
416 { |
|
417 if (file_name.empty ()) |
|
418 ::error ("called from `?unknown?'"); |
|
419 else |
|
420 ::error ("called from file `%s'", file_name.c_str ()); |
|
421 } |
|
422 else |
|
423 { |
|
424 if (file_name.empty ()) |
4748
|
425 ::error ("called from `%s'", my_name.c_str ()); |
2974
|
426 else |
|
427 ::error ("called from `%s' in file `%s'", |
4748
|
428 my_name.c_str (), file_name.c_str ()); |
2974
|
429 } |
|
430 } |
|
431 |
|
432 void |
|
433 octave_user_function::accept (tree_walker& tw) |
|
434 { |
|
435 tw.visit_octave_user_function (*this); |
|
436 } |
|
437 |
7336
|
438 #if 0 |
2974
|
439 void |
3933
|
440 octave_user_function::print_symtab_info (std::ostream& os) const |
|
441 { |
7336
|
442 symbol_table::print_info (os, local_scope); |
3933
|
443 } |
7336
|
444 #endif |
3933
|
445 |
|
446 void |
2974
|
447 octave_user_function::print_code_function_header (void) |
|
448 { |
5794
|
449 tree_print_code tpc (octave_stdout, VPS4); |
2974
|
450 |
|
451 tpc.visit_octave_user_function_header (*this); |
|
452 } |
|
453 |
|
454 void |
|
455 octave_user_function::print_code_function_trailer (void) |
|
456 { |
5794
|
457 tree_print_code tpc (octave_stdout, VPS4); |
2974
|
458 |
|
459 tpc.visit_octave_user_function_trailer (*this); |
|
460 } |
|
461 |
|
462 void |
|
463 octave_user_function::bind_automatic_vars |
3974
|
464 (const string_vector& arg_names, int nargin, int nargout, |
|
465 const octave_value_list& va_args) |
2974
|
466 { |
|
467 if (! arg_names.empty ()) |
7336
|
468 symbol_table::varref ("argn") = arg_names; |
2974
|
469 |
7336
|
470 symbol_table::varref (".nargin.") = nargin; |
|
471 symbol_table::varref (".nargout.") = nargout; |
|
472 |
|
473 symbol_table::mark_hidden (".nargin."); |
|
474 symbol_table::mark_hidden (".nargout."); |
3974
|
475 |
|
476 if (takes_varargs ()) |
|
477 { |
|
478 int n = va_args.length (); |
|
479 |
|
480 Cell varargin (1, n); |
|
481 |
|
482 for (int i = 0; i < n; i++) |
|
483 varargin(0,i) = va_args(i); |
|
484 |
7336
|
485 symbol_table::varref ("varargin") = varargin; |
3974
|
486 } |
2974
|
487 } |
|
488 |
4700
|
489 DEFUN (nargin, args, , |
|
490 "-*- texinfo -*-\n\ |
|
491 @deftypefn {Built-in Function} {} nargin ()\n\ |
|
492 @deftypefnx {Built-in Function} {} nargin (@var{fcn_name})\n\ |
|
493 Within a function, return the number of arguments passed to the function.\n\ |
|
494 At the top level, return the number of command line arguments passed to\n\ |
|
495 Octave. If called with the optional argument @var{fcn_name}, return the\n\ |
|
496 maximum number of arguments the named function can accept, or -1 if the\n\ |
|
497 function accepts a variable number of arguments.\n\ |
5642
|
498 @seealso{nargout, varargin, varargout}\n\ |
4700
|
499 @end deftypefn") |
|
500 { |
|
501 octave_value retval; |
|
502 |
|
503 int nargin = args.length (); |
|
504 |
|
505 if (nargin == 1) |
|
506 { |
|
507 std::string fname = args(0).string_value (); |
|
508 |
|
509 if (! error_state) |
|
510 { |
7336
|
511 octave_value fcn_val = symbol_table::find_user_function (fname); |
4930
|
512 |
|
513 octave_user_function *fcn = fcn_val.user_function_value (true); |
4700
|
514 |
|
515 if (fcn) |
|
516 { |
|
517 if (fcn->takes_varargs ()) |
|
518 retval = -1; |
|
519 else |
|
520 { |
|
521 tree_parameter_list *param_list = fcn->parameter_list (); |
|
522 |
|
523 retval = param_list ? param_list->length () : 0; |
|
524 } |
|
525 } |
|
526 else |
|
527 error ("nargin: invalid function"); |
|
528 } |
|
529 else |
|
530 error ("nargin: expecting string as first argument"); |
|
531 } |
|
532 else if (nargin == 0) |
|
533 { |
7336
|
534 retval = symbol_table::varval (".nargin."); |
4700
|
535 |
7336
|
536 if (retval.is_undefined ()) |
|
537 retval = 0; |
4700
|
538 } |
|
539 else |
5823
|
540 print_usage (); |
4700
|
541 |
|
542 return retval; |
|
543 } |
|
544 |
|
545 DEFUN (nargout, args, , |
|
546 "-*- texinfo -*-\n\ |
|
547 @deftypefn {Built-in Function} {} nargout ()\n\ |
|
548 @deftypefnx {Built-in Function} {} nargout (@var{fcn_name})\n\ |
|
549 Within a function, return the number of values the caller expects to\n\ |
|
550 receive. If called with the optional argument @var{fcn_name}, return the\n\ |
|
551 maximum number of values the named function can produce, or -1 if the\n\ |
|
552 function can produce a variable number of values.\n\ |
|
553 \n\ |
|
554 For example,\n\ |
|
555 \n\ |
|
556 @example\n\ |
|
557 f ()\n\ |
|
558 @end example\n\ |
|
559 \n\ |
|
560 @noindent\n\ |
4704
|
561 will cause @code{nargout} to return 0 inside the function @code{f} and\n\ |
4700
|
562 \n\ |
|
563 @example\n\ |
|
564 [s, t] = f ()\n\ |
|
565 @end example\n\ |
|
566 \n\ |
|
567 @noindent\n\ |
|
568 will cause @code{nargout} to return 2 inside the function\n\ |
|
569 @code{f}.\n\ |
|
570 \n\ |
|
571 At the top level, @code{nargout} is undefined.\n\ |
5642
|
572 @seealso{nargin, varargin, varargout}\n\ |
4700
|
573 @end deftypefn") |
|
574 { |
|
575 octave_value retval; |
|
576 |
|
577 int nargin = args.length (); |
|
578 |
|
579 if (nargin == 1) |
|
580 { |
|
581 std::string fname = args(0).string_value (); |
|
582 |
|
583 if (! error_state) |
|
584 { |
7336
|
585 octave_value fcn_val = symbol_table::find_user_function (fname); |
4930
|
586 |
|
587 octave_user_function *fcn = fcn_val.user_function_value (true); |
4700
|
588 |
|
589 if (fcn) |
|
590 { |
|
591 if (fcn->takes_var_return ()) |
|
592 retval = -1; |
|
593 else |
|
594 { |
|
595 tree_parameter_list *ret_list = fcn->return_list (); |
|
596 |
|
597 retval = ret_list ? ret_list->length () : 0; |
|
598 } |
|
599 } |
|
600 else |
|
601 error ("nargout: invalid function"); |
|
602 } |
|
603 else |
|
604 error ("nargout: expecting string as first argument"); |
|
605 } |
|
606 else if (nargin == 0) |
|
607 { |
7336
|
608 if (! symbol_table::at_top_level ()) |
4700
|
609 { |
7336
|
610 retval = symbol_table::varval (".nargout."); |
4700
|
611 |
7336
|
612 if (retval.is_undefined ()) |
|
613 retval = 0; |
4700
|
614 } |
|
615 else |
|
616 error ("nargout: invalid call at top level"); |
|
617 } |
|
618 else |
5823
|
619 print_usage (); |
4700
|
620 |
|
621 return retval; |
|
622 } |
|
623 |
5794
|
624 DEFUN (max_recursion_depth, args, nargout, |
|
625 "-*- texinfo -*-\n\ |
|
626 @deftypefn {Built-in Function} {@var{val} =} max_recursion_depth ()\n\ |
|
627 @deftypefnx {Built-in Function} {@var{old_val} =} max_recursion_depth (@var{new_val})\n\ |
|
628 Query or set the internal limit on the number of times a function may\n\ |
|
629 be called recursively. If the limit is exceeded, an error message is\n\ |
|
630 printed and control returns to the top level.\n\ |
|
631 @end deftypefn") |
3131
|
632 { |
5794
|
633 return SET_INTERNAL_VARIABLE (max_recursion_depth); |
2974
|
634 } |
|
635 |
|
636 /* |
|
637 ;;; Local Variables: *** |
|
638 ;;; mode: C++ *** |
|
639 ;;; End: *** |
|
640 */ |