GNU Octave 10.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-2025 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
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 void maybe_mutate () { m_value.maybe_mutate (); }
72
73 void print (std::ostream& os, bool pr_as_read_syntax = false,
74 bool pr_orig_txt = true);
75
76 void print_raw (std::ostream& os, bool pr_as_read_syntax = false,
77 bool pr_orig_txt = true);
78
79 bool rvalue_ok () const { return true; }
80
81 octave_value value () { return m_value; }
82
83 tree_expression * dup (symbol_scope& scope) const;
84
86 {
87 tw.visit_constant (*this);
88 }
89
90 // Store the original text corresponding to this constant for later
91 // pretty printing.
92
93 void stash_original_text (const std::string& s) { m_orig_text = s; }
94
95 std::string original_text () const { return m_orig_text; }
96
98 {
99 if (nargout > 1)
100 error ("invalid number of output arguments for constant expression");
101
102 return value ();
103 }
104
106 {
107 return ovl (evaluate (tw, nargout));
108 }
109
110private:
111
112 // The actual value that this constant refers to.
113 octave_value m_value;
114
115 // The original text form of this constant.
116 std::string m_orig_text;
117
118 token m_token;
119};
120
121OCTAVE_END_NAMESPACE(octave)
122
123#endif
void maybe_mutate()
Definition token.h:42
filepos end_pos() const
Definition token.h:129
filepos beg_pos() const
Definition token.h:128
bool rvalue_ok() const
Definition pt-const.h:79
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
void stash_original_text(const std::string &s)
Definition pt-const.h:93
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
filepos end_pos() const
Definition pt-const.h:69
std::string original_text() const
Definition pt-const.h:95
filepos beg_pos() const
Definition pt-const.h:68
void print_raw(std::ostream &os, bool pr_as_read_syntax=false, bool pr_orig_txt=true)
Definition pt-const.cc:54
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:97
octave_value_list evaluate_n(tree_evaluator &tw, int nargout=1)
Definition pt-const.h:105
void accept(tree_walker &tw)
Definition pt-const.h:85
~tree_constant()=default
void maybe_mutate()
Definition pt-const.h:71
octave_value value()
Definition pt-const.h:81
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:1003
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition ovl.h:217