[493c477] | 1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
[2dd068] | 2 | |
---|
[16f511] | 3 | #ifdef HAVE_CONFIG_H |
---|
[e4fe2b] | 4 | #include "config.h" |
---|
[16f511] | 5 | #endif /* HAVE_CONFIG_H */ |
---|
[9d7aaa] | 6 | |
---|
[2dd068] | 7 | #include <string.h> |
---|
| 8 | #include <stdlib.h> |
---|
| 9 | |
---|
[650f2d8] | 10 | #include "cf_assert.h" |
---|
[9d7aaa] | 11 | |
---|
[2dd068] | 12 | #include "cf_defs.h" |
---|
| 13 | #include "parseutil.h" |
---|
| 14 | |
---|
| 15 | class PUtilBase |
---|
| 16 | { |
---|
| 17 | public: |
---|
| 18 | PUtilBase() {}; |
---|
| 19 | virtual ~PUtilBase() {} |
---|
| 20 | virtual PUtilBase * copy() const = 0; |
---|
| 21 | virtual CanonicalForm getval() const = 0; |
---|
| 22 | virtual int getintval() const = 0; |
---|
| 23 | virtual bool isInt() const = 0; |
---|
| 24 | virtual bool isCF() const = 0; |
---|
| 25 | virtual bool isVar() const = 0; |
---|
| 26 | }; |
---|
| 27 | |
---|
| 28 | class PUtilInt : public PUtilBase |
---|
| 29 | { |
---|
| 30 | private: |
---|
| 31 | int val; |
---|
| 32 | public: |
---|
| 33 | PUtilInt() { val = 0; } |
---|
| 34 | PUtilInt( int i ) { val = i; } |
---|
| 35 | ~PUtilInt() {} |
---|
| 36 | PUtilBase * copy() const { return new PUtilInt( val ); } |
---|
| 37 | CanonicalForm getval() const { return CanonicalForm( val ); } |
---|
| 38 | int getintval() const { return val; } |
---|
| 39 | bool isInt() const { return true; } |
---|
| 40 | bool isCF() const { return false; } |
---|
| 41 | bool isVar() const { return false; } |
---|
| 42 | }; |
---|
| 43 | |
---|
| 44 | class PUtilCF : public PUtilBase |
---|
| 45 | { |
---|
| 46 | private: |
---|
| 47 | CanonicalForm val; |
---|
| 48 | public: |
---|
| 49 | PUtilCF() { val = 0; } |
---|
| 50 | PUtilCF( const CanonicalForm & cf ) { val = cf; } |
---|
| 51 | ~PUtilCF() {} |
---|
| 52 | PUtilBase * copy() const { return new PUtilCF( val ); } |
---|
| 53 | CanonicalForm getval() const { return val; } |
---|
| 54 | int getintval() const { return val.intval(); } |
---|
| 55 | bool isInt() const { return false; } |
---|
| 56 | bool isCF() const { return true; } |
---|
| 57 | bool isVar() const { return false; } |
---|
| 58 | }; |
---|
| 59 | |
---|
| 60 | class PUtilVar : public PUtilBase |
---|
| 61 | { |
---|
| 62 | private: |
---|
| 63 | Variable val; |
---|
| 64 | public: |
---|
| 65 | PUtilVar() { val = Variable(); } |
---|
| 66 | PUtilVar( const Variable & v ) { val = v; } |
---|
| 67 | ~PUtilVar() {} |
---|
| 68 | PUtilBase * copy() const { return new PUtilVar( val ); } |
---|
| 69 | CanonicalForm getval() const { return CanonicalForm( val ); } |
---|
| 70 | int getintval() const { return 0; } |
---|
| 71 | bool isInt() const { return false; } |
---|
| 72 | bool isCF() const { return false; } |
---|
| 73 | bool isVar() const { return true; } |
---|
| 74 | }; |
---|
| 75 | |
---|
| 76 | class PUtilFactory |
---|
| 77 | { |
---|
| 78 | public: |
---|
| 79 | static PUtilBase * create( ) { return new PUtilInt( 0 ); } |
---|
| 80 | static PUtilBase * create( int val ) { return new PUtilInt( val ); } |
---|
| 81 | static PUtilBase * create( const CanonicalForm & cf ) { return new PUtilCF( cf ); } |
---|
| 82 | static PUtilBase * create( const Variable & v ) { return new PUtilVar( v ); } |
---|
| 83 | static PUtilBase * create( const char * str ) |
---|
| 84 | { |
---|
| 85 | if ( strlen( str ) < 9 ) |
---|
| 86 | return new PUtilInt( atoi( str ) ); |
---|
| 87 | else |
---|
| 88 | return new PUtilCF( CanonicalForm( str ) ); |
---|
| 89 | } |
---|
| 90 | }; |
---|
| 91 | |
---|
| 92 | ParseUtil::ParseUtil() |
---|
| 93 | { |
---|
| 94 | value = PUtilFactory::create(); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | ParseUtil::ParseUtil( const ParseUtil &pu ) |
---|
| 98 | { |
---|
| 99 | value = pu.value->copy(); |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | ParseUtil::ParseUtil( const CanonicalForm &f ) |
---|
| 103 | { |
---|
| 104 | value = PUtilFactory::create( f ); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | ParseUtil::ParseUtil( int i ) |
---|
| 108 | { |
---|
| 109 | value = PUtilFactory::create( i ); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | ParseUtil::ParseUtil( char * str ) |
---|
| 113 | { |
---|
| 114 | value = PUtilFactory::create( str ); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | ParseUtil::~ParseUtil() |
---|
| 118 | { |
---|
| 119 | delete value; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | ParseUtil& ParseUtil::operator= ( const ParseUtil &pu ) |
---|
| 123 | { |
---|
| 124 | if ( this != &pu ) { |
---|
| 125 | delete value; |
---|
| 126 | value = pu.value->copy(); |
---|
| 127 | } |
---|
| 128 | return *this; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | ParseUtil& ParseUtil::operator= ( const CanonicalForm &f ) |
---|
| 132 | { |
---|
| 133 | delete value; |
---|
| 134 | value = PUtilFactory::create( f ); |
---|
| 135 | return *this; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | ParseUtil& ParseUtil::operator= ( int i ) |
---|
| 139 | { |
---|
| 140 | delete value; |
---|
| 141 | value = PUtilFactory::create( i ); |
---|
| 142 | return *this; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | ParseUtil& ParseUtil::operator= ( const Variable & v ) |
---|
| 146 | { |
---|
| 147 | delete value; |
---|
| 148 | value = PUtilFactory::create( v ); |
---|
| 149 | return *this; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | CanonicalForm ParseUtil::getval() |
---|
| 153 | { |
---|
| 154 | return value->getval(); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | int ParseUtil::getintval() |
---|
| 158 | { |
---|
| 159 | return value->getintval(); |
---|
| 160 | } |
---|