GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
pt-fcn-handle.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2003-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <iostream>
28 
29 #include "interpreter-private.h"
30 #include "pt-fcn-handle.h"
31 
32 namespace octave
33 {
34  void
35  tree_fcn_handle::print (std::ostream& os, bool pr_as_read_syntax,
36  bool pr_orig_text)
37  {
38  print_raw (os, pr_as_read_syntax, pr_orig_text);
39  }
40 
41  void
42  tree_fcn_handle::print_raw (std::ostream& os, bool pr_as_read_syntax,
43  bool pr_orig_text)
44  {
45  os << ((pr_as_read_syntax || pr_orig_text) ? "@" : "") << m_name;
46  }
47 
50  {
51  tree_fcn_handle *new_fh = new tree_fcn_handle (m_name, line (), column ());
52 
53  new_fh->copy_base (*this);
54 
55  return new_fh;
56  }
57 
59  {
60  delete m_parameter_list;
61  delete m_expression;
62  }
63 
66  {
67  tree_parameter_list *param_list = parameter_list ();
68  tree_expression *expr = expression ();
69 
70  symbol_scope af_scope = m_scope;
71  symbol_scope af_parent_scope = m_parent_scope;
72 
73  symbol_table& symtab
74  = __get_symbol_table__ ("tree_anon_fcn_handle::dup");
75 
76  symbol_scope new_scope;
77 
78  if (af_scope)
79  new_scope = af_scope.dup ();
80 
81  // FIXME: why should we inherit from the current scope here? That
82  // doesn't seem right, but with the way things work now it appears
83  // to be required for bug-31371.tst to pass.
84 
85  if (new_scope)
86  symtab.inherit (new_scope);
87 
88  // FIXME: if new scope is nullptr, then we are in big trouble here...
89 
90  tree_anon_fcn_handle *new_afh = new
91  tree_anon_fcn_handle (param_list ? param_list->dup (new_scope) : nullptr,
92  expr ? expr->dup (new_scope) : nullptr,
93  new_scope, af_parent_scope, line (), column ());
94 
95  new_afh->copy_base (*this);
96 
97  return new_afh;
98  }
99 }
100 
101 /*
102 %!function r = __f2 (f, x)
103 %! r = f (x);
104 %!endfunction
105 %!function f = __f1 (k)
106 %! f = @(x) __f2 (@(y) y-k, x);
107 %!endfunction
108 
109 %!assert ((__f1 (3)) (10) == 7)
110 
111 %!test
112 %! g = @(t) feval (@(x) t*x, 2);
113 %! assert (g(0.5) == 1);
114 
115 %!test
116 %! h = @(x) sin (x);
117 %! g = @(f, x) h (x);
118 %! f = @() g (@(x) h, pi);
119 %! assert (f () == sin (pi));
120 
121 The next two tests are intended to test parsing of a character string
122 vs. hermitian operator at the beginning of an anonymous function
123 expression. The use of ' for the character string and the spacing is
124 intentional, so don't change it.
125 
126 %!test
127 %! f = @() 'foo';
128 %! assert (f (), 'foo');
129 
130 %!test
131 %! f = @()'foo';
132 %! assert (f (), 'foo');
133 */
virtual int line(void) const
Definition: pt.h:56
tree_parameter_list * dup(symbol_scope &scope) const
Definition: pt-misc.cc:80
virtual int column(void) const
Definition: pt.h:58
symbol_scope dup(void) const
Definition: symscope.h:658
tree_expression * dup(symbol_scope &scope) const
tree_expression * dup(symbol_scope &scope) const
symbol_table & __get_symbol_table__(const std::string &who)
void inherit(symbol_scope &recipient_scope, const symbol_scope &donor_scope)
Definition: symtab.h:129
tree_parameter_list * parameter_list(void) const
virtual void copy_base(const tree_expression &e)
Definition: pt-exp.h:130
void print_raw(std::ostream &os, bool pr_as_read_syntax=false, bool pr_orig_txt=true)
tree_parameter_list * m_parameter_list
tree_expression * m_expression
tree_expression * expression(void) const
tree_anon_fcn_handle(int l=-1, int c=-1)
Definition: pt-fcn-handle.h:92
tree_fcn_handle(int l=-1, int c=-1)
Definition: pt-fcn-handle.h:49
void print(std::ostream &os, bool pr_as_read_syntax=false, bool pr_orig_txt=true)
octave::stream os
Definition: file-io.cc:627
virtual tree_expression * dup(symbol_scope &scope) const =0