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 |
|
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 { |
2086
|
69 octave_value *tmp; |
1741
|
70 |
2086
|
71 tmp = new octave_value (nargin); |
1741
|
72 nargin_sr->define (tmp); |
|
73 |
2086
|
74 tmp = new octave_value (nargout); |
1741
|
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 |
2086
|
161 octave_value |
1741
|
162 tree_function::octave_va_arg (void) |
|
163 { |
2086
|
164 octave_value retval; |
1741
|
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 |
2086
|
175 octave_value_list |
1741
|
176 tree_function::octave_all_va_args (void) |
|
177 { |
2086
|
178 octave_value_list retval; |
1741
|
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 |
2086
|
196 tree_function::octave_vr_val (const octave_value& val) |
1741
|
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 |
2086
|
209 octave_value |
1827
|
210 tree_function::eval (bool print) |
1741
|
211 { |
2086
|
212 octave_value retval; |
1741
|
213 |
|
214 if (error_state || ! cmd_list) |
|
215 return retval; |
|
216 |
2086
|
217 octave_value_list tmp_args; |
|
218 octave_value_list tmp = eval (print, 0, tmp_args); |
1741
|
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 |
2086
|
250 octave_value_list |
|
251 tree_function::eval (bool /* print */, int nargout, const octave_value_list& args) |
1741
|
252 { |
2086
|
253 octave_value_list retval; |
1741
|
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; |
2086
|
337 octave_value last_computed_value = cmd_list->eval (pf); |
1741
|
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 { |
2086
|
360 octave_value tmp = builtin_any_variable ("default_return_value"); |
1741
|
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_function_header (void) |
|
402 { |
2124
|
403 tree_print_code tpc (octave_stdout); |
1741
|
404 |
2124
|
405 tpc.visit_function_header (*this); |
1741
|
406 } |
|
407 |
|
408 void |
|
409 tree_function::print_code_function_trailer (void) |
|
410 { |
2124
|
411 tree_print_code tpc (octave_stdout); |
|
412 |
|
413 tpc.visit_function_trailer (*this); |
1741
|
414 } |
|
415 |
|
416 void |
2124
|
417 tree_function::accept (tree_walker& tw) |
1741
|
418 { |
2124
|
419 tw.visit_function (*this); |
1741
|
420 } |
|
421 |
1957
|
422 DEFUN (va_arg, args, , |
1741
|
423 "va_arg (): return next argument in a function that takes a\n\ |
|
424 variable number of parameters") |
|
425 { |
2086
|
426 octave_value_list retval; |
1741
|
427 |
|
428 int nargin = args.length (); |
|
429 |
|
430 if (nargin == 0) |
|
431 { |
|
432 if (curr_function) |
|
433 { |
|
434 if (curr_function->takes_varargs ()) |
|
435 retval = curr_function->octave_va_arg (); |
|
436 else |
|
437 { |
|
438 ::error ("va_arg only valid within function taking variable"); |
|
439 ::error ("number of arguments"); |
|
440 } |
|
441 } |
|
442 else |
|
443 ::error ("va_arg only valid within function body"); |
|
444 } |
|
445 else |
|
446 print_usage ("va_arg"); |
|
447 |
|
448 return retval; |
|
449 } |
|
450 |
1957
|
451 DEFUN (va_start, args, , |
1741
|
452 "va_start (): reset the pointer to the list of optional arguments\n\ |
|
453 to the beginning") |
|
454 { |
2086
|
455 octave_value_list retval; |
1741
|
456 |
|
457 int nargin = args.length (); |
|
458 |
|
459 if (nargin == 0) |
|
460 { |
|
461 if (curr_function) |
|
462 { |
|
463 if (curr_function->takes_varargs ()) |
|
464 curr_function->octave_va_start (); |
|
465 else |
|
466 { |
|
467 ::error ("va_start only valid within function taking variable"); |
|
468 ::error ("number of arguments"); |
|
469 } |
|
470 } |
|
471 else |
|
472 ::error ("va_start only valid within function body"); |
|
473 } |
|
474 else |
|
475 print_usage ("va_start"); |
|
476 |
|
477 return retval; |
|
478 } |
|
479 |
1957
|
480 DEFUN (vr_val, args, , |
1741
|
481 "vr_val (X): append X to the list of optional return values for a |
|
482 function that allows a variable number of return values") |
|
483 { |
2086
|
484 octave_value_list retval; |
1741
|
485 |
|
486 int nargin = args.length (); |
|
487 |
|
488 if (nargin == 1) |
|
489 { |
|
490 if (curr_function) |
|
491 { |
|
492 if (curr_function->takes_var_return ()) |
|
493 curr_function->octave_vr_val (args(0)); |
|
494 else |
|
495 { |
|
496 ::error ("vr_val only valid within function declared to"); |
|
497 ::error ("produce a variable number of values"); |
|
498 } |
|
499 } |
|
500 else |
|
501 ::error ("vr_val only valid within function body"); |
|
502 } |
|
503 else |
|
504 print_usage ("vr_val"); |
|
505 |
|
506 return retval; |
|
507 } |
|
508 |
|
509 /* |
|
510 ;;; Local Variables: *** |
|
511 ;;; mode: C++ *** |
|
512 ;;; End: *** |
|
513 */ |