GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
pt-const.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1993-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_const_h)
27 #define octave_pt_const_h 1
28 
29 #include "octave-config.h"
30 
31 #include <iosfwd>
32 #include <string>
33 
34 class octave_value_list;
35 
36 #include "error.h"
37 #include "ov.h"
38 #include "pt-bp.h"
39 #include "pt-exp.h"
40 #include "pt-walk.h"
41 
43 
44 class symbol_scope;
45 class tree_evaluator;
46 
48 {
49 public:
50 
51  tree_constant (int l = -1, int c = -1)
52  : tree_expression (l, c), m_value (), m_orig_text ()
53  { }
54 
55  tree_constant (const octave_value& v, int l = -1, int c = -1)
56  : tree_expression (l, c), m_value (v), m_orig_text ()
57  { }
58 
59  tree_constant (const octave_value& v, const std::string& ot,
60  int l = -1, int c = -1)
61  : tree_expression (l, c), m_value (v), m_orig_text (ot)
62  { }
63 
64  OCTAVE_DISABLE_COPY_MOVE (tree_constant)
65 
66  ~tree_constant () = default;
67 
68  // Type. It would be nice to eliminate the need for this.
69 
70  bool is_constant () const { return true; }
71 
72  void maybe_mutate () { m_value.maybe_mutate (); }
73 
74  void print (std::ostream& os, bool pr_as_read_syntax = false,
75  bool pr_orig_txt = true);
76 
77  void print_raw (std::ostream& os, bool pr_as_read_syntax = false,
78  bool pr_orig_txt = true);
79 
80  bool rvalue_ok () const { return true; }
81 
82  octave_value value () { return m_value; }
83 
84  tree_expression * dup (symbol_scope& scope) const;
85 
86  void accept (tree_walker& tw)
87  {
88  tw.visit_constant (*this);
89  }
90 
91  // Store the original text corresponding to this constant for later
92  // pretty printing.
93 
94  void stash_original_text (const std::string& s) { m_orig_text = s; }
95 
96  std::string original_text () const { return m_orig_text; }
97 
98  octave_value evaluate (tree_evaluator&, int nargout = 1)
99  {
100  if (nargout > 1)
101  error ("invalid number of output arguments for constant expression");
102 
103  return value ();
104  }
105 
107  {
108  return ovl (evaluate (tw, nargout));
109  }
110 
111 private:
112 
113  // The actual value that this constant refers to.
114  octave_value m_value;
115 
116  // The original text form of this constant.
117  std::string m_orig_text;
118 };
119 
120 OCTAVE_END_NAMESPACE(octave)
121 
122 #endif
void maybe_mutate()
bool rvalue_ok() const
Definition: pt-const.h:80
bool is_constant() const
Definition: pt-const.h:70
tree_constant(const octave_value &v, int l=-1, int c=-1)
Definition: pt-const.h:55
void stash_original_text(const std::string &s)
Definition: pt-const.h:94
void print(std::ostream &os, bool pr_as_read_syntax=false, bool pr_orig_txt=true)
Definition: pt-const.cc:44
tree_expression * dup(symbol_scope &scope) const
Definition: pt-const.cc:64
tree_constant(int l=-1, int c=-1)
Definition: pt-const.h:51
std::string original_text() const
Definition: pt-const.h:96
tree_constant(const octave_value &v, const std::string &ot, int l=-1, int c=-1)
Definition: pt-const.h:59
void print_raw(std::ostream &os, bool pr_as_read_syntax=false, bool pr_orig_txt=true)
Definition: pt-const.cc:54
octave_value evaluate(tree_evaluator &, int nargout=1)
Definition: pt-const.h:98
octave_value_list evaluate_n(tree_evaluator &tw, int nargout=1)
Definition: pt-const.h:106
void accept(tree_walker &tw)
Definition: pt-const.h:86
~tree_constant()=default
void maybe_mutate()
Definition: pt-const.h:72
octave_value value()
Definition: pt-const.h:82
virtual void visit_constant(tree_constant &)
Definition: pt-walk.cc:503
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void() error(const char *fmt,...)
Definition: error.cc:988
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:219