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