AbaKus – a complex calculator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.0KB

  1. /* This file was part of the SpeedCrunch project
  2. Copyright (C) 2004 Ariya Hidayat <ariya@kde.org>
  3. And is now part of abakus.
  4. Copyright (c) 2005 Michael Pyne <michael.pyne@kdemail.net>
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. */
  17. #ifndef ABAKUS_EVALUATOR_H
  18. #define ABAKUS_EVALUATOR_H
  19. #include <tqstring.h>
  20. #include <tqvaluevector.h>
  21. #include "numerictypes.h"
  22. class Token
  23. {
  24. public:
  25. typedef enum
  26. {
  27. Unknown,
  28. Number,
  29. Operator,
  30. Identifier
  31. } Type;
  32. typedef enum
  33. {
  34. InvalidOp = 0,
  35. Plus, // + (addition)
  36. Minus, // - (substraction, negation)
  37. Asterisk, // * (multiplication)
  38. Slash, // / (division)
  39. Caret, // ^ (power) or **.
  40. LeftPar, // (
  41. RightPar, // )
  42. Comma, // argument separator
  43. Percent,
  44. Equal // variable assignment
  45. } Op;
  46. Token( Type type = Unknown, const TQString& text = TQString(), int pos = -1 );
  47. Token( const Token& );
  48. Token& operator=( const Token& );
  49. Type type() const { return m_type; }
  50. TQString text() const { return m_text; }
  51. int pos() const { return m_pos; };
  52. bool isNumber() const { return m_type == Number; }
  53. bool isOperator() const { return m_type == Operator; }
  54. bool isIdentifier() const { return m_type == Identifier; }
  55. Abakus::number_t asNumber() const;
  56. Op asOperator() const;
  57. TQString description() const;
  58. static const Token null;
  59. protected:
  60. Type m_type;
  61. TQString m_text;
  62. int m_pos;
  63. };
  64. class Tokens: public TQValueVector<Token>
  65. {
  66. public:
  67. Tokens(): TQValueVector<Token>(), m_valid(true) {};
  68. bool valid() const { return m_valid; }
  69. void setValid( bool v ) { m_valid = v; }
  70. protected:
  71. bool m_valid;
  72. };
  73. class Variable
  74. {
  75. public:
  76. TQString name;
  77. Abakus::number_t value;
  78. };
  79. class Evaluator
  80. {
  81. public:
  82. Evaluator();
  83. ~Evaluator();
  84. void setExpression( const TQString& expr );
  85. TQString expression() const;
  86. void clear();
  87. bool isValid() const;
  88. Tokens tokens() const;
  89. static Tokens scan( const TQString& expr );
  90. TQString error() const;
  91. // Abakus::number_t eval();
  92. static TQString autoFix( const TQString& expr );
  93. private:
  94. Evaluator( const Evaluator& );
  95. Evaluator& operator=( const Evaluator& );
  96. };
  97. #endif // EVALUATOR
  98. // vim: set et ts=8 sw=4: