GNU Octave  8.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-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 (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  // No copying!
66 
68 
70 
72  {
73  if (! m_preserve_operands)
74  {
75  delete m_lhs;
76  delete m_rhs;
77  }
78  }
79 
80  void preserve_operands (void) { m_preserve_operands = true; }
81 
82  bool is_binary_expression (void) const { return true; }
83 
84  bool rvalue_ok (void) const { return true; }
85 
86  std::string oper (void) const;
87 
88  octave_value::binary_op op_type (void) const { return m_etype; }
89 
90  tree_expression * lhs (void) { return m_lhs; }
91  tree_expression * rhs (void) { return m_rhs; }
92 
93  void lhs (tree_expression *expr) { m_lhs = expr; }
94  void rhs (tree_expression *expr) { m_rhs = expr; }
95 
96  tree_expression * dup (symbol_scope& scope) const;
97 
98  octave_value evaluate (tree_evaluator&, int nargout = 1);
99 
101  {
102  return ovl (evaluate (tw, nargout));
103  }
104 
105  void accept (tree_walker& tw)
106  {
107  tw.visit_binary_expression (*this);
108  }
109 
110  std::string profiler_name (void) const { return "binary " + oper (); }
111 
112  void matlab_style_short_circuit_warning (const char *op);
113 
114 protected:
115 
116  // The operands for the expression.
119 
120 private:
121 
122  // The type of the expression.
124 
125  // If TRUE, don't delete m_lhs and m_rhs in destructor;
127 };
128 
130  : public tree_binary_expression
131 {
132 public:
133 
135  tree_expression *b,
136  int l, int c,
138  : tree_binary_expression (a, b, l, c, t)
139  { }
140 
141  // No copying!
142 
145 
148 
150 
151  tree_expression * dup (symbol_scope& scope) const;
152 
153  octave_value evaluate (tree_evaluator&, int nargout = 1);
154 
156 };
157 
158 // Boolean expressions.
159 
161 {
162 public:
163 
164  enum type
165  {
168  bool_or
169  };
170 
171  tree_boolean_expression (int l = -1, int c = -1, type t = unknown)
172  : tree_binary_expression (l, c), m_etype (t) { }
173 
175  int l = -1, int c = -1, type t = unknown)
176  : tree_binary_expression (a, b, l, c), m_etype (t) { }
177 
178  // No copying!
179 
181 
183 
184  ~tree_boolean_expression (void) = default;
185 
186  bool is_boolean_expression (void) const { return true; }
187 
188  bool rvalue_ok (void) const { return true; }
189 
190  std::string oper (void) const;
191 
192  type op_type (void) const { return m_etype; }
193 
194  tree_expression * dup (symbol_scope& scope) const;
195 
196  octave_value evaluate (tree_evaluator&, int nargout = 1);
197 
199  {
200  return ovl (evaluate (tw, nargout));
201  }
202 
203  void accept (tree_walker& tw)
204  {
205  tw.visit_boolean_expression (*this);
206  }
207 
208 private:
209 
210  // The type of the expression.
212 };
213 
215 
216 #endif
OCTAVE_END_NAMESPACE(octave)
binary_op
Definition: ov.h:90
@ unknown_binary_op
Definition: ov.h:111
tree_expression * m_lhs
Definition: pt-binop.h:117
void matlab_style_short_circuit_warning(const char *op)
Definition: pt-binop.cc:43
bool rvalue_ok(void) const
Definition: pt-binop.h:84
std::string profiler_name(void) const
Definition: pt-binop.h:110
tree_expression * rhs(void)
Definition: pt-binop.h:91
bool is_binary_expression(void) const
Definition: pt-binop.h:82
tree_expression * lhs(void)
Definition: pt-binop.h:90
tree_binary_expression & operator=(const tree_binary_expression &)=delete
octave_value::binary_op m_etype
Definition: pt-binop.h:123
~tree_binary_expression(void)
Definition: pt-binop.h:71
octave_value_list evaluate_n(tree_evaluator &tw, int nargout=1)
Definition: pt-binop.h:100
tree_expression * m_rhs
Definition: pt-binop.h:118
void rhs(tree_expression *expr)
Definition: pt-binop.h:94
tree_expression * dup(symbol_scope &scope) const
Definition: pt-binop.cc:57
tree_binary_expression(const tree_binary_expression &)=delete
void preserve_operands(void)
Definition: pt-binop.h:80
void accept(tree_walker &tw)
Definition: pt-binop.h:105
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
std::string oper(void) const
Definition: pt-binop.cc:51
void lhs(tree_expression *expr)
Definition: pt-binop.h:93
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
void accept(tree_walker &tw)
Definition: pt-binop.h:203
tree_boolean_expression(int l=-1, int c=-1, type t=unknown)
Definition: pt-binop.h:171
tree_boolean_expression & operator=(const tree_boolean_expression &)=delete
tree_boolean_expression(tree_expression *a, tree_expression *b, int l=-1, int c=-1, type t=unknown)
Definition: pt-binop.h:174
~tree_boolean_expression(void)=default
std::string oper(void) const
Definition: pt-binop.cc:167
octave_value_list evaluate_n(tree_evaluator &tw, int nargout=1)
Definition: pt-binop.h:198
bool rvalue_ok(void) const
Definition: pt-binop.h:188
bool is_boolean_expression(void) const
Definition: pt-binop.h:186
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_boolean_expression(const tree_boolean_expression &)=delete
type op_type(void) const
Definition: pt-binop.h:192
tree_braindead_shortcircuit_binary_expression(const tree_braindead_shortcircuit_binary_expression &)=delete
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 & operator=(const tree_braindead_shortcircuit_binary_expression &)=delete
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
virtual void visit_boolean_expression(tree_boolean_expression &)
Definition: pt-walk.cc:147
virtual void visit_binary_expression(tree_binary_expression &)
Definition: pt-walk.cc:134
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:211