IterationFormula.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #ifndef MANDEL_ITERATIONFORMULA_H
  2. #define MANDEL_ITERATIONFORMULA_H
  3. #include <variant>
  4. #include <memory>
  5. #include <string>
  6. #include <stdexcept>
  7. namespace mnd
  8. {
  9. struct IterationFormula;
  10. struct Constant;
  11. struct Variable;
  12. struct UnaryOperation;
  13. struct BinaryOperation;
  14. struct Addition;
  15. struct Multiplication;
  16. struct Division;
  17. struct Pow;
  18. using Expression = std::variant<
  19. Constant,
  20. Variable,
  21. UnaryOperation,
  22. Addition,
  23. Multiplication,
  24. Division,
  25. Pow
  26. >;
  27. struct ParseError : std::runtime_error
  28. {
  29. ParseError(const std::string& err) :
  30. std::runtime_error(err.c_str())
  31. {}
  32. };
  33. Expression parse(const std::string& formula);
  34. std::string toString(const mnd::Expression&);
  35. }
  36. struct mnd::IterationFormula
  37. {
  38. std::unique_ptr<Expression> expr;
  39. IterationFormula(Expression expr);
  40. };
  41. struct mnd::Constant
  42. {
  43. double value;
  44. inline Constant(double value) :
  45. value{ value }
  46. {}
  47. inline Constant() :
  48. value{ 1010 }
  49. {}
  50. };
  51. struct mnd::Variable
  52. {
  53. std::string name;
  54. };
  55. struct mnd::UnaryOperation
  56. {
  57. std::unique_ptr<Expression> operand;
  58. /*inline UnaryOperation(const UnaryOperation& other) :
  59. operand{ std::make_unique<Expression>(*other.operand) }
  60. {}*/
  61. };
  62. struct mnd::BinaryOperation
  63. {
  64. std::unique_ptr<Expression> left;
  65. std::unique_ptr<Expression> right;
  66. /*inline BinaryOperation(const BinaryOperation& other) :
  67. left{ std::make_unique<Expression>(*other.left) },
  68. right{ std::make_unique<Expression>(*other.right) }
  69. {}*/
  70. };
  71. struct mnd::Addition : mnd::BinaryOperation
  72. {
  73. bool subtraction = false;
  74. };
  75. struct mnd::Multiplication : mnd::BinaryOperation
  76. {
  77. };
  78. struct mnd::Division : mnd::BinaryOperation
  79. {
  80. };
  81. struct mnd::Pow : mnd::BinaryOperation
  82. {
  83. };
  84. namespace mnd
  85. {
  86. }
  87. #endif // MANDEL_ITERATIONFORMULA_H