2980
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 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 2, or (at your option) any |
|
10 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, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
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 * |
|
213 tree_colon_expression::dup (symbol_table *sym_tab) |
|
214 { |
|
215 tree_colon_expression *new_ce |
|
216 = new tree_colon_expression (op_base ? op_base->dup (sym_tab) : 0, |
|
217 op_limit ? op_limit->dup (sym_tab) : 0, |
|
218 op_increment ? op_increment->dup (sym_tab) : 0, |
|
219 line (), column ()); |
|
220 |
|
221 new_ce->copy_base (*new_ce); |
|
222 |
|
223 return new_ce; |
|
224 } |
|
225 |
2980
|
226 void |
|
227 tree_colon_expression::accept (tree_walker& tw) |
|
228 { |
|
229 tw.visit_colon_expression (*this); |
|
230 } |
|
231 |
|
232 /* |
|
233 ;;; Local Variables: *** |
|
234 ;;; mode: C++ *** |
|
235 ;;; End: *** |
|
236 */ |