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