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