GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
pt-id.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-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 (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include "error.h"
31 #include "interpreter-private.h"
32 #include "oct-lvalue.h"
33 #include "pt-const.h"
34 #include "pt-id.h"
35 #include "symscope.h"
36 #include "utils.h"
37 #include "variables.h"
38 
40 
41 // Symbols from the symbol table.
42 
43 void
45 {
46  int l = line ();
47  int c = column ();
48 
49  std::string msg = "'" + name () + "' undefined";
50 
51  if (l > 0)
52  {
53  msg += " near line " + std::to_string (l);
54 
55  if (c > 0)
56  msg += ", column " + std::to_string (c);
57  }
58 
59  std::string missing_msg = maybe_missing_function_hook (name ());
60 
61  if (! missing_msg.empty ())
62  msg += "\n\n" + missing_msg;
63 
64  error_with_id ("Octave:undefined-function", "%s", msg.c_str ());
65 }
66 
69 {
70  if (m_sym.is_added_static ())
72 
74 }
75 
78 {
79  // The new tree_identifier object contains a symbol_record
80  // entry from the duplicated scope.
81 
82  symbol_record new_sym = scope.find_symbol (name ());
83 
84  tree_identifier *new_id
85  = new tree_identifier (new_sym, line (), column ());
86 
87  new_id->copy_base (*this);
88 
89  return new_id;
90 }
91 
94 {
95  octave_value_list retval;
96 
97  octave_value val = tw.varval (m_sym);
98 
99  if (val.is_undefined ())
100  {
101  interpreter& interp = tw.get_interpreter ();
102 
103  symbol_table& symtab = interp.get_symbol_table ();
104 
105  val = symtab.find_function (m_sym.name ());
106  }
107 
108  if (val.is_defined ())
109  {
110  // GAGME -- this would be cleaner if we required
111  // parens to indicate function calls.
112  //
113  // If this identifier refers to a function, we need to know
114  // whether it is indexed so that we can do the same thing
115  // for 'f' and 'f()'. If the index is present and the function
116  // object declares it can handle it, return the function object
117  // and let tree_index_expression::rvalue handle indexing.
118  // Otherwise, arrange to call the function here, so that we don't
119  // return the function definition as a value.
120 
121  octave_function *fcn = nullptr;
122 
123  if (val.is_function ())
124  fcn = val.function_value (true);
125 
126  if (fcn && ! (is_postfix_indexed ()
127  && fcn->accepts_postfix_index (postfix_index ())))
128  {
129  retval = fcn->call (tw, nargout);
130  }
131  else
132  {
133  if (print_result () && nargout == 0
135  {
136  octave_value_list args = ovl (val);
137  args.stash_name_tags (string_vector (name ()));
138 
139  interpreter& interp = tw.get_interpreter ();
140 
141  interp.feval ("display", args);
142  }
143 
144  retval = ovl (val);
145  }
146  }
147  else if (m_sym.is_added_static ())
149  else
151 
152  return retval;
153 }
154 
157 {
159 
160  retval.mark_black_hole ();
161 
162  return retval;
163 }
164 
165 OCTAVE_END_NAMESPACE(octave)
symbol_table & get_symbol_table()
Definition: interpreter.h:298
octave_value_list feval(const char *name, const octave_value_list &args=octave_value_list(), int nargout=0)
Evaluate an Octave function (built-in or interpreted) and return the list of result values.
virtual bool accepts_postfix_index(char type) const
Definition: ov-fcn.h:225
virtual octave_value_list call(octave::tree_evaluator &tw, int nargout=0, const octave_value_list &args=octave_value_list())
Definition: ov-fcn.cc:56
void mark_black_hole()
Definition: oct-lvalue.h:53
void stash_name_tags(const string_vector &nm)
Definition: ovl.h:165
bool is_undefined() const
Definition: ov.h:595
bool is_defined() const
Definition: ov.h:592
bool is_function() const
Definition: ov.h:777
octave_function * function_value(bool silent=false) const
std::string name() const
Definition: symrec.h:207
bool is_added_static() const
Definition: symrec.h:213
symbol_record find_symbol(const std::string &name)
Definition: symscope.h:489
octave_value find_function(const std::string &name, const symbol_scope &search_scope=symbol_scope::invalid())
Definition: symtab.cc:254
octave_lvalue lvalue(tree_evaluator &tw)
Definition: pt-id.cc:156
interpreter & get_interpreter()
Definition: pt-eval.h:420
std::shared_ptr< stack_frame > get_current_stack_frame() const
Definition: pt-eval.h:443
octave_value varval(const symbol_record &sym) const
Definition: pt-eval.cc:1936
bool statement_printing_enabled()
Definition: pt-eval.cc:1371
char postfix_index() const
Definition: pt-exp.h:93
bool is_postfix_indexed() const
Definition: pt-exp.h:90
bool print_result() const
Definition: pt-exp.h:98
virtual void copy_base(const tree_expression &e)
Definition: pt-exp.h:128
tree_identifier(int l=-1, int c=-1)
Definition: pt-id.h:56
std::string name() const
Definition: pt-id.h:69
octave_lvalue lvalue(tree_evaluator &tw)
Definition: pt-id.cc:68
octave_value_list evaluate_n(tree_evaluator &tw, int nargout=1)
Definition: pt-id.cc:93
tree_identifier * dup(symbol_scope &scope) const
Definition: pt-id.cc:77
void static_workspace_error()
Definition: pt-id.h:84
symbol_record m_sym
Definition: pt-id.h:111
void eval_undefined_error()
Definition: pt-id.cc:44
virtual int line() const
Definition: pt.h:56
virtual int column() const
Definition: pt.h:58
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void error_with_id(const char *id, const char *fmt,...)
Definition: error.cc:1033
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:219
std::string maybe_missing_function_hook(const std::string &name)