1741
|
1 // tree-fcn.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
|
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 [] file_name; |
|
85 delete [] fcn_name; |
|
86 delete vr_list; |
|
87 } |
|
88 |
|
89 #if 0 |
|
90 tree_function * |
|
91 tree_function::define (tree statement_list *t) |
|
92 { |
|
93 cmd_list = t; |
|
94 return this; |
|
95 } |
|
96 #endif |
|
97 |
|
98 tree_function * |
|
99 tree_function::define_param_list (tree_parameter_list *t) |
|
100 { |
|
101 param_list = t; |
|
102 |
|
103 if (param_list) |
|
104 { |
|
105 num_named_args = param_list->length (); |
|
106 curr_va_arg_number = num_named_args; |
|
107 } |
|
108 |
|
109 return this; |
|
110 } |
|
111 |
|
112 tree_function * |
|
113 tree_function::define_ret_list (tree_parameter_list *t) |
|
114 { |
|
115 ret_list = t; |
|
116 |
|
117 if (ret_list && ret_list->takes_varargs ()) |
|
118 vr_list = new tree_va_return_list; |
|
119 |
|
120 return this; |
|
121 } |
|
122 |
|
123 void |
|
124 tree_function::stash_fcn_file_name (void) |
|
125 { |
|
126 delete [] file_name; |
|
127 file_name = fcn_name ? fcn_file_in_path (fcn_name) : 0; |
|
128 } |
|
129 |
|
130 void |
|
131 tree_function::mark_as_system_fcn_file (void) |
|
132 { |
|
133 if (file_name) |
|
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 |
|
144 char *ff_name = fcn_file_in_path (file_name); |
|
145 |
|
146 char *system_dir = octave_fcn_file_dir (); |
|
147 int len = strlen (system_dir); |
|
148 |
|
149 if (strncmp (system_dir, ff_name, len) == 0) |
|
150 system_fcn_file = 1; |
|
151 |
|
152 delete [] ff_name; |
|
153 } |
|
154 else |
|
155 system_fcn_file = 0; |
|
156 } |
|
157 |
|
158 int |
|
159 tree_function::takes_varargs (void) const |
|
160 { |
|
161 return (param_list && param_list->takes_varargs ()); |
|
162 } |
|
163 |
|
164 tree_constant |
|
165 tree_function::octave_va_arg (void) |
|
166 { |
|
167 tree_constant retval; |
|
168 |
|
169 if (curr_va_arg_number < num_args_passed) |
|
170 retval = args_passed (curr_va_arg_number++); |
|
171 else |
|
172 ::error ("va_arg: error getting arg number %d -- only %d provided", |
|
173 curr_va_arg_number + 1, num_args_passed); |
|
174 |
|
175 return retval; |
|
176 } |
|
177 |
|
178 Octave_object |
|
179 tree_function::octave_all_va_args (void) |
|
180 { |
|
181 Octave_object retval; |
|
182 |
|
183 retval.resize (num_args_passed - num_named_args); |
|
184 |
|
185 int k = 0; |
|
186 for (int i = num_named_args; i < num_args_passed; i++) |
|
187 retval(k++) = args_passed(i); |
|
188 |
|
189 return retval; |
|
190 } |
|
191 |
|
192 int |
|
193 tree_function::takes_var_return (void) const |
|
194 { |
|
195 return (ret_list && ret_list->takes_varargs ()); |
|
196 } |
|
197 |
|
198 void |
|
199 tree_function::octave_vr_val (const tree_constant& val) |
|
200 { |
|
201 assert (vr_list); |
|
202 |
|
203 vr_list->append (val); |
|
204 } |
|
205 |
|
206 void |
|
207 tree_function::stash_function_name (char *s) |
|
208 { |
|
209 delete [] fcn_name; |
|
210 fcn_name = strsave (s); |
|
211 } |
|
212 |
|
213 tree_constant |
|
214 tree_function::eval (int print) |
|
215 { |
|
216 tree_constant retval; |
|
217 |
|
218 if (error_state || ! cmd_list) |
|
219 return retval; |
|
220 |
|
221 Octave_object tmp_args; |
|
222 Octave_object tmp = eval (print, 0, tmp_args); |
|
223 |
|
224 if (! error_state && tmp.length () > 0) |
|
225 retval = tmp(0); |
|
226 |
|
227 return retval; |
|
228 } |
|
229 |
|
230 // For unwind protect. |
|
231 |
|
232 static void |
|
233 pop_symbol_table_context (void *table) |
|
234 { |
|
235 symbol_table *tmp = (symbol_table *) table; |
|
236 tmp->pop_context (); |
|
237 } |
|
238 |
|
239 static void |
|
240 delete_vr_list (void *list) |
|
241 { |
|
242 tree_va_return_list *tmp = (tree_va_return_list *) list; |
|
243 tmp->clear (); |
|
244 delete tmp; |
|
245 } |
|
246 |
|
247 static void |
|
248 clear_symbol_table (void *table) |
|
249 { |
|
250 symbol_table *tmp = (symbol_table *) table; |
|
251 tmp->clear (); |
|
252 } |
|
253 |
|
254 Octave_object |
|
255 tree_function::eval (int /* print */, int nargout, const Octave_object& args) |
|
256 { |
|
257 Octave_object retval; |
|
258 |
|
259 if (error_state) |
|
260 return retval; |
|
261 |
|
262 if (! cmd_list) |
|
263 return retval; |
|
264 |
|
265 int nargin = args.length (); |
|
266 |
|
267 begin_unwind_frame ("func_eval"); |
|
268 |
|
269 unwind_protect_int (call_depth); |
|
270 call_depth++; |
|
271 |
|
272 if (call_depth > 1) |
|
273 { |
|
274 sym_tab->push_context (); |
|
275 add_unwind_protect (pop_symbol_table_context, (void *) sym_tab); |
|
276 |
|
277 if (vr_list) |
|
278 { |
|
279 // Push new vr_list. |
|
280 |
|
281 unwind_protect_ptr (vr_list); |
|
282 vr_list = new tree_va_return_list; |
|
283 |
|
284 // Clear and delete the new one before restoring the old |
|
285 // one. |
|
286 |
|
287 add_unwind_protect (delete_vr_list, (void *) vr_list); |
|
288 } |
|
289 } |
|
290 |
|
291 if (vr_list) |
|
292 vr_list->clear (); |
|
293 |
|
294 // Force symbols to be undefined again when this function exits. |
|
295 |
|
296 add_unwind_protect (clear_symbol_table, (void *) sym_tab); |
|
297 |
|
298 // Save old and set current symbol table context, for |
|
299 // eval_undefined_error(). |
|
300 |
|
301 unwind_protect_ptr (curr_sym_tab); |
|
302 curr_sym_tab = sym_tab; |
|
303 |
|
304 unwind_protect_ptr (curr_function); |
|
305 curr_function = this; |
|
306 |
|
307 // unwind_protect_ptr (args_passed); |
|
308 |
|
309 args_passed = args; |
|
310 |
|
311 unwind_protect_int (num_args_passed); |
|
312 num_args_passed = nargin; |
|
313 |
|
314 unwind_protect_int (num_named_args); |
|
315 unwind_protect_int (curr_va_arg_number); |
|
316 |
|
317 if (param_list && ! param_list->varargs_only ()) |
|
318 { |
|
319 param_list->define_from_arg_vector (args); |
|
320 if (error_state) |
|
321 goto abort; |
|
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_nargin_and_nargout (nargin, nargout); |
|
330 |
|
331 int echo_commands |
|
332 = (user_pref.echo_executing_commands & ECHO_FUNCTIONS); |
|
333 |
|
334 if (echo_commands) |
|
335 print_code_function_header (); |
|
336 |
|
337 // Evaluate the commands that make up the function. |
|
338 |
|
339 int pf = ! user_pref.silent_functions; |
|
340 tree_constant last_computed_value = cmd_list->eval (pf); |
|
341 |
|
342 if (echo_commands) |
|
343 print_code_function_trailer (); |
|
344 |
|
345 if (returning) |
|
346 returning = 0; |
|
347 |
|
348 if (breaking) |
|
349 breaking--; |
|
350 |
|
351 if (error_state) |
|
352 { |
|
353 traceback_error (); |
|
354 goto abort; |
|
355 } |
|
356 |
|
357 // Copy return values out. |
|
358 |
|
359 if (ret_list) |
|
360 { |
|
361 if (nargout > 0 && user_pref.define_all_return_values) |
|
362 { |
|
363 tree_constant tmp = builtin_any_variable ("default_return_value"); |
|
364 if (tmp.is_defined ()) |
|
365 ret_list->initialize_undefined_elements (tmp); |
|
366 } |
|
367 |
|
368 retval = ret_list->convert_to_const_vector (vr_list); |
|
369 } |
|
370 else if (user_pref.return_last_computed_value) |
|
371 retval(0) = last_computed_value; |
|
372 } |
|
373 |
|
374 abort: |
|
375 run_unwind_frame ("func_eval"); |
|
376 |
|
377 return retval; |
|
378 } |
|
379 |
|
380 void |
|
381 tree_function::traceback_error (void) |
|
382 { |
|
383 if (error_state >= 0) |
|
384 error_state = -1; |
|
385 |
|
386 if (fcn_name) |
|
387 { |
|
388 if (file_name) |
|
389 ::error ("called from `%s' in file `%s'", fcn_name, file_name); |
|
390 else |
|
391 ::error ("called from `%s'", fcn_name); |
|
392 } |
|
393 else |
|
394 { |
|
395 if (file_name) |
|
396 ::error ("called from file `%s'", file_name); |
|
397 else |
|
398 ::error ("called from `?unknown?'"); |
|
399 } |
|
400 } |
|
401 |
|
402 void |
|
403 tree_function::print_code (ostream& os) |
|
404 { |
|
405 print_code_reset (); |
|
406 |
|
407 print_code_function_header (os); |
|
408 |
|
409 if (cmd_list) |
|
410 { |
|
411 increment_indent_level (); |
|
412 cmd_list->print_code (os); |
|
413 decrement_indent_level (); |
|
414 } |
|
415 |
|
416 print_code_function_trailer (os); |
|
417 } |
|
418 |
|
419 void |
|
420 tree_function::print_code_function_header (void) |
|
421 { |
|
422 ostrstream output_buf; |
|
423 print_code_function_header (output_buf); |
|
424 output_buf << ends; |
|
425 maybe_page_output (output_buf); |
|
426 } |
|
427 |
|
428 void |
|
429 tree_function::print_code_function_header (ostream& os) |
|
430 { |
|
431 print_code_indent (os); |
|
432 |
|
433 os << "function "; |
|
434 |
|
435 if (ret_list) |
|
436 { |
|
437 int len = ret_list->length (); |
|
438 |
|
439 if (len > 1) |
|
440 os << "["; |
|
441 |
|
442 ret_list->print_code (os); |
|
443 |
|
444 if (len > 1) |
|
445 os << "]"; |
|
446 |
|
447 os << " = "; |
|
448 } |
|
449 |
|
450 os << (fcn_name ? fcn_name : "(null)") << " "; |
|
451 |
|
452 if (param_list) |
|
453 { |
|
454 int len = param_list->length (); |
|
455 if (len > 0) |
|
456 os << "("; |
|
457 |
|
458 param_list->print_code (os); |
|
459 |
|
460 if (len > 0) |
|
461 { |
|
462 os << ")"; |
|
463 print_code_new_line (os); |
|
464 } |
|
465 } |
|
466 else |
|
467 { |
|
468 os << "()"; |
|
469 print_code_new_line (os); |
|
470 } |
|
471 } |
|
472 |
|
473 void |
|
474 tree_function::print_code_function_trailer (void) |
|
475 { |
|
476 ostrstream output_buf; |
|
477 print_code_function_trailer (output_buf); |
|
478 output_buf << ends; |
|
479 maybe_page_output (output_buf); |
|
480 } |
|
481 |
|
482 void |
|
483 tree_function::print_code_function_trailer (ostream& os) |
|
484 { |
|
485 print_code_indent (os); |
|
486 |
|
487 os << "endfunction"; |
|
488 |
|
489 print_code_new_line (os); |
|
490 } |
|
491 |
|
492 DEFUN ("va_arg", Fva_arg, Sva_arg, 10, |
|
493 "va_arg (): return next argument in a function that takes a\n\ |
|
494 variable number of parameters") |
|
495 { |
|
496 Octave_object retval; |
|
497 |
|
498 int nargin = args.length (); |
|
499 |
|
500 if (nargin == 0) |
|
501 { |
|
502 if (curr_function) |
|
503 { |
|
504 if (curr_function->takes_varargs ()) |
|
505 retval = curr_function->octave_va_arg (); |
|
506 else |
|
507 { |
|
508 ::error ("va_arg only valid within function taking variable"); |
|
509 ::error ("number of arguments"); |
|
510 } |
|
511 } |
|
512 else |
|
513 ::error ("va_arg only valid within function body"); |
|
514 } |
|
515 else |
|
516 print_usage ("va_arg"); |
|
517 |
|
518 return retval; |
|
519 } |
|
520 |
|
521 DEFUN ("va_start", Fva_start, Sva_start, 10, |
|
522 "va_start (): reset the pointer to the list of optional arguments\n\ |
|
523 to the beginning") |
|
524 { |
|
525 Octave_object retval; |
|
526 |
|
527 int nargin = args.length (); |
|
528 |
|
529 if (nargin == 0) |
|
530 { |
|
531 if (curr_function) |
|
532 { |
|
533 if (curr_function->takes_varargs ()) |
|
534 curr_function->octave_va_start (); |
|
535 else |
|
536 { |
|
537 ::error ("va_start only valid within function taking variable"); |
|
538 ::error ("number of arguments"); |
|
539 } |
|
540 } |
|
541 else |
|
542 ::error ("va_start only valid within function body"); |
|
543 } |
|
544 else |
|
545 print_usage ("va_start"); |
|
546 |
|
547 return retval; |
|
548 } |
|
549 |
|
550 DEFUN ("vr_val", Fvr_val, Svr_val, 10, |
|
551 "vr_val (X): append X to the list of optional return values for a |
|
552 function that allows a variable number of return values") |
|
553 { |
|
554 Octave_object retval; |
|
555 |
|
556 int nargin = args.length (); |
|
557 |
|
558 if (nargin == 1) |
|
559 { |
|
560 if (curr_function) |
|
561 { |
|
562 if (curr_function->takes_var_return ()) |
|
563 curr_function->octave_vr_val (args(0)); |
|
564 else |
|
565 { |
|
566 ::error ("vr_val only valid within function declared to"); |
|
567 ::error ("produce a variable number of values"); |
|
568 } |
|
569 } |
|
570 else |
|
571 ::error ("vr_val only valid within function body"); |
|
572 } |
|
573 else |
|
574 print_usage ("vr_val"); |
|
575 |
|
576 return retval; |
|
577 } |
|
578 |
|
579 /* |
|
580 ;;; Local Variables: *** |
|
581 ;;; mode: C++ *** |
|
582 ;;; page-delimiter: "^/\\*" *** |
|
583 ;;; End: *** |
|
584 */ |