GNU Octave 11.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
pt-id.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-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_id_h)
27#define octave_pt_id_h 1
28
29#include "octave-config.h"
30
31#include <iosfwd>
32#include <string>
33
34class octave_value;
36class octave_function;
37
38#include "comment-list.h"
39#include "oct-lvalue.h"
40#include "pt-bp.h"
41#include "pt-exp.h"
42#include "pt-walk.h"
43#include "symscope.h"
44#include "token.h"
45
47
48class tree_evaluator;
49
50// Symbols from the symbol table.
51
52class OCTINTERP_API tree_identifier : public tree_expression
53{
55
56public:
57
58 tree_identifier (const token& tok) : m_token (tok) { }
59
60 tree_identifier (symbol_scope& scope, const token& tok)
61 : m_sym (scope ? scope.insert (tok.text ()) : symbol_record (tok.text ())),
62 m_token (tok)
63 { }
64
65 OCTAVE_DISABLE_COPY_MOVE (tree_identifier)
66
67 ~tree_identifier () = default;
68
69 bool is_identifier () const { return true; }
70
71 std::string name () const { return m_sym.name (); }
72
73 filepos beg_pos () const { return m_token.beg_pos (); }
74 filepos end_pos () const { return m_token.end_pos (); }
75
76 comment_list leading_comments () const { return m_token.leading_comments (); }
77 comment_list trailing_comments () const { return m_token.trailing_comments (); }
78
79 virtual bool is_black_hole () const { return false; }
80
81 void mark_as_formal_parameter () { m_sym.mark_formal (); }
82
83 // We really need to know whether this symbol refers to a variable
84 // or a function, but we may not know that yet.
85
86 bool lvalue_ok () const { return true; }
87
89
90 void eval_undefined_error ();
91
93 {
94 error (R"(can not add variable "%s" to a static workspace)",
95 name ().c_str ());
96 }
97
98 tree_identifier * dup (symbol_scope& scope) const;
99
100 octave_value evaluate (tree_evaluator& tw, int nargout = 1)
101 {
102 octave_value_list retval = evaluate_n (tw, nargout);
103
104 return retval.length () > 0 ? retval(0) : octave_value ();
105 }
106
107 octave_value_list evaluate_n (tree_evaluator& tw, int nargout = 1);
108
110 {
111 tw.visit_identifier (*this);
112 }
113
114 symbol_record symbol () const { return m_sym; }
115
116 token get_set_token () const { return m_get_set_tok; }
117
118 token dot_token () const { return m_dot_tok; }
119
120 token identifier_token () const { return m_get_set_tok; }
121
122 tree_identifier * mark_get_set (const token& get_set_tok, const token& dot_tok)
123 {
124 m_get_set_tok = get_set_tok;
125 m_dot_tok = dot_tok;
126
127 return this;
128 }
129
130protected:
131
133 : m_sym (sym), m_token (tok)
134 { }
135
136 //--------
137
138 // The symbol record that this identifier references.
140
141 // These will be defined for get.ID or set.ID function names.
144
145 // The IDENT token from the lexer.
147};
148
149class OCTINTERP_API tree_black_hole : public tree_identifier
150{
151public:
152
156
157 OCTAVE_DISABLE_COPY_MOVE (tree_black_hole)
158
159 ~tree_black_hole () = default;
160
161 std::string name () const { return "~"; }
162
163 bool is_black_hole () const { return true; }
164
165 token tilde_token () const { return identifier_token (); }
166
168 {
169 return new tree_black_hole (m_token);
170 }
171
173};
174
175OCTAVE_END_NAMESPACE(octave)
176
177#endif
octave_idx_type length() const
Definition ovl.h:111
Definition token.h:42
~tree_black_hole()=default
tree_black_hole * dup(symbol_scope &) const
Definition pt-id.h:167
std::string name() const
Definition pt-id.h:161
token tilde_token() const
Definition pt-id.h:165
bool is_black_hole() const
Definition pt-id.h:163
tree_black_hole(const token &token)
Definition pt-id.h:153
virtual tree_expression * dup(symbol_scope &scope) const =0
virtual octave_lvalue lvalue(tree_evaluator &)
Definition pt-exp.cc:43
virtual octave_value_list evaluate_n(tree_evaluator &tw, int nargout=1)=0
token m_token
Definition pt-id.h:146
void mark_as_formal_parameter()
Definition pt-id.h:81
tree_identifier(const token &tok)
Definition pt-id.h:58
filepos beg_pos() const
Definition pt-id.h:73
tree_identifier * mark_get_set(const token &get_set_tok, const token &dot_tok)
Definition pt-id.h:122
token dot_token() const
Definition pt-id.h:118
virtual bool is_black_hole() const
Definition pt-id.h:79
symbol_record symbol() const
Definition pt-id.h:114
std::string name() const
Definition pt-id.h:71
tree_identifier(symbol_record &sym, const token &tok)
Definition pt-id.h:132
bool lvalue_ok() const
Definition pt-id.h:86
filepos end_pos() const
Definition pt-id.h:74
comment_list leading_comments() const
Definition pt-id.h:76
tree_identifier(symbol_scope &scope, const token &tok)
Definition pt-id.h:60
~tree_identifier()=default
bool is_identifier() const
Definition pt-id.h:69
comment_list trailing_comments() const
Definition pt-id.h:77
token get_set_token() const
Definition pt-id.h:116
void static_workspace_error()
Definition pt-id.h:92
token identifier_token() const
Definition pt-id.h:120
token m_get_set_tok
Definition pt-id.h:142
void accept(tree_walker &tw)
Definition pt-id.h:109
symbol_record m_sym
Definition pt-id.h:139
octave_value evaluate(tree_evaluator &tw, int nargout=1)
Definition pt-id.h:100
token m_dot_tok
Definition pt-id.h:143
virtual void visit_identifier(tree_identifier &)
Definition pt-walk.cc:316
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void error(const char *fmt,...)
Definition error.cc:1008