Guest User

Untitled

a guest
Aug 15th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. enum TypeKind
  2. {
  3.     INT,
  4.     DOUBLE,
  5.     BOOL,
  6.     STRING
  7. };
  8.  
  9. class Type
  10. {
  11.     TypeKind type;
  12. protected:
  13.     Type(TypeKind t)
  14.         : type(t)
  15.     {
  16.     }
  17. public:
  18.     TypeKind getType(void) { return type; }
  19.     virtual ~Type(void) { }
  20.     template<typename T> T getValue(void) { }
  21. };
  22.  
  23. class Integer : public Type
  24. {
  25.     int value;
  26. public:
  27.     Integer(int v)
  28.         : Type(INT), value(v)
  29.     {
  30.     }
  31.     int getValue()
  32.     {
  33.         return value;
  34.     }
  35. };
  36.  
  37. // klasy siÄ™ dorobi...
  38.  
  39. class Expression
  40. {
  41. public:
  42.     virtual Type* eval(void) = 0;
  43.     virtual ~Expression(void) { }
  44. };
  45.  
  46. class AddOp : public Expression
  47. {
  48.     Expression *left, *right;
  49. public:
  50.     //...
  51.     virtual Type *eval(void)
  52.     {
  53.         Type *l = left->eval(), *r = right->eval();
  54.         if(l->getType() == INT)
  55.         {
  56.             return Integer(dynamic_cast<Integer*>(l)->getValue() + dynamic_cast<Integer*>(r)->getValue());
  57.         }
  58.         //...
  59.     }
  60. };
Advertisement
Add Comment
Please, Sign In to add comment