Nicolas Winkler 5 éve
szülő
commit
3978f20b15

+ 6 - 2
Almond.cpp

@@ -11,8 +11,9 @@ mnd::MandelGenerator* generateTest()
 {
     mnd::Variable z{ "z" };
     mnd::Variable c{ "c" };
-    mnd::Pow m{ std::make_unique<mnd::Expression>(z), std::make_unique<mnd::Expression>(mnd::Constant{ 2.01 }) };
-    //mnd::Multiplication m2{ std::make_unique<mnd::Expression>(std::move(m)), std::make_unique<mnd::Expression>(z) };
+    mnd::Pow m{ {std::make_unique<mnd::Expression>(z), std::make_unique<mnd::Expression>(z)} };
+    //mnd::Multiplication m{ std::make_unique<mnd::Expression>(z), std::make_unique<mnd::Expression>(z) };
+    //mnd::Addition a1{ std::make_unique<mnd::Expression>(std::move(m)), std::make_unique<mnd::Expression>(c) };
     mnd::Addition a{ std::make_unique<mnd::Expression>(std::move(m)), std::make_unique<mnd::Expression>(c) };
 
     mnd::IterationFormula itf{ std::make_unique<mnd::Expression>(std::move(a)) };
@@ -25,6 +26,8 @@ Almond::Almond(QWidget* parent) :
 {
     ui.setupUi(this);
 
+    auto form = mnd::parse("123");
+    ::exit(0);
 
     mw = std::make_unique<MandelWidget>(mandelContext,
                                         &mandelContext.getDefaultGenerator(),
@@ -39,6 +42,7 @@ Almond::Almond(QWidget* parent) :
     mandelViewSave = mw->getViewport();
 
     mw->setGenerator(generateTest());
+    mw->setMaxIterations(200);
 
     QObject::connect(mw.get(), &MandelWidget::pointSelected, this, &Almond::pointSelected);
     ui.mainContainer->addWidget(mw.get());

+ 1 - 0
libmandel/CMakeLists.txt

@@ -37,6 +37,7 @@ SET(MandelSources
     src/OpenClCode.cpp
     src/JuliaGenerators.cpp
     src/IterationGenerator.cpp
+    src/IterationFormula.cpp
 )
 FILE(GLOB MandelHeaders include/*.h)
 

+ 5 - 1
libmandel/include/IterationFormula.h

@@ -3,6 +3,7 @@
 
 #include <variant>
 #include <memory>
+#include <string>
 
 namespace mnd
 {
@@ -27,7 +28,7 @@ namespace mnd
             Pow
     >;
 
-    class FormulaVisitor;
+    std::unique_ptr<Expression> parse(const std::string& formula);
 }
 
 
@@ -40,6 +41,9 @@ struct mnd::IterationFormula
 struct mnd::Constant
 {
     double value;
+    inline Constant(double value) :
+        value{ value }
+    {}
 };
 
 

+ 104 - 1
libmandel/src/IterationFormula.cpp

@@ -1,9 +1,112 @@
 #include "IterationFormula.h"
-
+#include <boost/spirit/include/qi.hpp>
+#include <boost/phoenix/object/construct.hpp>
+#include <iostream>
+#include <iomanip>
 
 using namespace mnd;
+using namespace boost::spirit;
+namespace phx = boost::phoenix;
+
+template <typename Parser, typename Skipper, typename ... Args>
+void PhraseParseOrDie(
+    const std::string& input, const Parser& p, const Skipper& s,
+    Args&& ... args)
+{
+    std::string::const_iterator begin = input.begin(), end = input.end();
+    boost::spirit::qi::phrase_parse(
+        begin, end, p, s, std::forward<Args>(args) ...);
+    if (begin != end) {
+        std::cout << "Unparseable: "
+            << std::quoted(std::string(begin, end)) << std::endl;
+        throw std::runtime_error("Parse error");
+    }
+}
+
+int simple(int a) {
+    printf("simple: %d\n", a);
+    return a;
+}
+
+int dopp(boost::fusion::vector<int, int> x) {
+    int a = boost::fusion::at_c<0>(x);
+    int b = boost::fusion::at_c<1>(x);
+    printf("dopp: %d + %d\n", a, b);
+    return a + b;
+}
+
+class ArithmeticGrammar4 : public qi::grammar<
+    // the string iterator to parse: can also be const char* or templated.
+    std::string::const_iterator,
+    // return value of the grammar, written in function syntax!
+    int(),
+    // the _type_ of the skip parser
+    ascii::space_type>
+{
+public:
+    using Iterator = std::string::const_iterator;
+
+    ArithmeticGrammar4() : ArithmeticGrammar4::base_type(start)
+    {
+        using qi::_val;
+        using qi::_1;
+        using qi::_2;
+
+        start = qi::int_[_val = _1];
+
+        /*start = (product >> '+' >> start)[&dopp]
+            |
+            //product[&simple];
+            qi::int_[_val = _1];
+        product = (factor >> '*' >> product) [&dopp]
+            |
+            factor[&simple];
+        factor = //qi::int_[&simple]
+            //|
+            group[&simple];
+        group = ('(' >> start >> ')')[&simple];*/
+    }
 
+    // as before, mirrors the template arguments of qi::grammar.
+    qi::rule<Iterator, int(), ascii::space_type> start, group, product, factor;
+};
 
+/*
+struct calculator : qi::grammar<std::string::const_iterator, int(), ascii::space_type>
+{
+    using Iterator = std::string::const_iterator;
+    qi::rule<Iterator, int(), ascii::space_type>
+        expression, addition;
+    calculator() : calculator::base_type(expression)
+    {
+        expression = (addition)[qi::_val = 5];
+        addition = (qi::alpha >> *qi::alnum)[qi::_val = 2];
+    }
+};
+*/
 
+namespace mnd
+{
+    std::unique_ptr<Expression> parse(const std::string& formula)
+    {
+        //mnd::Expression ret = mnd::Constant{ 1.0 };
+        int ret = 0;
+        //FormulaParser<std::string::const_iterator> grammar;
+        ArithmeticGrammar4 c;
+        auto begin = formula.begin();
+        auto end = formula.end();
+        bool r = true;
+        /*PhraseParseOrDie (
+            formula,
+            c,
+            ascii::space,
+            ret
+        );*/
+        boost::spirit::qi::phrase_parse(
+            begin, end, c, ascii::space, ret);
 
+        printf("%d\n\n", (int) ret);
+        return std::make_unique<Expression>(mnd::Constant{ 0.0 });
+    }
+}