GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
pt-colon.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2015 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include "error.h"
28 #include "oct-obj.h"
29 #include "pager.h"
30 #include "ov.h"
31 #include "pt-bp.h"
32 #include "pt-colon.h"
33 #include "pt-walk.h"
34 
35 // Colon expressions.
36 
39 {
40  tree_colon_expression *retval = 0;
41 
42  if (op_base)
43  {
44  if (op_limit)
45  {
46  if (op_increment)
47  ::error ("invalid colon expression");
48  else
49  {
50  // Stupid syntax:
51  //
52  // base : limit
53  // base : increment : limit
54 
56  op_limit = t;
57  }
58  }
59  else
60  op_limit = t;
61 
62  retval = this;
63  }
64  else
65  ::error ("invalid colon expression");
66 
67  return retval;
68 }
69 
72 {
73  octave_value_list retval;
74 
75  if (nargout > 1)
76  error ("invalid number of output arguments for colon expression");
77  else
78  retval = rvalue1 (nargout);
79 
80  return retval;
81 }
82 
85 {
86  octave_value retval;
87 
88  if (error_state || ! op_base || ! op_limit)
89  return retval;
90 
91  octave_value ov_base = op_base->rvalue1 ();
92 
93  if (error_state || ov_base.is_undefined ())
94  eval_error ("invalid base value in colon expression");
95  else
96  {
97  octave_value ov_limit = op_limit->rvalue1 ();
98 
99  if (error_state || ov_limit.is_undefined ())
100  eval_error ("invalid limit value in colon expression");
101  else if (ov_base.is_object () || ov_limit.is_object ())
102  {
103  octave_value_list tmp1;
104 
105  if (op_increment)
106  {
107  octave_value ov_increment = op_increment->rvalue1 ();
108 
109  if (error_state || ov_increment.is_undefined ())
110  eval_error ("invalid increment value in colon expression");
111  else
112  {
113  tmp1(2) = ov_limit;
114  tmp1(1) = ov_increment;
115  tmp1(0) = ov_base;
116  }
117  }
118  else
119  {
120  tmp1(1) = ov_limit;
121  tmp1(0) = ov_base;
122  }
123 
124  if (!error_state)
125  {
126  octave_value fcn = symbol_table::find_function ("colon", tmp1);
127 
128  if (fcn.is_defined ())
129  {
130  octave_value_list tmp2 = fcn.do_multi_index_op (1, tmp1);
131 
132  if (! error_state)
133  retval = tmp2 (0);
134  }
135  else
136  ::error ("can not find overloaded colon function");
137  }
138  }
139  else
140  {
141  octave_value ov_increment = 1.0;
142 
143  if (op_increment)
144  {
145  ov_increment = op_increment->rvalue1 ();
146 
147  if (error_state || ov_increment.is_undefined ())
148  eval_error ("invalid increment value in colon expression");
149  }
150 
151  if (! error_state)
152  retval = do_colon_op (ov_base, ov_increment, ov_limit,
153  is_for_cmd_expr ());
154  }
155  }
156 
157  return retval;
158 }
159 
160 void
161 tree_colon_expression::eval_error (const std::string& s) const
162 {
163  ::error ("%s", s.c_str ());
164 }
165 
166 int
168 {
169  return (op_base ? op_base->line ()
171  : (op_limit ? op_limit->line ()
172  : -1)));
173 }
174 
175 int
177 {
178  return (op_base ? op_base->column ()
180  : (op_limit ? op_limit->column ()
181  : -1)));
182 }
183 
187 {
188  tree_colon_expression *new_ce = new
189  tree_colon_expression (op_base ? op_base->dup (scope, context) : 0,
190  op_limit ? op_limit->dup (scope, context) : 0,
191  op_increment ? op_increment->dup (scope, context)
192  : 0,
193  line (), column ());
194 
195  new_ce->copy_base (*new_ce);
196 
197  return new_ce;
198 }
199 
200 void
202 {
203  tw.visit_colon_expression (*this);
204 }
bool is_object(void) const
Definition: ov.h:577
octave_value rvalue1(int nargout=1)
Definition: pt-colon.cc:84
bool is_defined(void) const
Definition: ov.h:520
virtual tree_expression * dup(symbol_table::scope_id, symbol_table::context_id context) const =0
void error(const char *fmt,...)
Definition: error.cc:476
int line(void) const
Definition: pt-colon.cc:167
void eval_error(const std::string &s) const
Definition: pt-colon.cc:161
tree_expression * op_increment
Definition: pt-colon.h:104
virtual void copy_base(const tree_expression &e)
Definition: pt-exp.h:129
int column(void) const
Definition: pt-colon.cc:176
static octave_value find_function(const std::string &name, const octave_value_list &args=octave_value_list(), bool local_funcs=true)
Definition: symtab.cc:1271
tree_colon_expression * append(tree_expression *t)
Definition: pt-colon.cc:38
bool is_for_cmd_expr(void) const
Definition: pt-exp.h:109
void accept(tree_walker &tw)
Definition: pt-colon.cc:201
octave_value_list do_multi_index_op(int nargout, const octave_value_list &idx)
Definition: ov.cc:1382
virtual void visit_colon_expression(tree_colon_expression &)=0
static llvm::LLVMContext & context
Definition: jit-typeinfo.cc:76
int error_state
Definition: error.cc:101
tree_colon_expression(int l=-1, int c=-1)
Definition: pt-colon.h:44
virtual int line(void) const
Definition: pt.h:45
tree_expression * op_limit
Definition: pt-colon.h:103
virtual octave_value rvalue1(int nargout=1)
Definition: pt-exp.cc:58
bool is_undefined(void) const
Definition: ov.h:523
octave_value_list rvalue(int nargout)
Definition: pt-colon.cc:71
virtual int column(void) const
Definition: pt.h:47
octave_value do_colon_op(const octave_value &base, const octave_value &increment, const octave_value &limit, bool is_for_cmd_expr)
Definition: ov.cc:2393
tree_expression * op_base
Definition: pt-colon.h:102
tree_expression * dup(symbol_table::scope_id scope, symbol_table::context_id context) const
Definition: pt-colon.cc:185