GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
oct-lvalue.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 "errwarn.h"
32 #include "interpreter-private.h"
33 #include "interpreter.h"
34 #include "ovl.h"
35 #include "oct-lvalue.h"
36 #include "ov.h"
37 
39 
40 bool octave_lvalue::is_defined () const
41 {
42  return ! is_black_hole () && m_frame->is_defined (m_sym);
43 }
44 
45 bool
47 {
48  return ! is_defined ();
49 }
50 
51 void
53 {
54  m_frame->assign (m_sym, v);
55 }
56 
57 void
59  const octave_value& rhs)
60 {
61  if (! is_black_hole ())
62  m_frame->assign (op, m_sym, m_type, m_idx, rhs);
63 }
64 
67 {
68  // Return 1 if there is no index because without an index there
69  // should be no way to have a cs-list here. Cs-lists may be passed
70  // around internally but they are not supposed to be stored as
71  // single symbols in a stack frame.
72 
73  std::size_t num_indices = m_idx.size ();
74 
75  if (num_indices == 0)
76  return 1;
77 
78  switch (m_type[num_indices-1])
79  {
80  case '(':
81  return 1;
82 
83  case '{':
84  {
85  // FIXME: Duplicate code in '.' case below...
86 
87  // Evaluate, skipping the last index.
88 
89  std::string tmp_type = m_type;
90  std::list<octave_value_list> tmp_idx = m_idx;
91 
92  tmp_type.pop_back ();
93  tmp_idx.pop_back ();
94 
95  octave_value tmp = eval_for_numel (tmp_type, tmp_idx);
96 
97  octave_value_list tidx = m_idx.back ();
98 
99  if (tmp.is_undefined ())
100  {
101  if (tidx.has_magic_colon ())
103 
104  tmp = Cell ();
105  }
106  else if (tmp.is_zero_by_zero ()
107  && (tmp.is_matrix_type () || tmp.is_string ()))
108  {
109  tmp = Cell ();
110  }
111 
112  return tmp.xnumel (tidx);
113  }
114  break;
115 
116  case '.':
117  {
118  // Evaluate, skipping either the last index or the last two
119  // indices if we are looking at "(idx).field".
120 
121  std::string tmp_type = m_type;
122  std::list<octave_value_list> tmp_idx = m_idx;
123 
124  tmp_type.pop_back ();
125  tmp_idx.pop_back ();
126 
127  bool paren_dot = num_indices > 1 && m_type[num_indices-2] == '(';
128 
129  // Index for paren operator, if any.
130  octave_value_list pidx;
131 
132  if (paren_dot)
133  {
134  pidx = tmp_idx.back ();
135 
136  tmp_type.pop_back ();
137  tmp_idx.pop_back ();
138  }
139 
140  octave_value tmp = eval_for_numel (tmp_type, tmp_idx);
141 
142  bool autoconv = (tmp.is_zero_by_zero ()
143  && (tmp.is_matrix_type () || tmp.is_string ()
144  || tmp.iscell ()));
145 
146  if (paren_dot)
147  {
148  // Use octave_map, not octave_scalar_map so that the
149  // dimensions are 0x0, not 1x1.
150 
151  if (tmp.is_undefined ())
152  {
153  if (pidx.has_magic_colon ())
155 
156  tmp = octave_map ();
157  }
158  else if (autoconv)
159  tmp = octave_map ();
160 
161  return tmp.xnumel (pidx);
162  }
163  else if (tmp.is_undefined () || autoconv)
164  return 1;
165  else
166  return tmp.xnumel (octave_value_list ());
167  }
168  break;
169 
170  default:
171  panic_impossible ();
172  }
173 }
174 
175 void
176 octave_lvalue::set_index (const std::string& t,
177  const std::list<octave_value_list>& i)
178 {
179  if (! m_idx.empty ())
180  error ("invalid index expression in assignment");
181 
182  m_type = t;
183  m_idx = i;
184 }
185 
186 bool
188 {
189  bool retval = false;
190 
191  if (m_idx.size () == 1)
192  {
193  octave_value_list tmp = m_idx.front ();
194 
195  retval = (tmp.length () == 1 && tmp(0).isempty ());
196  }
197 
198  return retval;
199 }
200 
201 bool
203 {
204  bool retval = false;
205 
206  if (m_idx.size () == 1)
207  {
208  octave_value_list tmp = m_idx.front ();
209 
210  retval = (tmp.length () == 1 && tmp(0).is_magic_colon ());
211  }
212 
213  return retval;
214 }
215 
216 void
218 {
219  if (! is_black_hole ())
220  m_frame->non_const_unary_op (op, m_sym, m_type, m_idx);
221 }
222 
225 {
226  return (is_black_hole ()
227  ? octave_value () : m_frame->value (m_sym, m_type, m_idx));
228 }
229 
231 octave_lvalue::eval_for_numel (const std::string& type,
232  const std::list<octave_value_list>& idx) const
233 {
234  octave_value retval;
235 
236  try
237  {
238  retval = m_frame->varval (m_sym);
239 
240  if (retval.is_constant () && ! idx.empty ())
241  retval = retval.subsref (type, idx);
242  }
243  catch (const execution_exception&)
244  {
245  // Ignore an error and treat it as undefined. The error
246  // could happen because there is an index is out of range
247  // and we will be resizing a cell array.
248 
249  interpreter& interp = __get_interpreter__ ();
250 
251  interp.recover_from_exception ();
252 
253  retval = octave_value ();
254  }
255 
256  return retval;
257 }
258 
259 OCTAVE_END_NAMESPACE(octave)
Definition: Cell.h:43
void recover_from_exception()
bool index_is_empty() const
Definition: oct-lvalue.cc:187
bool is_black_hole() const
Definition: oct-lvalue.h:51
bool is_defined() const
Definition: oct-lvalue.cc:40
bool is_undefined() const
Definition: oct-lvalue.cc:46
void set_index(const std::string &t, const std::list< octave_value_list > &i)
Definition: oct-lvalue.cc:176
void assign(octave_value::assign_op, const octave_value &)
Definition: oct-lvalue.cc:58
void define(const octave_value &v)
Definition: oct-lvalue.cc:52
octave_idx_type numel() const
Definition: oct-lvalue.cc:66
octave_value value() const
Definition: oct-lvalue.cc:224
void unary_op(octave_value::unary_op op)
Definition: oct-lvalue.cc:217
bool index_is_colon() const
Definition: oct-lvalue.cc:202
bool has_magic_colon() const
Definition: ovl.cc:215
octave_idx_type length() const
Definition: ovl.h:113
bool is_undefined() const
Definition: ov.h:595
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov.h:476
unary_op
Definition: ov.h:79
bool is_string() const
Definition: ov.h:637
bool is_constant() const
Definition: ov.h:765
octave_idx_type xnumel(const octave_value_list &idx)
Definition: ov.h:467
assign_op
Definition: ov.h:134
bool is_zero_by_zero() const
Definition: ov.h:556
bool iscell() const
Definition: ov.h:604
bool is_matrix_type() const
Definition: ov.h:747
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void() error(const char *fmt,...)
Definition: error.cc:988
#define panic_impossible()
Definition: error.h:503
void err_invalid_inquiry_subscript()
Definition: errwarn.cc:77
interpreter & __get_interpreter__()
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))