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 } |
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 |
|
244 // Does this constant correspond to a truth value? |
|
245 |
|
246 int is_true (void) const { return rep->is_true (); } |
|
247 |
|
248 // Is at least one of the dimensions of this constant zero? |
|
249 |
|
250 int is_empty (void) const |
|
251 { |
922
|
252 return ((! (is_magic_colon () || is_all_va_args () || is_unknown ())) |
620
|
253 && (rows () == 0 || columns () == 0)); |
|
254 } |
|
255 |
|
256 // Are the dimensions of this constant zero by zero? |
|
257 |
|
258 int is_zero_by_zero (void) const |
|
259 { |
922
|
260 return ((! (is_magic_colon () || is_all_va_args () || is_unknown ())) |
620
|
261 && rows () == 0 && columns () == 0); |
|
262 } |
|
263 |
|
264 // Values. |
|
265 |
628
|
266 double double_value (int force_string_conversion = 0) const |
|
267 { return rep->double_value (force_string_conversion); } |
|
268 |
|
269 Matrix matrix_value (int force_string_conversion = 0) const |
|
270 { return rep->matrix_value (force_string_conversion); } |
|
271 |
|
272 Complex complex_value (int force_string_conversion = 0) const |
|
273 { return rep->complex_value (force_string_conversion); } |
|
274 |
|
275 ComplexMatrix complex_matrix_value (int force_string_conversion = 0) const |
|
276 { return rep->complex_matrix_value (force_string_conversion); } |
|
277 |
|
278 char *string_value (void) const |
|
279 { return rep->string_value (); } |
|
280 |
|
281 Range range_value (void) const |
|
282 { return rep->range_value (); } |
|
283 |
747
|
284 Octave_map map_value (void) const; |
|
285 |
|
286 tree_constant lookup_map_element (SLList<char*>& list); |
|
287 |
628
|
288 ColumnVector vector_value (int force_string_conversion = 0, |
|
289 int force_vector_conversion = 0) const |
|
290 { return rep->vector_value (); } |
|
291 |
|
292 ComplexColumnVector complex_vector_value (int force_string_conv = 0, |
|
293 int force_vec_conv = 0) const |
|
294 { return rep->complex_vector_value (); } |
1
|
295 |
620
|
296 // Conversions. These should probably be private. If a user of this |
|
297 // class wants a certain kind of constant, he should simply ask for |
|
298 // it, and we should convert it if possible. |
1
|
299 |
628
|
300 tree_constant convert_to_str (void) |
|
301 { return rep->convert_to_str (); } |
1
|
302 |
435
|
303 void convert_to_row_or_column_vector (void) |
455
|
304 { rep->convert_to_row_or_column_vector (); } |
435
|
305 |
620
|
306 // Increment or decrement this constant. |
1
|
307 |
578
|
308 void bump_value (tree_expression::type et) |
1
|
309 { |
|
310 if (rep->count > 1) |
|
311 { |
|
312 --rep->count; |
|
313 rep = new tree_constant_rep (*rep); |
|
314 rep->count = 1; |
|
315 } |
1004
|
316 |
1
|
317 rep->bump_value (et); |
|
318 } |
|
319 |
620
|
320 // Evaluate this constant, possibly converting complex to real, or |
|
321 // matrix to scalar, etc. |
|
322 |
1
|
323 tree_constant eval (int print) |
495
|
324 { |
1004
|
325 if (! is_scalar_type ()) |
|
326 rep->maybe_mutate (); |
|
327 |
495
|
328 if (print) |
|
329 rep->print (); |
1004
|
330 |
495
|
331 return *this; |
|
332 } |
1
|
333 |
506
|
334 Octave_object eval (int print, int nargout, const Octave_object& args) |
1
|
335 { |
565
|
336 Octave_object retval; |
495
|
337 |
506
|
338 // XXX FIXME XXX -- make it safe to call do_index() with |
|
339 // args.length () == 0 |
1004
|
340 |
506
|
341 if (args.length () > 0) |
|
342 retval(0) = rep->do_index (args); |
495
|
343 else |
500
|
344 retval(0) = *this; |
495
|
345 |
500
|
346 if (retval(0).is_defined ()) |
|
347 retval(0).eval (print); |
1004
|
348 |
1
|
349 return retval; |
|
350 } |
|
351 |
620
|
352 // Store the original text corresponding to this constant for later |
|
353 // pretty printing. |
|
354 |
|
355 void stash_original_text (char *s) |
|
356 { rep->stash_original_text (s); } |
|
357 |
|
358 // Pretty print this constant. |
|
359 |
581
|
360 void print_code (ostream& os); |
|
361 |
628
|
362 char *type_as_string (void) const |
|
363 { return rep->type_as_string (); } |
|
364 |
747
|
365 // We really do need this, and it should be private: |
|
366 |
|
367 private: |
|
368 |
|
369 void make_unique (void); |
|
370 |
|
371 tree_constant_rep *make_unique_map (void); |
|
372 |
|
373 public: |
|
374 |
620
|
375 // ------------------------------------------------------------------- |
|
376 |
|
377 // We want to eliminate this, or at least make it private. |
|
378 |
|
379 tree_constant_rep::constant_type const_type (void) const |
|
380 { return rep->const_type (); } |
|
381 |
636
|
382 private: |
|
383 |
|
384 // Can we make these go away? |
|
385 |
|
386 // These need better names, since a range really is a numeric type. |
|
387 |
|
388 void force_numeric (int force_str_conv = 0) |
|
389 { rep->force_numeric (force_str_conv); } |
|
390 |
|
391 tree_constant make_numeric (int force_str_conv = 0) const |
|
392 { |
|
393 if (is_numeric_type ()) |
|
394 return *this; |
|
395 else |
|
396 return rep->make_numeric (force_str_conv); |
|
397 } |
|
398 |
|
399 #if 0 |
|
400 tree_constant make_numeric_or_range (void) const |
|
401 { |
|
402 if (is_numeric_type () || is_range ()) |
|
403 return *this; |
|
404 else |
|
405 return rep->make_numeric (); |
|
406 } |
|
407 #endif |
|
408 |
|
409 tree_constant make_numeric_or_magic (void) const |
|
410 { |
922
|
411 if (is_numeric_type () || is_all_va_args () || is_magic_colon ()) |
636
|
412 return *this; |
|
413 else |
|
414 return rep->make_numeric (); |
|
415 } |
|
416 |
|
417 tree_constant make_numeric_or_range_or_magic (void) const |
|
418 { |
922
|
419 if (is_numeric_type () || is_range () || is_all_va_args () |
|
420 || is_magic_colon ()) |
636
|
421 return *this; |
|
422 else |
|
423 return rep->make_numeric (); |
|
424 } |
1
|
425 }; |
|
426 |
529
|
427 // XXX FIXME XXX -- this is not used very much now. Perhaps it can be |
|
428 // eliminated. |
500
|
429 extern Octave_object vector_of_empties (int nargout, const char *fcn_name); |
94
|
430 |
1
|
431 #endif |
|
432 |
|
433 /* |
|
434 ;;; Local Variables: *** |
|
435 ;;; mode: C++ *** |
|
436 ;;; page-delimiter: "^/\\*" *** |
|
437 ;;; End: *** |
|
438 */ |