comparison src/pt-fcn-handle.cc @ 9327:e12a5ec6cf0d

tree_anon_fcn_handle::dup: don't convert to tree_constant
author John W. Eaton <jwe@octave.org>
date Wed, 10 Jun 2009 15:55:21 -0400
parents eb63fbe60fab
children cd96d29c5efa
comparison
equal deleted inserted replaced
9326:754f8f438a10 9327:e12a5ec6cf0d
137 %!function f = f1 (k) 137 %!function f = f1 (k)
138 %! f = @(x) f2 (@(y) y-k, x); 138 %! f = @(x) f2 (@(y) y-k, x);
139 %!test 139 %!test
140 %! assert ((f1 (3)) (10) == 7) 140 %! assert ((f1 (3)) (10) == 7)
141 %! 141 %!
142 %!shared g
143 %! g = @(t) feval (@(x) t*x, 2);
144 %!assert (g(0.5) == 1)
145 %!
142 %!shared f, g, h 146 %!shared f, g, h
143 %! h = @(x) sin (x); 147 %! h = @(x) sin (x);
144 %! g = @(f, x) h (x); 148 %! g = @(f, x) h (x);
145 %! f = @() g (@(x) h, pi); 149 %! f = @() g (@(x) h, pi);
146 %!assert (f () == sin (pi)) 150 %!assert (f () == sin (pi))
157 retval = rvalue1 (nargout); 161 retval = rvalue1 (nargout);
158 162
159 return retval; 163 return retval;
160 } 164 }
161 165
162 #if 0
163 tree_expression * 166 tree_expression *
164 tree_anon_fcn_handle::dup (symbol_table::scope_id parent_scope, 167 tree_anon_fcn_handle::dup (symbol_table::scope_id,
165 symbol_table::context_id parent_context) const 168 symbol_table::context_id) const
166 { 169 {
167 tree_parameter_list *param_list = parameter_list (); 170 tree_parameter_list *param_list = parameter_list ();
168 tree_parameter_list *ret_list = return_list (); 171 tree_parameter_list *ret_list = return_list ();
169 tree_statement_list *cmd_list = body (); 172 tree_statement_list *cmd_list = body ();
170 symbol_table::scope_id this_scope = scope (); 173 symbol_table::scope_id this_scope = scope ();
171 174
172 symbol_table::scope_id new_scope = symbol_table::dup_scope (this_scope); 175 symbol_table::scope_id new_scope = symbol_table::dup_scope (this_scope);
173 176
174 if (new_scope > 0) 177 if (new_scope > 0)
175 symbol_table::inherit (new_scope, parent_scope, parent_context); 178 symbol_table::inherit (new_scope, symbol_table::current_scope (),
179 symbol_table::current_context ());
176 180
177 tree_anon_fcn_handle *new_afh = new 181 tree_anon_fcn_handle *new_afh = new
178 tree_anon_fcn_handle (param_list ? param_list->dup (new_scope, 0) : 0, 182 tree_anon_fcn_handle (param_list ? param_list->dup (new_scope, 0) : 0,
179 ret_list ? ret_list->dup (new_scope, 0) : 0, 183 ret_list ? ret_list->dup (new_scope, 0) : 0,
180 cmd_list ? cmd_list->dup (new_scope, 0) : 0, 184 cmd_list ? cmd_list->dup (new_scope, 0) : 0,
182 186
183 new_afh->copy_base (*this); 187 new_afh->copy_base (*this);
184 188
185 return new_afh; 189 return new_afh;
186 } 190 }
187 #endif
188
189 tree_expression *
190 tree_anon_fcn_handle::dup (symbol_table::scope_id,
191 symbol_table::context_id) const
192 {
193 // Instead of simply duplicating, transform to a tree_constant
194 // object that contains an octave_fcn_handle object with the symbol
195 // table of the referenced function primed with values from the
196 // current scope and context.
197
198 tree_parameter_list *param_list = parameter_list ();
199 tree_parameter_list *ret_list = return_list ();
200 tree_statement_list *cmd_list = body ();
201 symbol_table::scope_id this_scope = scope ();
202
203 symbol_table::scope_id new_scope = symbol_table::dup_scope (this_scope);
204
205 if (new_scope > 0)
206 symbol_table::inherit (new_scope, symbol_table::current_scope (),
207 symbol_table::current_context ());
208
209 octave_user_function *uf
210 = new octave_user_function (new_scope,
211 param_list ? param_list->dup (new_scope, 0) : 0,
212 ret_list ? ret_list->dup (new_scope, 0) : 0,
213 cmd_list ? cmd_list->dup (new_scope, 0) : 0);
214
215 octave_function *curr_fcn = octave_call_stack::current ();
216
217 if (curr_fcn)
218 {
219 uf->stash_parent_fcn_name (curr_fcn->name ());
220
221 symbol_table::scope_id parent_scope = curr_fcn->parent_fcn_scope ();
222
223 if (parent_scope < 0)
224 parent_scope = curr_fcn->scope ();
225
226 uf->stash_parent_fcn_scope (parent_scope);
227 }
228
229 uf->mark_as_inline_function ();
230
231 octave_value ov_fcn (uf);
232
233 octave_value fh (new octave_fcn_handle (ov_fcn, "@<anonymous>"));
234
235 return new tree_constant (fh, line (), column ());
236 }
237 191
238 void 192 void
239 tree_anon_fcn_handle::accept (tree_walker& tw) 193 tree_anon_fcn_handle::accept (tree_walker& tw)
240 { 194 {
241 tw.visit_anon_fcn_handle (*this); 195 tw.visit_anon_fcn_handle (*this);