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), |
4748
|
76 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; |
|
92 delete sym_tab; |
|
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 { |
|
280 sym_tab->push_context (); |
2985
|
281 unwind_protect::add (pop_symbol_table_context, sym_tab); |
2974
|
282 } |
|
283 |
4929
|
284 install_automatic_vars (); |
|
285 |
2974
|
286 // Force symbols to be undefined again when this function exits. |
|
287 |
2985
|
288 unwind_protect::add (clear_symbol_table, 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); |
|
297 curr_sym_tab = sym_tab; |
|
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 |
|
384 symbol_record *sr = sym_tab->lookup ("varargout"); |
|
385 |
|
386 if (sr && sr->is_variable ()) |
|
387 { |
|
388 octave_value v = sr->def (); |
3974
|
389 |
5848
|
390 varargout = v.cell_value (); |
|
391 |
|
392 if (error_state) |
|
393 error ("expecting varargout to be a cell array object"); |
|
394 } |
|
395 |
|
396 if (! error_state) |
|
397 retval = ret_list->convert_to_const_vector (varargout); |
3871
|
398 } |
2974
|
399 } |
|
400 |
|
401 abort: |
4987
|
402 unwind_protect::run_frame ("user_func_eval"); |
2974
|
403 |
|
404 return retval; |
|
405 } |
|
406 |
|
407 void |
4346
|
408 octave_user_function::traceback_error (void) const |
2974
|
409 { |
|
410 if (error_state >= 0) |
|
411 error_state = -1; |
|
412 |
4748
|
413 if (my_name.empty ()) |
2974
|
414 { |
|
415 if (file_name.empty ()) |
|
416 ::error ("called from `?unknown?'"); |
|
417 else |
|
418 ::error ("called from file `%s'", file_name.c_str ()); |
|
419 } |
|
420 else |
|
421 { |
|
422 if (file_name.empty ()) |
4748
|
423 ::error ("called from `%s'", my_name.c_str ()); |
2974
|
424 else |
|
425 ::error ("called from `%s' in file `%s'", |
4748
|
426 my_name.c_str (), file_name.c_str ()); |
2974
|
427 } |
|
428 } |
|
429 |
|
430 void |
|
431 octave_user_function::accept (tree_walker& tw) |
|
432 { |
|
433 tw.visit_octave_user_function (*this); |
|
434 } |
|
435 |
|
436 void |
3933
|
437 octave_user_function::print_symtab_info (std::ostream& os) const |
|
438 { |
|
439 if (sym_tab) |
|
440 sym_tab->print_info (os); |
|
441 else |
4748
|
442 warning ("%s: no symbol table info!", my_name.c_str ()); |
3933
|
443 } |
|
444 |
|
445 void |
2974
|
446 octave_user_function::print_code_function_header (void) |
|
447 { |
5794
|
448 tree_print_code tpc (octave_stdout, VPS4); |
2974
|
449 |
|
450 tpc.visit_octave_user_function_header (*this); |
|
451 } |
|
452 |
|
453 void |
|
454 octave_user_function::print_code_function_trailer (void) |
|
455 { |
5794
|
456 tree_print_code tpc (octave_stdout, VPS4); |
2974
|
457 |
|
458 tpc.visit_octave_user_function_trailer (*this); |
|
459 } |
|
460 |
|
461 void |
|
462 octave_user_function::install_automatic_vars (void) |
|
463 { |
4645
|
464 if (sym_tab) |
|
465 { |
|
466 argn_sr = sym_tab->lookup ("argn", true); |
4700
|
467 nargin_sr = sym_tab->lookup ("__nargin__", true); |
|
468 nargout_sr = sym_tab->lookup ("__nargout__", true); |
3974
|
469 |
4645
|
470 if (takes_varargs ()) |
4929
|
471 varargin_sr = sym_tab->lookup ("varargin", true); |
4645
|
472 } |
2974
|
473 } |
|
474 |
|
475 void |
|
476 octave_user_function::bind_automatic_vars |
3974
|
477 (const string_vector& arg_names, int nargin, int nargout, |
|
478 const octave_value_list& va_args) |
2974
|
479 { |
|
480 if (! arg_names.empty ()) |
|
481 argn_sr->define (arg_names); |
|
482 |
4233
|
483 nargin_sr->define (nargin); |
|
484 nargout_sr->define (nargout); |
3974
|
485 |
|
486 if (takes_varargs ()) |
|
487 { |
|
488 int n = va_args.length (); |
|
489 |
|
490 Cell varargin (1, n); |
|
491 |
|
492 for (int i = 0; i < n; i++) |
|
493 varargin(0,i) = va_args(i); |
|
494 |
|
495 varargin_sr->define (varargin); |
|
496 } |
2974
|
497 } |
|
498 |
4700
|
499 DEFUN (nargin, args, , |
|
500 "-*- texinfo -*-\n\ |
|
501 @deftypefn {Built-in Function} {} nargin ()\n\ |
|
502 @deftypefnx {Built-in Function} {} nargin (@var{fcn_name})\n\ |
|
503 Within a function, return the number of arguments passed to the function.\n\ |
|
504 At the top level, return the number of command line arguments passed to\n\ |
|
505 Octave. If called with the optional argument @var{fcn_name}, return the\n\ |
|
506 maximum number of arguments the named function can accept, or -1 if the\n\ |
|
507 function accepts a variable number of arguments.\n\ |
5642
|
508 @seealso{nargout, varargin, varargout}\n\ |
4700
|
509 @end deftypefn") |
|
510 { |
|
511 octave_value retval; |
|
512 |
|
513 int nargin = args.length (); |
|
514 |
|
515 if (nargin == 1) |
|
516 { |
|
517 std::string fname = args(0).string_value (); |
|
518 |
|
519 if (! error_state) |
|
520 { |
4930
|
521 octave_value fcn_val = lookup_user_function (fname); |
|
522 |
|
523 octave_user_function *fcn = fcn_val.user_function_value (true); |
4700
|
524 |
|
525 if (fcn) |
|
526 { |
|
527 if (fcn->takes_varargs ()) |
|
528 retval = -1; |
|
529 else |
|
530 { |
|
531 tree_parameter_list *param_list = fcn->parameter_list (); |
|
532 |
|
533 retval = param_list ? param_list->length () : 0; |
|
534 } |
|
535 } |
|
536 else |
|
537 error ("nargin: invalid function"); |
|
538 } |
|
539 else |
|
540 error ("nargin: expecting string as first argument"); |
|
541 } |
|
542 else if (nargin == 0) |
|
543 { |
|
544 symbol_record *sr = curr_sym_tab->lookup ("__nargin__"); |
|
545 |
|
546 retval = sr ? sr->def () : 0; |
|
547 } |
|
548 else |
5823
|
549 print_usage (); |
4700
|
550 |
|
551 return retval; |
|
552 } |
|
553 |
|
554 DEFUN (nargout, args, , |
|
555 "-*- texinfo -*-\n\ |
|
556 @deftypefn {Built-in Function} {} nargout ()\n\ |
|
557 @deftypefnx {Built-in Function} {} nargout (@var{fcn_name})\n\ |
|
558 Within a function, return the number of values the caller expects to\n\ |
|
559 receive. If called with the optional argument @var{fcn_name}, return the\n\ |
|
560 maximum number of values the named function can produce, or -1 if the\n\ |
|
561 function can produce a variable number of values.\n\ |
|
562 \n\ |
|
563 For example,\n\ |
|
564 \n\ |
|
565 @example\n\ |
|
566 f ()\n\ |
|
567 @end example\n\ |
|
568 \n\ |
|
569 @noindent\n\ |
4704
|
570 will cause @code{nargout} to return 0 inside the function @code{f} and\n\ |
4700
|
571 \n\ |
|
572 @example\n\ |
|
573 [s, t] = f ()\n\ |
|
574 @end example\n\ |
|
575 \n\ |
|
576 @noindent\n\ |
|
577 will cause @code{nargout} to return 2 inside the function\n\ |
|
578 @code{f}.\n\ |
|
579 \n\ |
|
580 At the top level, @code{nargout} is undefined.\n\ |
5642
|
581 @seealso{nargin, varargin, varargout}\n\ |
4700
|
582 @end deftypefn") |
|
583 { |
|
584 octave_value retval; |
|
585 |
|
586 int nargin = args.length (); |
|
587 |
|
588 if (nargin == 1) |
|
589 { |
|
590 std::string fname = args(0).string_value (); |
|
591 |
|
592 if (! error_state) |
|
593 { |
4930
|
594 octave_value fcn_val = lookup_user_function (fname); |
|
595 |
|
596 octave_user_function *fcn = fcn_val.user_function_value (true); |
4700
|
597 |
|
598 if (fcn) |
|
599 { |
|
600 if (fcn->takes_var_return ()) |
|
601 retval = -1; |
|
602 else |
|
603 { |
|
604 tree_parameter_list *ret_list = fcn->return_list (); |
|
605 |
|
606 retval = ret_list ? ret_list->length () : 0; |
|
607 } |
|
608 } |
|
609 else |
|
610 error ("nargout: invalid function"); |
|
611 } |
|
612 else |
|
613 error ("nargout: expecting string as first argument"); |
|
614 } |
|
615 else if (nargin == 0) |
|
616 { |
|
617 if (! at_top_level ()) |
|
618 { |
|
619 symbol_record *sr = curr_sym_tab->lookup ("__nargout__"); |
|
620 |
|
621 retval = sr ? sr->def () : 0; |
|
622 } |
|
623 else |
|
624 error ("nargout: invalid call at top level"); |
|
625 } |
|
626 else |
5823
|
627 print_usage (); |
4700
|
628 |
|
629 return retval; |
|
630 } |
|
631 |
5794
|
632 DEFUN (max_recursion_depth, args, nargout, |
|
633 "-*- texinfo -*-\n\ |
|
634 @deftypefn {Built-in Function} {@var{val} =} max_recursion_depth ()\n\ |
|
635 @deftypefnx {Built-in Function} {@var{old_val} =} max_recursion_depth (@var{new_val})\n\ |
|
636 Query or set the internal limit on the number of times a function may\n\ |
|
637 be called recursively. If the limit is exceeded, an error message is\n\ |
|
638 printed and control returns to the top level.\n\ |
|
639 @end deftypefn") |
3131
|
640 { |
5794
|
641 return SET_INTERNAL_VARIABLE (max_recursion_depth); |
2974
|
642 } |
|
643 |
|
644 /* |
|
645 ;;; Local Variables: *** |
|
646 ;;; mode: C++ *** |
|
647 ;;; End: *** |
|
648 */ |