GNU Octave  9.1.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-2024 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 
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.unary_op (m_etype);
75 
76  val = op_ref.value ();
77  }
78  else
79  {
80  // Evaluate with unknown number of output arguments
81  octave_value op_val = m_op->evaluate (tw, -1);
82 
83  if (op_val.is_defined ())
84  {
86  block (tw.get_profiler (), *this);
87 
88  // Attempt to do the operation in-place if it is unshared
89  // (a temporary expression).
90  if (op_val.get_count () == 1)
91  val = op_val.non_const_unary_op (m_etype);
92  else
93  {
94  interpreter& interp = tw.get_interpreter ();
95 
96  type_info& ti = interp.get_type_info ();
97 
98  val = unary_op (ti, m_etype, op_val);
99  }
100  }
101  }
102  }
103 
104  return val;
105 }
106 
107 // Postfix expressions.
108 
111 {
113  = new tree_postfix_expression (m_op ? m_op->dup (scope) : nullptr,
114  line (), column (), m_etype);
115 
116  new_pe->copy_base (*this);
117 
118  return new_pe;
119 }
120 
123 {
124  octave_value val;
125 
126  if (m_op)
127  {
130  {
131  octave_lvalue ref = m_op->lvalue (tw);
132 
133  val = ref.value ();
134 
136  block (tw.get_profiler (), *this);
137 
138  ref.unary_op (m_etype);
139  }
140  else
141  {
142  // Evaluate with unknown number of output arguments
143  octave_value op_val = m_op->evaluate (tw, -1);
144 
145  if (op_val.is_defined ())
146  {
148  block (tw.get_profiler (), *this);
149 
150  interpreter& interp = tw.get_interpreter ();
151 
152  type_info& ti = interp.get_type_info ();
153 
154  val = unary_op (ti, m_etype, op_val);
155  }
156  }
157  }
158 
159  return val;
160 }
161 
162 OCTAVE_END_NAMESPACE(octave)
type_info & get_type_info()
Definition: interpreter.h:293
octave_value value() const
Definition: oct-lvalue.cc:224
void unary_op(octave_value::unary_op op)
Definition: oct-lvalue.cc:217
@ op_incr
Definition: ov.h:85
@ op_decr
Definition: ov.h:86
bool is_defined() const
Definition: ov.h:592
static std::string unary_op_as_string(unary_op)
Definition: ov.cc:128
octave_idx_type get_count() const
Definition: ov.h:421
octave_value & non_const_unary_op(unary_op op)
interpreter & get_interpreter()
Definition: pt-eval.h:420
profiler & get_profiler()
Definition: pt-eval.h:424
virtual octave_value evaluate(tree_evaluator &tw, int nargout=1)=0
virtual tree_expression * dup(symbol_scope &scope) const =0
virtual void copy_base(const tree_expression &e)
Definition: pt-exp.h:128
virtual octave_lvalue lvalue(tree_evaluator &)
Definition: pt-exp.cc:43
octave_value evaluate(tree_evaluator &, int nargout=1)
Definition: pt-unop.cc:122
tree_expression * dup(symbol_scope &scope) const
Definition: pt-unop.cc:110
tree_postfix_expression(int l=-1, int c=-1)
Definition: pt-unop.h:126
tree_expression * dup(symbol_scope &scope) const
Definition: pt-unop.cc:48
octave_value evaluate(tree_evaluator &, int nargout=1)
Definition: pt-unop.cc:60
tree_prefix_expression(int l=-1, int c=-1)
Definition: pt-unop.h:89
octave_value::unary_op m_etype
Definition: pt-unop.h:80
tree_expression * m_op
Definition: pt-unop.h:77
std::string oper() const
Definition: pt-unop.cc:40
virtual int column() const
Definition: pt.h:58
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
octave_value unary_op(type_info &ti, octave_value::unary_op op, const octave_value &a)