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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
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 |
2974
|
54 // User defined functions. |
|
55 |
3219
|
56 DEFINE_OCTAVE_ALLOCATOR (octave_user_function); |
2974
|
57 |
3219
|
58 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_user_function, |
4612
|
59 "user-defined function", |
3219
|
60 "user-defined function"); |
2974
|
61 |
5744
|
62 DEFINE_OCTAVE_ALLOCATOR (octave_user_script); |
|
63 |
|
64 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_user_script, |
|
65 "user-defined script", |
|
66 "user-defined script"); |
|
67 |
2974
|
68 // Ugh. This really needs to be simplified (code/data? |
|
69 // extrinsic/intrinsic state?). |
|
70 |
|
71 octave_user_function::octave_user_function |
|
72 (tree_parameter_list *pl, tree_parameter_list *rl, |
|
73 tree_statement_list *cl, symbol_table *st) |
3523
|
74 : octave_function (std::string (), std::string ()), |
2974
|
75 param_list (pl), ret_list (rl), cmd_list (cl), |
6505
|
76 local_sym_tab (st), lead_comm (), trail_comm (), file_name (), |
6323
|
77 parent_name (), t_parsed (static_cast<time_t> (0)), |
3255
|
78 t_checked (static_cast<time_t> (0)), |
6149
|
79 system_fcn_file (false), call_depth (0), num_named_args (0), |
|
80 nested_function (false), inline_function (false), args_passed (), |
|
81 num_args_passed (0), symtab_entry (0), argn_sr (0), |
|
82 nargin_sr (0), nargout_sr (0), varargin_sr (0) |
2974
|
83 { |
|
84 if (param_list) |
5848
|
85 num_named_args = param_list->length (); |
2974
|
86 } |
|
87 |
|
88 octave_user_function::~octave_user_function (void) |
|
89 { |
|
90 delete param_list; |
|
91 delete ret_list; |
6505
|
92 delete local_sym_tab; |
2974
|
93 delete cmd_list; |
3665
|
94 delete lead_comm; |
|
95 delete trail_comm; |
2974
|
96 } |
|
97 |
|
98 octave_user_function * |
|
99 octave_user_function::define_ret_list (tree_parameter_list *t) |
|
100 { |
|
101 ret_list = t; |
|
102 |
|
103 return this; |
|
104 } |
|
105 |
|
106 void |
4343
|
107 octave_user_function::stash_fcn_file_name (const std::string& nm) |
2974
|
108 { |
4343
|
109 file_name = nm; |
2974
|
110 } |
|
111 |
|
112 void |
|
113 octave_user_function::mark_as_system_fcn_file (void) |
|
114 { |
|
115 if (! file_name.empty ()) |
|
116 { |
|
117 // We really should stash the whole path to the file we found, |
|
118 // when we looked it up, to avoid possible race conditions... |
5775
|
119 // FIXME |
2974
|
120 // |
|
121 // We probably also don't need to get the library directory |
|
122 // every time, but since this function is only called when the |
|
123 // function file is parsed, it probably doesn't matter that |
|
124 // much. |
|
125 |
3523
|
126 std::string ff_name = fcn_file_in_path (file_name); |
2974
|
127 |
3565
|
128 if (Vfcn_file_dir == ff_name.substr (0, Vfcn_file_dir.length ())) |
2974
|
129 system_fcn_file = 1; |
|
130 } |
|
131 else |
|
132 system_fcn_file = 0; |
|
133 } |
|
134 |
|
135 bool |
|
136 octave_user_function::takes_varargs (void) const |
|
137 { |
|
138 return (param_list && param_list->takes_varargs ()); |
|
139 } |
|
140 |
5848
|
141 bool |
|
142 octave_user_function::takes_var_return (void) const |
2974
|
143 { |
5848
|
144 return (ret_list && ret_list->takes_varargs ()); |
2974
|
145 } |
|
146 |
|
147 octave_value_list |
|
148 octave_user_function::octave_all_va_args (void) |
|
149 { |
|
150 octave_value_list retval; |
|
151 |
3178
|
152 int n = num_args_passed - num_named_args; |
2974
|
153 |
3178
|
154 if (n > 0) |
|
155 { |
|
156 retval.resize (n); |
|
157 |
|
158 int k = 0; |
|
159 for (int i = num_named_args; i < num_args_passed; i++) |
|
160 retval(k++) = args_passed(i); |
|
161 } |
2974
|
162 |
|
163 return retval; |
|
164 } |
|
165 |
|
166 // For unwind protect. |
|
167 |
|
168 static void |
|
169 pop_symbol_table_context (void *table) |
|
170 { |
|
171 symbol_table *tmp = static_cast<symbol_table *> (table); |
|
172 tmp->pop_context (); |
|
173 } |
|
174 |
|
175 static void |
|
176 clear_symbol_table (void *table) |
|
177 { |
|
178 symbol_table *tmp = static_cast<symbol_table *> (table); |
4239
|
179 tmp->clear (); |
2974
|
180 } |
|
181 |
|
182 static void |
3239
|
183 clear_param_list (void *lst) |
|
184 { |
|
185 tree_parameter_list *tmp = static_cast<tree_parameter_list *> (lst); |
|
186 |
|
187 if (tmp) |
4219
|
188 tmp->undefine (); |
3239
|
189 } |
|
190 |
|
191 static void |
3875
|
192 restore_args_passed (void *fcn) |
3239
|
193 { |
|
194 octave_user_function *tmp = static_cast<octave_user_function *> (fcn); |
|
195 |
|
196 if (tmp) |
3875
|
197 tmp->restore_args_passed (); |
3239
|
198 } |
|
199 |
|
200 static void |
2974
|
201 unprotect_function (void *sr_arg) |
|
202 { |
|
203 symbol_record *sr = static_cast<symbol_record *> (sr_arg); |
|
204 sr->unprotect (); |
|
205 } |
|
206 |
|
207 octave_value_list |
4247
|
208 octave_user_function::subsref (const std::string& type, |
4219
|
209 const std::list<octave_value_list>& idx, |
3933
|
210 int nargout) |
|
211 { |
|
212 octave_value_list retval; |
|
213 |
|
214 switch (type[0]) |
|
215 { |
|
216 case '(': |
5154
|
217 { |
5155
|
218 int tmp_nargout = (type.length () > 1 && nargout == 0) ? 1 : nargout; |
5154
|
219 |
|
220 retval = do_multi_index_op (tmp_nargout, idx.front ()); |
|
221 } |
3933
|
222 break; |
|
223 |
|
224 case '{': |
|
225 case '.': |
|
226 { |
|
227 std::string nm = type_name (); |
|
228 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
229 } |
|
230 break; |
|
231 |
|
232 default: |
|
233 panic_impossible (); |
|
234 } |
|
235 |
5775
|
236 // FIXME -- perhaps there should be an |
4059
|
237 // octave_value_list::next_subsref member function? See also |
|
238 // octave_builtin::subsref. |
3933
|
239 |
4219
|
240 if (idx.size () > 1) |
4994
|
241 retval = retval(0).next_subsref (nargout, type, idx); |
4059
|
242 |
|
243 return retval; |
3933
|
244 } |
|
245 |
|
246 octave_value_list |
3544
|
247 octave_user_function::do_multi_index_op (int nargout, |
|
248 const octave_value_list& args) |
2974
|
249 { |
|
250 octave_value_list retval; |
|
251 |
|
252 if (error_state) |
|
253 return retval; |
|
254 |
|
255 if (! cmd_list) |
|
256 return retval; |
|
257 |
|
258 int nargin = args.length (); |
|
259 |
4987
|
260 unwind_protect::begin_frame ("user_func_eval"); |
2974
|
261 |
|
262 unwind_protect_int (call_depth); |
|
263 call_depth++; |
|
264 |
3131
|
265 if (call_depth > Vmax_recursion_depth) |
|
266 { |
|
267 ::error ("max_recursion_limit exceeded"); |
4987
|
268 unwind_protect::run_frame ("user_func_eval"); |
3131
|
269 return retval; |
|
270 } |
|
271 |
2974
|
272 if (symtab_entry && ! symtab_entry->is_read_only ()) |
|
273 { |
|
274 symtab_entry->protect (); |
2985
|
275 unwind_protect::add (unprotect_function, symtab_entry); |
2974
|
276 } |
|
277 |
|
278 if (call_depth > 1) |
|
279 { |
6505
|
280 local_sym_tab->push_context (); |
|
281 unwind_protect::add (pop_symbol_table_context, local_sym_tab); |
2974
|
282 } |
|
283 |
4929
|
284 install_automatic_vars (); |
|
285 |
2974
|
286 // Force symbols to be undefined again when this function exits. |
|
287 |
6505
|
288 unwind_protect::add (clear_symbol_table, local_sym_tab); |
2974
|
289 |
|
290 // Save old and set current symbol table context, for |
|
291 // eval_undefined_error(). |
|
292 |
4245
|
293 unwind_protect_ptr (curr_caller_sym_tab); |
|
294 curr_caller_sym_tab = curr_sym_tab; |
|
295 |
2974
|
296 unwind_protect_ptr (curr_sym_tab); |
6505
|
297 curr_sym_tab = local_sym_tab; |
2974
|
298 |
4975
|
299 unwind_protect_ptr (curr_caller_statement); |
5743
|
300 curr_caller_statement = curr_statement; |
4975
|
301 |
5743
|
302 octave_call_stack::push (this); |
|
303 |
|
304 unwind_protect::add (octave_call_stack::unwind_pop, 0); |
2974
|
305 |
6149
|
306 if (! (is_nested_function () || is_inline_function ())) |
4238
|
307 { |
|
308 unwind_protect_ptr (curr_parent_function); |
|
309 curr_parent_function = this; |
|
310 } |
|
311 |
3875
|
312 // Save and restore args passed for recursive calls. |
2974
|
313 |
3875
|
314 save_args_passed (args); |
3239
|
315 |
3875
|
316 unwind_protect::add (::restore_args_passed, this); |
3239
|
317 |
2974
|
318 string_vector arg_names = args.name_tags (); |
|
319 |
|
320 unwind_protect_int (num_args_passed); |
|
321 num_args_passed = nargin; |
|
322 |
|
323 if (param_list && ! param_list->varargs_only ()) |
|
324 { |
|
325 param_list->define_from_arg_vector (args); |
|
326 if (error_state) |
|
327 goto abort; |
|
328 } |
|
329 |
3239
|
330 // Force parameter 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 function parameters. |
|
333 |
|
334 unwind_protect::add (clear_param_list, param_list); |
|
335 |
|
336 // Force return list to be undefined when this function exits. |
|
337 // Doing so decrements the reference counts on the values of local |
|
338 // variables that are also named values returned by this function. |
|
339 |
|
340 unwind_protect::add (clear_param_list, ret_list); |
|
341 |
2974
|
342 // The following code is in a separate scope to avoid warnings from |
|
343 // G++ about `goto abort' crossing the initialization of some |
|
344 // variables. |
|
345 |
|
346 { |
3974
|
347 bind_automatic_vars (arg_names, nargin, nargout, octave_all_va_args ()); |
2974
|
348 |
|
349 bool echo_commands = (Vecho_executing_commands & ECHO_FUNCTIONS); |
|
350 |
|
351 if (echo_commands) |
|
352 print_code_function_header (); |
|
353 |
|
354 // Evaluate the commands that make up the function. |
|
355 |
3489
|
356 unwind_protect_bool (evaluating_function_body); |
|
357 evaluating_function_body = true; |
|
358 |
5856
|
359 cmd_list->eval (); |
2974
|
360 |
|
361 if (echo_commands) |
|
362 print_code_function_trailer (); |
|
363 |
4207
|
364 if (tree_return_command::returning) |
|
365 tree_return_command::returning = 0; |
2974
|
366 |
4207
|
367 if (tree_break_command::breaking) |
|
368 tree_break_command::breaking--; |
2974
|
369 |
|
370 if (error_state) |
|
371 { |
|
372 traceback_error (); |
|
373 goto abort; |
|
374 } |
|
375 |
|
376 // Copy return values out. |
|
377 |
|
378 if (ret_list) |
3871
|
379 { |
4748
|
380 ret_list->initialize_undefined_elements (my_name, nargout, Matrix ()); |
3871
|
381 |
5848
|
382 Cell varargout; |
|
383 |
6591
|
384 if (ret_list->takes_varargs ()) |
5848
|
385 { |
6591
|
386 symbol_record *sr = local_sym_tab->lookup ("varargout"); |
|
387 |
|
388 if (sr && sr->is_variable ()) |
|
389 { |
|
390 octave_value v = sr->def (); |
3974
|
391 |
6591
|
392 varargout = v.cell_value (); |
5848
|
393 |
6591
|
394 if (error_state) |
|
395 error ("expecting varargout to be a cell array object"); |
|
396 } |
5848
|
397 } |
|
398 |
|
399 if (! error_state) |
|
400 retval = ret_list->convert_to_const_vector (varargout); |
3871
|
401 } |
2974
|
402 } |
|
403 |
|
404 abort: |
4987
|
405 unwind_protect::run_frame ("user_func_eval"); |
2974
|
406 |
|
407 return retval; |
|
408 } |
|
409 |
|
410 void |
4346
|
411 octave_user_function::traceback_error (void) const |
2974
|
412 { |
|
413 if (error_state >= 0) |
|
414 error_state = -1; |
|
415 |
4748
|
416 if (my_name.empty ()) |
2974
|
417 { |
|
418 if (file_name.empty ()) |
|
419 ::error ("called from `?unknown?'"); |
|
420 else |
|
421 ::error ("called from file `%s'", file_name.c_str ()); |
|
422 } |
|
423 else |
|
424 { |
|
425 if (file_name.empty ()) |
4748
|
426 ::error ("called from `%s'", my_name.c_str ()); |
2974
|
427 else |
|
428 ::error ("called from `%s' in file `%s'", |
4748
|
429 my_name.c_str (), file_name.c_str ()); |
2974
|
430 } |
|
431 } |
|
432 |
|
433 void |
|
434 octave_user_function::accept (tree_walker& tw) |
|
435 { |
|
436 tw.visit_octave_user_function (*this); |
|
437 } |
|
438 |
|
439 void |
3933
|
440 octave_user_function::print_symtab_info (std::ostream& os) const |
|
441 { |
6505
|
442 if (local_sym_tab) |
|
443 local_sym_tab->print_info (os); |
3933
|
444 else |
4748
|
445 warning ("%s: no symbol table info!", my_name.c_str ()); |
3933
|
446 } |
|
447 |
|
448 void |
2974
|
449 octave_user_function::print_code_function_header (void) |
|
450 { |
5794
|
451 tree_print_code tpc (octave_stdout, VPS4); |
2974
|
452 |
|
453 tpc.visit_octave_user_function_header (*this); |
|
454 } |
|
455 |
|
456 void |
|
457 octave_user_function::print_code_function_trailer (void) |
|
458 { |
5794
|
459 tree_print_code tpc (octave_stdout, VPS4); |
2974
|
460 |
|
461 tpc.visit_octave_user_function_trailer (*this); |
|
462 } |
|
463 |
|
464 void |
|
465 octave_user_function::install_automatic_vars (void) |
|
466 { |
6505
|
467 if (local_sym_tab) |
4645
|
468 { |
6505
|
469 argn_sr = local_sym_tab->lookup ("argn", true); |
|
470 nargin_sr = local_sym_tab->lookup ("__nargin__", true); |
|
471 nargout_sr = local_sym_tab->lookup ("__nargout__", true); |
3974
|
472 |
4645
|
473 if (takes_varargs ()) |
6505
|
474 varargin_sr = local_sym_tab->lookup ("varargin", true); |
4645
|
475 } |
2974
|
476 } |
|
477 |
|
478 void |
|
479 octave_user_function::bind_automatic_vars |
3974
|
480 (const string_vector& arg_names, int nargin, int nargout, |
|
481 const octave_value_list& va_args) |
2974
|
482 { |
|
483 if (! arg_names.empty ()) |
|
484 argn_sr->define (arg_names); |
|
485 |
4233
|
486 nargin_sr->define (nargin); |
|
487 nargout_sr->define (nargout); |
3974
|
488 |
|
489 if (takes_varargs ()) |
|
490 { |
|
491 int n = va_args.length (); |
|
492 |
|
493 Cell varargin (1, n); |
|
494 |
|
495 for (int i = 0; i < n; i++) |
|
496 varargin(0,i) = va_args(i); |
|
497 |
|
498 varargin_sr->define (varargin); |
|
499 } |
2974
|
500 } |
|
501 |
4700
|
502 DEFUN (nargin, args, , |
|
503 "-*- texinfo -*-\n\ |
|
504 @deftypefn {Built-in Function} {} nargin ()\n\ |
|
505 @deftypefnx {Built-in Function} {} nargin (@var{fcn_name})\n\ |
|
506 Within a function, return the number of arguments passed to the function.\n\ |
|
507 At the top level, return the number of command line arguments passed to\n\ |
|
508 Octave. If called with the optional argument @var{fcn_name}, return the\n\ |
|
509 maximum number of arguments the named function can accept, or -1 if the\n\ |
|
510 function accepts a variable number of arguments.\n\ |
5642
|
511 @seealso{nargout, varargin, varargout}\n\ |
4700
|
512 @end deftypefn") |
|
513 { |
|
514 octave_value retval; |
|
515 |
|
516 int nargin = args.length (); |
|
517 |
|
518 if (nargin == 1) |
|
519 { |
|
520 std::string fname = args(0).string_value (); |
|
521 |
|
522 if (! error_state) |
|
523 { |
4930
|
524 octave_value fcn_val = lookup_user_function (fname); |
|
525 |
|
526 octave_user_function *fcn = fcn_val.user_function_value (true); |
4700
|
527 |
|
528 if (fcn) |
|
529 { |
|
530 if (fcn->takes_varargs ()) |
|
531 retval = -1; |
|
532 else |
|
533 { |
|
534 tree_parameter_list *param_list = fcn->parameter_list (); |
|
535 |
|
536 retval = param_list ? param_list->length () : 0; |
|
537 } |
|
538 } |
|
539 else |
|
540 error ("nargin: invalid function"); |
|
541 } |
|
542 else |
|
543 error ("nargin: expecting string as first argument"); |
|
544 } |
|
545 else if (nargin == 0) |
|
546 { |
|
547 symbol_record *sr = curr_sym_tab->lookup ("__nargin__"); |
|
548 |
|
549 retval = sr ? sr->def () : 0; |
|
550 } |
|
551 else |
5823
|
552 print_usage (); |
4700
|
553 |
|
554 return retval; |
|
555 } |
|
556 |
|
557 DEFUN (nargout, args, , |
|
558 "-*- texinfo -*-\n\ |
|
559 @deftypefn {Built-in Function} {} nargout ()\n\ |
|
560 @deftypefnx {Built-in Function} {} nargout (@var{fcn_name})\n\ |
|
561 Within a function, return the number of values the caller expects to\n\ |
|
562 receive. If called with the optional argument @var{fcn_name}, return the\n\ |
|
563 maximum number of values the named function can produce, or -1 if the\n\ |
|
564 function can produce a variable number of values.\n\ |
|
565 \n\ |
|
566 For example,\n\ |
|
567 \n\ |
|
568 @example\n\ |
|
569 f ()\n\ |
|
570 @end example\n\ |
|
571 \n\ |
|
572 @noindent\n\ |
4704
|
573 will cause @code{nargout} to return 0 inside the function @code{f} and\n\ |
4700
|
574 \n\ |
|
575 @example\n\ |
|
576 [s, t] = f ()\n\ |
|
577 @end example\n\ |
|
578 \n\ |
|
579 @noindent\n\ |
|
580 will cause @code{nargout} to return 2 inside the function\n\ |
|
581 @code{f}.\n\ |
|
582 \n\ |
|
583 At the top level, @code{nargout} is undefined.\n\ |
5642
|
584 @seealso{nargin, varargin, varargout}\n\ |
4700
|
585 @end deftypefn") |
|
586 { |
|
587 octave_value retval; |
|
588 |
|
589 int nargin = args.length (); |
|
590 |
|
591 if (nargin == 1) |
|
592 { |
|
593 std::string fname = args(0).string_value (); |
|
594 |
|
595 if (! error_state) |
|
596 { |
4930
|
597 octave_value fcn_val = lookup_user_function (fname); |
|
598 |
|
599 octave_user_function *fcn = fcn_val.user_function_value (true); |
4700
|
600 |
|
601 if (fcn) |
|
602 { |
|
603 if (fcn->takes_var_return ()) |
|
604 retval = -1; |
|
605 else |
|
606 { |
|
607 tree_parameter_list *ret_list = fcn->return_list (); |
|
608 |
|
609 retval = ret_list ? ret_list->length () : 0; |
|
610 } |
|
611 } |
|
612 else |
|
613 error ("nargout: invalid function"); |
|
614 } |
|
615 else |
|
616 error ("nargout: expecting string as first argument"); |
|
617 } |
|
618 else if (nargin == 0) |
|
619 { |
|
620 if (! at_top_level ()) |
|
621 { |
|
622 symbol_record *sr = curr_sym_tab->lookup ("__nargout__"); |
|
623 |
|
624 retval = sr ? sr->def () : 0; |
|
625 } |
|
626 else |
|
627 error ("nargout: invalid call at top level"); |
|
628 } |
|
629 else |
5823
|
630 print_usage (); |
4700
|
631 |
|
632 return retval; |
|
633 } |
|
634 |
5794
|
635 DEFUN (max_recursion_depth, args, nargout, |
|
636 "-*- texinfo -*-\n\ |
|
637 @deftypefn {Built-in Function} {@var{val} =} max_recursion_depth ()\n\ |
|
638 @deftypefnx {Built-in Function} {@var{old_val} =} max_recursion_depth (@var{new_val})\n\ |
|
639 Query or set the internal limit on the number of times a function may\n\ |
|
640 be called recursively. If the limit is exceeded, an error message is\n\ |
|
641 printed and control returns to the top level.\n\ |
|
642 @end deftypefn") |
3131
|
643 { |
5794
|
644 return SET_INTERNAL_VARIABLE (max_recursion_depth); |
2974
|
645 } |
|
646 |
|
647 /* |
|
648 ;;; Local Variables: *** |
|
649 ;;; mode: C++ *** |
|
650 ;;; End: *** |
|
651 */ |