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