home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Graphics 16,000
/
graphics-16000.iso
/
msdos
/
utils
/
graphtal.lzh
/
Graphtal.Amiga
/
Expression.h
< prev
next >
Wrap
C/C++ Source or Header
|
1992-11-17
|
3KB
|
110 lines
/*
* Expression.h - class definitions for arithmetic expression handling.
*
* Copyright (C) 1992, Christoph Streit (streit@iam.unibe.ch)
* University of Berne, Switzerland
* All rights reserved.
*
* This software may be freely copied, modified, and redistributed
* provided that this copyright notice is preserved on all copies.
*
* You may not distribute this software, in whole or in part, as part of
* any commercial product without the express consent of the authors.
*
* There is no warranty or other guarantee of fitness of this software
* for any purpose. It is provided solely "as is".
*
*/
#ifndef Expression_H
# define Expression_H
#include <iostream.h>
#include "list.h"
#include "ValueStack.h"
//___________________________________________________________ ExprItem
//
// An expression is a list of ExprItem's, where an ExprItem is a
// constant value, a variable, a function or an operator.
class ExprItem
{
public:
ExprItem();
virtual ~ExprItem();
// if the ExprItem is a
// - value, variable: just push it's current value on the stack
// - function, operator: process by poping the arguments from the stack
// and push back the result
virtual void process(ValueStack*)=0;
// do the same as process, but construct a string on the stack.
virtual void print(ValueStack*)=0;
// how many parameters to simplify; if < 0, no simplification is
// possible
virtual int params()=0;
// make a copy of myself.
virtual ExprItem* copy()=0;
};
typedef ExprItem* ExprItemPtr;
declareList(ExprItemList, ExprItemPtr);
//___________________________________________________________ ValueItem
//
// A ValueItem stores a constant number or string value.
class ValueItem : public ExprItem
{
public:
ValueItem(const Value&);
ValueItem(Value*);
~ValueItem();
void process(ValueStack*);
void print(ValueStack*);
int params();
ExprItem* copy();
private:
Value v;
};
//___________________________________________________________ Expression
//
// An expression is a list of ExprItem's. The expression is stored in
// postfix order with the arguments to binary operators stored in wrong
// order:
// e.g. 2 / 3 is stored as / 3 2
// This simplifies the processing of the different ExprItem's.
class Expression
{
public:
Expression(ExprItem*);
Expression(const Expression&);
Expression(ExprItem*, Expression*);
Expression(ExprItem*, Expression*, Expression*);
Expression(ExprItem*, Expression*, Expression*, Expression*);
~Expression();
Value& evaluate();
Expression* simplify();
friend ostream& operator<<(ostream&, const Expression&);
private:
Expression();
ExprItemList* theExpression;
ValueStack* theStack;
};
typedef Expression* ExpressionPtr;
#endif // Expression_H