Mercurial > octave
annotate libinterp/parse-tree/pt-fcn-handle.cc @ 28430:5bfa8e018704 stable
store local init vars for anonymous functions in handle, not function object
This change is step toward revamping function handles by storing
variable init values for anonymous functions in function handle
objects instead of in the corresponding functions.
* call-stack.h, call-stack.cc (call_stack::push): New overload that
accepts local variable map in addition to function object.
* stack-frame.h (user_fcn_stack_frame::user_fcn_stack_frame):
New constructor that accepts local variable map in addition to
function object.
(stack_frame::local_vars_map): New typedef.
* ov-fcn-handle.h, ov-fcn-handle.cc (octave_fcn_handle::m_local_vars):
New data member.
(octave_fcn_handle::octave_fcn_handle): Update existing constructors
and provide new one to construct handle from function object and local
variable map.
(octave_fcn_handle::call): If m_local_vars is defined, push stack
frame with that info and execute function here.
(octave_fcn_handle::workspace): Create workspace struct from
m_local_vars instead of getting that info from the function object.
(octave_fcn_handle::parse_anon_fcn_handle): Copy m_local_vars from new
function handle object.
(octave_fcn_handle::save_ascii, octave_fcn_handle::save_binary,
octave_fcn_handle::save_hdf5): Use m_local_vars instead of getting
info from function object.
* ov-usr-fcn.h, ov-usr-fcn.cc (octave_user_function::local_vars_map):
Delete typedef.
(octave_user_function::m_local_var_init_vals): Delete data member and
all uses.
(octave_user_function::local_var_init_vals): Delete.
* pt-eval.h, pt-eval.cc (tree_evaluator::push_stack_frame):
New overload that accepts local variable map and user function.
(tree_evaluator::init_local_fcn_vars): Delete function and all uses.
* pt-fcn-handle.cc (tree_anon_fcn_handle::evaluate): Store local
variables in function handle object instead of function object.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 30 Mar 2020 15:14:10 -0400 |
parents | 9a3deb17b4ea |
children | d05a4194f1ad |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 //////////////////////////////////////////////////////////////////////// |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
3 // Copyright (C) 2003-2020 The Octave Project Developers |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
4 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 // See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 // distribution or <https://octave.org/copyright/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
7 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
8 // This file is part of Octave. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
9 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
10 // Octave is free software: you can redistribute it and/or modify it |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
11 // under the terms of the GNU General Public License as published by |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
12 // the Free Software Foundation, either version 3 of the License, or |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
13 // (at your option) any later version. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
14 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
15 // Octave is distributed in the hope that it will be useful, but |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
16 // WITHOUT ANY WARRANTY; without even the implied warranty of |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
18 // GNU General Public License for more details. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
19 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
20 // You should have received a copy of the GNU General Public License |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
21 // along with Octave; see the file COPYING. If not, see |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
22 // <https://www.gnu.org/licenses/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 //////////////////////////////////////////////////////////////////////// |
4343 | 25 |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
26 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21200
diff
changeset
|
27 # include "config.h" |
4343 | 28 #endif |
29 | |
25438
cb1606f78f6b
prefer <istream>, <ostream>, or <iosfwd> to <iostream> where possible
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
30 #include <ostream> |
4343 | 31 |
23599
5cb3a2bb5e1e
don't use singleton for symbol_table
John W. Eaton <jwe@octave.org>
parents:
23435
diff
changeset
|
32 #include "interpreter-private.h" |
27371
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
33 #include "pt-anon-scopes.h" |
4343 | 34 #include "pt-fcn-handle.h" |
28430
5bfa8e018704
store local init vars for anonymous functions in handle, not function object
John W. Eaton <jwe@octave.org>
parents:
28426
diff
changeset
|
35 #include "stack-frame.h" |
4343 | 36 |
23075
4e3d47dc7e25
move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23052
diff
changeset
|
37 namespace octave |
4343 | 38 { |
23075
4e3d47dc7e25
move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23052
diff
changeset
|
39 void |
4e3d47dc7e25
move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23052
diff
changeset
|
40 tree_fcn_handle::print (std::ostream& os, bool pr_as_read_syntax, |
4e3d47dc7e25
move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23052
diff
changeset
|
41 bool pr_orig_text) |
4e3d47dc7e25
move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23052
diff
changeset
|
42 { |
4e3d47dc7e25
move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23052
diff
changeset
|
43 print_raw (os, pr_as_read_syntax, pr_orig_text); |
4e3d47dc7e25
move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23052
diff
changeset
|
44 } |
4e3d47dc7e25
move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23052
diff
changeset
|
45 |
4e3d47dc7e25
move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23052
diff
changeset
|
46 void |
4e3d47dc7e25
move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23052
diff
changeset
|
47 tree_fcn_handle::print_raw (std::ostream& os, bool pr_as_read_syntax, |
4e3d47dc7e25
move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23052
diff
changeset
|
48 bool pr_orig_text) |
4e3d47dc7e25
move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23052
diff
changeset
|
49 { |
24846
7620f1f5290d
use m_ prefix for member variables in parse tree classes
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
50 os << ((pr_as_read_syntax || pr_orig_text) ? "@" : "") << m_name; |
23075
4e3d47dc7e25
move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23052
diff
changeset
|
51 } |
4343 | 52 |
23075
4e3d47dc7e25
move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23052
diff
changeset
|
53 tree_expression * |
24270
bc3819b7cca1
don't use symbol_table:: nesting for symbol_record, symbol_scope, or fcn_info
John W. Eaton <jwe@octave.org>
parents:
23795
diff
changeset
|
54 tree_fcn_handle::dup (symbol_scope&) const |
23075
4e3d47dc7e25
move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23052
diff
changeset
|
55 { |
24846
7620f1f5290d
use m_ prefix for member variables in parse tree classes
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
56 tree_fcn_handle *new_fh = new tree_fcn_handle (m_name, line (), column ()); |
23075
4e3d47dc7e25
move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23052
diff
changeset
|
57 |
4e3d47dc7e25
move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23052
diff
changeset
|
58 new_fh->copy_base (*this); |
21055
5e00ed38a58b
maint: Replace if/error/else paradigm with just if/error.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
59 |
23075
4e3d47dc7e25
move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23052
diff
changeset
|
60 return new_fh; |
4e3d47dc7e25
move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23052
diff
changeset
|
61 } |
4343 | 62 |
27371
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
63 octave_value |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
64 tree_fcn_handle::evaluate (tree_evaluator& tw, int) |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
65 { |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
66 return make_fcn_handle (tw.get_interpreter (), m_name); |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
67 } |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
68 |
23599
5cb3a2bb5e1e
don't use singleton for symbol_table
John W. Eaton <jwe@octave.org>
parents:
23435
diff
changeset
|
69 tree_anon_fcn_handle::~tree_anon_fcn_handle (void) |
5cb3a2bb5e1e
don't use singleton for symbol_table
John W. Eaton <jwe@octave.org>
parents:
23435
diff
changeset
|
70 { |
5cb3a2bb5e1e
don't use singleton for symbol_table
John W. Eaton <jwe@octave.org>
parents:
23435
diff
changeset
|
71 delete m_parameter_list; |
23600
db947ba52118
explicitly limit anonymous functions to a single expression
John W. Eaton <jwe@octave.org>
parents:
23599
diff
changeset
|
72 delete m_expression; |
23599
5cb3a2bb5e1e
don't use singleton for symbol_table
John W. Eaton <jwe@octave.org>
parents:
23435
diff
changeset
|
73 } |
5cb3a2bb5e1e
don't use singleton for symbol_table
John W. Eaton <jwe@octave.org>
parents:
23435
diff
changeset
|
74 |
23075
4e3d47dc7e25
move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23052
diff
changeset
|
75 tree_expression * |
24270
bc3819b7cca1
don't use symbol_table:: nesting for symbol_record, symbol_scope, or fcn_info
John W. Eaton <jwe@octave.org>
parents:
23795
diff
changeset
|
76 tree_anon_fcn_handle::dup (symbol_scope&) const |
23075
4e3d47dc7e25
move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23052
diff
changeset
|
77 { |
4e3d47dc7e25
move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23052
diff
changeset
|
78 tree_parameter_list *param_list = parameter_list (); |
23600
db947ba52118
explicitly limit anonymous functions to a single expression
John W. Eaton <jwe@octave.org>
parents:
23599
diff
changeset
|
79 tree_expression *expr = expression (); |
23599
5cb3a2bb5e1e
don't use singleton for symbol_table
John W. Eaton <jwe@octave.org>
parents:
23435
diff
changeset
|
80 |
24361
8bcfddad15ec
use shared_ptr to manage symbol_scope objects
John W. Eaton <jwe@octave.org>
parents:
24354
diff
changeset
|
81 symbol_scope af_scope = m_scope; |
8bcfddad15ec
use shared_ptr to manage symbol_scope objects
John W. Eaton <jwe@octave.org>
parents:
24354
diff
changeset
|
82 symbol_scope af_parent_scope = m_parent_scope; |
8045
24701aa75ecb
scope fixes for anonymous and inline functions that appear inside subfunctions
John W. Eaton <jwe@octave.org>
parents:
8032
diff
changeset
|
83 |
24361
8bcfddad15ec
use shared_ptr to manage symbol_scope objects
John W. Eaton <jwe@octave.org>
parents:
24354
diff
changeset
|
84 symbol_scope new_scope; |
8bcfddad15ec
use shared_ptr to manage symbol_scope objects
John W. Eaton <jwe@octave.org>
parents:
24354
diff
changeset
|
85 |
8bcfddad15ec
use shared_ptr to manage symbol_scope objects
John W. Eaton <jwe@octave.org>
parents:
24354
diff
changeset
|
86 if (af_scope) |
8bcfddad15ec
use shared_ptr to manage symbol_scope objects
John W. Eaton <jwe@octave.org>
parents:
24354
diff
changeset
|
87 new_scope = af_scope.dup (); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
88 |
24354
11d3603dd880
give names to anonymous function scopes
John W. Eaton <jwe@octave.org>
parents:
24270
diff
changeset
|
89 // FIXME: if new scope is nullptr, then we are in big trouble here... |
11d3603dd880
give names to anonymous function scopes
John W. Eaton <jwe@octave.org>
parents:
24270
diff
changeset
|
90 |
23075
4e3d47dc7e25
move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23052
diff
changeset
|
91 tree_anon_fcn_handle *new_afh = new |
24361
8bcfddad15ec
use shared_ptr to manage symbol_scope objects
John W. Eaton <jwe@octave.org>
parents:
24354
diff
changeset
|
92 tree_anon_fcn_handle (param_list ? param_list->dup (new_scope) : nullptr, |
8bcfddad15ec
use shared_ptr to manage symbol_scope objects
John W. Eaton <jwe@octave.org>
parents:
24354
diff
changeset
|
93 expr ? expr->dup (new_scope) : nullptr, |
23602
214cb58ccc1c
use pointer to scope instead of scope id
John W. Eaton <jwe@octave.org>
parents:
23600
diff
changeset
|
94 new_scope, af_parent_scope, line (), column ()); |
6656 | 95 |
23075
4e3d47dc7e25
move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23052
diff
changeset
|
96 new_afh->copy_base (*this); |
4e3d47dc7e25
move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23052
diff
changeset
|
97 |
4e3d47dc7e25
move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23052
diff
changeset
|
98 return new_afh; |
4e3d47dc7e25
move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23052
diff
changeset
|
99 } |
27371
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
100 |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
101 octave_value |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
102 tree_anon_fcn_handle::evaluate (tree_evaluator& tw, int) |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
103 { |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
104 // FIXME: should CMD_LIST be limited to a single expression? |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
105 // I think that is what Matlab does. |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
106 |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
107 symbol_scope new_scope; |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
108 if (m_scope) |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
109 new_scope = m_scope.dup (); |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
110 |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
111 tree_parameter_list *param_list_dup |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
112 = m_parameter_list ? m_parameter_list->dup (new_scope) : nullptr; |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
113 |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
114 tree_parameter_list *ret_list = nullptr; |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
115 |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
116 tree_statement_list *stmt_list = nullptr; |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
117 |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
118 symbol_scope parent_scope = tw.get_current_scope (); |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
119 |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
120 new_scope.set_parent (parent_scope); |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
121 new_scope.set_primary_parent (parent_scope); |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
122 |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
123 if (m_expression) |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
124 { |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
125 tree_expression *expr_dup = m_expression->dup (new_scope); |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
126 tree_statement *stmt = new tree_statement (expr_dup, nullptr); |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
127 stmt_list = new tree_statement_list (stmt); |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
128 } |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
129 |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
130 tree_anon_scopes anon_fcn_ctx (*this); |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
131 |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
132 std::set<std::string> free_vars = anon_fcn_ctx.free_variables (); |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
133 |
28430
5bfa8e018704
store local init vars for anonymous functions in handle, not function object
John W. Eaton <jwe@octave.org>
parents:
28426
diff
changeset
|
134 stack_frame::local_vars_map local_vars; |
27371
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
135 |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
136 call_stack& cs = tw.get_call_stack (); |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
137 |
28426
9a3deb17b4ea
use shared_ptr for stack frames in call stack and for accesss and static links
John W. Eaton <jwe@octave.org>
parents:
28026
diff
changeset
|
138 std::shared_ptr<stack_frame> frame = cs.get_current_stack_frame (); |
27371
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
139 |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
140 for (auto& name : free_vars) |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
141 { |
28426
9a3deb17b4ea
use shared_ptr for stack frames in call stack and for accesss and static links
John W. Eaton <jwe@octave.org>
parents:
28026
diff
changeset
|
142 octave_value val = frame->varval (name); |
27371
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
143 |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
144 if (val.is_defined ()) |
28430
5bfa8e018704
store local init vars for anonymous functions in handle, not function object
John W. Eaton <jwe@octave.org>
parents:
28426
diff
changeset
|
145 local_vars[name] = val; |
27371
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
146 } |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
147 |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
148 octave_user_function *af |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
149 = new octave_user_function (new_scope, param_list_dup, ret_list, |
28430
5bfa8e018704
store local init vars for anonymous functions in handle, not function object
John W. Eaton <jwe@octave.org>
parents:
28426
diff
changeset
|
150 stmt_list); |
27371
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
151 |
27873
020d0e8f7ac6
reafactor mlock, munlock, and mislocked functions
John W. Eaton <jwe@octave.org>
parents:
27371
diff
changeset
|
152 octave_function *curr_fcn = cs.current_function (); |
27371
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
153 |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
154 if (curr_fcn) |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
155 { |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
156 // FIXME: maybe it would be better to just stash curr_fcn |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
157 // instead of individual bits of info about it? |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
158 |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
159 af->stash_parent_fcn_name (curr_fcn->name ()); |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
160 af->stash_dir_name (curr_fcn->dir_name ()); |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
161 |
28026
262cdfc6faf9
allow reloading of handles to private functions (bug #57439)
John W. Eaton <jwe@octave.org>
parents:
27923
diff
changeset
|
162 new_scope.cache_fcn_file_name (curr_fcn->fcn_file_name ()); |
262cdfc6faf9
allow reloading of handles to private functions (bug #57439)
John W. Eaton <jwe@octave.org>
parents:
27923
diff
changeset
|
163 new_scope.cache_dir_name (curr_fcn->dir_name ()); |
262cdfc6faf9
allow reloading of handles to private functions (bug #57439)
John W. Eaton <jwe@octave.org>
parents:
27923
diff
changeset
|
164 |
27371
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
165 // The following is needed so that class method dispatch works |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
166 // properly for anonymous functions that wrap class methods. |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
167 |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
168 if (curr_fcn->is_class_method () || curr_fcn->is_class_constructor ()) |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
169 af->stash_dispatch_class (curr_fcn->dispatch_class ()); |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
170 |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
171 af->stash_fcn_file_name (curr_fcn->fcn_file_name ()); |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
172 } |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
173 |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
174 af->mark_as_anonymous_function (); |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
175 |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
176 octave_value ov_fcn (af); |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
177 |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
178 // octave_value fh (octave_fcn_binder::maybe_binder (ov_fcn, m_interpreter)); |
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
179 |
28430
5bfa8e018704
store local init vars for anonymous functions in handle, not function object
John W. Eaton <jwe@octave.org>
parents:
28426
diff
changeset
|
180 return octave_value (new octave_fcn_handle (ov_fcn, local_vars)); |
27371
fcaecdbc8d8a
don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents:
26661
diff
changeset
|
181 } |
5861 | 182 } |
183 | |
8906
ab87d08d9a1b
improve symbol inheritance for anonymous functions
John W. Eaton <jwe@octave.org>
parents:
8658
diff
changeset
|
184 /* |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
13241
diff
changeset
|
185 %!function r = __f2 (f, x) |
8906
ab87d08d9a1b
improve symbol inheritance for anonymous functions
John W. Eaton <jwe@octave.org>
parents:
8658
diff
changeset
|
186 %! r = f (x); |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
13241
diff
changeset
|
187 %!endfunction |
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
13241
diff
changeset
|
188 %!function f = __f1 (k) |
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
13241
diff
changeset
|
189 %! f = @(x) __f2 (@(y) y-k, x); |
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
13241
diff
changeset
|
190 %!endfunction |
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
13241
diff
changeset
|
191 |
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
13241
diff
changeset
|
192 %!assert ((__f1 (3)) (10) == 7) |
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
13241
diff
changeset
|
193 |
8906
ab87d08d9a1b
improve symbol inheritance for anonymous functions
John W. Eaton <jwe@octave.org>
parents:
8658
diff
changeset
|
194 %!test |
9327
e12a5ec6cf0d
tree_anon_fcn_handle::dup: don't convert to tree_constant
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
195 %! g = @(t) feval (@(x) t*x, 2); |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
13241
diff
changeset
|
196 %! assert (g(0.5) == 1); |
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
13241
diff
changeset
|
197 |
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
13241
diff
changeset
|
198 %!test |
8906
ab87d08d9a1b
improve symbol inheritance for anonymous functions
John W. Eaton <jwe@octave.org>
parents:
8658
diff
changeset
|
199 %! h = @(x) sin (x); |
ab87d08d9a1b
improve symbol inheritance for anonymous functions
John W. Eaton <jwe@octave.org>
parents:
8658
diff
changeset
|
200 %! g = @(f, x) h (x); |
ab87d08d9a1b
improve symbol inheritance for anonymous functions
John W. Eaton <jwe@octave.org>
parents:
8658
diff
changeset
|
201 %! f = @() g (@(x) h, pi); |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
13241
diff
changeset
|
202 %! assert (f () == sin (pi)); |
16681
d3619d4d267c
recognize character string at beginning of statement (bug #38926, #38958)
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
203 |
d3619d4d267c
recognize character string at beginning of statement (bug #38926, #38958)
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
204 The next two tests are intended to test parsing of a character string |
d3619d4d267c
recognize character string at beginning of statement (bug #38926, #38958)
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
205 vs. hermitian operator at the beginning of an anonymous function |
d3619d4d267c
recognize character string at beginning of statement (bug #38926, #38958)
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
206 expression. The use of ' for the character string and the spacing is |
d3619d4d267c
recognize character string at beginning of statement (bug #38926, #38958)
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
207 intentional, so don't change it. |
d3619d4d267c
recognize character string at beginning of statement (bug #38926, #38958)
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
208 |
d3619d4d267c
recognize character string at beginning of statement (bug #38926, #38958)
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
209 %!test |
d3619d4d267c
recognize character string at beginning of statement (bug #38926, #38958)
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
210 %! f = @() 'foo'; |
d3619d4d267c
recognize character string at beginning of statement (bug #38926, #38958)
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
211 %! assert (f (), 'foo'); |
d3619d4d267c
recognize character string at beginning of statement (bug #38926, #38958)
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
212 |
d3619d4d267c
recognize character string at beginning of statement (bug #38926, #38958)
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
213 %!test |
d3619d4d267c
recognize character string at beginning of statement (bug #38926, #38958)
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
214 %! f = @()'foo'; |
d3619d4d267c
recognize character string at beginning of statement (bug #38926, #38958)
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
215 %! assert (f (), 'foo'); |
8906
ab87d08d9a1b
improve symbol inheritance for anonymous functions
John W. Eaton <jwe@octave.org>
parents:
8658
diff
changeset
|
216 */ |