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