Subversion Repositories Kolibri OS

Rev

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

  1.  
  2.  
  3. #ifndef __lispuserfunc_h__
  4. #define __lispuserfunc_h__
  5.  
  6. #include "yacasbase.h"
  7. #include "lispobject.h"
  8. #include "lispenvironment.h"
  9. #include "lisphash.h"
  10. #include "grower.h"
  11. #include "evalfunc.h"
  12.  
  13. /// Abstract class providing the basic user function API.
  14. /// Instances of this class are associated to the name of the function
  15. /// via an associated hash table. When obtained, they can be used to
  16. /// evaluate the function with some arguments.
  17.  
  18. class LispUserFunction : public EvalFuncBase
  19. {
  20. public:
  21.     LispUserFunction() : iFenced(LispTrue),iTraced(LispFalse) {};
  22.     virtual ~LispUserFunction();
  23.     virtual void Evaluate(LispPtr& aResult,LispEnvironment& aEnvironment,
  24.                   LispPtr& aArguments)=0;
  25.     virtual void HoldArgument(LispString * aVariable) = 0;
  26.     virtual void DeclareRule(LispInt aPrecedence, LispPtr& aPredicate,
  27.                              LispPtr& aBody) = 0;
  28.     virtual void DeclareRule(LispInt aPrecedence, LispPtr& aBody) = 0;
  29.     virtual void DeclarePattern(LispInt aPrecedence, LispPtr& aPredicate,
  30.                              LispPtr& aBody) = 0;
  31.     virtual LispPtr& ArgList() = 0;
  32.  
  33. public: //unfencing
  34.     inline void UnFence() {iFenced = LispFalse;};
  35.     inline LispBoolean Fenced() {return iFenced;};
  36. public: //tracing
  37.     inline void Trace() {iTraced = LispTrue;};
  38.     inline void UnTrace() {iTraced = LispFalse;};
  39.     inline LispBoolean Traced() {return iTraced;};
  40. private:
  41.     LispBoolean iFenced;
  42.     LispBoolean iTraced;
  43. };
  44.  
  45.  
  46. /// User function with a specific arity.
  47. /// This is still an abstract class, but the arity (number of
  48. /// arguments) of the function is now fixed.
  49.  
  50. class LispArityUserFunction : public LispUserFunction
  51. {
  52. public:
  53.     virtual LispInt Arity() const = 0;
  54.     virtual LispInt IsArity(LispInt aArity) const = 0;
  55. };
  56.  
  57.  
  58. class LispDefFile;
  59.  
  60.  
  61. /// Set of LispArityUserFunction's.
  62. /// By using this class, you can associate multiple functions (with
  63. /// different arities) to one name. A specific LispArityUserFunction
  64. /// can be selected by providing its name. Additionally, the name of
  65. /// the file in which the function is defined, can be specified.
  66.  
  67. class LispMultiUserFunction : public YacasBase
  68. {
  69. public:
  70.   /// Constructor.
  71.   LispMultiUserFunction() : iFunctions(),iFileToOpen(NULL) {};
  72.  
  73.   /** When adding a multi-user function to the association hash table, the copy constructor is used.
  74.    *  We should at least make sure that iFunctions is empty, so there is no copying needed (for efficiency).
  75.    *  Casually having a copy-constructor on CDeletingArrayGrower should be avoided, to make sure it is
  76.    *  not used accidentally.
  77.    */
  78.   inline LispMultiUserFunction(const LispMultiUserFunction& aOther) : iFunctions(), iFileToOpen(NULL)
  79.   {
  80.     LISPASSERT(aOther.iFileToOpen == 0);
  81.     LISPASSERT(aOther.iFunctions.Size() == 0);
  82.   }
  83.   inline LispMultiUserFunction& operator=(const LispMultiUserFunction& aOther)
  84.   {
  85.     // copy constructor not written yet, hence the assert
  86.     LISPASSERT(aOther.iFileToOpen == 0);
  87.     LISPASSERT(aOther.iFunctions.Size() == 0);
  88.  
  89.     LISPASSERT(iFileToOpen == 0);
  90.     LISPASSERT(iFunctions.Size() == 0);
  91.     return *this;
  92.   }
  93.  
  94.   /// Return user function with given arity.
  95.   LispUserFunction* UserFunc(LispInt aArity);
  96.  
  97.   /// Destructor.
  98.   virtual ~LispMultiUserFunction();
  99.  
  100.   /// Specify that some argument should be held.
  101.   virtual void HoldArgument(LispString * aVariable);
  102.  
  103.   /// Add another LispArityUserFunction to #iFunctions.
  104.   virtual void DefineRuleBase(LispArityUserFunction* aNewFunction);
  105.  
  106.   /// Delete tuser function with given arity.
  107.   virtual void DeleteBase(LispInt aArity);
  108.  
  109. private:
  110.   /// Set of LispArityUserFunction's provided by this LispMultiUserFunction.
  111.   CDeletingArrayGrower<LispArityUserFunction*, ArrOpsDeletingPtr<LispArityUserFunction> > iFunctions;
  112.  
  113. public:
  114.   /// File to read for the definition of this function.
  115.   LispDefFile* iFileToOpen;
  116. };
  117.  
  118.  
  119. /// Associated hash of LispMultiUserFunction objects.
  120.  
  121. class LispUserFunctions : public LispAssociatedHash<LispMultiUserFunction>
  122. {
  123. };
  124.  
  125.  
  126. #endif
  127.  
  128.