529
|
1 // tree-const.h -*- C++ -*- |
1
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1992, 1993, 1994, 1995 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 } |
1004
|
173 |
506
|
174 rep->assign (rhs, args); |
1004
|
175 |
1
|
176 return *this; |
|
177 } |
|
178 |
747
|
179 // Simple structure assignment. |
|
180 |
782
|
181 tree_constant assign_map_element (SLList<char*>& list, |
|
182 tree_constant& rhs); |
747
|
183 |
|
184 // Indexed structure assignment. |
|
185 |
782
|
186 tree_constant assign_map_element (SLList<char*>& list, |
|
187 tree_constant& rhs, |
747
|
188 const Octave_object& args); |
|
189 |
620
|
190 // Type. It would be nice to eliminate the need for this. |
|
191 |
|
192 int is_constant (void) const { return 1; } |
|
193 |
|
194 // Size. |
|
195 |
|
196 int rows (void) const { return rep->rows (); } |
|
197 int columns (void) const { return rep->columns (); } |
|
198 |
|
199 // Does this constant have a type? Both of these are provided since |
|
200 // it is sometimes more natural to write is_undefined() instead of |
|
201 // ! is_defined(). |
|
202 |
|
203 int is_defined (void) const { return rep->is_defined (); } |
|
204 int is_undefined (void) const { return rep->is_undefined (); } |
|
205 |
|
206 // What type is this constant? |
|
207 |
|
208 int is_unknown (void) const { return rep->is_unknown (); } |
|
209 int is_real_scalar (void) const { return rep->is_real_scalar (); } |
|
210 int is_real_matrix (void) const { return rep->is_real_matrix (); } |
|
211 int is_complex_scalar (void) const { return rep->is_complex_scalar (); } |
|
212 int is_complex_matrix (void) const { return rep->is_complex_matrix (); } |
|
213 int is_string (void) const { return rep->is_string (); } |
|
214 int is_range (void) const { return rep->is_range (); } |
747
|
215 int is_map (void) const { return rep->is_map (); } |
620
|
216 int is_magic_colon (void) const { return rep->is_magic_colon (); } |
922
|
217 int is_all_va_args (void) const { return rep->is_all_va_args (); } |
620
|
218 |
|
219 // Are any or all of the elements in this constant nonzero? |
|
220 |
|
221 tree_constant all (void) const { return rep->all (); } |
|
222 tree_constant any (void) const { return rep->any (); } |
|
223 |
|
224 int is_real_type (void) const { return rep->is_real_type (); } |
628
|
225 |
620
|
226 int is_complex_type (void) const { return rep->is_complex_type (); } |
|
227 |
636
|
228 // Would be nice to get rid of the next four functions: |
|
229 |
|
230 int is_scalar_type (void) const { return rep->is_scalar_type (); } |
|
231 int is_matrix_type (void) const { return rep->is_matrix_type (); } |
620
|
232 |
628
|
233 int is_numeric_type (void) const |
|
234 { return rep->is_numeric_type (); } |
620
|
235 |
|
236 int is_numeric_or_range_type (void) const |
|
237 { return rep->is_numeric_or_range_type (); } |
|
238 |
|
239 // Is this constant valid as a scalar index? |
|
240 |
|
241 int valid_as_scalar_index (void) const |
|
242 { return rep->valid_as_scalar_index (); } |
|
243 |
1041
|
244 // Is this constant valid as a zero scalar index? |
|
245 |
|
246 int valid_as_zero_index (void) const |
|
247 { return rep->valid_as_zero_index (); } |
|
248 |
620
|
249 // Does this constant correspond to a truth value? |
|
250 |
|
251 int is_true (void) const { return rep->is_true (); } |
|
252 |
|
253 // Is at least one of the dimensions of this constant zero? |
|
254 |
|
255 int is_empty (void) const |
|
256 { |
922
|
257 return ((! (is_magic_colon () || is_all_va_args () || is_unknown ())) |
620
|
258 && (rows () == 0 || columns () == 0)); |
|
259 } |
|
260 |
|
261 // Are the dimensions of this constant zero by zero? |
|
262 |
|
263 int is_zero_by_zero (void) const |
|
264 { |
922
|
265 return ((! (is_magic_colon () || is_all_va_args () || is_unknown ())) |
620
|
266 && rows () == 0 && columns () == 0); |
|
267 } |
|
268 |
|
269 // Values. |
|
270 |
628
|
271 double double_value (int force_string_conversion = 0) const |
|
272 { return rep->double_value (force_string_conversion); } |
|
273 |
|
274 Matrix matrix_value (int force_string_conversion = 0) const |
|
275 { return rep->matrix_value (force_string_conversion); } |
|
276 |
|
277 Complex complex_value (int force_string_conversion = 0) const |
|
278 { return rep->complex_value (force_string_conversion); } |
|
279 |
|
280 ComplexMatrix complex_matrix_value (int force_string_conversion = 0) const |
|
281 { return rep->complex_matrix_value (force_string_conversion); } |
|
282 |
|
283 char *string_value (void) const |
|
284 { return rep->string_value (); } |
|
285 |
|
286 Range range_value (void) const |
|
287 { return rep->range_value (); } |
|
288 |
747
|
289 Octave_map map_value (void) const; |
|
290 |
|
291 tree_constant lookup_map_element (SLList<char*>& list); |
|
292 |
628
|
293 ColumnVector vector_value (int force_string_conversion = 0, |
|
294 int force_vector_conversion = 0) const |
|
295 { return rep->vector_value (); } |
|
296 |
|
297 ComplexColumnVector complex_vector_value (int force_string_conv = 0, |
|
298 int force_vec_conv = 0) const |
|
299 { return rep->complex_vector_value (); } |
1
|
300 |
620
|
301 // Conversions. These should probably be private. If a user of this |
|
302 // class wants a certain kind of constant, he should simply ask for |
|
303 // it, and we should convert it if possible. |
1
|
304 |
628
|
305 tree_constant convert_to_str (void) |
|
306 { return rep->convert_to_str (); } |
1
|
307 |
435
|
308 void convert_to_row_or_column_vector (void) |
455
|
309 { rep->convert_to_row_or_column_vector (); } |
435
|
310 |
620
|
311 // Increment or decrement this constant. |
1
|
312 |
578
|
313 void bump_value (tree_expression::type et) |
1
|
314 { |
|
315 if (rep->count > 1) |
|
316 { |
|
317 --rep->count; |
|
318 rep = new tree_constant_rep (*rep); |
|
319 rep->count = 1; |
|
320 } |
1004
|
321 |
1
|
322 rep->bump_value (et); |
|
323 } |
|
324 |
620
|
325 // Evaluate this constant, possibly converting complex to real, or |
|
326 // matrix to scalar, etc. |
|
327 |
1
|
328 tree_constant eval (int print) |
495
|
329 { |
1004
|
330 if (! is_scalar_type ()) |
|
331 rep->maybe_mutate (); |
|
332 |
495
|
333 if (print) |
|
334 rep->print (); |
1004
|
335 |
495
|
336 return *this; |
|
337 } |
1
|
338 |
506
|
339 Octave_object eval (int print, int nargout, const Octave_object& args) |
1
|
340 { |
565
|
341 Octave_object retval; |
495
|
342 |
506
|
343 // XXX FIXME XXX -- make it safe to call do_index() with |
|
344 // args.length () == 0 |
1004
|
345 |
506
|
346 if (args.length () > 0) |
|
347 retval(0) = rep->do_index (args); |
495
|
348 else |
500
|
349 retval(0) = *this; |
495
|
350 |
500
|
351 if (retval(0).is_defined ()) |
|
352 retval(0).eval (print); |
1004
|
353 |
1
|
354 return retval; |
|
355 } |
|
356 |
620
|
357 // Store the original text corresponding to this constant for later |
|
358 // pretty printing. |
|
359 |
|
360 void stash_original_text (char *s) |
|
361 { rep->stash_original_text (s); } |
|
362 |
|
363 // Pretty print this constant. |
|
364 |
581
|
365 void print_code (ostream& os); |
|
366 |
628
|
367 char *type_as_string (void) const |
|
368 { return rep->type_as_string (); } |
|
369 |
747
|
370 // We really do need this, and it should be private: |
|
371 |
|
372 private: |
|
373 |
|
374 void make_unique (void); |
|
375 |
|
376 tree_constant_rep *make_unique_map (void); |
|
377 |
|
378 public: |
|
379 |
620
|
380 // ------------------------------------------------------------------- |
|
381 |
|
382 // We want to eliminate this, or at least make it private. |
|
383 |
|
384 tree_constant_rep::constant_type const_type (void) const |
|
385 { return rep->const_type (); } |
|
386 |
636
|
387 private: |
|
388 |
|
389 // Can we make these go away? |
|
390 |
|
391 // These need better names, since a range really is a numeric type. |
|
392 |
|
393 void force_numeric (int force_str_conv = 0) |
|
394 { rep->force_numeric (force_str_conv); } |
|
395 |
|
396 tree_constant make_numeric (int force_str_conv = 0) const |
|
397 { |
|
398 if (is_numeric_type ()) |
|
399 return *this; |
|
400 else |
|
401 return rep->make_numeric (force_str_conv); |
|
402 } |
|
403 |
|
404 #if 0 |
|
405 tree_constant make_numeric_or_range (void) const |
|
406 { |
|
407 if (is_numeric_type () || is_range ()) |
|
408 return *this; |
|
409 else |
|
410 return rep->make_numeric (); |
|
411 } |
|
412 #endif |
|
413 |
|
414 tree_constant make_numeric_or_magic (void) const |
|
415 { |
922
|
416 if (is_numeric_type () || is_all_va_args () || is_magic_colon ()) |
636
|
417 return *this; |
|
418 else |
|
419 return rep->make_numeric (); |
|
420 } |
|
421 |
|
422 tree_constant make_numeric_or_range_or_magic (void) const |
|
423 { |
922
|
424 if (is_numeric_type () || is_range () || is_all_va_args () |
|
425 || is_magic_colon ()) |
636
|
426 return *this; |
|
427 else |
|
428 return rep->make_numeric (); |
|
429 } |
1
|
430 }; |
|
431 |
529
|
432 // XXX FIXME XXX -- this is not used very much now. Perhaps it can be |
|
433 // eliminated. |
500
|
434 extern Octave_object vector_of_empties (int nargout, const char *fcn_name); |
94
|
435 |
1
|
436 #endif |
|
437 |
|
438 /* |
|
439 ;;; Local Variables: *** |
|
440 ;;; mode: C++ *** |
|
441 ;;; page-delimiter: "^/\\*" *** |
|
442 ;;; End: *** |
|
443 */ |