Mercurial > octave
diff libinterp/parse-tree/oct-lvalue.cc @ 28804:3719f5d452d4 stable
refactor implementation of END indexing in interpreter (bug #58953)
* oct-lvalue.h, oct-lvalue.cc (octave_lvalue::eval_for_numel): New function.
(octave_lvalue::m_nel): Delete data member.
(octave_lvalue::numel (octave_idx_type)): Delete.
(octave_lvalue::numel (void) const): Compute result instead of
returning cached value.
* pt-eval.h, pt-eval.cc (tree_evaluator::convert_to_const_vector):
Simplify.
(Fend): Call tree_evaluator::evaluate_end_expression to do the real work.
(tree_evaluator::make_value_list): Don't check for magic_end here.
(end_value, tree_evaluator::evaluate_end_expression): New functions.
(tree_evaluator::m_index_list, tree_evaluator::m_index_type): New data members.
(tree_evaluator::set_indexed_object, tree_evaluator::index_list,
tree_evaluator::set_index_list, tree_evaluator::clear_index_list,
tree_evaluator::append_index_list, tree_evaluator::index_type):
New functions.
* pt-idx.cc (make_value_list): Delete.
(tree_index_expression::lvalue, tree_index_expression::evaluate_n):
Refactor to avoid evaluating partial expressions to obtain values for
the END function. Instead, cache info necessary to do that job as
needed and inside the END function itself.
author | John W. Eaton <jwe@octave.org> and Fernando Alvarruiz |
---|---|
date | Mon, 21 Sep 2020 10:45:18 -0400 |
parents | 9a3deb17b4ea |
children | d5efdf5cfc56 7c9a40fb3337 |
line wrap: on
line diff
--- a/libinterp/parse-tree/oct-lvalue.cc Sun Sep 27 12:24:24 2020 -0400 +++ b/libinterp/parse-tree/oct-lvalue.cc Mon Sep 21 10:45:18 2020 -0400 @@ -28,6 +28,7 @@ #endif #include "error.h" +#include "errwarn.h" #include "ovl.h" #include "oct-lvalue.h" #include "ov.h" @@ -56,6 +57,115 @@ m_frame->assign (op, m_sym, m_type, m_idx, rhs); } + octave_idx_type octave_lvalue::numel (void) const + { + // Return 1 if there is no index because without an index there + // should be no way to have a cs-list here. Cs-lists may be passed + // around internally but they are not supposed to be stored as + // single symbols in a stack frame. + + size_t num_indices = m_idx.size (); + + if (num_indices == 0) + return 1; + + switch (m_type[num_indices-1]) + { + case '(': + return 1; + + case '{': + { + // FIXME: Duplicate code in '.' case below... + + // Evaluate, skipping the last index. + + std::string tmp_type = m_type; + std::list<octave_value_list> tmp_idx = m_idx; + + tmp_type.pop_back (); + tmp_idx.pop_back (); + + octave_value tmp = eval_for_numel (tmp_type, tmp_idx); + + octave_value_list tidx = m_idx.back (); + + if (tmp.is_undefined ()) + { + if (tidx.has_magic_colon ()) + err_invalid_inquiry_subscript (); + + tmp = Cell (); + } + else if (tmp.is_zero_by_zero () + && (tmp.is_matrix_type () || tmp.is_string ())) + { + tmp = Cell (); + } + + return tmp.xnumel (tidx); + } + break; + + case '.': + { + // Evaluate, skipping either the last index or the last two + // indices if we are looking at "(idx).field". + + std::string tmp_type = m_type; + std::list<octave_value_list> tmp_idx = m_idx; + + tmp_type.pop_back (); + tmp_idx.pop_back (); + + bool paren_dot = num_indices > 1 && m_type[num_indices-2] == '('; + + // Index for paren operator, if any. + octave_value_list pidx; + + if (paren_dot) + { + pidx = tmp_idx.back (); + + tmp_type.pop_back (); + tmp_idx.pop_back (); + } + + octave_value tmp = eval_for_numel (tmp_type, tmp_idx); + + bool autoconv = (tmp.is_zero_by_zero () + && (tmp.is_matrix_type () || tmp.is_string () + || tmp.iscell ())); + + if (paren_dot) + { + // Use octave_map, not octave_scalar_map so that the + // dimensions are 0x0, not 1x1. + + if (tmp.is_undefined ()) + { + if (pidx.has_magic_colon ()) + err_invalid_inquiry_subscript (); + + tmp = octave_map (); + } + else if (autoconv) + tmp = octave_map (); + + return tmp.xnumel (pidx); + } + else if (tmp.is_undefined () || autoconv) + return 1; + else + return tmp.xnumel (octave_value_list ()); + } + break; + + default: + panic_impossible (); + } + } + void octave_lvalue::set_index (const std::string& t, const std::list<octave_value_list>& i) { @@ -105,4 +215,29 @@ return (is_black_hole () ? octave_value () : m_frame->value (m_sym, m_type, m_idx)); } + + octave_value + octave_lvalue::eval_for_numel (const std::string& type, + const std::list<octave_value_list>& idx) const + { + octave_value retval; + + try + { + retval = m_frame->varval (m_sym); + + if (retval.is_constant () && ! idx.empty ()) + retval = retval.subsref (type, idx); + } + catch (const execution_exception&) + { + // Ignore an error and treat it as undefined. The error + // could happen because there is an index is out of range + // and we will be resizing a cell array. + + retval = octave_value (); + } + + return retval; + } }