GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
pt-unop.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include "interpreter.h"
31 #include "ov.h"
32 #include "profiler.h"
33 #include "pt-unop.h"
34 
35 namespace octave
36 {
37  // Unary expressions.
38 
39  std::string
41  {
43  }
44 
45  // Prefix expressions.
46 
49  {
51  = new tree_prefix_expression (m_op ? m_op->dup (scope) : nullptr,
52  line (), column (), m_etype);
53 
54  new_pe->copy_base (*this);
55 
56  return new_pe;
57  }
58 
61  {
62  octave_value val;
63 
64  if (m_op)
65  {
68  {
69  octave_lvalue op_ref = m_op->lvalue (tw);
70 
72  block (tw.get_profiler (), *this);
73 
74  op_ref.do_unary_op (m_etype);
75 
76  val = op_ref.value ();
77  }
78  else
79  {
80  octave_value op_val = m_op->evaluate (tw);
81 
82  if (op_val.is_defined ())
83  {
85  block (tw.get_profiler (), *this);
86 
87  // Attempt to do the operation in-place if it is unshared
88  // (a temporary expression).
89  if (op_val.get_count () == 1)
90  val = op_val.do_non_const_unary_op (m_etype);
91  else
92  {
93  interpreter& interp = tw.get_interpreter ();
94 
95  type_info& ti = interp.get_type_info ();
96 
97  val = ::do_unary_op (ti, m_etype, op_val);
98  }
99  }
100  }
101  }
102 
103  return val;
104  }
105 
106  // Postfix expressions.
107 
110  {
112  = new tree_postfix_expression (m_op ? m_op->dup (scope) : nullptr,
113  line (), column (), m_etype);
114 
115  new_pe->copy_base (*this);
116 
117  return new_pe;
118  }
119 
122  {
123  octave_value val;
124 
125  if (m_op)
126  {
129  {
130  octave_lvalue ref = m_op->lvalue (tw);
131 
132  val = ref.value ();
133 
135  block (tw.get_profiler (), *this);
136 
137  ref.do_unary_op (m_etype);
138  }
139  else
140  {
141  octave_value op_val = m_op->evaluate (tw);
142 
143  if (op_val.is_defined ())
144  {
146  block (tw.get_profiler (), *this);
147 
148  interpreter& interp = tw.get_interpreter ();
149 
150  type_info& ti = interp.get_type_info ();
151 
152  val = ::do_unary_op (ti, m_etype, op_val);
153  }
154  }
155  }
156 
157  return val;
158  }
159 }
type_info & get_type_info(void)
Definition: interpreter.h:253
octave_value value(void) const
Definition: oct-lvalue.cc:215
void do_unary_op(octave_value::unary_op op)
Definition: oct-lvalue.cc:209
interpreter & get_interpreter(void)
Definition: pt-eval.h:370
profiler & get_profiler(void)
Definition: pt-eval.h:374
virtual void copy_base(const tree_expression &e)
Definition: pt-exp.h:131
virtual tree_expression * dup(symbol_scope &scope) const =0
virtual octave_lvalue lvalue(tree_evaluator &)
Definition: pt-exp.cc:43
virtual octave_value evaluate(tree_evaluator &tw, int nargout=1)=0
octave_value evaluate(tree_evaluator &, int nargout=1)
Definition: pt-unop.cc:121
tree_expression * dup(symbol_scope &scope) const
Definition: pt-unop.cc:109
tree_postfix_expression(int l=-1, int c=-1)
Definition: pt-unop.h:134
tree_expression * dup(symbol_scope &scope) const
Definition: pt-unop.cc:48
tree_prefix_expression(int l=-1, int c=-1)
Definition: pt-unop.h:93
octave_value evaluate(tree_evaluator &, int nargout=1)
Definition: pt-unop.cc:60
std::string oper(void) const
Definition: pt-unop.cc:40
tree_expression * m_op
Definition: pt-unop.h:81
octave_value::unary_op m_etype
Definition: pt-unop.h:84
virtual int column(void) const
Definition: pt.h:62
octave_idx_type get_count(void) const
Definition: ov.h:399
@ op_incr
Definition: ov.h:89
@ op_decr
Definition: ov.h:90
bool is_defined(void) const
Definition: ov.h:551
static std::string unary_op_as_string(unary_op)
Definition: ov.cc:122
octave_value & do_non_const_unary_op(unary_op op)
Definition: ov.cc:2701
octave_value do_unary_op(octave::type_info &ti, octave_value::unary_op op, const octave_value &v)
Definition: ov.cc:2635