Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /// \file
  2. /// Definitions of DefaultYacasEnvironment and CYacas.
  3.  
  4. #ifndef __yacas_h__
  5. #define __yacas_h__
  6.  
  7. #include "yacasbase.h"
  8. #include "lispstring.h"
  9. #include "stringio.h"
  10. #include "tokenizer.h"
  11. #include "lisphash.h"
  12. #include "lispevalhash.h"
  13. #include "infixparser.h"
  14. #include "stdfileio.h"
  15. #include "lispatom.h"
  16. #include "lispeval.h"
  17. #include "mathenvironment.h"
  18. #include "lispglobals.h"
  19. #include "lisperror.h"
  20. #include "lispuserfunc.h"
  21.  
  22.  
  23. /// The default environment for a Yacas session.
  24. /// This class constructs a LispEnvironment (to be found in
  25. /// #iEnvironment), and defines the Yacas core functions. The core
  26. /// functions are listed in corefunctions.h . Examples of core
  27. /// functions are \c Head, \c Set and \c Eval.
  28.  
  29. class DefaultYacasEnvironment : public YacasBase
  30. {
  31. public:
  32.   DefaultYacasEnvironment();
  33.   DefaultYacasEnvironment(LispOutput* aOutput, LispInt aStackSize);
  34.   virtual ~DefaultYacasEnvironment();
  35.   LispEnvironment& getEnv() {return iEnvironment;}
  36.  
  37. private:
  38.   DefaultYacasEnvironment(const DefaultYacasEnvironment& aOther)
  39.     : output(NULL),hash(),printer(),coreCommands(),globals(),prefixoperators(),infixoperators(),postfixoperators(),bodiedoperators(),infixprinter(aOther.infixprinter),userFunctions(),
  40.       iEnvironment(coreCommands,userFunctions,
  41.                  globals,hash,output,infixprinter,
  42.                  prefixoperators,infixoperators,
  43.                  postfixoperators,bodiedoperators,&input,0),
  44.     input(aOther.input.Status())
  45.   {
  46.     // copy constructor not written yet, hence the assert
  47.     LISPASSERT(0);
  48.   }
  49.   DefaultYacasEnvironment& operator=(const DefaultYacasEnvironment& aOther)
  50.   {
  51.     // copy constructor not written yet, hence the assert
  52.     LISPASSERT(0);
  53.     return *this;
  54.   }
  55.  
  56. private:
  57.   LispOutput* output;
  58.   LispHashTable hash;
  59.   LispPrinter printer;
  60.  
  61.   YacasCoreCommands coreCommands;
  62.   LispGlobal globals;
  63.  
  64.   //Define the default operators.
  65.   LispOperators prefixoperators;
  66.   LispOperators infixoperators;
  67.   LispOperators postfixoperators;
  68.   LispOperators bodiedoperators;
  69.   InfixPrinter infixprinter;
  70.  
  71.   LispUserFunctions userFunctions;
  72.  
  73.   LispEnvironment iEnvironment;
  74.  
  75. public:
  76.   CachedStdUserInput input;
  77. };
  78.  
  79.  
  80. /// The Yacas engine.
  81. /// This is the only class that applications need to use. It can
  82. /// evaluate Yacas expressions. Every instance has its own Yacas
  83. /// environment, in which the expressions are evaluated.
  84.  
  85.  
  86.  
  87. class CYacas : public YacasBase
  88. {
  89. public:
  90.  
  91.     /// Pseudo-constructor.
  92.     /// \return A new instance of CYacas, with output connected to \p aOutput.
  93.     /// If aOutput is NULL, connect to a new instance of  StdUserOutput.
  94.     LISPIMPORT static CYacas* NewL(LispOutput* aOutput=0, LispInt aStackSize=50000);
  95.  
  96.     /// Destructor.
  97.     LISPIMPORT virtual ~CYacas();
  98.  
  99.     /// Return the underlying Yacas environment.
  100.     inline DefaultYacasEnvironment& getDefEnv() {return environment;}
  101.  
  102.     /// Evaluate a Yacas expression.
  103.     /// First, \p aExpression is parsed by an InfixParser. Then it is
  104.     /// evaluated in the underlying Lisp environment. Finally, the
  105.     /// result is printed to #iResultOutput via the pretty printer or,
  106.     /// if this is not defined, via an InfixPrinter.
  107.     virtual void Evaluate(const LispChar * aExpression);
  108.  
  109.     /// Return the result of the expression.
  110.     /// This is stored in #iResult.
  111.     virtual LispChar * Result();
  112.  
  113.     /// Return the error message produced by the last evaluation.
  114.     /// The error is retrieved from #environment.
  115.     virtual LispChar * Error();
  116.  
  117.     /// Whether an error occured during the last evaluation.
  118.     inline LispBoolean IsError();
  119.  
  120. private:
  121.  
  122.     /// Constructor.
  123.     /// The output of #environment is directed to \p aOutput.
  124.     CYacas(LispOutput* aOutput,LispInt aStackSize);
  125.  
  126. private:
  127.  
  128.     /// The underlying Yacas environment
  129.     DefaultYacasEnvironment environment;
  130.  
  131.     /// String containing the result of the last evaluation
  132.     LispString iResult;
  133.  
  134.     /// Stream pointing to #iResult.
  135.     StringOutput iResultOutput;
  136. };
  137.  
  138. inline LispBoolean CYacas::IsError()
  139. {
  140.     return (Error()[0] != '\0');
  141. }
  142.  
  143. #endif
  144.  
  145.  
  146.