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