Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /** \file lispeval.h
  2.  *  Evaluation of expressions.
  3.  *
  4.  */
  5.  
  6. #ifndef __lispeval_h__
  7. #define __lispeval_h__
  8.  
  9. #include "yacasbase.h"
  10. #include "lispobject.h"
  11. #include "lispenvironment.h"
  12.  
  13. /*
  14. void InternalEval(LispEnvironment& aEnvironment, LispPtr& aResult, LispPtr& aExpression);
  15. */
  16.  
  17.  
  18. class UserStackInformation : public YacasBase
  19. {
  20. public:
  21.     UserStackInformation()
  22.       : iOperator(),iExpression(),iRulePrecedence(-1),iSide(0)
  23. #ifdef YACAS_DEBUG
  24.       , iFileName("(no file)"),iLine(0)
  25. #endif // YACAS_DEBUG
  26.     {
  27.     }
  28.     LispPtr iOperator;
  29.     LispPtr iExpression;
  30.     LispInt iRulePrecedence;
  31.     LispInt iSide; // 0=pattern, 1=body
  32.     DBG_( LispChar * iFileName; )
  33.     DBG_( LispInt iLine; )
  34. };
  35.  
  36. /// Abstract evaluator for Lisp expressions.
  37. /// Eval() is a pure virtual function, to be provided by the derived class.
  38. /// The other functions are stubs.
  39.  
  40. class LispEvaluatorBase : public YacasBase
  41. {
  42. public:
  43.   LispEvaluatorBase() : iBasicInfo() {}
  44.   virtual ~LispEvaluatorBase();
  45.   virtual void Eval(LispEnvironment& aEnvironment, LispPtr& aResult, LispPtr& aExpression)=0;
  46.   virtual void ResetStack();
  47.   virtual UserStackInformation& StackInformation();
  48.   virtual void ShowStack(LispEnvironment& aEnvironment, LispOutput& aOutput);
  49. private:
  50.   UserStackInformation iBasicInfo;
  51. };
  52.  
  53. /// The basic evaluator for Lisp expressions.
  54.  
  55. class BasicEvaluator : public LispEvaluatorBase
  56. {
  57. public:
  58.   /// Evaluate a Lisp expression
  59.   /// \param aEnvironment the Lisp environment, in which the
  60.   /// evaluation should take place.
  61.   /// \param aResult the result of the evaluation.
  62.   /// \param aExpression the expression to evaluate.
  63.   ///
  64.   /// First, the evaluation depth is checked. An error is raised if
  65.   /// the maximum evaluation depth is exceeded.
  66.   ///
  67.   /// The next step is the actual evaluation. \a aExpression is a
  68.   /// LispObject, so we can distinguish three cases.
  69.   ///   - If \a aExpression is a string starting with \c " , it is
  70.   ///     simply copied in \a aResult. If it starts with another
  71.   ///     character (this includes the case where it represents a
  72.   ///     number), the environment is checked to see whether a
  73.   ///     variable with this name exists. If it does, its value is
  74.   ///     copied in \a aResult, otherwise \a aExpression is copied.
  75.   ///   - If \a aExpression is a list, the head of the list is
  76.   ///     examined. If the head is not a string. InternalApplyPure()
  77.   ///     is called. If the head is a string, it is checked against
  78.   ///     the core commands; if there is a check, the corresponding
  79.   ///     evaluator is called. Then it is checked agaist the list of
  80.   ///     user function with GetUserFunction() . Again, the
  81.   ///     corresponding evaluator is called if there is a check. If
  82.   ///     all fails, ReturnUnEvaluated() is called.
  83.   ///   - Otherwise (ie. if \a aExpression is a generic object), it is
  84.   ///     copied in \a aResult.
  85.   ///
  86.   /// \note The result of this operation must be a unique (copied)
  87.   /// element! Eg. its Next might be set...
  88.   ///
  89.   /// The LispPtr it can be stored in to is passed in as argument, so it
  90.   /// does not need to be constructed by the calling environment.
  91.   virtual void Eval(LispEnvironment& aEnvironment, LispPtr& aResult, LispPtr& aExpression);
  92.   virtual ~BasicEvaluator();
  93. };
  94.  
  95. class TracedEvaluator : public BasicEvaluator
  96. {
  97. public:
  98.   TracedEvaluator() :  BasicEvaluator(),errorStr(),errorOutput(errorStr){}
  99.   virtual void Eval(LispEnvironment& aEnvironment, LispPtr& aResult, LispPtr& aExpression);
  100. protected:
  101.   LispString errorStr;
  102.   StringOutput errorOutput;
  103. };
  104.  
  105.  
  106. class TracedStackEvaluator : public BasicEvaluator
  107. {
  108. public:
  109.   TracedStackEvaluator() : objs() {}
  110.   virtual ~TracedStackEvaluator();
  111.   virtual void Eval(LispEnvironment& aEnvironment, LispPtr& aResult, LispPtr& aExpression);
  112.   virtual void ResetStack();
  113.   virtual UserStackInformation& StackInformation();
  114.   virtual void ShowStack(LispEnvironment& aEnvironment, LispOutput& aOutput);
  115. private:
  116.   void PushFrame();
  117.   void PopFrame();
  118. private:
  119.   CArrayGrower<UserStackInformation*, ArrOpsCustomPtr<UserStackInformation> > objs;
  120. };
  121.  
  122.  
  123.  
  124. /* GetUserFunction : get user function, possibly loading the required
  125.    files to read in the function definition */
  126. LispUserFunction* GetUserFunction(LispEnvironment& aEnvironment,
  127.                                   LispPtr* subList);
  128.  
  129.  
  130. /* Tracing functions */
  131. void TraceShowEnter(LispEnvironment& aEnvironment,
  132.                     LispPtr& aExpression);
  133. void TraceShowLeave(LispEnvironment& aEnvironment, LispPtr& aResult,
  134.                     LispPtr& aExpression);
  135. void TraceShowArg(LispEnvironment& aEnvironment,LispPtr& aParam,
  136.                   LispPtr& aValue);
  137.  
  138.  
  139. void ShowExpression(LispString& outString, LispEnvironment& aEnvironment,
  140.                     LispPtr& aExpression);
  141.  
  142.  
  143. class YacasDebuggerBase : public YacasBase
  144. {
  145. public:
  146.   virtual ~YacasDebuggerBase();
  147.   virtual void Start() = 0;
  148.   virtual void Finish() = 0;
  149.   virtual void Enter(LispEnvironment& aEnvironment,
  150.                      LispPtr& aExpression) = 0;
  151.   virtual void Leave(LispEnvironment& aEnvironment, LispPtr& aResult,
  152.                      LispPtr& aExpression) = 0;
  153.   virtual void Error(LispEnvironment& aEnvironment) = 0;
  154.   virtual LispBoolean Stopped() = 0;
  155. };
  156.  
  157. class DefaultDebugger : public YacasDebuggerBase
  158. {
  159. public:
  160.   inline DefaultDebugger(LispPtr& aEnter, LispPtr& aLeave, LispPtr& aError)
  161.     : iEnter(aEnter), iLeave(aLeave), iError(aError), iTopExpr(),iTopResult(),iStopped(LispFalse),defaultEval() {};
  162.   virtual void Start();
  163.   virtual void Finish();
  164.   virtual void Enter(LispEnvironment& aEnvironment,
  165.                       LispPtr& aExpression);
  166.   virtual void Leave(LispEnvironment& aEnvironment, LispPtr& aResult,
  167.                       LispPtr& aExpression);
  168.   virtual void Error(LispEnvironment& aEnvironment);
  169.   virtual LispBoolean Stopped();
  170.   LispPtr iEnter;
  171.   LispPtr iLeave;
  172.   LispPtr iError;
  173.   LispPtr iTopExpr;
  174.   LispPtr iTopResult;
  175.   LispBoolean iStopped;
  176. protected:
  177.   BasicEvaluator defaultEval;
  178. };
  179.  
  180.  
  181. #endif
  182.  
  183.  
  184.  
  185.