home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / graphtal / expressi.h < prev    next >
C/C++ Source or Header  |  1992-10-27  |  3KB  |  109 lines

  1. /*
  2.  * Expression.h - class definitions for arithmetic expression handling.
  3.  *
  4.  * Copyright (C) 1992, Christoph Streit (streit@iam.unibe.ch)
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  */
  17.  
  18. #ifndef Expression_H
  19. # define Expression_H
  20.  
  21. #include <iostream.h>
  22. #include "list.h"
  23. #include "ValueStack.h"
  24.  
  25. //___________________________________________________________ ExprItem
  26. //
  27. // An expression is a list of ExprItem's, where an ExprItem is a
  28. //  constant value, a variable, a function or an operator.
  29.  
  30. class ExprItem
  31. {
  32. public:
  33.   ExprItem();
  34.   virtual ~ExprItem();
  35.  
  36.   // if the ExprItem is a
  37.   //  - value, variable:    just push it's current value on the stack
  38.   //  - function, operator: process by poping the arguments from the stack
  39.   //                        and push back the result
  40.   virtual void process(ValueStack*)=0;
  41.  
  42.   // do the same as process, but construct a string on the stack.
  43.   virtual void print(ValueStack*)=0;
  44.  
  45.   // how many parameters to simplify; if < 0, no simplification is
  46.   // possible
  47.   virtual int params()=0;  
  48.  
  49.   // make a copy of myself.
  50.   virtual ExprItem* copy()=0;
  51. };
  52.  
  53. typedef ExprItem* ExprItemPtr;
  54. declareList(ExprItemList, ExprItemPtr);
  55.  
  56. //___________________________________________________________ ValueItem
  57. //
  58. // A ValueItem stores a constant number or string value.
  59.  
  60. class ValueItem : public ExprItem
  61. {
  62. public:
  63.   ValueItem(const Value&);
  64.   ValueItem(Value*);
  65.   ~ValueItem();
  66.   
  67.   void process(ValueStack*);
  68.   void print(ValueStack*);
  69.   int params();
  70.   ExprItem* copy();
  71.  
  72. private:
  73.   Value v;
  74. };
  75.  
  76. //___________________________________________________________ Expression
  77. //
  78. // An expression is a list of ExprItem's. The expression is stored in
  79. //  postfix order with the arguments to binary operators stored in wrong
  80. //  order:
  81. //    e.g. 2 / 3 is stored as / 3 2
  82. //  This simplifies the processing of the different ExprItem's.
  83.  
  84. class Expression
  85. {
  86. public:
  87.   Expression(ExprItem*);
  88.   Expression(const Expression&);
  89.   Expression(ExprItem*, Expression*);
  90.   Expression(ExprItem*, Expression*, Expression*);
  91.   Expression(ExprItem*, Expression*, Expression*, Expression*);
  92.  
  93.   ~Expression();
  94.   
  95.   Value& evaluate();
  96.   Expression* simplify();
  97.   friend ostream& operator<<(ostream&, const Expression&);
  98.  
  99. private: 
  100.   Expression();
  101.  
  102.   ExprItemList* theExpression;
  103.   ValueStack* theStack;
  104. };
  105.  
  106. typedef Expression* ExpressionPtr;
  107.  
  108. #endif // Expression_H
  109.