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> |
|
34 #include "defun.h" |
|
35 #include "error.h" |
|
36 #include "input.h" |
|
37 #include "oct-obj.h" |
|
38 #include "ov-usr-fcn.h" |
|
39 #include "ov.h" |
|
40 #include "pager.h" |
2985
|
41 #include "pt-jump.h" |
2974
|
42 #include "pt-misc.h" |
|
43 #include "pt-pr-code.h" |
2982
|
44 #include "pt-stmt.h" |
2974
|
45 #include "pt-walk.h" |
|
46 #include "symtab.h" |
|
47 #include "toplev.h" |
|
48 #include "unwind-prot.h" |
|
49 #include "utils.h" |
|
50 #include "variables.h" |
|
51 |
|
52 // If TRUE, variables returned from functions have default values even |
|
53 // if they are not explicitly initialized. |
|
54 static bool Vdefine_all_return_values; |
|
55 |
3131
|
56 // Maximum nesting level for functions called recursively. |
|
57 static int Vmax_recursion_depth; |
|
58 |
2974
|
59 // If TRUE, the last computed value is returned from functions that |
|
60 // don't actually define any return variables. |
|
61 static bool Vreturn_last_computed_value; |
|
62 |
|
63 // User defined functions. |
|
64 |
3219
|
65 DEFINE_OCTAVE_ALLOCATOR (octave_user_function); |
2974
|
66 |
3219
|
67 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_user_function, |
|
68 "user-defined function"); |
2974
|
69 |
|
70 // Ugh. This really needs to be simplified (code/data? |
|
71 // extrinsic/intrinsic state?). |
|
72 |
|
73 octave_user_function::octave_user_function |
|
74 (tree_parameter_list *pl, tree_parameter_list *rl, |
|
75 tree_statement_list *cl, symbol_table *st) |
|
76 : octave_function (string (), string ()), |
|
77 param_list (pl), ret_list (rl), cmd_list (cl), |
|
78 sym_tab (st), file_name (), fcn_name (), t_parsed (0), |
3165
|
79 t_checked (0), system_fcn_file (false), call_depth (0), |
|
80 num_named_args (0), args_passed (), num_args_passed (0), |
|
81 curr_va_arg_number (0), vr_list (0), symtab_entry (0), |
|
82 argn_sr (0), nargin_sr (0), nargout_sr (0) |
2974
|
83 { |
|
84 install_automatic_vars (); |
|
85 |
|
86 if (param_list) |
|
87 { |
|
88 num_named_args = param_list->length (); |
|
89 curr_va_arg_number = num_named_args; |
|
90 } |
|
91 } |
|
92 |
|
93 octave_user_function::~octave_user_function (void) |
|
94 { |
|
95 delete param_list; |
|
96 delete ret_list; |
|
97 delete sym_tab; |
|
98 delete cmd_list; |
|
99 delete vr_list; |
|
100 } |
|
101 |
|
102 octave_user_function * |
|
103 octave_user_function::define_ret_list (tree_parameter_list *t) |
|
104 { |
|
105 ret_list = t; |
|
106 |
|
107 if (ret_list && ret_list->takes_varargs ()) |
|
108 vr_list = new tree_va_return_list; |
|
109 |
|
110 return this; |
|
111 } |
|
112 |
|
113 void |
|
114 octave_user_function::stash_fcn_file_name (void) |
|
115 { |
|
116 if (fcn_name.empty ()) |
|
117 file_name = ""; |
|
118 else |
|
119 file_name = fcn_file_in_path (fcn_name); |
|
120 } |
|
121 |
|
122 void |
|
123 octave_user_function::mark_as_system_fcn_file (void) |
|
124 { |
|
125 if (! file_name.empty ()) |
|
126 { |
|
127 // We really should stash the whole path to the file we found, |
|
128 // when we looked it up, to avoid possible race conditions... |
|
129 // XXX FIXME XXX |
|
130 // |
|
131 // We probably also don't need to get the library directory |
|
132 // every time, but since this function is only called when the |
|
133 // function file is parsed, it probably doesn't matter that |
|
134 // much. |
|
135 |
|
136 string ff_name = fcn_file_in_path (file_name); |
|
137 |
|
138 if (Vfcn_file_dir.compare (ff_name, 0, Vfcn_file_dir.length ()) == 0) |
|
139 system_fcn_file = 1; |
|
140 } |
|
141 else |
|
142 system_fcn_file = 0; |
|
143 } |
|
144 |
|
145 bool |
|
146 octave_user_function::takes_varargs (void) const |
|
147 { |
|
148 return (param_list && param_list->takes_varargs ()); |
|
149 } |
|
150 |
|
151 octave_value |
|
152 octave_user_function::octave_va_arg (void) |
|
153 { |
|
154 octave_value retval; |
|
155 |
|
156 if (curr_va_arg_number < num_args_passed) |
|
157 retval = args_passed (curr_va_arg_number++); |
|
158 else |
|
159 ::error ("va_arg: error getting arg number %d -- only %d provided", |
|
160 curr_va_arg_number + 1, num_args_passed); |
|
161 |
|
162 return retval; |
|
163 } |
|
164 |
|
165 octave_value_list |
|
166 octave_user_function::octave_all_va_args (void) |
|
167 { |
|
168 octave_value_list retval; |
|
169 |
3178
|
170 int n = num_args_passed - num_named_args; |
2974
|
171 |
3178
|
172 if (n > 0) |
|
173 { |
|
174 retval.resize (n); |
|
175 |
|
176 int k = 0; |
|
177 for (int i = num_named_args; i < num_args_passed; i++) |
|
178 retval(k++) = args_passed(i); |
|
179 } |
2974
|
180 |
|
181 return retval; |
|
182 } |
|
183 |
|
184 bool |
|
185 octave_user_function::takes_var_return (void) const |
|
186 { |
|
187 return (ret_list && ret_list->takes_varargs ()); |
|
188 } |
|
189 |
|
190 void |
|
191 octave_user_function::octave_vr_val (const octave_value& val) |
|
192 { |
|
193 assert (vr_list); |
|
194 |
|
195 vr_list->append (val); |
|
196 } |
|
197 |
|
198 void |
|
199 octave_user_function::stash_function_name (const string& s) |
|
200 { |
|
201 fcn_name = s; |
|
202 } |
|
203 |
|
204 // For unwind protect. |
|
205 |
|
206 static void |
|
207 pop_symbol_table_context (void *table) |
|
208 { |
|
209 symbol_table *tmp = static_cast<symbol_table *> (table); |
|
210 tmp->pop_context (); |
|
211 } |
|
212 |
|
213 static void |
|
214 delete_vr_list (void *list) |
|
215 { |
|
216 tree_va_return_list *tmp = static_cast<tree_va_return_list *> (list); |
|
217 tmp->clear (); |
|
218 delete tmp; |
|
219 } |
|
220 |
|
221 static void |
|
222 clear_symbol_table (void *table) |
|
223 { |
|
224 symbol_table *tmp = static_cast<symbol_table *> (table); |
|
225 tmp->clear (); |
|
226 } |
|
227 |
|
228 static void |
|
229 unprotect_function (void *sr_arg) |
|
230 { |
|
231 symbol_record *sr = static_cast<symbol_record *> (sr_arg); |
|
232 sr->unprotect (); |
|
233 } |
|
234 |
|
235 octave_value_list |
|
236 octave_user_function::do_index_op (int nargout, const octave_value_list& args) |
|
237 { |
|
238 octave_value_list retval; |
|
239 |
|
240 if (error_state) |
|
241 return retval; |
|
242 |
|
243 if (! cmd_list) |
|
244 return retval; |
|
245 |
|
246 int nargin = args.length (); |
|
247 |
2985
|
248 unwind_protect::begin_frame ("func_eval"); |
2974
|
249 |
|
250 unwind_protect_int (call_depth); |
|
251 call_depth++; |
|
252 |
3131
|
253 if (call_depth > Vmax_recursion_depth) |
|
254 { |
|
255 ::error ("max_recursion_limit exceeded"); |
|
256 return retval; |
|
257 } |
|
258 |
2974
|
259 if (symtab_entry && ! symtab_entry->is_read_only ()) |
|
260 { |
|
261 symtab_entry->protect (); |
2985
|
262 unwind_protect::add (unprotect_function, symtab_entry); |
2974
|
263 } |
|
264 |
|
265 if (call_depth > 1) |
|
266 { |
|
267 sym_tab->push_context (); |
2985
|
268 unwind_protect::add (pop_symbol_table_context, sym_tab); |
2974
|
269 |
|
270 if (vr_list) |
|
271 { |
|
272 // Push new vr_list. |
|
273 |
|
274 unwind_protect_ptr (vr_list); |
|
275 vr_list = new tree_va_return_list; |
|
276 |
|
277 // Clear and delete the new one before restoring the old |
|
278 // one. |
|
279 |
2985
|
280 unwind_protect::add (delete_vr_list, vr_list); |
2974
|
281 } |
|
282 } |
|
283 |
|
284 if (vr_list) |
|
285 vr_list->clear (); |
|
286 |
|
287 // Force symbols to be undefined again when this function exits. |
|
288 |
2985
|
289 unwind_protect::add (clear_symbol_table, sym_tab); |
2974
|
290 |
|
291 // Save old and set current symbol table context, for |
|
292 // eval_undefined_error(). |
|
293 |
|
294 unwind_protect_ptr (curr_sym_tab); |
|
295 curr_sym_tab = sym_tab; |
|
296 |
|
297 unwind_protect_ptr (curr_function); |
|
298 curr_function = this; |
|
299 |
|
300 // XXX FIXME XXX -- ??? |
|
301 // unwind_protect_ptr (args_passed); |
|
302 |
|
303 args_passed = args; |
|
304 |
|
305 string_vector arg_names = args.name_tags (); |
|
306 |
|
307 unwind_protect_int (num_args_passed); |
|
308 num_args_passed = nargin; |
|
309 |
|
310 unwind_protect_int (num_named_args); |
|
311 unwind_protect_int (curr_va_arg_number); |
|
312 |
|
313 if (param_list && ! param_list->varargs_only ()) |
|
314 { |
|
315 param_list->define_from_arg_vector (args); |
|
316 if (error_state) |
|
317 goto abort; |
|
318 } |
|
319 |
|
320 if (ret_list && Vdefine_all_return_values) |
|
321 { |
|
322 octave_value tmp = builtin_any_variable ("default_return_value"); |
|
323 |
|
324 if (tmp.is_defined ()) |
|
325 ret_list->initialize_undefined_elements (tmp); |
|
326 } |
|
327 |
|
328 // The following code is in a separate scope to avoid warnings from |
|
329 // G++ about `goto abort' crossing the initialization of some |
|
330 // variables. |
|
331 |
|
332 { |
|
333 bind_automatic_vars (arg_names, nargin, nargout); |
|
334 |
|
335 bool echo_commands = (Vecho_executing_commands & ECHO_FUNCTIONS); |
|
336 |
|
337 if (echo_commands) |
|
338 print_code_function_header (); |
|
339 |
|
340 // Evaluate the commands that make up the function. |
|
341 |
|
342 octave_value_list tmp = cmd_list->eval (); |
|
343 |
|
344 octave_value last_computed_value; |
|
345 |
|
346 if (! tmp.empty ()) |
|
347 last_computed_value = tmp(0); |
|
348 |
|
349 if (echo_commands) |
|
350 print_code_function_trailer (); |
|
351 |
2985
|
352 if (tree_return_command::returning) |
|
353 tree_return_command::returning = 0; |
2974
|
354 |
2985
|
355 if (tree_break_command::breaking) |
|
356 tree_break_command::breaking--; |
2974
|
357 |
|
358 if (error_state) |
|
359 { |
|
360 traceback_error (); |
|
361 goto abort; |
|
362 } |
|
363 |
|
364 // Copy return values out. |
|
365 |
|
366 if (ret_list) |
|
367 retval = ret_list->convert_to_const_vector (vr_list); |
|
368 else if (Vreturn_last_computed_value) |
|
369 retval(0) = last_computed_value; |
|
370 } |
|
371 |
|
372 abort: |
2985
|
373 unwind_protect::run_frame ("func_eval"); |
2974
|
374 |
|
375 return retval; |
|
376 } |
|
377 |
|
378 void |
|
379 octave_user_function::traceback_error (void) |
|
380 { |
|
381 if (error_state >= 0) |
|
382 error_state = -1; |
|
383 |
|
384 if (fcn_name.empty ()) |
|
385 { |
|
386 if (file_name.empty ()) |
|
387 ::error ("called from `?unknown?'"); |
|
388 else |
|
389 ::error ("called from file `%s'", file_name.c_str ()); |
|
390 } |
|
391 else |
|
392 { |
|
393 if (file_name.empty ()) |
|
394 ::error ("called from `%s'", fcn_name.c_str ()); |
|
395 else |
|
396 ::error ("called from `%s' in file `%s'", |
|
397 fcn_name.c_str (), file_name.c_str ()); |
|
398 } |
|
399 } |
|
400 |
|
401 void |
|
402 octave_user_function::accept (tree_walker& tw) |
|
403 { |
|
404 tw.visit_octave_user_function (*this); |
|
405 } |
|
406 |
|
407 void |
|
408 octave_user_function::print_code_function_header (void) |
|
409 { |
|
410 tree_print_code tpc (octave_stdout, Vps4); |
|
411 |
|
412 tpc.visit_octave_user_function_header (*this); |
|
413 } |
|
414 |
|
415 void |
|
416 octave_user_function::print_code_function_trailer (void) |
|
417 { |
|
418 tree_print_code tpc (octave_stdout, Vps4); |
|
419 |
|
420 tpc.visit_octave_user_function_trailer (*this); |
|
421 } |
|
422 |
|
423 void |
|
424 octave_user_function::install_automatic_vars (void) |
|
425 { |
|
426 argn_sr = sym_tab->lookup ("argn", true); |
|
427 nargin_sr = sym_tab->lookup ("nargin", true); |
|
428 nargout_sr = sym_tab->lookup ("nargout", true); |
|
429 } |
|
430 |
|
431 void |
|
432 octave_user_function::bind_automatic_vars |
|
433 (const string_vector& arg_names, int nargin, int nargout) |
|
434 { |
|
435 if (! arg_names.empty ()) |
|
436 argn_sr->define (arg_names); |
|
437 |
|
438 nargin_sr->define (static_cast<double> (nargin)); |
|
439 nargout_sr->define (static_cast<double> (nargout)); |
|
440 } |
|
441 |
|
442 DEFUN (va_arg, args, , |
|
443 "va_arg (): return next argument in a function that takes a\n\ |
|
444 variable number of parameters") |
|
445 { |
|
446 octave_value_list retval; |
|
447 |
|
448 int nargin = args.length (); |
|
449 |
|
450 if (nargin == 0) |
|
451 { |
|
452 if (curr_function) |
|
453 { |
|
454 if (curr_function->takes_varargs ()) |
|
455 retval = curr_function->octave_va_arg (); |
|
456 else |
|
457 { |
|
458 ::error ("va_arg only valid within function taking variable"); |
|
459 ::error ("number of arguments"); |
|
460 } |
|
461 } |
|
462 else |
|
463 ::error ("va_arg only valid within function body"); |
|
464 } |
|
465 else |
|
466 print_usage ("va_arg"); |
|
467 |
|
468 return retval; |
|
469 } |
|
470 |
|
471 DEFUN (va_start, args, , |
|
472 "va_start (): reset the pointer to the list of optional arguments\n\ |
|
473 to the beginning") |
|
474 { |
|
475 octave_value_list retval; |
|
476 |
|
477 int nargin = args.length (); |
|
478 |
|
479 if (nargin == 0) |
|
480 { |
|
481 if (curr_function) |
|
482 { |
|
483 if (curr_function->takes_varargs ()) |
|
484 curr_function->octave_va_start (); |
|
485 else |
|
486 { |
|
487 ::error ("va_start only valid within function taking variable"); |
|
488 ::error ("number of arguments"); |
|
489 } |
|
490 } |
|
491 else |
|
492 ::error ("va_start only valid within function body"); |
|
493 } |
|
494 else |
|
495 print_usage ("va_start"); |
|
496 |
|
497 return retval; |
|
498 } |
|
499 |
|
500 DEFUN (vr_val, args, , |
|
501 "vr_val (X): append X to the list of optional return values for a\n\ |
|
502 function that allows a variable number of return values") |
|
503 { |
|
504 octave_value_list retval; |
|
505 |
|
506 int nargin = args.length (); |
|
507 |
|
508 if (nargin == 1) |
|
509 { |
|
510 if (curr_function) |
|
511 { |
|
512 if (curr_function->takes_var_return ()) |
|
513 curr_function->octave_vr_val (args(0)); |
|
514 else |
|
515 { |
|
516 ::error ("vr_val only valid within function declared to"); |
|
517 ::error ("produce a variable number of values"); |
|
518 } |
|
519 } |
|
520 else |
|
521 ::error ("vr_val only valid within function body"); |
|
522 } |
|
523 else |
|
524 print_usage ("vr_val"); |
|
525 |
|
526 return retval; |
|
527 } |
|
528 |
|
529 static int |
|
530 define_all_return_values (void) |
|
531 { |
|
532 Vdefine_all_return_values = check_preference ("define_all_return_values"); |
|
533 |
|
534 return 0; |
|
535 } |
|
536 |
|
537 static int |
3131
|
538 max_recursion_depth (void) |
|
539 { |
|
540 Vmax_recursion_depth = check_preference ("max_recursion_depth"); |
|
541 |
|
542 return 0; |
|
543 } |
|
544 |
|
545 static int |
2974
|
546 return_last_computed_value (void) |
|
547 { |
|
548 Vreturn_last_computed_value |
|
549 = check_preference ("return_last_computed_value"); |
|
550 |
|
551 return 0; |
|
552 } |
|
553 |
|
554 void |
|
555 symbols_of_ov_usr_fcn (void) |
|
556 { |
|
557 DEFVAR (default_return_value, Matrix (), 0, 0, |
|
558 "the default for value for unitialized variables returned from\n\ |
|
559 functions. Only used if the variable initialize_return_values is\n\ |
3156
|
560 nonzero."); |
2974
|
561 |
|
562 DEFVAR (define_all_return_values, 0.0, 0, define_all_return_values, |
|
563 "control whether values returned from functions should have a\n\ |
|
564 value even if one has not been explicitly assigned. See also\n\ |
|
565 default_return_value"); |
|
566 |
3131
|
567 DEFVAR (max_recursion_depth, 256.0, 0, max_recursion_depth, |
|
568 "maximum nesting level for functions called recursively.\n\ |
|
569 The default value is 256."); |
|
570 |
2974
|
571 DEFVAR (return_last_computed_value, 0.0, 0, return_last_computed_value, |
|
572 "if a function does not return any values explicitly, return the\n\ |
|
573 last computed value"); |
|
574 } |
|
575 |
|
576 /* |
|
577 ;;; Local Variables: *** |
|
578 ;;; mode: C++ *** |
|
579 ;;; End: *** |
|
580 */ |