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.

124 lines
3.0KB

  1. #ifndef ABAKUS_FUNCTION_H
  2. #define ABAKUS_FUNCTION_H
  3. /*
  4. * function.h - part of abakus
  5. * Copyright (C) 2004, 2005 Michael Pyne <michael.pyne@kdemail.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "numerictypes.h"
  22. #include <tqobject.h>
  23. #include <tqstringlist.h>
  24. #include <tqstring.h>
  25. #include <tqmap.h>
  26. #include <tqdict.h>
  27. class BaseFunction;
  28. struct UserFunction
  29. {
  30. int sequenceNumber;
  31. BaseFunction *fn;
  32. TQString varName;
  33. };
  34. // Ugly pointer-to-member typedef ahead
  35. typedef Abakus::number_t (Abakus::number_t::*function_t)() const;
  36. struct Function {
  37. TQString name;
  38. TQString description;
  39. // A function is either builtin or user defined, this union is
  40. // used for both cases.
  41. union {
  42. function_t fn; // Builtin.
  43. UserFunction *userFn; // User defined
  44. };
  45. bool returnsTrig;
  46. bool needsTrig;
  47. bool userDefined;
  48. };
  49. void setTrigMode(Abakus::TrigMode mode);
  50. Abakus::TrigMode trigMode();
  51. class FunctionManager : public TQObject
  52. {
  53. Q_OBJECT
  54. public:
  55. typedef TQDict<Function> functionDict;
  56. static FunctionManager *instance();
  57. Function *function(const TQString &name);
  58. bool isFunction(const TQString &name);
  59. bool isFunctionUserDefined(const TQString &name);
  60. bool addFunction(BaseFunction *fn, const TQString &dependantVar);
  61. bool addFunction(const TQString &name, function_t fn, const TQString &desc);
  62. void removeFunction(const TQString &name);
  63. typedef enum { Builtin, UserDefined, All } FunctionType;
  64. TQStringList functionList(FunctionType type);
  65. signals:
  66. void signalFunctionAdded(const TQString &name);
  67. void signalFunctionRemoved(const TQString &name);
  68. private:
  69. FunctionManager(TQObject *parent = 0, const char *name = "function manager");
  70. static FunctionManager *m_manager;
  71. functionDict m_dict;
  72. };
  73. Abakus::number_t evaluateFunction(const Function *func, const Abakus::number_t value);
  74. // Implemented in lexer.l due to prototype issues.
  75. Abakus::number_t parseString(const char *str);
  76. // Implemented in lexer.l due to prototype issues.
  77. class Lexer
  78. {
  79. public:
  80. Lexer(const TQString &expr);
  81. ~Lexer();
  82. bool hasNext() const;
  83. int nextType();
  84. int tokenPos() const;
  85. // Can call this after nextType to find the associated string value of the
  86. // token.
  87. TQString tokenValue() const;
  88. private:
  89. class Private;
  90. Private *m_private;
  91. };
  92. #endif