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