1742
|
1 // pt-fcn.cc -*- C++ -*- |
1741
|
2 /* |
|
3 |
1827
|
4 Copyright (C) 1996 John W. Eaton |
1741
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 #ifdef HAVE_CONFIG_H |
|
29 #include <config.h> |
|
30 #endif |
|
31 |
|
32 #include <iostream.h> |
|
33 #include <strstream.h> |
|
34 |
|
35 #include "defun.h" |
|
36 #include "error.h" |
|
37 #include "gripes.h" |
|
38 #include "help.h" |
|
39 #include "pager.h" |
|
40 #include "symtab.h" |
|
41 #include "toplev.h" |
|
42 #include "pt-const.h" |
|
43 #include "pt-exp.h" |
|
44 #include "pt-fcn.h" |
|
45 #include "pt-misc.h" |
|
46 #include "unwind-prot.h" |
|
47 #include "user-prefs.h" |
|
48 #include "utils.h" |
|
49 #include "variables.h" |
|
50 |
|
51 // Nonzero means we're returning from a function. |
|
52 extern int returning; |
|
53 |
|
54 // Nonzero means we're breaking out of a loop or function body. |
|
55 extern int breaking; |
|
56 |
|
57 // User defined functions. |
|
58 |
|
59 void |
|
60 tree_function::install_nargin_and_nargout (void) |
|
61 { |
|
62 nargin_sr = sym_tab->lookup ("nargin", 1, 0); |
|
63 nargout_sr = sym_tab->lookup ("nargout", 1, 0); |
|
64 } |
|
65 |
|
66 void |
|
67 tree_function::bind_nargin_and_nargout (int nargin, int nargout) |
|
68 { |
|
69 tree_constant *tmp; |
|
70 |
|
71 tmp = new tree_constant (nargin); |
|
72 nargin_sr->define (tmp); |
|
73 |
|
74 tmp = new tree_constant (nargout); |
|
75 nargout_sr->define (tmp); |
|
76 } |
|
77 |
|
78 tree_function::~tree_function (void) |
|
79 { |
|
80 delete param_list; |
|
81 delete ret_list; |
|
82 delete sym_tab; |
|
83 delete cmd_list; |
|
84 delete vr_list; |
|
85 } |
|
86 |
|
87 #if 0 |
|
88 tree_function * |
|
89 tree_function::define (tree statement_list *t) |
|
90 { |
|
91 cmd_list = t; |
|
92 return this; |
|
93 } |
|
94 #endif |
|
95 |
|
96 tree_function * |
|
97 tree_function::define_param_list (tree_parameter_list *t) |
|
98 { |
|
99 param_list = t; |
|
100 |
|
101 if (param_list) |
|
102 { |
|
103 num_named_args = param_list->length (); |
|
104 curr_va_arg_number = num_named_args; |
|
105 } |
|
106 |
|
107 return this; |
|
108 } |
|
109 |
|
110 tree_function * |
|
111 tree_function::define_ret_list (tree_parameter_list *t) |
|
112 { |
|
113 ret_list = t; |
|
114 |
|
115 if (ret_list && ret_list->takes_varargs ()) |
|
116 vr_list = new tree_va_return_list; |
|
117 |
|
118 return this; |
|
119 } |
|
120 |
|
121 void |
|
122 tree_function::stash_fcn_file_name (void) |
|
123 { |
1755
|
124 if (fcn_name.empty ()) |
|
125 file_name = ""; |
|
126 else |
|
127 file_name = fcn_file_in_path (fcn_name); |
1741
|
128 } |
|
129 |
|
130 void |
|
131 tree_function::mark_as_system_fcn_file (void) |
|
132 { |
1755
|
133 if (! file_name.empty ()) |
1741
|
134 { |
|
135 // We really should stash the whole path to the file we found, |
|
136 // when we looked it up, to avoid possible race conditions... |
|
137 // XXX FIXME XXX |
|
138 // |
|
139 // We probably also don't need to get the library directory |
|
140 // every time, but since this function is only called when the |
|
141 // function file is parsed, it probably doesn't matter that |
|
142 // much. |
|
143 |
1755
|
144 string ff_name = fcn_file_in_path (file_name); |
1741
|
145 |
1755
|
146 string system_dir = octave_fcn_file_dir (); |
|
147 |
|
148 if (system_dir.compare (ff_name, 0, system_dir.length ()) == 0) |
1741
|
149 system_fcn_file = 1; |
|
150 } |
|
151 else |
|
152 system_fcn_file = 0; |
|
153 } |
|
154 |
1827
|
155 bool |
1741
|
156 tree_function::takes_varargs (void) const |
|
157 { |
|
158 return (param_list && param_list->takes_varargs ()); |
|
159 } |
|
160 |
|
161 tree_constant |
|
162 tree_function::octave_va_arg (void) |
|
163 { |
|
164 tree_constant retval; |
|
165 |
|
166 if (curr_va_arg_number < num_args_passed) |
|
167 retval = args_passed (curr_va_arg_number++); |
|
168 else |
|
169 ::error ("va_arg: error getting arg number %d -- only %d provided", |
|
170 curr_va_arg_number + 1, num_args_passed); |
|
171 |
|
172 return retval; |
|
173 } |
|
174 |
|
175 Octave_object |
|
176 tree_function::octave_all_va_args (void) |
|
177 { |
|
178 Octave_object retval; |
|
179 |
|
180 retval.resize (num_args_passed - num_named_args); |
|
181 |
|
182 int k = 0; |
|
183 for (int i = num_named_args; i < num_args_passed; i++) |
|
184 retval(k++) = args_passed(i); |
|
185 |
|
186 return retval; |
|
187 } |
|
188 |
1827
|
189 bool |
1741
|
190 tree_function::takes_var_return (void) const |
|
191 { |
|
192 return (ret_list && ret_list->takes_varargs ()); |
|
193 } |
|
194 |
|
195 void |
|
196 tree_function::octave_vr_val (const tree_constant& val) |
|
197 { |
|
198 assert (vr_list); |
|
199 |
|
200 vr_list->append (val); |
|
201 } |
|
202 |
|
203 void |
1755
|
204 tree_function::stash_function_name (const string& s) |
1741
|
205 { |
1755
|
206 fcn_name = s; |
1741
|
207 } |
|
208 |
|
209 tree_constant |
1827
|
210 tree_function::eval (bool print) |
1741
|
211 { |
|
212 tree_constant retval; |
|
213 |
|
214 if (error_state || ! cmd_list) |
|
215 return retval; |
|
216 |
|
217 Octave_object tmp_args; |
|
218 Octave_object tmp = eval (print, 0, tmp_args); |
|
219 |
|
220 if (! error_state && tmp.length () > 0) |
|
221 retval = tmp(0); |
|
222 |
|
223 return retval; |
|
224 } |
|
225 |
|
226 // For unwind protect. |
|
227 |
|
228 static void |
|
229 pop_symbol_table_context (void *table) |
|
230 { |
|
231 symbol_table *tmp = (symbol_table *) table; |
|
232 tmp->pop_context (); |
|
233 } |
|
234 |
|
235 static void |
|
236 delete_vr_list (void *list) |
|
237 { |
|
238 tree_va_return_list *tmp = (tree_va_return_list *) list; |
|
239 tmp->clear (); |
|
240 delete tmp; |
|
241 } |
|
242 |
|
243 static void |
|
244 clear_symbol_table (void *table) |
|
245 { |
|
246 symbol_table *tmp = (symbol_table *) table; |
|
247 tmp->clear (); |
|
248 } |
|
249 |
|
250 Octave_object |
1827
|
251 tree_function::eval (bool /* print */, int nargout, const Octave_object& args) |
1741
|
252 { |
|
253 Octave_object retval; |
|
254 |
|
255 if (error_state) |
|
256 return retval; |
|
257 |
|
258 if (! cmd_list) |
|
259 return retval; |
|
260 |
|
261 int nargin = args.length (); |
|
262 |
|
263 begin_unwind_frame ("func_eval"); |
|
264 |
|
265 unwind_protect_int (call_depth); |
|
266 call_depth++; |
|
267 |
|
268 if (call_depth > 1) |
|
269 { |
|
270 sym_tab->push_context (); |
|
271 add_unwind_protect (pop_symbol_table_context, (void *) sym_tab); |
|
272 |
|
273 if (vr_list) |
|
274 { |
|
275 // Push new vr_list. |
|
276 |
|
277 unwind_protect_ptr (vr_list); |
|
278 vr_list = new tree_va_return_list; |
|
279 |
|
280 // Clear and delete the new one before restoring the old |
|
281 // one. |
|
282 |
|
283 add_unwind_protect (delete_vr_list, (void *) vr_list); |
|
284 } |
|
285 } |
|
286 |
|
287 if (vr_list) |
|
288 vr_list->clear (); |
|
289 |
|
290 // Force symbols to be undefined again when this function exits. |
|
291 |
|
292 add_unwind_protect (clear_symbol_table, (void *) sym_tab); |
|
293 |
|
294 // Save old and set current symbol table context, for |
|
295 // eval_undefined_error(). |
|
296 |
|
297 unwind_protect_ptr (curr_sym_tab); |
|
298 curr_sym_tab = sym_tab; |
|
299 |
|
300 unwind_protect_ptr (curr_function); |
|
301 curr_function = this; |
|
302 |
1755
|
303 // XXX FIXME XXX -- ??? |
|
304 // unwind_protect_ptr (args_passed); |
1741
|
305 |
|
306 args_passed = args; |
|
307 |
|
308 unwind_protect_int (num_args_passed); |
|
309 num_args_passed = nargin; |
|
310 |
|
311 unwind_protect_int (num_named_args); |
|
312 unwind_protect_int (curr_va_arg_number); |
|
313 |
|
314 if (param_list && ! param_list->varargs_only ()) |
|
315 { |
|
316 param_list->define_from_arg_vector (args); |
|
317 if (error_state) |
|
318 goto abort; |
|
319 } |
|
320 |
|
321 // The following code is in a separate scope to avoid warnings from |
|
322 // G++ about `goto abort' crossing the initialization of some |
|
323 // variables. |
|
324 |
|
325 { |
|
326 bind_nargin_and_nargout (nargin, nargout); |
|
327 |
1827
|
328 bool echo_commands |
1741
|
329 = (user_pref.echo_executing_commands & ECHO_FUNCTIONS); |
|
330 |
|
331 if (echo_commands) |
|
332 print_code_function_header (); |
|
333 |
|
334 // Evaluate the commands that make up the function. |
|
335 |
1827
|
336 bool pf = ! user_pref.silent_functions; |
1741
|
337 tree_constant last_computed_value = cmd_list->eval (pf); |
|
338 |
|
339 if (echo_commands) |
|
340 print_code_function_trailer (); |
|
341 |
|
342 if (returning) |
|
343 returning = 0; |
|
344 |
|
345 if (breaking) |
|
346 breaking--; |
|
347 |
|
348 if (error_state) |
|
349 { |
|
350 traceback_error (); |
|
351 goto abort; |
|
352 } |
|
353 |
|
354 // Copy return values out. |
|
355 |
|
356 if (ret_list) |
|
357 { |
|
358 if (nargout > 0 && user_pref.define_all_return_values) |
|
359 { |
|
360 tree_constant tmp = builtin_any_variable ("default_return_value"); |
|
361 if (tmp.is_defined ()) |
|
362 ret_list->initialize_undefined_elements (tmp); |
|
363 } |
|
364 |
|
365 retval = ret_list->convert_to_const_vector (vr_list); |
|
366 } |
|
367 else if (user_pref.return_last_computed_value) |
|
368 retval(0) = last_computed_value; |
|
369 } |
|
370 |
|
371 abort: |
|
372 run_unwind_frame ("func_eval"); |
|
373 |
|
374 return retval; |
|
375 } |
|
376 |
|
377 void |
|
378 tree_function::traceback_error (void) |
|
379 { |
|
380 if (error_state >= 0) |
|
381 error_state = -1; |
|
382 |
1755
|
383 if (fcn_name.empty ()) |
1741
|
384 { |
1755
|
385 if (file_name.empty ()) |
|
386 ::error ("called from `?unknown?'"); |
|
387 else |
|
388 ::error ("called from file `%s'", file_name.c_str ()); |
1741
|
389 } |
|
390 else |
|
391 { |
1755
|
392 if (file_name.empty ()) |
|
393 ::error ("called from `%s'", fcn_name.c_str ()); |
|
394 else |
|
395 ::error ("called from `%s' in file `%s'", |
|
396 fcn_name.c_str (), file_name.c_str ()); |
1741
|
397 } |
|
398 } |
|
399 |
|
400 void |
|
401 tree_function::print_code (ostream& os) |
|
402 { |
|
403 print_code_reset (); |
|
404 |
|
405 print_code_function_header (os); |
|
406 |
|
407 if (cmd_list) |
|
408 { |
|
409 increment_indent_level (); |
|
410 cmd_list->print_code (os); |
|
411 decrement_indent_level (); |
|
412 } |
|
413 |
|
414 print_code_function_trailer (os); |
|
415 } |
|
416 |
|
417 void |
|
418 tree_function::print_code_function_header (void) |
|
419 { |
|
420 ostrstream output_buf; |
|
421 print_code_function_header (output_buf); |
|
422 output_buf << ends; |
|
423 maybe_page_output (output_buf); |
|
424 } |
|
425 |
|
426 void |
|
427 tree_function::print_code_function_header (ostream& os) |
|
428 { |
|
429 print_code_indent (os); |
|
430 |
|
431 os << "function "; |
|
432 |
|
433 if (ret_list) |
|
434 { |
|
435 int len = ret_list->length (); |
|
436 |
|
437 if (len > 1) |
|
438 os << "["; |
|
439 |
|
440 ret_list->print_code (os); |
|
441 |
|
442 if (len > 1) |
|
443 os << "]"; |
|
444 |
|
445 os << " = "; |
|
446 } |
|
447 |
1755
|
448 os << (fcn_name.empty () ? string ("(empty)") : fcn_name) << " "; |
1741
|
449 |
|
450 if (param_list) |
|
451 { |
|
452 int len = param_list->length (); |
|
453 if (len > 0) |
|
454 os << "("; |
|
455 |
|
456 param_list->print_code (os); |
|
457 |
|
458 if (len > 0) |
|
459 { |
|
460 os << ")"; |
|
461 print_code_new_line (os); |
|
462 } |
|
463 } |
|
464 else |
|
465 { |
|
466 os << "()"; |
|
467 print_code_new_line (os); |
|
468 } |
|
469 } |
|
470 |
|
471 void |
|
472 tree_function::print_code_function_trailer (void) |
|
473 { |
|
474 ostrstream output_buf; |
|
475 print_code_function_trailer (output_buf); |
|
476 output_buf << ends; |
|
477 maybe_page_output (output_buf); |
|
478 } |
|
479 |
|
480 void |
|
481 tree_function::print_code_function_trailer (ostream& os) |
|
482 { |
|
483 print_code_indent (os); |
|
484 |
|
485 os << "endfunction"; |
|
486 |
|
487 print_code_new_line (os); |
|
488 } |
|
489 |
|
490 DEFUN ("va_arg", Fva_arg, Sva_arg, 10, |
|
491 "va_arg (): return next argument in a function that takes a\n\ |
|
492 variable number of parameters") |
|
493 { |
|
494 Octave_object retval; |
|
495 |
|
496 int nargin = args.length (); |
|
497 |
|
498 if (nargin == 0) |
|
499 { |
|
500 if (curr_function) |
|
501 { |
|
502 if (curr_function->takes_varargs ()) |
|
503 retval = curr_function->octave_va_arg (); |
|
504 else |
|
505 { |
|
506 ::error ("va_arg only valid within function taking variable"); |
|
507 ::error ("number of arguments"); |
|
508 } |
|
509 } |
|
510 else |
|
511 ::error ("va_arg only valid within function body"); |
|
512 } |
|
513 else |
|
514 print_usage ("va_arg"); |
|
515 |
|
516 return retval; |
|
517 } |
|
518 |
|
519 DEFUN ("va_start", Fva_start, Sva_start, 10, |
|
520 "va_start (): reset the pointer to the list of optional arguments\n\ |
|
521 to the beginning") |
|
522 { |
|
523 Octave_object retval; |
|
524 |
|
525 int nargin = args.length (); |
|
526 |
|
527 if (nargin == 0) |
|
528 { |
|
529 if (curr_function) |
|
530 { |
|
531 if (curr_function->takes_varargs ()) |
|
532 curr_function->octave_va_start (); |
|
533 else |
|
534 { |
|
535 ::error ("va_start only valid within function taking variable"); |
|
536 ::error ("number of arguments"); |
|
537 } |
|
538 } |
|
539 else |
|
540 ::error ("va_start only valid within function body"); |
|
541 } |
|
542 else |
|
543 print_usage ("va_start"); |
|
544 |
|
545 return retval; |
|
546 } |
|
547 |
|
548 DEFUN ("vr_val", Fvr_val, Svr_val, 10, |
|
549 "vr_val (X): append X to the list of optional return values for a |
|
550 function that allows a variable number of return values") |
|
551 { |
|
552 Octave_object retval; |
|
553 |
|
554 int nargin = args.length (); |
|
555 |
|
556 if (nargin == 1) |
|
557 { |
|
558 if (curr_function) |
|
559 { |
|
560 if (curr_function->takes_var_return ()) |
|
561 curr_function->octave_vr_val (args(0)); |
|
562 else |
|
563 { |
|
564 ::error ("vr_val only valid within function declared to"); |
|
565 ::error ("produce a variable number of values"); |
|
566 } |
|
567 } |
|
568 else |
|
569 ::error ("vr_val only valid within function body"); |
|
570 } |
|
571 else |
|
572 print_usage ("vr_val"); |
|
573 |
|
574 return retval; |
|
575 } |
|
576 |
|
577 /* |
|
578 ;;; Local Variables: *** |
|
579 ;;; mode: C++ *** |
|
580 ;;; page-delimiter: "^/\\*" *** |
|
581 ;;; End: *** |
|
582 */ |