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