GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
pt-binop.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 "error.h"
31 #include "interpreter.h"
32 #include "ov.h"
33 #include "profiler.h"
34 #include "pt-binop.h"
35 #include "pt-eval.h"
36 #include "variables.h"
37 
39 
40 // Binary expressions.
41 
42 void
44 {
45  warning_with_id ("Octave:possible-matlab-short-circuit-operator",
46  "Matlab-style short-circuit operation performed for operator %s",
47  op);
48 }
49 
50 std::string
52 {
53  return octave_value::binary_op_as_string (m_etype);
54 }
55 
58 {
60  = new tree_binary_expression (m_lhs ? m_lhs->dup (scope) : nullptr,
61  m_rhs ? m_rhs->dup (scope) : nullptr,
62  line (), column (), m_etype);
63 
64  new_be->copy_base (*this);
65 
66  return new_be;
67 }
68 
71 {
72  if (m_lhs)
73  {
74  // Evaluate with unknown number of output arguments
75  octave_value a = m_lhs->evaluate (tw, -1);
76 
77  if (a.is_defined () && m_rhs)
78  {
79  // Evaluate with unknown number of output arguments
80  octave_value b = m_rhs->evaluate (tw, -1);
81 
82  if (b.is_defined ())
83  {
85  block (tw.get_profiler (), *this);
86 
87  // Note: The profiler does not catch the braindead
88  // short-circuit evaluation code above, but that should be
89  // ok. The evaluation of operands and the operator itself
90  // is entangled and it's not clear where to start/stop
91  // timing the operator to make it reasonable.
92 
93  interpreter& interp = tw.get_interpreter ();
94 
95  type_info& ti = interp.get_type_info ();
96 
97  return binary_op (ti, m_etype, a, b);
98  }
99  }
100  }
101 
102  return octave_value ();
103 }
104 
107 {
110  (m_lhs ? m_lhs->dup (scope) : nullptr,
111  m_rhs ? m_rhs->dup (scope) : nullptr,
112  line (), column (), op_type ());
113 
114  new_be->copy_base (*this);
115 
116  return new_be;
117 }
118 
121  int)
122 {
123  if (m_lhs)
124  {
125  octave_value a = m_lhs->evaluate (tw);
126 
127  if (a.ndims () == 2 && a.rows () == 1 && a.columns () == 1)
128  {
129  bool result = false;
130 
131  bool a_true = a.is_true ();
132 
133  octave_value::binary_op oper_type = op_type ();
134 
135  if (a_true)
136  {
137  if (oper_type == octave_value::op_el_or)
138  {
140  return octave_value (true);
141  }
142  }
143  else
144  {
145  if (oper_type == octave_value::op_el_and)
146  {
148  return octave_value (false);
149  }
150  }
151 
152  if (m_rhs)
153  {
154  octave_value b = m_rhs->evaluate (tw);
155 
156  result = b.is_true ();
157  }
158 
159  return octave_value (result);
160  }
161  else
163  }
164 
165  return octave_value ();
166 }
167 
168 // Boolean expressions.
169 
170 std::string
172 {
173  std::string retval = "<unknown>";
174 
175  switch (m_etype)
176  {
177  case bool_and:
178  retval = "&&";
179  break;
180 
181  case bool_or:
182  retval = "||";
183  break;
184 
185  default:
186  break;
187  }
188 
189  return retval;
190 }
191 
194 {
196  = new tree_boolean_expression (m_lhs ? m_lhs->dup (scope) : nullptr,
197  m_rhs ? m_rhs->dup (scope) : nullptr,
198  line (), column (), m_etype);
199 
200  new_be->copy_base (*this);
201 
202  return new_be;
203 }
204 
207 {
208  octave_value val;
209 
210  bool result = false;
211 
212  // This evaluation is not caught by the profiler, since we can't find
213  // a reasonable place where to time. Note that we don't want to
214  // include evaluation of LHS or RHS into the timing, but this is
215  // entangled together with short-circuit evaluation here.
216 
217  if (m_lhs)
218  {
219  octave_value a = m_lhs->evaluate (tw);
220 
221  bool a_true = a.is_true ();
222 
223  if (a_true)
224  {
225  if (m_etype == tree_boolean_expression::bool_or)
226  return octave_value (true);
227  }
228  else
229  {
230  if (m_etype == tree_boolean_expression::bool_and)
231  return octave_value (false);
232  }
233 
234  if (m_rhs)
235  {
236  octave_value b = m_rhs->evaluate (tw);
237 
238  result = b.is_true ();
239  }
240 
241  val = octave_value (result);
242  }
243 
244  return val;
245 }
246 
247 OCTAVE_END_NAMESPACE(octave)
type_info & get_type_info()
Definition: interpreter.h:293
static std::string binary_op_as_string(binary_op)
Definition: ov.cc:184
bool is_true() const
Definition: ov.h:758
int ndims() const
Definition: ov.h:551
octave_idx_type rows() const
Definition: ov.h:545
bool is_defined() const
Definition: ov.h:592
binary_op
Definition: ov.h:92
@ op_el_or
Definition: ov.h:110
@ op_el_and
Definition: ov.h:109
octave_idx_type columns() const
Definition: ov.h:547
tree_expression * m_lhs
Definition: pt-binop.h:114
void matlab_style_short_circuit_warning(const char *op)
Definition: pt-binop.cc:43
octave_value::binary_op op_type() const
Definition: pt-binop.h:84
tree_expression * m_rhs
Definition: pt-binop.h:115
tree_expression * dup(symbol_scope &scope) const
Definition: pt-binop.cc:57
tree_binary_expression(int l=-1, int c=-1, octave_value::binary_op t=octave_value::unknown_binary_op)
Definition: pt-binop.h:50
std::string oper() const
Definition: pt-binop.cc:51
octave_value evaluate(tree_evaluator &, int nargout=1)
Definition: pt-binop.cc:70
tree_boolean_expression(int l=-1, int c=-1, type t=unknown)
Definition: pt-binop.h:164
tree_expression * dup(symbol_scope &scope) const
Definition: pt-binop.cc:193
std::string oper() const
Definition: pt-binop.cc:171
octave_value evaluate(tree_evaluator &, int nargout=1)
Definition: pt-binop.cc:206
tree_expression * dup(symbol_scope &scope) const
Definition: pt-binop.cc:106
octave_value evaluate(tree_evaluator &, int nargout=1)
Definition: pt-binop.cc:120
tree_braindead_shortcircuit_binary_expression(tree_expression *a, tree_expression *b, int l, int c, octave_value::binary_op t)
Definition: pt-binop.h:131
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 int column() const
Definition: pt.h:58
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void warning_with_id(const char *id, const char *fmt,...)
Definition: error.cc:1078
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
octave_value binary_op(type_info &ti, octave_value::binary_op op, const octave_value &a, const octave_value &b)