529
|
1 // tree-const.h -*- C++ -*- |
1
|
2 /* |
|
3 |
296
|
4 Copyright (C) 1992, 1993, 1994 John W. Eaton |
1
|
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 |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
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 |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
383
|
24 #if !defined (octave_tree_const_h) |
|
25 #define octave_tree_const_h 1 |
1
|
26 |
455
|
27 #if defined (__GNUG__) |
|
28 #pragma interface |
|
29 #endif |
|
30 |
581
|
31 #include <iostream.h> |
|
32 |
1
|
33 #include <stdlib.h> |
|
34 |
500
|
35 #include "mx-base.h" |
|
36 #include "Range.h" |
|
37 |
1
|
38 #include "tree-base.h" |
495
|
39 #include "tree-expr.h" |
500
|
40 #include "oct-obj.h" |
164
|
41 |
|
42 class idx_vector; |
1
|
43 |
529
|
44 struct Mapper_fcn; |
|
45 |
581
|
46 // Constants. |
|
47 |
1
|
48 class |
495
|
49 tree_constant : public tree_fvc |
1
|
50 { |
620
|
51 private: |
|
52 |
|
53 #include "tc-rep.h" |
|
54 |
|
55 // The real representation of a constant, declared in tc-rep.h |
|
56 |
|
57 tree_constant_rep *rep; |
1
|
58 |
|
59 public: |
620
|
60 |
|
61 enum magic_colon { magic_colon_t }; |
|
62 |
|
63 // Constructors. It is possible to create the following types of |
|
64 // constants: |
|
65 // |
|
66 // constant type constructor arguments |
|
67 // ------------- --------------------- |
|
68 // unknown none |
|
69 // real scalar double |
|
70 // real matrix Matrix |
|
71 // DiagMatrix |
|
72 // RowVector |
|
73 // ColumnVector |
|
74 // complex scalar Complex |
|
75 // complex matrix ComplexMatrix |
|
76 // ComplexDiagMatrix |
|
77 // ComplexRowVector |
|
78 // ComplexColumnVector |
|
79 // string char* (null terminated) |
|
80 // range double, double, dobule |
|
81 // Range |
|
82 // magic colon tree_constant::magic_colon |
|
83 |
581
|
84 tree_constant (void) : tree_fvc () |
1
|
85 { rep = new tree_constant_rep (); rep->count = 1; } |
|
86 |
581
|
87 tree_constant (double d) : tree_fvc () |
1
|
88 { rep = new tree_constant_rep (d); rep->count = 1; } |
620
|
89 |
581
|
90 tree_constant (const Matrix& m) : tree_fvc () |
1
|
91 { rep = new tree_constant_rep (m); rep->count = 1; } |
620
|
92 |
581
|
93 tree_constant (const DiagMatrix& d) : tree_fvc () |
1
|
94 { rep = new tree_constant_rep (d); rep->count = 1; } |
620
|
95 |
581
|
96 tree_constant (const RowVector& v, int pcv = -1) : tree_fvc () |
1
|
97 { rep = new tree_constant_rep (v, pcv); rep->count = 1; } |
620
|
98 |
581
|
99 tree_constant (const ColumnVector& v, int pcv = -1) : tree_fvc () |
1
|
100 { rep = new tree_constant_rep (v, pcv); rep->count = 1; } |
|
101 |
581
|
102 tree_constant (const Complex& c) : tree_fvc () |
1
|
103 { rep = new tree_constant_rep (c); rep->count = 1; } |
620
|
104 |
581
|
105 tree_constant (const ComplexMatrix& m) : tree_fvc () |
1
|
106 { rep = new tree_constant_rep (m); rep->count = 1; } |
620
|
107 |
581
|
108 tree_constant (const ComplexDiagMatrix& d) : tree_fvc () |
1
|
109 { rep = new tree_constant_rep (d); rep->count = 1; } |
620
|
110 |
581
|
111 tree_constant (const ComplexRowVector& v, int pcv = -1) : tree_fvc () |
|
112 { rep = new tree_constant_rep (v, pcv); rep->count = 1; } |
620
|
113 |
581
|
114 tree_constant (const ComplexColumnVector& v, int pcv = -1) : tree_fvc () |
|
115 { rep = new tree_constant_rep (v, pcv); rep->count = 1; } |
1
|
116 |
581
|
117 tree_constant (const char *s) : tree_fvc () |
1
|
118 { rep = new tree_constant_rep (s); rep->count = 1; } |
|
119 |
581
|
120 tree_constant (double base, double limit, double inc) : tree_fvc () |
1
|
121 { rep = new tree_constant_rep (base, limit, inc); rep->count = 1; } |
620
|
122 |
581
|
123 tree_constant (const Range& r) : tree_fvc () |
1
|
124 { rep = new tree_constant_rep (r); rep->count = 1; } |
|
125 |
620
|
126 tree_constant (tree_constant::magic_colon t) : tree_fvc () |
|
127 { |
|
128 tree_constant_rep::constant_type tmp; |
|
129 tmp = tree_constant_rep::magic_colon; |
|
130 rep = new tree_constant_rep (tmp); |
|
131 rep->count = 1; |
|
132 } |
|
133 |
|
134 // Copy constructor. |
1
|
135 |
581
|
136 tree_constant (const tree_constant& a) : tree_fvc () |
1
|
137 { rep = a.rep; rep->count++; } |
|
138 |
620
|
139 // Delete the representation of this constant if the count drops to |
|
140 // zero. |
|
141 |
1
|
142 ~tree_constant (void); |
|
143 |
|
144 #if defined (MDEBUG) |
|
145 void *operator new (size_t size); |
|
146 void operator delete (void *p, size_t size); |
|
147 #endif |
|
148 |
620
|
149 // Simple assignment. |
|
150 |
529
|
151 tree_constant operator = (const tree_constant& a) |
1
|
152 { |
|
153 if (--rep->count <= 0 && rep != a.rep) |
|
154 delete rep; |
|
155 |
|
156 rep = a.rep; |
|
157 rep->count++; |
|
158 return *this; |
|
159 } |
|
160 |
620
|
161 // Indexed assignment. |
1
|
162 |
506
|
163 tree_constant assign (tree_constant& rhs, const Octave_object& args) |
1
|
164 { |
|
165 if (rep->count > 1) |
|
166 { |
|
167 --rep->count; |
|
168 rep = new tree_constant_rep (*rep); |
|
169 rep->count = 1; |
|
170 } |
506
|
171 rep->assign (rhs, args); |
1
|
172 return *this; |
|
173 } |
|
174 |
620
|
175 // Type. It would be nice to eliminate the need for this. |
|
176 |
|
177 int is_constant (void) const { return 1; } |
|
178 |
|
179 // Size. |
|
180 |
|
181 int rows (void) const { return rep->rows (); } |
|
182 int columns (void) const { return rep->columns (); } |
|
183 |
|
184 // Does this constant have a type? Both of these are provided since |
|
185 // it is sometimes more natural to write is_undefined() instead of |
|
186 // ! is_defined(). |
|
187 |
|
188 int is_defined (void) const { return rep->is_defined (); } |
|
189 int is_undefined (void) const { return rep->is_undefined (); } |
|
190 |
|
191 // What type is this constant? |
|
192 |
|
193 int is_unknown (void) const { return rep->is_unknown (); } |
|
194 int is_real_scalar (void) const { return rep->is_real_scalar (); } |
|
195 int is_real_matrix (void) const { return rep->is_real_matrix (); } |
|
196 int is_complex_scalar (void) const { return rep->is_complex_scalar (); } |
|
197 int is_complex_matrix (void) const { return rep->is_complex_matrix (); } |
|
198 int is_string (void) const { return rep->is_string (); } |
|
199 int is_range (void) const { return rep->is_range (); } |
|
200 int is_magic_colon (void) const { return rep->is_magic_colon (); } |
|
201 |
|
202 // Are any or all of the elements in this constant nonzero? |
|
203 |
|
204 tree_constant all (void) const { return rep->all (); } |
|
205 tree_constant any (void) const { return rep->any (); } |
|
206 |
|
207 int is_real_type (void) const { return rep->is_real_type (); } |
628
|
208 |
620
|
209 int is_complex_type (void) const { return rep->is_complex_type (); } |
|
210 |
636
|
211 // Would be nice to get rid of the next four functions: |
|
212 |
|
213 int is_scalar_type (void) const { return rep->is_scalar_type (); } |
|
214 int is_matrix_type (void) const { return rep->is_matrix_type (); } |
620
|
215 |
628
|
216 int is_numeric_type (void) const |
|
217 { return rep->is_numeric_type (); } |
620
|
218 |
|
219 int is_numeric_or_range_type (void) const |
|
220 { return rep->is_numeric_or_range_type (); } |
|
221 |
|
222 // Is this constant valid as a scalar index? |
|
223 |
|
224 int valid_as_scalar_index (void) const |
|
225 { return rep->valid_as_scalar_index (); } |
|
226 |
|
227 // Does this constant correspond to a truth value? |
|
228 |
|
229 int is_true (void) const { return rep->is_true (); } |
|
230 |
|
231 // Is at least one of the dimensions of this constant zero? |
|
232 |
|
233 int is_empty (void) const |
|
234 { |
|
235 return ((! (is_magic_colon () || is_unknown ())) |
|
236 && (rows () == 0 || columns () == 0)); |
|
237 } |
|
238 |
|
239 // Are the dimensions of this constant zero by zero? |
|
240 |
|
241 int is_zero_by_zero (void) const |
|
242 { |
|
243 return ((! (is_magic_colon () || is_unknown ())) |
|
244 && rows () == 0 && columns () == 0); |
|
245 } |
|
246 |
|
247 // Values. |
|
248 |
628
|
249 double double_value (int force_string_conversion = 0) const |
|
250 { return rep->double_value (force_string_conversion); } |
|
251 |
|
252 Matrix matrix_value (int force_string_conversion = 0) const |
|
253 { return rep->matrix_value (force_string_conversion); } |
|
254 |
|
255 Complex complex_value (int force_string_conversion = 0) const |
|
256 { return rep->complex_value (force_string_conversion); } |
|
257 |
|
258 ComplexMatrix complex_matrix_value (int force_string_conversion = 0) const |
|
259 { return rep->complex_matrix_value (force_string_conversion); } |
|
260 |
|
261 char *string_value (void) const |
|
262 { return rep->string_value (); } |
|
263 |
|
264 Range range_value (void) const |
|
265 { return rep->range_value (); } |
|
266 |
|
267 ColumnVector vector_value (int force_string_conversion = 0, |
|
268 int force_vector_conversion = 0) const |
|
269 { return rep->vector_value (); } |
|
270 |
|
271 ComplexColumnVector complex_vector_value (int force_string_conv = 0, |
|
272 int force_vec_conv = 0) const |
|
273 { return rep->complex_vector_value (); } |
1
|
274 |
620
|
275 // Conversions. These should probably be private. If a user of this |
|
276 // class wants a certain kind of constant, he should simply ask for |
|
277 // it, and we should convert it if possible. |
1
|
278 |
628
|
279 tree_constant convert_to_str (void) |
|
280 { return rep->convert_to_str (); } |
1
|
281 |
435
|
282 void convert_to_row_or_column_vector (void) |
455
|
283 { rep->convert_to_row_or_column_vector (); } |
435
|
284 |
620
|
285 // Increment or decrement this constant. |
1
|
286 |
578
|
287 void bump_value (tree_expression::type et) |
1
|
288 { |
|
289 if (rep->count > 1) |
|
290 { |
|
291 --rep->count; |
|
292 rep = new tree_constant_rep (*rep); |
|
293 rep->count = 1; |
|
294 } |
|
295 rep->bump_value (et); |
|
296 } |
|
297 |
620
|
298 // Evaluate this constant, possibly converting complex to real, or |
|
299 // matrix to scalar, etc. |
|
300 |
1
|
301 tree_constant eval (int print) |
495
|
302 { |
|
303 rep->maybe_mutate (); |
|
304 if (print) |
|
305 rep->print (); |
|
306 return *this; |
|
307 } |
1
|
308 |
506
|
309 Octave_object eval (int print, int nargout, const Octave_object& args) |
1
|
310 { |
565
|
311 Octave_object retval; |
495
|
312 |
506
|
313 // XXX FIXME XXX -- make it safe to call do_index() with |
|
314 // args.length () == 0 |
|
315 if (args.length () > 0) |
|
316 retval(0) = rep->do_index (args); |
495
|
317 else |
500
|
318 retval(0) = *this; |
495
|
319 |
500
|
320 if (retval(0).is_defined ()) |
|
321 retval(0).eval (print); |
1
|
322 return retval; |
|
323 } |
|
324 |
620
|
325 // Store the original text corresponding to this constant for later |
|
326 // pretty printing. |
|
327 |
|
328 void stash_original_text (char *s) |
|
329 { rep->stash_original_text (s); } |
|
330 |
|
331 // Pretty print this constant. |
|
332 |
581
|
333 void print_code (ostream& os); |
|
334 |
620
|
335 // Complain about unknown types used as args. |
|
336 |
|
337 friend void gripe_wrong_type_arg (const char *name, const tree_constant& tc); |
|
338 |
628
|
339 char *type_as_string (void) const |
|
340 { return rep->type_as_string (); } |
|
341 |
620
|
342 // ------------------------------------------------------------------- |
|
343 |
|
344 // These may not need to be member functions. |
|
345 |
|
346 tree_constant cumprod (void) const { return rep->cumprod (); } |
|
347 tree_constant cumsum (void) const { return rep->cumsum (); } |
|
348 tree_constant prod (void) const { return rep->prod (); } |
|
349 tree_constant sum (void) const { return rep->sum (); } |
|
350 tree_constant sumsq (void) const { return rep->sumsq (); } |
|
351 |
|
352 tree_constant diag (void) const { return rep->diag (); } |
|
353 tree_constant diag (const tree_constant& a) const { return rep->diag (a); } |
|
354 |
|
355 tree_constant mapper (Mapper_fcn& m_fcn, int print) const |
|
356 { return rep->mapper (m_fcn, print); } |
|
357 |
|
358 // ------------------------------------------------------------------- |
|
359 |
|
360 // We want to eliminate this, or at least make it private. |
|
361 |
|
362 tree_constant_rep::constant_type const_type (void) const |
|
363 { return rep->const_type (); } |
|
364 |
|
365 // ------------------------------------------------------------------- |
636
|
366 |
650
|
367 #if defined (__GNUG__) && __GNUC_MINOR__ > 5 |
636
|
368 private: |
650
|
369 #endif |
636
|
370 |
|
371 // Can we make these go away? |
|
372 |
|
373 // These need better names, since a range really is a numeric type. |
|
374 |
|
375 void force_numeric (int force_str_conv = 0) |
|
376 { rep->force_numeric (force_str_conv); } |
|
377 |
|
378 tree_constant make_numeric (int force_str_conv = 0) const |
|
379 { |
|
380 if (is_numeric_type ()) |
|
381 return *this; |
|
382 else |
|
383 return rep->make_numeric (force_str_conv); |
|
384 } |
|
385 |
|
386 #if 0 |
|
387 tree_constant make_numeric_or_range (void) const |
|
388 { |
|
389 if (is_numeric_type () || is_range ()) |
|
390 return *this; |
|
391 else |
|
392 return rep->make_numeric (); |
|
393 } |
|
394 #endif |
|
395 |
|
396 tree_constant make_numeric_or_magic (void) const |
|
397 { |
|
398 if (is_numeric_type () || is_magic_colon ()) |
|
399 return *this; |
|
400 else |
|
401 return rep->make_numeric (); |
|
402 } |
|
403 |
|
404 tree_constant make_numeric_or_range_or_magic (void) const |
|
405 { |
|
406 if (is_numeric_type () || is_range () || is_magic_colon ()) |
|
407 return *this; |
|
408 else |
|
409 return rep->make_numeric (); |
|
410 } |
1
|
411 }; |
|
412 |
529
|
413 // XXX FIXME XXX -- this is not used very much now. Perhaps it can be |
|
414 // eliminated. |
500
|
415 extern Octave_object vector_of_empties (int nargout, const char *fcn_name); |
94
|
416 |
1
|
417 #endif |
|
418 |
|
419 /* |
|
420 ;;; Local Variables: *** |
|
421 ;;; mode: C++ *** |
|
422 ;;; page-delimiter: "^/\\*" *** |
|
423 ;;; End: *** |
|
424 */ |