GNU Octave 7.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-2022 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 "parse.h"
34#include "pt-const.h"
35#include "pt-id.h"
36#include "symscope.h"
37#include "utils.h"
38#include "variables.h"
39
40namespace octave
41{
42 // Symbols from the symbol table.
43
44 void
46 {
47 int l = line ();
48 int c = column ();
49
50 std::string msg = "'" + name () + "' undefined";
51
52 if (l > 0)
53 {
54 msg += " near line " + std::to_string (l);
55
56 if (c > 0)
57 msg += ", column " + std::to_string (c);
58 }
59
60 std::string missing_msg = maybe_missing_function_hook (name ());
61
62 if (! missing_msg.empty ())
63 msg += "\n\n" + missing_msg;
64
65 error_with_id ("Octave:undefined-function", "%s", msg.c_str ());
66 }
67
70 {
73
75 }
76
79 {
80 // The new tree_identifier object contains a symbol_record
81 // entry from the duplicated scope.
82
83 symbol_record new_sym = scope.find_symbol (name ());
84
85 tree_identifier *new_id
86 = new tree_identifier (new_sym, line (), column ());
87
88 new_id->copy_base (*this);
89
90 return new_id;
91 }
92
95 {
96 octave_value_list retval;
97
98 octave_value val = tw.varval (m_sym);
99
100 if (val.is_undefined ())
101 {
102 interpreter& interp = tw.get_interpreter ();
103
104 symbol_table& symtab = interp.get_symbol_table ();
105
106 val = symtab.find_function (m_sym.name ());
107 }
108
109 if (val.is_defined ())
110 {
111 // GAGME -- this would be cleaner if we required
112 // parens to indicate function calls.
113 //
114 // If this identifier refers to a function, we need to know
115 // whether it is indexed so that we can do the same thing
116 // for 'f' and 'f()'. If the index is present and the function
117 // object declares it can handle it, return the function object
118 // and let tree_index_expression::rvalue handle indexing.
119 // Otherwise, arrange to call the function here, so that we don't
120 // return the function definition as a value.
121
122 octave_function *fcn = nullptr;
123
124 if (val.is_function ())
125 fcn = val.function_value (true);
126
127 if (fcn && ! (is_postfix_indexed ()
129 {
130 retval = fcn->call (tw, nargout);
131 }
132 else
133 {
134 if (print_result () && nargout == 0
136 {
137 octave_value_list args = ovl (val);
139 feval ("display", args);
140 }
141
142 retval = ovl (val);
143 }
144 }
145 else if (m_sym.is_added_static ())
147 else
149
150 return retval;
151 }
152
155 {
157
158 retval.mark_black_hole ();
159
160 return retval;
161 }
162}
symbol_table & get_symbol_table(void)
Definition: interpreter.h:296
void mark_black_hole(void)
Definition: oct-lvalue.h:57
bool is_added_static(void) const
Definition: symrec.h:213
std::string name(void) const
Definition: symrec.h:207
symbol_record find_symbol(const std::string &name)
Definition: symscope.h:474
octave_lvalue lvalue(tree_evaluator &tw)
Definition: pt-id.cc:154
virtual void copy_base(const tree_expression &e)
Definition: pt-exp.h:133
char postfix_index(void) const
Definition: pt-exp.h:96
bool is_postfix_indexed(void) const
Definition: pt-exp.h:94
bool print_result(void) const
Definition: pt-exp.h:101
tree_identifier(int l=-1, int c=-1)
Definition: pt-id.h:56
tree_identifier * dup(symbol_scope &scope) const
Definition: pt-id.cc:78
octave_value_list evaluate_n(tree_evaluator &tw, int nargout=1)
Definition: pt-id.cc:94
symbol_record m_sym
Definition: pt-id.h:115
void eval_undefined_error(void)
Definition: pt-id.cc:45
octave_lvalue lvalue(tree_evaluator &tw)
Definition: pt-id.cc:69
std::string name(void) const
Definition: pt-id.h:73
void static_workspace_error(void)
Definition: pt-id.h:88
virtual int column(void) const
Definition: pt.h:62
virtual int line(void) const
Definition: pt.h:60
virtual bool accepts_postfix_index(char type) const
Definition: ov-fcn.h:226
virtual octave_value_list call(octave::tree_evaluator &tw, int nargout=0, const octave_value_list &args=octave_value_list())
Definition: ov-fcn.cc:50
void stash_name_tags(const string_vector &nm)
Definition: ovl.h:165
bool is_function(void) const
Definition: ov.h:822
OCTINTERP_API octave_function * function_value(bool silent=false) const
bool is_defined(void) const
Definition: ov.h:637
bool is_undefined(void) const
Definition: ov.h:640
octave_value find_function(const std::string &name, const symbol_scope &search_scope=symbol_scope())
Definition: symtab.cc:249
interpreter & get_interpreter(void)
Definition: pt-eval.h:414
bool statement_printing_enabled(void)
Definition: pt-eval.cc:1355
std::shared_ptr< stack_frame > get_current_stack_frame(void) const
Definition: pt-eval.h:437
octave_value varval(const symbol_record &sym) const
Definition: pt-eval.cc:1940
void error_with_id(const char *id, const char *fmt,...)
Definition: error.cc:1025
OCTINTERP_API octave_value_list feval(const char *name, const octave_value_list &args=octave_value_list(), int nargout=0)
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:211
std::string maybe_missing_function_hook(const std::string &name)
Definition: variables.cc:1413