2974
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
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 (octave_user_function_h) |
|
24 #define octave_user_function_h 1 |
|
25 |
4192
|
26 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
2974
|
27 #pragma interface |
|
28 #endif |
|
29 |
|
30 #include <ctime> |
|
31 |
|
32 #include <string> |
4214
|
33 #include <stack> |
2974
|
34 |
3665
|
35 #include "comment-list.h" |
2974
|
36 #include "oct-obj.h" |
|
37 #include "ov-fcn.h" |
|
38 #include "ov-typeinfo.h" |
|
39 |
|
40 class string_vector; |
|
41 |
|
42 class octave_value; |
|
43 class tree_parameter_list; |
|
44 class tree_statement_list; |
|
45 class tree_va_return_list; |
|
46 class tree_walker; |
|
47 class symbol_table; |
|
48 class symbol_record; |
|
49 |
|
50 // Builtin functions. |
|
51 |
|
52 class |
|
53 octave_user_function : public octave_function |
|
54 { |
|
55 public: |
|
56 |
|
57 octave_user_function (tree_parameter_list *pl = 0, |
|
58 tree_parameter_list *rl = 0, |
|
59 tree_statement_list *cl = 0, |
|
60 symbol_table *st = 0); |
|
61 |
|
62 ~octave_user_function (void); |
|
63 |
|
64 octave_function *function_value (bool) { return this; } |
|
65 |
|
66 octave_user_function *define_param_list (tree_parameter_list *t); |
|
67 |
|
68 octave_user_function *define_ret_list (tree_parameter_list *t); |
|
69 |
4343
|
70 void stash_fcn_file_name (const std::string& nm); |
2974
|
71 |
3665
|
72 void stash_leading_comment (octave_comment_list *lc) { lead_comm = lc; } |
|
73 |
|
74 void stash_trailing_comment (octave_comment_list *tc) { trail_comm = tc; } |
|
75 |
3325
|
76 void mark_fcn_file_up_to_date (const octave_time& t) { t_checked = t; } |
3165
|
77 |
3255
|
78 void stash_fcn_file_time (const octave_time& t) |
3165
|
79 { |
|
80 t_parsed = t; |
|
81 mark_fcn_file_up_to_date (t); |
|
82 } |
2974
|
83 |
4346
|
84 void stash_symtab_ptr (symbol_record *sr) { symtab_entry = sr; } |
2974
|
85 |
4346
|
86 std::string fcn_file_name (void) const { return file_name; } |
2974
|
87 |
4346
|
88 octave_time time_parsed (void) const { return t_parsed; } |
2974
|
89 |
4346
|
90 octave_time time_checked (void) const { return t_checked; } |
3165
|
91 |
2974
|
92 void mark_as_system_fcn_file (void); |
|
93 |
4346
|
94 bool is_system_fcn_file (void) const { return system_fcn_file; } |
2974
|
95 |
|
96 bool takes_varargs (void) const; |
|
97 |
4346
|
98 void octave_va_start (void) { curr_va_arg_number = num_named_args; } |
2974
|
99 |
|
100 octave_value octave_va_arg (void); |
|
101 |
|
102 octave_value_list octave_all_va_args (void); |
|
103 |
|
104 bool takes_var_return (void) const; |
|
105 |
|
106 void octave_vr_val (const octave_value& val); |
|
107 |
3974
|
108 void varargout_to_vr_val (void); |
|
109 |
|
110 bool has_varargout (void) const; |
|
111 |
3523
|
112 void stash_function_name (const std::string& s); |
2974
|
113 |
4346
|
114 std::string function_name (void) const { return fcn_name; } |
2974
|
115 |
4238
|
116 void mark_as_nested_function (void) { nested_function = true; } |
|
117 |
|
118 bool is_nested_function (void) const { return nested_function; } |
|
119 |
3875
|
120 void save_args_passed (const octave_value_list& args) |
|
121 { |
|
122 if (call_depth > 1) |
|
123 saved_args.push (args_passed); |
|
124 |
|
125 args_passed = args; |
|
126 } |
|
127 |
|
128 void restore_args_passed (void) |
|
129 { |
3933
|
130 if (saved_args.empty ()) |
|
131 args_passed = octave_value_list (); |
|
132 else |
4214
|
133 { |
|
134 args_passed = saved_args.top (); |
|
135 saved_args.pop (); |
|
136 } |
3875
|
137 } |
3239
|
138 |
4271
|
139 octave_value subsref (const std::string& type, |
|
140 const std::list<octave_value_list>& idx) |
|
141 { |
|
142 panic_impossible (); |
|
143 return octave_value (); |
|
144 } |
|
145 |
4247
|
146 octave_value_list subsref (const std::string& type, |
4219
|
147 const std::list<octave_value_list>& idx, |
3933
|
148 int nargout); |
|
149 |
3544
|
150 octave_value_list |
|
151 do_multi_index_op (int nargout, const octave_value_list& args); |
2974
|
152 |
4346
|
153 void traceback_error (void) const; |
2974
|
154 |
|
155 tree_parameter_list *parameter_list (void) { return param_list; } |
|
156 |
|
157 tree_parameter_list *return_list (void) { return ret_list; } |
|
158 |
|
159 tree_statement_list *body (void) { return cmd_list; } |
|
160 |
3665
|
161 octave_comment_list *leading_comment (void) { return lead_comm; } |
|
162 |
|
163 octave_comment_list *trailing_comment (void) { return trail_comm; } |
|
164 |
2974
|
165 void accept (tree_walker& tw); |
|
166 |
3933
|
167 void print_symtab_info (std::ostream& os) const; |
|
168 |
2974
|
169 private: |
|
170 |
|
171 // List of arguments for this function. These are local variables. |
|
172 tree_parameter_list *param_list; |
|
173 |
|
174 // List of parameters we return. These are also local variables in |
|
175 // this function. |
|
176 tree_parameter_list *ret_list; |
|
177 |
|
178 // The list of commands that make up the body of this function. |
|
179 tree_statement_list *cmd_list; |
|
180 |
|
181 // The local symbol table for this function. |
|
182 symbol_table *sym_tab; |
|
183 |
3665
|
184 // The comments preceding the FUNCTION token. |
|
185 octave_comment_list *lead_comm; |
|
186 |
|
187 // The comments preceding the ENDFUNCTION token. |
|
188 octave_comment_list *trail_comm; |
|
189 |
2974
|
190 // The name of the file we parsed |
3523
|
191 std::string file_name; |
2974
|
192 |
|
193 // The name of the function. |
3523
|
194 std::string fcn_name; |
2974
|
195 |
|
196 // The time the file was parsed. |
3255
|
197 octave_time t_parsed; |
2974
|
198 |
3165
|
199 // The time the file was last checked to see if it needs to be |
|
200 // parsed again. |
3255
|
201 octave_time t_checked; |
3165
|
202 |
2974
|
203 // True if this function came from a file that is considered to be a |
|
204 // system function. This affects whether we check the time stamp |
|
205 // on the file to see if it has changed. |
|
206 bool system_fcn_file; |
|
207 |
|
208 // Used to keep track of recursion depth. |
|
209 int call_depth; |
|
210 |
|
211 // The number of arguments that have names. |
|
212 int num_named_args; |
|
213 |
4238
|
214 // TRUE means this is a nested function. |
|
215 bool nested_function; |
|
216 |
2974
|
217 // The values that were passed as arguments. |
|
218 octave_value_list args_passed; |
|
219 |
3875
|
220 // A place to store the passed args for recursive calls. |
4214
|
221 std::stack<octave_value_list> saved_args; |
3875
|
222 |
2974
|
223 // The number of arguments passed in. |
|
224 int num_args_passed; |
|
225 |
|
226 // Used to keep track of the current offset into the list of va_args. |
|
227 int curr_va_arg_number; |
|
228 |
|
229 // The list of return values when an unspecified number can be |
|
230 // returned. |
|
231 tree_va_return_list *vr_list; |
|
232 |
|
233 // The symbol record for this function. |
|
234 symbol_record *symtab_entry; |
|
235 |
|
236 // The symbol record for argn in the local symbol table. |
|
237 symbol_record *argn_sr; |
|
238 |
|
239 // The symbol record for nargin in the local symbol table. |
|
240 symbol_record *nargin_sr; |
|
241 |
|
242 // The symbol record for nargout in the local symbol table. |
|
243 symbol_record *nargout_sr; |
|
244 |
3974
|
245 // The symbol record for varargin in the local symbol table. |
|
246 symbol_record *varargin_sr; |
|
247 |
2974
|
248 void print_code_function_header (void); |
|
249 |
|
250 void print_code_function_trailer (void); |
|
251 |
|
252 void install_automatic_vars (void); |
|
253 |
|
254 void bind_automatic_vars (const string_vector& arg_names, int nargin, |
3974
|
255 int nargout, const octave_value_list& va_args); |
3219
|
256 |
4645
|
257 |
|
258 // No copying! |
|
259 |
|
260 octave_user_function (const octave_user_function& fn); |
|
261 |
|
262 octave_user_function& operator = (const octave_user_function& fn); |
|
263 |
4612
|
264 DECLARE_OCTAVE_ALLOCATOR |
3219
|
265 |
4612
|
266 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA |
2974
|
267 }; |
|
268 |
|
269 #endif |
|
270 |
|
271 /* |
|
272 ;;; Local Variables: *** |
|
273 ;;; mode: C++ *** |
|
274 ;;; End: *** |
|
275 */ |