IterationFormula.h 1.9 KB

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