Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /** \file infixparser.h
  2.  *  parsing and printing in the infix style.
  3.  *
  4.  */
  5.  
  6.  
  7. #ifndef __infixparser_h__
  8. #define __infixparser_h__
  9.  
  10. #include "yacasbase.h"
  11. #include "lispparser.h"
  12.  
  13.  
  14. #define KMaxPrecedence 60000
  15.  
  16. class LispInFixOperator : public YacasBase
  17. {
  18. public:
  19.     inline LispInFixOperator(LispInt aPrecedence)
  20.         : iPrecedence(aPrecedence),
  21.         iLeftPrecedence(aPrecedence),
  22.         iRightPrecedence(aPrecedence),
  23.         iRightAssociative(0)
  24.     {};
  25.     inline void SetRightAssociative(void)
  26.     {
  27.         iRightAssociative = 1;
  28.     }
  29.     inline void SetLeftPrecedence(LispInt aPrecedence)
  30.     {
  31.         iLeftPrecedence = aPrecedence;
  32.     }
  33.     inline void SetRightPrecedence(LispInt aPrecedence)
  34.     {
  35.         iRightPrecedence = aPrecedence;
  36.     }
  37. public:
  38.     LispInt iPrecedence;
  39.     LispInt iLeftPrecedence;
  40.     LispInt iRightPrecedence;
  41.     LispInt iRightAssociative;
  42. };
  43.  
  44. class LispOperators : public LispAssociatedHash<LispInFixOperator>
  45. {
  46. public:
  47.     void SetOperator(LispInt aPrecedence,LispString * aString);
  48.     void SetRightAssociative(LispString * aString);
  49.     void SetLeftPrecedence(LispString * aString,LispInt aPrecedence);
  50.     void SetRightPrecedence(LispString * aString,LispInt aPrecedence);
  51. };
  52.  
  53. class InfixParser : public LispParser
  54. {
  55. public:
  56.     InfixParser(LispTokenizer& aTokenizer, LispInput& aInput,
  57.                 LispEnvironment& aEnvironment,
  58.                 LispOperators& aPrefixOperators,
  59.                 LispOperators& aInfixOperators,
  60.                 LispOperators& aPostfixOperators,
  61.                 LispOperators& aBodiedOperators);
  62.     ~InfixParser();
  63.  
  64.     virtual void Parse(LispPtr& aResult);
  65. //private:
  66.     void ParseCont(LispPtr& aResult);
  67. public:
  68.     LispOperators& iPrefixOperators;
  69.     LispOperators& iInfixOperators;
  70.     LispOperators& iPostfixOperators;
  71.     LispOperators& iBodiedOperators;
  72.  
  73. //    LispEnvironment* iEnvironment;
  74. };
  75.  
  76. class ParsedObject : public YacasBase
  77. {
  78. public:
  79.     ParsedObject(InfixParser& aParser)
  80.         : iParser(aParser),
  81.           iError(LispFalse),
  82.           iEndOfFile(LispFalse),
  83.           iLookAhead(NULL),iResult()  {};
  84.     void Parse();
  85. private:
  86.     void ReadToken();
  87.     void MatchToken(LispString * aToken);
  88.     void ReadExpression(LispInt depth);
  89.     void ReadAtom();
  90. private:
  91.     void GetOtherSide(LispInt aNrArgsToCombine, LispInt depth);
  92.     void Combine(LispInt aNrArgsToCombine);
  93.     void InsertAtom(LispString * aString);
  94. private:
  95.     void Fail(); // called when parsing fails, raising an exception
  96.  
  97. private:
  98.     InfixParser& iParser;
  99. private:
  100.     LispBoolean iError;
  101.     LispBoolean iEndOfFile;
  102.     LispString * iLookAhead;
  103. public:
  104.     LispPtr iResult;
  105. };
  106.  
  107.  
  108. class InfixPrinter : public LispPrinter
  109. {
  110. public:
  111.     InfixPrinter(LispOperators& aPrefixOperators,
  112.                  LispOperators& aInfixOperators,
  113.                  LispOperators& aPostfixOperators,
  114.                  LispOperators& aBodiedOperators)
  115.         : iPrefixOperators(aPrefixOperators),
  116.           iInfixOperators(aInfixOperators),
  117.           iPostfixOperators(aPostfixOperators),
  118.           iBodiedOperators(aBodiedOperators),
  119.           iPrevLastChar(0),iCurrentEnvironment(NULL){}
  120.  
  121.     virtual void Print(LispPtr& aExpression, LispOutput& aOutput,
  122.                        LispEnvironment& aEnvironment);
  123.   virtual void RememberLastChar(LispChar aChar);
  124. private:
  125.     void Print(LispPtr& aExpression, LispOutput& aOutput,
  126.                LispInt iPrecedence);
  127.     void WriteToken(LispOutput& aOutput,LispChar * aString);
  128. private:
  129.     LispOperators& iPrefixOperators;
  130.     LispOperators& iInfixOperators;
  131.     LispOperators& iPostfixOperators;
  132.     LispOperators& iBodiedOperators;
  133.     LispChar iPrevLastChar;
  134.     LispEnvironment* iCurrentEnvironment;
  135. };
  136.  
  137.  
  138. #endif
  139.  
  140.