home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / oct93 / graphics / graphtal.lha / Graphtal / Expression.h < prev    next >
C/C++ Source or Header  |  1992-11-17  |  3KB  |  110 lines

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