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