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