/* Collection of code snippets by Arne Vajhøj */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
#ifndef EXPR_H
#define EXPR_H

#define BOOL int
#define PI 3.14159265358979323846
#define EE 2.7182818284590452354
#define EQUAL(s1,s2) (strcmp(s1,s2)==0)
#define EM_GII "Garbage in input"
#define EM_ND "Not defined"
#define EM_EXPR_ACTP "Already converted to postfix"
#define EM_EXPR_NCTP "Not converted to postfix"
#define EM_EXPR_CND "Can not derive"
#define EM_EXPR_NSV "No such variable"
#define EM_UF "Unknown function %s"
#define EM_SE "Stack error"
#define EM_NIY "Not implemented yet"

#include "sym.h"
#include "list.h"

enum token_type { CONSTANT=0,VARIABLE=1,
                  UNARYMINUS=2,
                  PLUS=3,MINUS=4,MULTIPLY=5,DIVIDE=6,REMAINDER=7,POWER=8,
                  OR=9,AND=10,NOT=11,
                  FUNCTION=12,
                  LEFTBRACKET=13,RIGHTBRACKET=14,COMMA=15 };

//**********************************************************************
//
//  Class definitions:
//     ExprSymbolTable : expression symbol table
//     Expression      : expression of tokens
//     FloatExpression : floating point expression
//     IntExpression   : integer expression
//     BoolExpression  : boolean expression
//     ListExpression  : list expression
//
//**********************************************************************

struct ExprSymbol {
   char *s;
};

struct ExprValue {
   union {
      double floatval;
      long int intval;
      BOOL boolval;
   };
};

class ExprSymbolTable : public SymbolTable {
   private:
   public:
      ExprSymbolTable(int size);
      ~ExprSymbolTable();
      virtual UINT HashValue(void *p);
      virtual int Length(void *p);
      virtual void Copy(void *p1,void *p2);
      virtual BOOL Compare(void *p1,void *p2);
      virtual int ValLength(void *valp);
      virtual void ValCopy(void *valp1,void *valp2);
      void InsertSymbol(char *s,double *val);
      void InsertSymbol(char *s,long int *val);
      void InsertSymbol(char *s,BOOL *val);
      BOOL FindSymbol(char *s,double *val);
      BOOL FindSymbol(char *s,long int *val);
      BOOL FindSymbol(char *s,BOOL *val);
   };

void ExprDefineVariable(char *name,double value);
void ExprDefineVariable(char *name,long int value);
void ExprDefineVariable(char *name,BOOL value);
void ExprClearSymbols();

struct Token { enum token_type Typ;
               char *Val; };

// expression of tokens
class Expression {
   protected:
      // description
      BOOL BoolConst;
      BOOL ConvPostFix;
      // stack of tokens
      class Stack *Tokens;
      // push token
      void Push(enum token_type t,char *v);
      void Push(enum token_type t,double v);
      void Push(enum token_type t,long int v);
      void Push(enum token_type t,BOOL v);
      // pop token
      void Pop(enum token_type *t = NULL,char *v = NULL);
      // more tokens
      inline BOOL More() { return Tokens->MoreElm(); };
      // top token
      inline struct Token *GetTop() { return (struct Token *)(Tokens->GetTop()); };
      // save token
      void SaveToken(char *tok);
   public:
      // constructor
      Expression(char *s,BOOL bc);
      Expression(const class Expression& e);
      void operator=(const class Expression& e);
      // desctructor
      ~Expression();
      // get string
      char *String();
      // convert from infix to postfix
      friend class Expression infix2postfix(class Expression e);
      // convert from postfix to infix
      friend class Expression postfix2infix(class Expression e);
      // append
      friend void append(class Expression *e1,class Expression *e2);
   };

// floating point expression
class FloatExpression : public Expression {
   private:
      // simplify on top
      virtual BOOL TopSimplify();
   public:
      // constructor
      FloatExpression(char *s);
      FloatExpression(const class Expression& e);
      // desctructor
      ~FloatExpression();
      // get value
      double Value();
      // simplify
      class FloatExpression Simplify();
      // derive
      class FloatExpression Derive(char *var);
   };

// integer expression
class IntExpression : public Expression {
   private:
      // simplify on top
      virtual BOOL TopSimplify();
   public:
      // constructor
      IntExpression(char *s);
      IntExpression(const class Expression& e);
      // desctructor
      ~IntExpression();
      // get value
      long int Value();
      // simplify
      class IntExpression Simplify();
   };

// boolean expression
class BoolExpression : public Expression {
   private:
      // simplify on top
      virtual BOOL TopSimplify();
   public:
      // constructor
      BoolExpression(char *s);
      BoolExpression(const class Expression& e);
      // desctructor
      ~BoolExpression();
      // get value
      BOOL Value();
      // simplify
      class BoolExpression Simplify();
   };

// list expression
class ListExpression : public Expression {
   private:
      // simplify on top
      virtual BOOL TopSimplify();
   public:
      // constructor
      ListExpression(char *s);
      ListExpression(const class Expression& e);
      // desctructor
      ~ListExpression();
      // get value
      long int *Value(long int *(*GetList)(char *name));
      // simplify
      class ListExpression Simplify();
   };

#endif // EXPR_H
