GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
pt-binop.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2025 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
42void
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
50std::string
55
58{
60 = new tree_binary_expression (m_lhs ? m_lhs->dup (scope) : nullptr,
62 m_rhs ? m_rhs->dup (scope) : nullptr,
63 m_etype);
64
65 new_be->copy_base (*this);
66
67 return new_be;
68}
69
72{
73 if (m_lhs)
74 {
75 // Evaluate with unknown number of output arguments
76 octave_value a = m_lhs->evaluate (tw, -1);
77
78 if (a.is_defined () && m_rhs)
79 {
80 // Evaluate with unknown number of output arguments
81 octave_value b = m_rhs->evaluate (tw, -1);
82
83 if (b.is_defined ())
84 {
86 block (tw.get_profiler (), *this);
87
88 // Note: The profiler does not catch the braindead
89 // short-circuit evaluation code above, but that should be
90 // ok. The evaluation of operands and the operator itself
91 // is entangled and it's not clear where to start/stop
92 // timing the operator to make it reasonable.
93
94 interpreter& interp = tw.get_interpreter ();
95
96 type_info& ti = interp.get_type_info ();
97
98 return binary_op (ti, m_etype, a, b);
99 }
100 }
101 }
102
103 return octave_value ();
104}
105
108{
111 (m_lhs ? m_lhs->dup (scope) : nullptr,
112 m_op_tok,
113 m_rhs ? m_rhs->dup (scope) : nullptr,
114 op_type ());
115
116 new_be->copy_base (*this);
117
118 return new_be;
119}
120
123{
124 if (m_lhs)
125 {
126 octave_value a = m_lhs->evaluate (tw);
127
128 if (a.ndims () == 2 && a.rows () == 1 && a.columns () == 1)
129 {
130 bool result = false;
131
132 bool a_true = a.is_true ();
133
134 octave_value::binary_op oper_type = op_type ();
135
136 if (a_true)
137 {
138 if (oper_type == octave_value::op_el_or)
139 {
141 return octave_value (true);
142 }
143 }
144 else
145 {
146 if (oper_type == octave_value::op_el_and)
147 {
149 return octave_value (false);
150 }
151 }
152
153 if (m_rhs)
154 {
155 octave_value b = m_rhs->evaluate (tw);
156
157 result = b.is_true ();
158 }
159
160 return octave_value (result);
161 }
162 else
164 }
165
166 return octave_value ();
167}
168
169// Boolean expressions.
170
171std::string
173{
174 std::string retval = "<unknown>";
175
176 switch (m_etype)
177 {
178 case bool_and:
179 retval = "&&";
180 break;
181
182 case bool_or:
183 retval = "||";
184 break;
185
186 default:
187 break;
188 }
189
190 return retval;
191}
192
195{
197 = new tree_boolean_expression (m_lhs ? m_lhs->dup (scope) : nullptr,
198 m_op_tok,
199 m_rhs ? m_rhs->dup (scope) : nullptr,
200 m_etype);
201
202 new_be->copy_base (*this);
203
204 return new_be;
205}
206
209{
210 octave_value val;
211
212 bool result = false;
213
214 // This evaluation is not caught by the profiler, since we can't find
215 // a reasonable place where to time. Note that we don't want to
216 // include evaluation of LHS or RHS into the timing, but this is
217 // entangled together with short-circuit evaluation here.
218
219 if (m_lhs)
220 {
221 octave_value a = m_lhs->evaluate (tw);
222
223 bool a_true = a.is_true ();
224
225 if (a_true)
226 {
228 return octave_value (true);
229 }
230 else
231 {
233 return octave_value (false);
234 }
235
236 if (m_rhs)
237 {
238 octave_value b = m_rhs->evaluate (tw);
239
240 result = b.is_true ();
241 }
242
243 val = octave_value (result);
244 }
245
246 return val;
247}
248
249OCTAVE_END_NAMESPACE(octave)
type_info & get_type_info()
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:113
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:82
tree_expression * m_rhs
Definition pt-binop.h:117
tree_expression * dup(symbol_scope &scope) const
Definition pt-binop.cc:57
std::string oper() const
Definition pt-binop.cc:51
octave_value evaluate(tree_evaluator &, int nargout=1)
Definition pt-binop.cc:71
tree_expression * dup(symbol_scope &scope) const
Definition pt-binop.cc:194
std::string oper() const
Definition pt-binop.cc:172
octave_value evaluate(tree_evaluator &, int nargout=1)
Definition pt-binop.cc:208
tree_expression * dup(symbol_scope &scope) const
Definition pt-binop.cc:107
octave_value evaluate(tree_evaluator &, int nargout=1)
Definition pt-binop.cc:122
interpreter & get_interpreter()
Definition pt-eval.h:422
profiler & get_profiler()
Definition pt-eval.h:426
virtual octave_value evaluate(tree_evaluator &tw, int nargout=1)=0
virtual void copy_base(const tree_expression &e)
Definition pt-exp.h:137
virtual tree_expression * dup(symbol_scope &scope) const =0
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void warning_with_id(const char *id, const char *fmt,...)
Definition error.cc:1093
octave_value binary_op(type_info &ti, octave_value::binary_op op, const octave_value &a, const octave_value &b)