GNU Octave  8.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-2023 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 {
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  octave_value a = m_lhs->evaluate (tw);
75 
76  if (a.is_defined () && m_rhs)
77  {
78  octave_value b = m_rhs->evaluate (tw);
79 
80  if (b.is_defined ())
81  {
83  block (tw.get_profiler (), *this);
84 
85  // Note: The profiler does not catch the braindead
86  // short-circuit evaluation code above, but that should be
87  // ok. The evaluation of operands and the operator itself
88  // is entangled and it's not clear where to start/stop
89  // timing the operator to make it reasonable.
90 
91  interpreter& interp = tw.get_interpreter ();
92 
93  type_info& ti = interp.get_type_info ();
94 
95  return binary_op (ti, m_etype, a, b);
96  }
97  }
98  }
99 
100  return octave_value ();
101 }
102 
105 {
108  (m_lhs ? m_lhs->dup (scope) : nullptr,
109  m_rhs ? m_rhs->dup (scope) : nullptr,
110  line (), column (), op_type ());
111 
112  new_be->copy_base (*this);
113 
114  return new_be;
115 }
116 
119  int)
120 {
121  if (m_lhs)
122  {
123  octave_value a = m_lhs->evaluate (tw);
124 
125  if (a.ndims () == 2 && a.rows () == 1 && a.columns () == 1)
126  {
127  bool result = false;
128 
129  bool a_true = a.is_true ();
130 
131  octave_value::binary_op oper_type = op_type ();
132 
133  if (a_true)
134  {
135  if (oper_type == octave_value::op_el_or)
136  {
138  return octave_value (true);
139  }
140  }
141  else
142  {
143  if (oper_type == octave_value::op_el_and)
144  {
146  return octave_value (false);
147  }
148  }
149 
150  if (m_rhs)
151  {
152  octave_value b = m_rhs->evaluate (tw);
153 
154  result = b.is_true ();
155  }
156 
157  return octave_value (result);
158  }
159  }
160 
161  return octave_value ();
162 }
163 
164 // Boolean expressions.
165 
166 std::string
168 {
169  std::string retval = "<unknown>";
170 
171  switch (m_etype)
172  {
173  case bool_and:
174  retval = "&&";
175  break;
176 
177  case bool_or:
178  retval = "||";
179  break;
180 
181  default:
182  break;
183  }
184 
185  return retval;
186 }
187 
190 {
192  = new tree_boolean_expression (m_lhs ? m_lhs->dup (scope) : nullptr,
193  m_rhs ? m_rhs->dup (scope) : nullptr,
194  line (), column (), m_etype);
195 
196  new_be->copy_base (*this);
197 
198  return new_be;
199 }
200 
203 {
204  octave_value val;
205 
206  bool result = false;
207 
208  // This evaluation is not caught by the profiler, since we can't find
209  // a reasonable place where to time. Note that we don't want to
210  // include evaluation of LHS or RHS into the timing, but this is
211  // entangled together with short-circuit evaluation here.
212 
213  if (m_lhs)
214  {
215  octave_value a = m_lhs->evaluate (tw);
216 
217  bool a_true = a.is_true ();
218 
219  if (a_true)
220  {
222  return octave_value (true);
223  }
224  else
225  {
227  return octave_value (false);
228  }
229 
230  if (m_rhs)
231  {
232  octave_value b = m_rhs->evaluate (tw);
233 
234  result = b.is_true ();
235  }
236 
237  val = octave_value (result);
238  }
239 
240  return val;
241 }
242 
OCTAVE_END_NAMESPACE(octave)
type_info & get_type_info(void)
Definition: interpreter.h:293
static OCTINTERP_API std::string binary_op_as_string(binary_op)
Definition: ov.cc:184
bool is_true(void) const
Definition: ov.h:803
octave_idx_type rows(void) const
Definition: ov.h:590
bool is_defined(void) const
Definition: ov.h:637
octave_idx_type columns(void) const
Definition: ov.h:592
int ndims(void) const
Definition: ov.h:596
binary_op
Definition: ov.h:90
@ op_el_or
Definition: ov.h:108
@ op_el_and
Definition: ov.h:107
tree_expression * m_lhs
Definition: pt-binop.h:117
void matlab_style_short_circuit_warning(const char *op)
Definition: pt-binop.cc:43
octave_value::binary_op m_etype
Definition: pt-binop.h:123
tree_expression * m_rhs
Definition: pt-binop.h:118
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(void) const
Definition: pt-binop.cc:51
octave_value::binary_op op_type(void) const
Definition: pt-binop.h:88
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:171
std::string oper(void) const
Definition: pt-binop.cc:167
tree_expression * dup(symbol_scope &scope) const
Definition: pt-binop.cc:189
octave_value evaluate(tree_evaluator &, int nargout=1)
Definition: pt-binop.cc:202
tree_expression * dup(symbol_scope &scope) const
Definition: pt-binop.cc:104
octave_value evaluate(tree_evaluator &, int nargout=1)
Definition: pt-binop.cc:118
tree_braindead_shortcircuit_binary_expression(tree_expression *a, tree_expression *b, int l, int c, octave_value::binary_op t)
Definition: pt-binop.h:134
profiler & get_profiler(void)
Definition: pt-eval.h:431
interpreter & get_interpreter(void)
Definition: pt-eval.h:427
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:132
virtual int column(void) const
Definition: pt.h:62
virtual int line(void) const
Definition: pt.h:60
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void warning_with_id(const char *id, const char *fmt,...)
Definition: error.cc:1069
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
OCTINTERP_API octave_value binary_op(type_info &ti, octave_value::binary_op op, const octave_value &a, const octave_value &b)