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 |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
2980
|
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 |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
2980
|
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" |
3770
|
31 #include "pt-bp.h" |
2980
|
32 #include "pt-colon.h" |
|
33 #include "pt-walk.h" |
|
34 |
|
35 // Colon expressions. |
|
36 |
|
37 tree_colon_expression * |
|
38 tree_colon_expression::append (tree_expression *t) |
|
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 |
|
55 op_increment = op_limit; |
|
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 |
|
70 octave_value_list |
|
71 tree_colon_expression::rvalue (int nargout) |
|
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 = rvalue (); |
|
79 |
|
80 return retval; |
|
81 } |
|
82 |
|
83 octave_value |
5347
|
84 tree_colon_expression::make_range (const Matrix& m_base, |
|
85 const Matrix& m_limit, |
|
86 const Matrix& m_increment, |
|
87 bool result_is_str, bool dq_str) const |
|
88 { |
|
89 octave_value retval; |
|
90 |
|
91 bool base_empty = m_base.is_empty (); |
|
92 bool limit_empty = m_limit.is_empty (); |
|
93 bool increment_empty = m_increment.is_empty (); |
|
94 |
5358
|
95 if (base_empty || limit_empty || increment_empty) |
|
96 retval = Range (); |
|
97 else |
5347
|
98 { |
5358
|
99 retval = Range (m_base(0), m_limit(0), m_increment(0)); |
5347
|
100 |
5358
|
101 if (result_is_str) |
|
102 retval = retval.convert_to_str (false, true, dq_str ? '"' : '\''); |
5347
|
103 } |
|
104 |
|
105 return retval; |
|
106 } |
|
107 |
|
108 octave_value |
|
109 tree_colon_expression::make_range (const octave_value& ov_base, |
|
110 const octave_value& ov_limit, |
|
111 const octave_value& ov_increment) const |
|
112 { |
|
113 octave_value retval; |
|
114 |
|
115 bool result_is_str = (ov_base.is_string () && ov_limit.is_string ()); |
|
116 bool dq_str = (ov_base.is_dq_string () || ov_limit.is_dq_string ()); |
|
117 |
|
118 Matrix m_base = ov_base.matrix_value (true); |
|
119 |
|
120 if (error_state) |
|
121 eval_error ("invalid base value in colon expression"); |
|
122 else |
|
123 { |
|
124 Matrix m_limit = ov_limit.matrix_value (true); |
|
125 |
|
126 if (error_state) |
|
127 eval_error ("invalid limit value in colon expression"); |
|
128 else |
|
129 { |
|
130 Matrix m_increment = ov_increment.matrix_value (true); |
|
131 |
|
132 if (error_state) |
|
133 eval_error ("invalid increment value in colon expression"); |
|
134 else |
|
135 retval = make_range (m_base, m_limit, m_increment, |
|
136 result_is_str, dq_str); |
|
137 } |
|
138 } |
|
139 |
|
140 return retval; |
|
141 } |
|
142 |
|
143 octave_value |
2980
|
144 tree_colon_expression::rvalue (void) |
|
145 { |
|
146 octave_value retval; |
|
147 |
3770
|
148 MAYBE_DO_BREAKPOINT; |
|
149 |
2980
|
150 if (error_state || ! op_base || ! op_limit) |
|
151 return retval; |
|
152 |
5347
|
153 octave_value ov_base = op_base->rvalue (); |
2980
|
154 |
5347
|
155 if (error_state || ov_base.is_undefined ()) |
|
156 eval_error ("invalid base value in colon expression"); |
|
157 else |
2980
|
158 { |
5347
|
159 octave_value ov_limit = op_limit->rvalue (); |
2980
|
160 |
5347
|
161 if (error_state || ov_limit.is_undefined ()) |
|
162 eval_error ("invalid limit value in colon expression"); |
|
163 else |
2980
|
164 { |
5347
|
165 octave_value ov_increment = 1.0; |
2980
|
166 |
5347
|
167 if (op_increment) |
|
168 { |
|
169 ov_increment = op_increment->rvalue (); |
2980
|
170 |
5347
|
171 if (error_state || ov_increment.is_undefined ()) |
|
172 eval_error ("invalid increment value in colon expression"); |
|
173 } |
|
174 |
|
175 if (! error_state) |
|
176 retval = make_range (ov_base, ov_limit, ov_increment); |
2980
|
177 } |
|
178 } |
|
179 |
|
180 return retval; |
|
181 } |
|
182 |
|
183 void |
5347
|
184 tree_colon_expression::eval_error (const std::string& s) const |
2980
|
185 { |
3965
|
186 if (! s.empty ()) |
|
187 ::error ("%s", s.c_str ()); |
2980
|
188 |
3965
|
189 ::error ("evaluating colon expression near line %d column %d", |
|
190 line (), column ()); |
2980
|
191 } |
|
192 |
5066
|
193 int |
|
194 tree_colon_expression::line (void) const |
|
195 { |
|
196 return (op_base ? op_base->line () |
5067
|
197 : (op_increment ? op_increment->line () |
5066
|
198 : (op_limit ? op_limit->line () |
|
199 : -1))); |
|
200 } |
|
201 |
|
202 int |
|
203 tree_colon_expression::column (void) const |
|
204 { |
|
205 return (op_base ? op_base->column () |
5067
|
206 : (op_increment ? op_increment->column () |
5066
|
207 : (op_limit ? op_limit->column () |
|
208 : -1))); |
|
209 } |
|
210 |
5861
|
211 tree_expression * |
|
212 tree_colon_expression::dup (symbol_table *sym_tab) |
|
213 { |
|
214 tree_colon_expression *new_ce |
|
215 = new tree_colon_expression (op_base ? op_base->dup (sym_tab) : 0, |
|
216 op_limit ? op_limit->dup (sym_tab) : 0, |
|
217 op_increment ? op_increment->dup (sym_tab) : 0, |
|
218 line (), column ()); |
|
219 |
|
220 new_ce->copy_base (*new_ce); |
|
221 |
|
222 return new_ce; |
|
223 } |
|
224 |
2980
|
225 void |
|
226 tree_colon_expression::accept (tree_walker& tw) |
|
227 { |
|
228 tw.visit_colon_expression (*this); |
|
229 } |
|
230 |
|
231 /* |
|
232 ;;; Local Variables: *** |
|
233 ;;; mode: C++ *** |
|
234 ;;; End: *** |
|
235 */ |