GNU Octave  6.2.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-2021 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 
42 namespace octave
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  // No copying!
65 
66  tree_constant (const tree_constant&) = delete;
67 
69 
70  ~tree_constant (void) = default;
71 
72  // Type. It would be nice to eliminate the need for this.
73 
74  bool is_constant (void) const { return true; }
75 
76  void maybe_mutate (void) { m_value.maybe_mutate (); }
77 
78  void print (std::ostream& os, bool pr_as_read_syntax = false,
79  bool pr_orig_txt = true);
80 
81  void print_raw (std::ostream& os, bool pr_as_read_syntax = false,
82  bool pr_orig_txt = true);
83 
84  bool rvalue_ok (void) const { return true; }
85 
86  octave_value value (void) { return m_value; }
87 
88  tree_expression * dup (symbol_scope& scope) const;
89 
90  void accept (tree_walker& tw)
91  {
92  tw.visit_constant (*this);
93  }
94 
95  // Store the original text corresponding to this constant for later
96  // pretty printing.
97 
98  void stash_original_text (const std::string& s) { m_orig_text = s; }
99 
100  std::string original_text (void) const { return m_orig_text; }
101 
103  {
104  if (nargout > 1)
105  error ("invalid number of output arguments for constant expression");
106 
107  return value ();
108  }
109 
111  {
112  return ovl (evaluate (tw, nargout));
113  }
114 
115  private:
116 
117  // The actual value that this constant refers to.
119 
120  // The original text form of this constant.
121  std::string m_orig_text;
122  };
123 }
124 
125 #endif
bool is_constant(void) const
Definition: pt-const.h:74
std::string m_orig_text
Definition: pt-const.h:121
octave_value value(void)
Definition: pt-const.h:86
octave_value m_value
Definition: pt-const.h:118
tree_constant(const octave_value &v, int l=-1, int c=-1)
Definition: pt-const.h:55
tree_constant(const octave_value &v, const std::string &ot, int l=-1, int c=-1)
Definition: pt-const.h:59
void maybe_mutate(void)
Definition: pt-const.h:76
~tree_constant(void)=default
void stash_original_text(const std::string &s)
Definition: pt-const.h:98
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
octave_value evaluate(tree_evaluator &, int nargout=1)
Definition: pt-const.h:102
tree_constant(const tree_constant &)=delete
tree_constant(int l=-1, int c=-1)
Definition: pt-const.h:51
octave_value_list evaluate_n(tree_evaluator &tw, int nargout=1)
Definition: pt-const.h:110
std::string original_text(void) const
Definition: pt-const.h:100
tree_constant & operator=(const tree_constant &)=delete
void print_raw(std::ostream &os, bool pr_as_read_syntax=false, bool pr_orig_txt=true)
Definition: pt-const.cc:54
void accept(tree_walker &tw)
Definition: pt-const.h:90
bool rvalue_ok(void) const
Definition: pt-const.h:84
virtual void visit_constant(tree_constant &)
Definition: pt-walk.cc:378
void maybe_mutate(void)
void error(const char *fmt,...)
Definition: error.cc:968
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:211