GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
pt-binop.h
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 (octave_pt_binop_h)
27 #define octave_pt_binop_h 1
28 
29 #include "octave-config.h"
30 
31 #include <string>
32 
33 class octave_value;
34 class octave_value_list;
35 
36 #include "ov.h"
37 #include "pt-exp.h"
38 #include "pt-walk.h"
39 
41 
42 class symbol_scope;
43 
44 // Binary expressions.
45 
47 {
48 public:
49 
50  tree_binary_expression (int l = -1, int c = -1,
53  : tree_expression (l, c), m_lhs (nullptr), m_rhs (nullptr), m_etype (t),
54  m_preserve_operands (false)
55  { }
56 
58  int l = -1, int c = -1,
61  : tree_expression (l, c), m_lhs (a), m_rhs (b), m_etype (t),
62  m_preserve_operands (false)
63  { }
64 
65  OCTAVE_DISABLE_COPY_MOVE (tree_binary_expression)
66 
68  {
69  if (! m_preserve_operands)
70  {
71  delete m_lhs;
72  delete m_rhs;
73  }
74  }
75 
76  void preserve_operands () { m_preserve_operands = true; }
77 
78  bool is_binary_expression () const { return true; }
79 
80  bool rvalue_ok () const { return true; }
81 
82  std::string oper () const;
83 
84  octave_value::binary_op op_type () const { return m_etype; }
85 
86  tree_expression * lhs () { return m_lhs; }
87  tree_expression * rhs () { return m_rhs; }
88 
89  void lhs (tree_expression *expr) { m_lhs = expr; }
90  void rhs (tree_expression *expr) { m_rhs = expr; }
91 
92  tree_expression * dup (symbol_scope& scope) const;
93 
94  octave_value evaluate (tree_evaluator&, int nargout = 1);
95 
97  {
98  return ovl (evaluate (tw, nargout));
99  }
100 
101  void accept (tree_walker& tw)
102  {
103  tw.visit_binary_expression (*this);
104  }
105 
106  std::string profiler_name () const { return "binary " + oper (); }
107 
108  void matlab_style_short_circuit_warning (const char *op);
109 
110  virtual bool is_braindead () const { return false; }
111 protected:
112 
113  // The operands for the expression.
116 
117 private:
118 
119  // The type of the expression.
120  octave_value::binary_op m_etype;
121 
122  // If TRUE, don't delete m_lhs and m_rhs in destructor;
123  bool m_preserve_operands;
124 };
125 
127  : public tree_binary_expression
128 {
129 public:
130 
132  tree_expression *b,
133  int l, int c,
135  : tree_binary_expression (a, b, l, c, t)
136  { }
137 
138  OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (tree_braindead_shortcircuit_binary_expression)
139 
141 
142  tree_expression * dup (symbol_scope& scope) const;
143 
144  octave_value evaluate (tree_evaluator&, int nargout = 1);
145 
147 
148  bool is_braindead () const { return true; }
149 };
150 
151 // Boolean expressions.
152 
154 {
155 public:
156 
157  enum type
158  {
161  bool_or
162  };
163 
164  tree_boolean_expression (int l = -1, int c = -1, type t = unknown)
165  : tree_binary_expression (l, c), m_etype (t) { }
166 
168  int l = -1, int c = -1, type t = unknown)
169  : tree_binary_expression (a, b, l, c), m_etype (t) { }
170 
171  OCTAVE_DISABLE_COPY_MOVE (tree_boolean_expression)
172 
174 
175  bool is_boolean_expression () const { return true; }
176 
177  bool rvalue_ok () const { return true; }
178 
179  std::string oper () const;
180 
181  type op_type () const { return m_etype; }
182 
183  tree_expression * dup (symbol_scope& scope) const;
184 
185  octave_value evaluate (tree_evaluator&, int nargout = 1);
186 
188  {
189  return ovl (evaluate (tw, nargout));
190  }
191 
192  void accept (tree_walker& tw)
193  {
194  tw.visit_boolean_expression (*this);
195  }
196 
197 private:
198 
199  // The type of the expression.
200  type m_etype;
201 };
202 
203 OCTAVE_END_NAMESPACE(octave)
204 
205 #endif
binary_op
Definition: ov.h:92
@ unknown_binary_op
Definition: ov.h:113
tree_expression * m_lhs
Definition: pt-binop.h:114
void matlab_style_short_circuit_warning(const char *op)
Definition: pt-binop.cc:43
tree_expression * lhs()
Definition: pt-binop.h:86
octave_value::binary_op op_type() const
Definition: pt-binop.h:84
tree_expression * rhs()
Definition: pt-binop.h:87
octave_value_list evaluate_n(tree_evaluator &tw, int nargout=1)
Definition: pt-binop.h:96
bool is_binary_expression() const
Definition: pt-binop.h:78
tree_expression * m_rhs
Definition: pt-binop.h:115
void rhs(tree_expression *expr)
Definition: pt-binop.h:90
tree_expression * dup(symbol_scope &scope) const
Definition: pt-binop.cc:57
void accept(tree_walker &tw)
Definition: pt-binop.h:101
tree_binary_expression(int l=-1, int c=-1, octave_value::binary_op t=octave_value::unknown_binary_op)
Definition: pt-binop.h:50
tree_binary_expression(tree_expression *a, tree_expression *b, int l=-1, int c=-1, octave_value::binary_op t=octave_value::unknown_binary_op)
Definition: pt-binop.h:57
virtual bool is_braindead() const
Definition: pt-binop.h:110
void lhs(tree_expression *expr)
Definition: pt-binop.h:89
std::string oper() const
Definition: pt-binop.cc:51
void preserve_operands()
Definition: pt-binop.h:76
bool rvalue_ok() const
Definition: pt-binop.h:80
std::string profiler_name() const
Definition: pt-binop.h:106
octave_value evaluate(tree_evaluator &, int nargout=1)
Definition: pt-binop.cc:70
void accept(tree_walker &tw)
Definition: pt-binop.h:192
tree_boolean_expression(int l=-1, int c=-1, type t=unknown)
Definition: pt-binop.h:164
bool rvalue_ok() const
Definition: pt-binop.h:177
tree_boolean_expression(tree_expression *a, tree_expression *b, int l=-1, int c=-1, type t=unknown)
Definition: pt-binop.h:167
octave_value_list evaluate_n(tree_evaluator &tw, int nargout=1)
Definition: pt-binop.h:187
~tree_boolean_expression()=default
type op_type() const
Definition: pt-binop.h:181
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
bool is_boolean_expression() const
Definition: pt-binop.h:175
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
virtual void visit_boolean_expression(tree_boolean_expression &)
Definition: pt-walk.cc:156
virtual void visit_binary_expression(tree_binary_expression &)
Definition: pt-walk.cc:142
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:219