Mercurial > hg > octave-lojdl
annotate src/pt-colon.cc @ 8920:eb63fbe60fab
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 07 Mar 2009 10:41:27 -0500 |
parents | 35cd375d4bb3 |
children | cd96d29c5efa |
rev | line source |
---|---|
2980 | 1 /* |
2 | |
8920 | 3 Copyright (C) 1996, 1997, 2000, 2001, 2002, 2004, 2005, 2006, 2007, |
4 2008, 2009 John W. Eaton | |
2980 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
2980 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
2980 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
28 #include "error.h" | |
29 #include "oct-obj.h" | |
30 #include "pager.h" | |
31 #include "ov.h" | |
3770 | 32 #include "pt-bp.h" |
2980 | 33 #include "pt-colon.h" |
34 #include "pt-walk.h" | |
35 | |
36 // Colon expressions. | |
37 | |
38 tree_colon_expression * | |
39 tree_colon_expression::append (tree_expression *t) | |
40 { | |
41 tree_colon_expression *retval = 0; | |
42 | |
43 if (op_base) | |
44 { | |
45 if (op_limit) | |
46 { | |
47 if (op_increment) | |
48 ::error ("invalid colon expression"); | |
49 else | |
50 { | |
51 // Stupid syntax: | |
52 // | |
53 // base : limit | |
54 // base : increment : limit | |
55 | |
56 op_increment = op_limit; | |
57 op_limit = t; | |
58 } | |
59 } | |
60 else | |
61 op_limit = t; | |
62 | |
63 retval = this; | |
64 } | |
65 else | |
66 ::error ("invalid colon expression"); | |
67 | |
68 return retval; | |
69 } | |
70 | |
71 octave_value_list | |
72 tree_colon_expression::rvalue (int nargout) | |
73 { | |
74 octave_value_list retval; | |
75 | |
76 if (nargout > 1) | |
77 error ("invalid number of output arguments for colon expression"); | |
78 else | |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8213
diff
changeset
|
79 retval = rvalue1 (nargout); |
2980 | 80 |
81 return retval; | |
82 } | |
83 | |
84 octave_value | |
5347 | 85 tree_colon_expression::make_range (const Matrix& m_base, |
86 const Matrix& m_limit, | |
87 const Matrix& m_increment, | |
88 bool result_is_str, bool dq_str) const | |
89 { | |
90 octave_value retval; | |
91 | |
92 bool base_empty = m_base.is_empty (); | |
93 bool limit_empty = m_limit.is_empty (); | |
94 bool increment_empty = m_increment.is_empty (); | |
95 | |
5358 | 96 if (base_empty || limit_empty || increment_empty) |
97 retval = Range (); | |
98 else | |
5347 | 99 { |
5358 | 100 retval = Range (m_base(0), m_limit(0), m_increment(0)); |
5347 | 101 |
5358 | 102 if (result_is_str) |
103 retval = retval.convert_to_str (false, true, dq_str ? '"' : '\''); | |
5347 | 104 } |
105 | |
106 return retval; | |
107 } | |
108 | |
109 octave_value | |
110 tree_colon_expression::make_range (const octave_value& ov_base, | |
111 const octave_value& ov_limit, | |
112 const octave_value& ov_increment) const | |
113 { | |
114 octave_value retval; | |
115 | |
8213
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
116 if (ov_base.is_object () || ov_limit.is_object () || |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
117 ov_increment.is_object ()) |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
118 { |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
119 octave_value_list tmp1; |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
120 tmp1(2) = ov_limit; |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
121 tmp1(1) = ov_increment; |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
122 tmp1(0) = ov_base; |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
123 |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
124 octave_value fcn = symbol_table::find_function ("colon", tmp1); |
5347 | 125 |
8213
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
126 if (fcn.is_defined ()) |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
127 { |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
128 octave_value_list tmp2 = fcn.do_multi_index_op (1, tmp1); |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
129 |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
130 if (! error_state) |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
131 retval = tmp2 (0); |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
132 } |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
133 else |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
134 ::error ("can not find overloaded colon function"); |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
135 } |
5347 | 136 else |
137 { | |
8213
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
138 bool result_is_str = (ov_base.is_string () && ov_limit.is_string ()); |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
139 bool dq_str = (ov_base.is_dq_string () || ov_limit.is_dq_string ()); |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
140 |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
141 Matrix m_base = ov_base.matrix_value (true); |
5347 | 142 |
143 if (error_state) | |
8213
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
144 eval_error ("invalid base value in colon expression"); |
5347 | 145 else |
146 { | |
8213
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
147 Matrix m_limit = ov_limit.matrix_value (true); |
5347 | 148 |
149 if (error_state) | |
8213
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
150 eval_error ("invalid limit value in colon expression"); |
5347 | 151 else |
8213
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
152 { |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
153 Matrix m_increment = ov_increment.matrix_value (true); |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
154 |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
155 if (error_state) |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
156 eval_error ("invalid increment value in colon expression"); |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
157 else |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
158 retval = make_range (m_base, m_limit, m_increment, |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
159 result_is_str, dq_str); |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
160 } |
5347 | 161 } |
162 } | |
163 | |
164 return retval; | |
165 } | |
166 | |
167 octave_value | |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8213
diff
changeset
|
168 tree_colon_expression::rvalue1 (int) |
2980 | 169 { |
170 octave_value retval; | |
171 | |
172 if (error_state || ! op_base || ! op_limit) | |
173 return retval; | |
174 | |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8213
diff
changeset
|
175 octave_value ov_base = op_base->rvalue1 (); |
2980 | 176 |
5347 | 177 if (error_state || ov_base.is_undefined ()) |
178 eval_error ("invalid base value in colon expression"); | |
179 else | |
2980 | 180 { |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8213
diff
changeset
|
181 octave_value ov_limit = op_limit->rvalue1 (); |
2980 | 182 |
5347 | 183 if (error_state || ov_limit.is_undefined ()) |
184 eval_error ("invalid limit value in colon expression"); | |
8213
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
185 else if (ov_base.is_object () || ov_limit.is_object ()) |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
186 { |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
187 octave_value_list tmp1; |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
188 |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
189 if (op_increment) |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
190 { |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8213
diff
changeset
|
191 octave_value ov_increment = op_increment->rvalue1 (); |
8213
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
192 |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
193 if (error_state || ov_increment.is_undefined ()) |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
194 eval_error ("invalid increment value in colon expression"); |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
195 else |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
196 { |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
197 tmp1(2) = ov_limit; |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
198 tmp1(1) = ov_increment; |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
199 tmp1(0) = ov_base; |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
200 } |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
201 } |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
202 else |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
203 { |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
204 tmp1(1) = ov_limit; |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
205 tmp1(0) = ov_base; |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
206 } |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
207 |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
208 if (!error_state) |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
209 { |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
210 octave_value fcn = symbol_table::find_function ("colon", tmp1); |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
211 |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
212 if (fcn.is_defined ()) |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
213 { |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
214 octave_value_list tmp2 = fcn.do_multi_index_op (1, tmp1); |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
215 |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
216 if (! error_state) |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
217 retval = tmp2 (0); |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
218 } |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
219 else |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
220 ::error ("can not find overloaded colon function"); |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
221 } |
d5e08881bba8
Add overloading of the colon operator
David Bateman <dbateman@free.fr>
parents:
8011
diff
changeset
|
222 } |
5347 | 223 else |
2980 | 224 { |
5347 | 225 octave_value ov_increment = 1.0; |
2980 | 226 |
5347 | 227 if (op_increment) |
228 { | |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8213
diff
changeset
|
229 ov_increment = op_increment->rvalue1 (); |
2980 | 230 |
5347 | 231 if (error_state || ov_increment.is_undefined ()) |
232 eval_error ("invalid increment value in colon expression"); | |
233 } | |
234 | |
235 if (! error_state) | |
236 retval = make_range (ov_base, ov_limit, ov_increment); | |
2980 | 237 } |
238 } | |
239 | |
240 return retval; | |
241 } | |
242 | |
243 void | |
5347 | 244 tree_colon_expression::eval_error (const std::string& s) const |
2980 | 245 { |
8011
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7767
diff
changeset
|
246 ::error ("%s", s.c_str ()); |
2980 | 247 } |
248 | |
5066 | 249 int |
250 tree_colon_expression::line (void) const | |
251 { | |
252 return (op_base ? op_base->line () | |
5067 | 253 : (op_increment ? op_increment->line () |
5066 | 254 : (op_limit ? op_limit->line () |
255 : -1))); | |
256 } | |
257 | |
258 int | |
259 tree_colon_expression::column (void) const | |
260 { | |
261 return (op_base ? op_base->column () | |
5067 | 262 : (op_increment ? op_increment->column () |
5066 | 263 : (op_limit ? op_limit->column () |
264 : -1))); | |
265 } | |
266 | |
5861 | 267 tree_expression * |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
268 tree_colon_expression::dup (symbol_table::scope_id scope, |
8913
35cd375d4bb3
make tree::dup functions const
John W. Eaton <jwe@octave.org>
parents:
8658
diff
changeset
|
269 symbol_table::context_id context) const |
5861 | 270 { |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
271 tree_colon_expression *new_ce = new |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
272 tree_colon_expression (op_base ? op_base->dup (scope, context) : 0, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
273 op_limit ? op_limit->dup (scope, context) : 0, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
274 op_increment ? op_increment->dup (scope, context) : 0, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
275 line (), column ()); |
5861 | 276 |
277 new_ce->copy_base (*new_ce); | |
278 | |
279 return new_ce; | |
280 } | |
281 | |
2980 | 282 void |
283 tree_colon_expression::accept (tree_walker& tw) | |
284 { | |
285 tw.visit_colon_expression (*this); | |
286 } | |
287 | |
288 /* | |
289 ;;; Local Variables: *** | |
290 ;;; mode: C++ *** | |
291 ;;; End: *** | |
292 */ |