// This may look like C code, but it is really -*- C++ -*-
// Evaluateur d'expression C   -   R. Ansari 03/2004
// LAL (Orsay) / IN2P3-CNRS  DAPNIA/SPP (Saclay) / CEA

#ifndef CEXPREVAL_SEEN
#define CEXPREVAL_SEEN

#include "machdefs.h"
#include <iostream>
#include <string>
#include "pexceptions.h"

using namespace std;
namespace SOPHYA {

/*! 
  \ingroup SysTools
  \brief Exception class used by  CExpressionEvaluator
*/
class CExprException : public PException {
  public:
  explicit CExprException(const char * m) : PException(m) {}
  explicit CExprException(const string& m) : PException(m) {}
};

//--------------------------------------------------------------------
/*! 
  \ingroup SysTools
  \brief Base class for arithmetic expressions used by CExpressionEvaluator
  \sa CExpressionEvaluator
*/
class CExprBase {
public:
  explicit CExprBase(); 
  virtual ~CExprBase();
  virtual double Evaluate() const = 0;
  virtual bool   CheckE(string& errmsg) const { return true; }
  virtual void   CheckThrow(const char * msg) const;
  inline  void   CheckThrow(string const & msg) const { CheckThrow(msg.c_str()); }
  virtual void   Print(ostream& os) const = 0;
  static long    NbCreate() { return totnexp_create; }
  static long    NbDelete() { return totnexp_delete; }
protected:
  static long totnexp_create;
  static long totnexp_delete;
};

/*! 
  \ingroup SysTools
  \brief For formatted write (print) of expressions in a stream
*/
inline ostream& operator << (ostream& s, CExprBase const & ex)
  {  ex.Print(s);  return(s);  }

class CE_BinExp;
class CE_FuncExp;
//---------------------------------------------------------
class CExpressionEvaluator : public CExprBase {
public:
  CExpressionEvaluator(string const & sex);
  virtual ~CExpressionEvaluator();
//! Evaluate the expression and returns the corresponding value.
  virtual double Evaluate() const;
//! Alias for Evaluate()
  inline double Value() const { return Evaluate(); }
//! Formatted output on a stream
  virtual void  Print(ostream& os) const;

protected:
//! Does the parsing and builds an CExprBase object.
  CExprBase* ParseString(int extype, string fname, string const & sex, 
		    size_t off, size_t& stop, string& errmsg);

  CExprBase * _exp;
};

}   // End of namespace SOPHYA

/* end of ifdef CEXPREVAL_SEEN */
#endif
