1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
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 |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
383
|
23 #if !defined (octave_tree_const_h) |
|
24 #define octave_tree_const_h 1 |
1
|
25 |
1298
|
26 #if defined (__GNUG__) |
|
27 #pragma interface |
|
28 #endif |
|
29 |
1728
|
30 #include <string> |
|
31 |
1740
|
32 class ostream; |
581
|
33 |
1355
|
34 #include "Range.h" |
500
|
35 #include "mx-base.h" |
2475
|
36 #include "oct-alloc.h" |
1755
|
37 #include "str-vec.h" |
500
|
38 |
1740
|
39 #include "pt-fvc.h" |
164
|
40 |
747
|
41 class Octave_map; |
2086
|
42 class octave_value_list; |
529
|
43 |
2124
|
44 class tree_walker; |
|
45 |
2390
|
46 #include "ov.h" |
581
|
47 |
1
|
48 class |
2390
|
49 tree_constant : public tree_fvc |
1
|
50 { |
|
51 public: |
620
|
52 |
|
53 enum magic_colon { magic_colon_t }; |
922
|
54 enum all_va_args { all_va_args_t }; |
620
|
55 |
1558
|
56 // Constructors. It is possible to create the following types of |
|
57 // constants: |
|
58 // |
|
59 // constant type constructor arguments |
|
60 // ------------- --------------------- |
|
61 // unknown none |
|
62 // real scalar double |
|
63 // real matrix Matrix |
|
64 // DiagMatrix |
|
65 // RowVector |
|
66 // ColumnVector |
|
67 // complex scalar Complex |
|
68 // complex matrix ComplexMatrix |
|
69 // ComplexDiagMatrix |
|
70 // ComplexRowVector |
|
71 // ComplexColumnVector |
1572
|
72 // char matrix charMatrix |
1558
|
73 // string char* (null terminated) |
1731
|
74 // string |
1572
|
75 // charMatrix |
1558
|
76 // range double, double, double |
|
77 // Range |
|
78 // map Octave_map |
2390
|
79 // magic colon tree_constant::magic_colon |
|
80 // all_va_args tree_constant::all_va_args |
620
|
81 |
2390
|
82 tree_constant (void) |
|
83 : tree_fvc (), val (), orig_text () { } |
|
84 |
|
85 tree_constant (double d, int l = -1, int c = -1) |
|
86 : tree_fvc (l, c), val (d), orig_text () { } |
1
|
87 |
2390
|
88 tree_constant (const Matrix& m) |
|
89 : tree_fvc (), val (m), orig_text () { } |
620
|
90 |
2390
|
91 tree_constant (const DiagMatrix& d) |
|
92 : tree_fvc (), val (d), orig_text () { } |
620
|
93 |
2390
|
94 tree_constant (const RowVector& v, int pcv = -1) |
|
95 : tree_fvc (), val (v, pcv), orig_text () { } |
620
|
96 |
2390
|
97 tree_constant (const ColumnVector& v, int pcv = -1) |
|
98 : tree_fvc (), val (v, pcv), orig_text () { } |
620
|
99 |
2390
|
100 tree_constant (const Complex& C, int l = -1, int c = -1) |
|
101 : tree_fvc (l, c), val (C), orig_text () { } |
|
102 |
|
103 tree_constant (const ComplexMatrix& m) |
|
104 : tree_fvc (), val (m), orig_text () { } |
1
|
105 |
2390
|
106 tree_constant (const ComplexDiagMatrix& d) |
|
107 : tree_fvc (), val (d), orig_text () { } |
|
108 |
|
109 tree_constant (const ComplexRowVector& v, int pcv = -1) |
|
110 : tree_fvc (), val (v, pcv), orig_text () { } |
620
|
111 |
2390
|
112 tree_constant (const ComplexColumnVector& v, int pcv = -1) |
|
113 : tree_fvc (), val (v, pcv), orig_text () { } |
620
|
114 |
2390
|
115 tree_constant (const char *s, int l = -1, int c = -1) |
|
116 : tree_fvc (l, c), val (s), orig_text () { } |
620
|
117 |
2390
|
118 tree_constant (const string& s, int l = -1, int c = -1) |
|
119 : tree_fvc (l, c), val (s), orig_text () { } |
620
|
120 |
2390
|
121 tree_constant (const string_vector& s, int l = -1, int c = -1) |
|
122 : tree_fvc (l, c), val (s), orig_text () { } |
|
123 |
|
124 tree_constant (const charMatrix& chm, bool is_string = false) |
|
125 : tree_fvc (), val (chm, is_string), orig_text () { } |
1
|
126 |
2390
|
127 tree_constant (double base, double limit, double inc) |
|
128 : tree_fvc (), val (base, limit, inc), orig_text () { } |
1
|
129 |
2390
|
130 tree_constant (const Range& r) |
|
131 : tree_fvc (), val (r), orig_text () { } |
1731
|
132 |
2390
|
133 tree_constant (const Octave_map& m) |
|
134 : tree_fvc (), val (m), orig_text () { } |
1755
|
135 |
2390
|
136 tree_constant (tree_constant::magic_colon, int l = -1, int c = -1) |
|
137 : tree_fvc (l, c), val (octave_value::magic_colon_t), orig_text () { } |
1355
|
138 |
2390
|
139 tree_constant (tree_constant::all_va_args, int l = -1, int c = -1) |
|
140 : tree_fvc (l, c), val (octave_value::all_va_args_t), orig_text () { } |
620
|
141 |
2390
|
142 tree_constant (const octave_value& v, int l = -1, int c = -1) |
|
143 : tree_fvc (l, c), val (v), orig_text () { } |
1
|
144 |
2390
|
145 tree_constant (const tree_constant& a) |
2431
|
146 : tree_fvc (), val (a.val), orig_text () { } |
747
|
147 |
2390
|
148 ~tree_constant (void) { } |
|
149 |
|
150 tree_constant& operator = (const tree_constant& a) |
620
|
151 { |
2390
|
152 if (this != &a) |
|
153 { |
|
154 tree_fvc::operator = (a); |
|
155 val = a.val; |
|
156 } |
|
157 return *this; |
620
|
158 } |
|
159 |
2475
|
160 void *operator new (size_t size) |
|
161 { return allocator.alloc (size); } |
|
162 |
|
163 void operator delete (void *p, size_t size) |
|
164 { allocator.free (p, size); } |
1
|
165 |
1558
|
166 // Indexed assignment. |
1
|
167 |
2390
|
168 octave_value index (const octave_value_list& idx) const |
|
169 { return val.index (idx); } |
|
170 |
|
171 octave_value& reference (void) |
1
|
172 { |
2390
|
173 val.make_unique (); |
|
174 return val; |
1
|
175 } |
|
176 |
2390
|
177 octave_value value (void) const |
|
178 { |
|
179 return val; |
|
180 } |
|
181 |
|
182 octave_value assign (const octave_value_list& idx, const octave_value& rhs) |
|
183 { |
|
184 val.assign (idx, rhs); |
|
185 return val; |
|
186 } |
|
187 |
1558
|
188 // Type. It would be nice to eliminate the need for this. |
620
|
189 |
1827
|
190 bool is_constant (void) const { return true; } |
620
|
191 |
1558
|
192 // Size. |
620
|
193 |
2390
|
194 int rows (void) const { return val.rows (); } |
|
195 int columns (void) const { return val.columns (); } |
620
|
196 |
1558
|
197 // Does this constant have a type? Both of these are provided since |
|
198 // it is sometimes more natural to write is_undefined() instead of |
|
199 // ! is_defined(). |
620
|
200 |
2390
|
201 bool is_defined (void) const { return val.is_defined (); } |
|
202 bool is_undefined (void) const { return val.is_undefined (); } |
620
|
203 |
1558
|
204 // Is this constant a particular type, or does it belong to a |
|
205 // particular class of types? |
620
|
206 |
2390
|
207 bool is_real_scalar (void) const { return val.is_real_scalar (); } |
|
208 bool is_real_matrix (void) const { return val.is_real_matrix (); } |
|
209 bool is_complex_scalar (void) const { return val.is_complex_scalar (); } |
|
210 bool is_complex_matrix (void) const { return val.is_complex_matrix (); } |
|
211 bool is_char_matrix (void) const { return val.is_char_matrix (); } |
|
212 bool is_string (void) const { return val.is_string (); } |
|
213 bool is_range (void) const { return val.is_range (); } |
|
214 bool is_map (void) const { return val.is_map (); } |
|
215 bool is_magic_colon (void) const { return val.is_magic_colon (); } |
|
216 bool is_all_va_args (void) const { return val.is_all_va_args (); } |
620
|
217 |
1558
|
218 // Are any or all of the elements in this constant nonzero? |
620
|
219 |
2390
|
220 octave_value all (void) const { return val.all (); } |
|
221 octave_value any (void) const { return val.any (); } |
620
|
222 |
1558
|
223 // Other type stuff. |
|
224 |
2390
|
225 bool is_real_type (void) const { return val.is_real_type (); } |
628
|
226 |
2390
|
227 bool is_complex_type (void) const { return val.is_complex_type (); } |
620
|
228 |
2390
|
229 bool is_scalar_type (void) const { return val.is_scalar_type (); } |
|
230 bool is_matrix_type (void) const { return val.is_matrix_type (); } |
620
|
231 |
1827
|
232 bool is_numeric_type (void) const |
2390
|
233 { return val.is_numeric_type (); } |
620
|
234 |
1827
|
235 bool valid_as_scalar_index (void) const |
2390
|
236 { return val.valid_as_scalar_index (); } |
620
|
237 |
1827
|
238 bool valid_as_zero_index (void) const |
2390
|
239 { return val.valid_as_zero_index (); } |
1041
|
240 |
1558
|
241 // Does this constant correspond to a truth value? |
620
|
242 |
2390
|
243 bool is_true (void) const { return val.is_true (); } |
620
|
244 |
1558
|
245 // Is at least one of the dimensions of this constant zero? |
620
|
246 |
1827
|
247 bool is_empty (void) const |
2390
|
248 { return val.is_empty (); } |
620
|
249 |
1558
|
250 // Are the dimensions of this constant zero by zero? |
620
|
251 |
1827
|
252 bool is_zero_by_zero (void) const |
2390
|
253 { return val.is_zero_by_zero (); } |
620
|
254 |
1558
|
255 // Values. |
620
|
256 |
1827
|
257 double double_value (bool frc_str_conv = false) const |
2390
|
258 { return val.double_value (frc_str_conv); } |
628
|
259 |
1827
|
260 Matrix matrix_value (bool frc_str_conv = false) const |
2390
|
261 { return val.matrix_value (frc_str_conv); } |
628
|
262 |
1827
|
263 Complex complex_value (bool frc_str_conv = false) const |
2390
|
264 { return val.complex_value (frc_str_conv); } |
628
|
265 |
1827
|
266 ComplexMatrix complex_matrix_value (bool frc_str_conv = false) const |
2390
|
267 { return val.complex_matrix_value (frc_str_conv); } |
628
|
268 |
1827
|
269 charMatrix char_matrix_value (bool frc_str_conv = false) const |
2390
|
270 { return val.char_matrix_value (frc_str_conv); } |
1572
|
271 |
|
272 charMatrix all_strings (void) const |
2390
|
273 { return val.all_strings (); } |
1355
|
274 |
1728
|
275 string string_value (void) const |
2390
|
276 { return val.string_value (); } |
628
|
277 |
|
278 Range range_value (void) const |
2390
|
279 { return val.range_value (); } |
628
|
280 |
747
|
281 Octave_map map_value (void) const; |
|
282 |
2086
|
283 octave_value lookup_map_element (const string& ref, |
2532
|
284 bool insert = false, |
|
285 bool silent = false); |
1277
|
286 |
2086
|
287 octave_value lookup_map_element (SLList<string>& list, |
2532
|
288 bool insert = false, |
|
289 bool silent = false); |
747
|
290 |
1827
|
291 ColumnVector vector_value (bool /* frc_str_conv */ = false, |
|
292 bool /* frc_vec_conv */ = false) const |
2390
|
293 { return val.vector_value (); } |
628
|
294 |
1827
|
295 ComplexColumnVector |
|
296 complex_vector_value (bool /* frc_str_conv */ = false, |
|
297 bool /* frc_vec_conv */ = false) const |
2390
|
298 { return val.complex_vector_value (); } |
1
|
299 |
1558
|
300 // Binary and unary operations. |
1204
|
301 |
2086
|
302 friend octave_value do_binary_op (octave_value& a, octave_value& b, |
2390
|
303 tree_expression::type t); |
1204
|
304 |
2086
|
305 friend octave_value do_unary_op (octave_value& a, |
2390
|
306 tree_expression::type t); |
1204
|
307 |
1558
|
308 // Conversions. These should probably be private. If a user of this |
|
309 // class wants a certain kind of constant, he should simply ask for |
|
310 // it, and we should convert it if possible. |
1
|
311 |
2089
|
312 octave_value convert_to_str (void) const |
2390
|
313 { return val.convert_to_str (); } |
1
|
314 |
435
|
315 void convert_to_row_or_column_vector (void) |
2390
|
316 { val.convert_to_row_or_column_vector (); } |
435
|
317 |
2409
|
318 void maybe_mutate (void) |
|
319 { val.maybe_mutate (); } |
|
320 |
1558
|
321 // Increment or decrement this constant. |
1
|
322 |
2390
|
323 void increment (void) { val.increment (); } |
1004
|
324 |
2390
|
325 void decrement (void) { val.decrement (); } |
1
|
326 |
1199
|
327 void print (void); |
2532
|
328 void print (ostream& os, bool pr_as_read_syntax = false, |
|
329 bool pr_orig_txt = true); |
1199
|
330 |
1827
|
331 void print_with_name (const string& name, bool print_padding = true); |
1755
|
332 void print_with_name (ostream& os, const string& name, |
1827
|
333 bool print_padding = true); |
1755
|
334 |
2390
|
335 octave_value eval (bool print_result); |
1
|
336 |
2086
|
337 octave_value_list eval (bool, int, const octave_value_list&); |
1
|
338 |
1558
|
339 // Store the original text corresponding to this constant for later |
|
340 // pretty printing. |
620
|
341 |
2532
|
342 void stash_original_text (const string& s); |
620
|
343 |
2532
|
344 string original_text (void) const; |
2124
|
345 |
|
346 void accept (tree_walker& tw); |
581
|
347 |
2390
|
348 string type_name (void) const { return val.type_name (); } |
747
|
349 |
|
350 private: |
|
351 |
2475
|
352 // For custom memory management. |
|
353 static octave_allocator allocator; |
|
354 |
|
355 // The actual value that this constant refers to. |
2390
|
356 octave_value val; |
747
|
357 |
2475
|
358 // The original text form of this constant. |
2390
|
359 string orig_text; |
|
360 }; |
2179
|
361 |
1
|
362 #endif |
|
363 |
|
364 /* |
|
365 ;;; Local Variables: *** |
|
366 ;;; mode: C++ *** |
|
367 ;;; End: *** |
|
368 */ |