GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
pt-colon.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2021 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 "interpreter.h"
31 #include "parse.h"
32 #include "pt-colon.h"
33 #include "pt-eval.h"
34 
35 namespace octave
36 {
37  // Colon expressions.
38 
39  tree_expression *
41  {
42  tree_colon_expression *new_ce
43  = new tree_colon_expression (m_base ? m_base->dup (scope) : nullptr,
44  m_limit ? m_limit->dup (scope) : nullptr,
45  m_increment ? m_increment->dup (scope) : nullptr,
46  line (), column ());
47 
48  new_ce->copy_base (*this);
49 
50  return new_ce;
51  }
52 
54  {
55  octave_value val;
56 
57  if (! m_base || ! m_limit)
58  return val;
59 
60  octave_value ov_base = m_base->evaluate (tw);
61 
62  octave_value ov_limit = m_limit->evaluate (tw);
63 
64  if (ov_base.isobject () || ov_limit.isobject ())
65  {
66  octave_value_list tmp1;
67 
68  if (m_increment)
69  {
70  octave_value ov_increment = m_increment->evaluate (tw);
71 
72  tmp1(2) = ov_limit;
73  tmp1(1) = ov_increment;
74  tmp1(0) = ov_base;
75  }
76  else
77  {
78  tmp1(1) = ov_limit;
79  tmp1(0) = ov_base;
80  }
81 
82  interpreter& interp = tw.get_interpreter ();
83 
84  symbol_table& symtab = interp.get_symbol_table ();
85 
86  octave_value fcn = symtab.find_function ("colon", tmp1);
87 
88  if (! fcn.is_defined ())
89  error ("can not find overloaded colon function");
90 
91  octave_value_list tmp2 = feval (fcn, tmp1, 1);
92 
93  val = tmp2 (0);
94  }
95  else
96  {
97  octave_value ov_increment = 1.0;
98 
99  if (m_increment)
100  ov_increment = m_increment->evaluate (tw);
101 
102  val = do_colon_op (ov_base, ov_increment, ov_limit,
103  is_for_cmd_expr ());
104  }
105 
106  return val;
107  }
108 }
symbol_table & get_symbol_table(void)
Definition: interpreter.h:258
octave_value find_function(const std::string &name, const symbol_scope &search_scope=symbol_scope())
Definition: symtab.cc:249
tree_expression * m_limit
Definition: pt-colon.h:108
tree_colon_expression(int l=-1, int c=-1)
Definition: pt-colon.h:49
tree_expression * m_increment
Definition: pt-colon.h:109
tree_expression * m_base
Definition: pt-colon.h:107
octave_value evaluate(tree_evaluator &, int nargout=1)
Definition: pt-colon.cc:53
tree_expression * dup(symbol_scope &scope) const
Definition: pt-colon.cc:40
interpreter & get_interpreter(void)
Definition: pt-eval.h:370
virtual void copy_base(const tree_expression &e)
Definition: pt-exp.h:131
bool is_for_cmd_expr(void) const
Definition: pt-exp.h:111
virtual tree_expression * dup(symbol_scope &scope) const =0
virtual octave_value evaluate(tree_evaluator &tw, int nargout=1)=0
virtual int column(void) const
Definition: pt.h:62
bool is_defined(void) const
Definition: ov.h:551
bool isobject(void) const
Definition: ov.h:620
void error(const char *fmt,...)
Definition: error.cc:968
octave_value_list feval(const char *name, const octave_value_list &args, int nargout)
Evaluate an Octave function (built-in or interpreted) and return the list of result values.
Definition: oct-parse.cc:9580
OCTINTERP_API octave_value do_colon_op(const octave_value &base, const octave_value &limit, bool is_for_cmd_expr=false)
Definition: ov.h:1280