Mercurial > octave-nkf
annotate src/pt-colon.cc @ 7961:a5d1e27ee1f4 ss-3-1-51
3.1.51 snapshot
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 22 Jul 2008 11:40:48 -0400 |
parents | 71f068b22fcc |
children | 3100283874d7 |
rev | line source |
---|---|
2980 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 2000, 2001, 2002, 2004, 2005, 2006, 2007 |
4 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 | |
79 retval = rvalue (); | |
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 | |
116 bool result_is_str = (ov_base.is_string () && ov_limit.is_string ()); | |
117 bool dq_str = (ov_base.is_dq_string () || ov_limit.is_dq_string ()); | |
118 | |
119 Matrix m_base = ov_base.matrix_value (true); | |
120 | |
121 if (error_state) | |
122 eval_error ("invalid base value in colon expression"); | |
123 else | |
124 { | |
125 Matrix m_limit = ov_limit.matrix_value (true); | |
126 | |
127 if (error_state) | |
128 eval_error ("invalid limit value in colon expression"); | |
129 else | |
130 { | |
131 Matrix m_increment = ov_increment.matrix_value (true); | |
132 | |
133 if (error_state) | |
134 eval_error ("invalid increment value in colon expression"); | |
135 else | |
136 retval = make_range (m_base, m_limit, m_increment, | |
137 result_is_str, dq_str); | |
138 } | |
139 } | |
140 | |
141 return retval; | |
142 } | |
143 | |
144 octave_value | |
2980 | 145 tree_colon_expression::rvalue (void) |
146 { | |
147 octave_value retval; | |
148 | |
3770 | 149 MAYBE_DO_BREAKPOINT; |
150 | |
2980 | 151 if (error_state || ! op_base || ! op_limit) |
152 return retval; | |
153 | |
5347 | 154 octave_value ov_base = op_base->rvalue (); |
2980 | 155 |
5347 | 156 if (error_state || ov_base.is_undefined ()) |
157 eval_error ("invalid base value in colon expression"); | |
158 else | |
2980 | 159 { |
5347 | 160 octave_value ov_limit = op_limit->rvalue (); |
2980 | 161 |
5347 | 162 if (error_state || ov_limit.is_undefined ()) |
163 eval_error ("invalid limit value in colon expression"); | |
164 else | |
2980 | 165 { |
5347 | 166 octave_value ov_increment = 1.0; |
2980 | 167 |
5347 | 168 if (op_increment) |
169 { | |
170 ov_increment = op_increment->rvalue (); | |
2980 | 171 |
5347 | 172 if (error_state || ov_increment.is_undefined ()) |
173 eval_error ("invalid increment value in colon expression"); | |
174 } | |
175 | |
176 if (! error_state) | |
177 retval = make_range (ov_base, ov_limit, ov_increment); | |
2980 | 178 } |
179 } | |
180 | |
181 return retval; | |
182 } | |
183 | |
184 void | |
5347 | 185 tree_colon_expression::eval_error (const std::string& s) const |
2980 | 186 { |
3965 | 187 if (! s.empty ()) |
188 ::error ("%s", s.c_str ()); | |
2980 | 189 |
3965 | 190 ::error ("evaluating colon expression near line %d column %d", |
191 line (), column ()); | |
2980 | 192 } |
193 | |
5066 | 194 int |
195 tree_colon_expression::line (void) const | |
196 { | |
197 return (op_base ? op_base->line () | |
5067 | 198 : (op_increment ? op_increment->line () |
5066 | 199 : (op_limit ? op_limit->line () |
200 : -1))); | |
201 } | |
202 | |
203 int | |
204 tree_colon_expression::column (void) const | |
205 { | |
206 return (op_base ? op_base->column () | |
5067 | 207 : (op_increment ? op_increment->column () |
5066 | 208 : (op_limit ? op_limit->column () |
209 : -1))); | |
210 } | |
211 | |
5861 | 212 tree_expression * |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
213 tree_colon_expression::dup (symbol_table::scope_id scope, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
214 symbol_table::context_id context) |
5861 | 215 { |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
216 tree_colon_expression *new_ce = new |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
217 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
|
218 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
|
219 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
|
220 line (), column ()); |
5861 | 221 |
222 new_ce->copy_base (*new_ce); | |
223 | |
224 return new_ce; | |
225 } | |
226 | |
2980 | 227 void |
228 tree_colon_expression::accept (tree_walker& tw) | |
229 { | |
230 tw.visit_colon_expression (*this); | |
231 } | |
232 | |
233 /* | |
234 ;;; Local Variables: *** | |
235 ;;; mode: C++ *** | |
236 ;;; End: *** | |
237 */ |