GNU Octave 11.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
pt-const.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1993-2026 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
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#include "token.h"
42
44
45class symbol_scope;
46class tree_evaluator;
47
48class OCTINTERP_API tree_constant : public tree_expression
49{
50public:
51
52 tree_constant (const octave_value& v, const token& tok)
53 : m_value (v), m_token (tok)
54 { }
55
56 tree_constant (const octave_value& v, const std::string& ot, const token& tok)
57 : m_value (v), m_orig_text (ot), m_token (tok)
58 { }
59
60 OCTAVE_DISABLE_COPY_MOVE (tree_constant)
61
62 ~tree_constant () = default;
63
64 // Type. It would be nice to eliminate the need for this.
65
66 bool is_constant () const { return true; }
67
68 filepos beg_pos () const { return m_token.beg_pos (); }
69 filepos end_pos () const { return m_token.end_pos (); }
70
71 comment_list leading_comments () const { return m_token.leading_comments (); }
72 comment_list trailing_comments () const { return m_token.trailing_comments (); }
73
74 void maybe_mutate () { m_value.maybe_mutate (); }
75
76 void print (std::ostream& os, bool pr_as_read_syntax = false,
77 bool pr_orig_txt = true);
78
79 void print_raw (std::ostream& os, bool pr_as_read_syntax = false,
80 bool pr_orig_txt = true);
81
82 bool rvalue_ok () const { return true; }
83
84 octave_value value () { return m_value; }
85
86 token const_token () const { return m_token; }
87
88 tree_expression * dup (symbol_scope& scope) const;
89
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 () 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
115private:
116
117 // The actual value that this constant refers to.
118 octave_value m_value;
119
120 // The original text form of this constant.
121 std::string m_orig_text;
122
123 token m_token;
124};
125
126OCTAVE_END_NAMESPACE(octave)
127
128#endif
Definition token.h:42
bool rvalue_ok() const
Definition pt-const.h:82
bool is_constant() const
Definition pt-const.h:66
tree_constant(const octave_value &v, const std::string &ot, const token &tok)
Definition pt-const.h:56
comment_list leading_comments() const
Definition pt-const.h:71
void stash_original_text(const std::string &s)
Definition pt-const.h:98
filepos end_pos() const
Definition pt-const.h:69
std::string original_text() const
Definition pt-const.h:100
filepos beg_pos() const
Definition pt-const.h:68
comment_list trailing_comments() const
Definition pt-const.h:72
tree_constant(const octave_value &v, const token &tok)
Definition pt-const.h:52
octave_value evaluate(tree_evaluator &, int nargout=1)
Definition pt-const.h:102
octave_value_list evaluate_n(tree_evaluator &tw, int nargout=1)
Definition pt-const.h:110
void accept(tree_walker &tw)
Definition pt-const.h:90
~tree_constant()=default
void maybe_mutate()
Definition pt-const.h:74
octave_value value()
Definition pt-const.h:84
token const_token() const
Definition pt-const.h:86
virtual tree_expression * dup(symbol_scope &scope) const =0
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:1008
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition ovl.h:217