Browse Source

better error recovery

Nicolas Winkler 5 years ago
parent
commit
484a5cfe45

+ 25 - 2
customgenerator.cpp

@@ -1,6 +1,7 @@
 #include "customgenerator.h"
 #include "ui_customgenerator.h"
 
+#include <QMessageBox>
 
 #include <IterationCompiler.h>
 
@@ -22,8 +23,30 @@ void CustomGenerator::compile()
 {
     QString z0formula = this->ui->formula_z0->text();
     QString ziformula = this->ui->formula_zi->text();
-    mnd::IterationFormula zi{ mnd::parse(ziformula.toStdString()), { "c", "z" } };
-    mnd::IterationFormula z0{ mnd::parse(z0formula.toStdString()), { "c" } };
+
+    auto msgError = [this] (const std::string& msgText) {
+        QMessageBox msg("Compile Error", QString::fromStdString(msgText),
+                        QMessageBox::Icon::Critical, 0, 0, 0, this);
+        return msg.exec();
+    };
+
+    mnd::IterationFormula zi;
+    mnd::IterationFormula z0;
+
+    try {
+        zi = { mnd::parse(ziformula.toStdString()), { "c", "z" } };
+    } catch (const mnd::ParseError& pe) {
+        msgError(pe.what());
+        return;
+    }
+
+    try {
+        z0 = { mnd::parse(z0formula.toStdString()), { "c" } };
+    } catch (const mnd::ParseError& pe) {
+        msgError(pe.what());
+        return;
+    }
+
 
     mnd::GeneratorCollection cr;
 

+ 16 - 0
libmandel/include/Arena.h

@@ -5,6 +5,7 @@
 #include <array>
 #include <utility>
 #include <memory>
+#include <functional>
 
 namespace mnd
 {
@@ -36,6 +37,11 @@ namespace mnd
                     return new(reinterpret_cast<T*>(&data[(used++) * sizeof(T)])) T(std::forward<Args>(args)...);
                 }
 
+                T& at(int index)
+                {
+                    return *reinterpret_cast<T*>(&data[index * sizeof(T)]);
+                }
+
                 ~Chunk(void)
                 {
                     for (int i = used - 1; i >= 0; i--) {
@@ -74,6 +80,16 @@ namespace mnd
 
                 return lastChunk().allocate(std::forward<Args>(args)...);
             }
+
+
+            void forAll(std::function<void(T&)> f)
+            {
+                for (auto& chunk : chunks) {
+                    for (int i = 0; i < chunk->used; i++) {
+                        f(chunk->at(i));
+                    }
+                }
+            }
         };
     }
 }

+ 1 - 0
libmandel/include/IterationFormula.h

@@ -49,6 +49,7 @@ struct mnd::IterationFormula
 {
     std::vector<std::string> variables;
     std::unique_ptr<Expression> expr;
+    IterationFormula(void) = default;
     IterationFormula(std::unique_ptr<Expression> expr, const std::vector<std::string>& variables = { "c", "z" });
     IterationFormula(Expression expr, const std::vector<std::string>& variables = { "c", "z" });
 

+ 1 - 0
libmandel/include/IterationIR.h

@@ -65,6 +65,7 @@ namespace mnd
 
             void constantPropagation(void);
             void optimize(void);
+            void clearNodeData(void);
         };
     }
 

+ 1 - 0
libmandel/src/IterationCompiler.cpp

@@ -807,6 +807,7 @@ namespace mnd
         auto fl = compileCl(irf, dev);
         vec.push_back(std::move(fl));
         if (dev.supportsDouble()) {
+            irf.clearNodeData();
             auto fld = compileClDouble(irf, dev);
             vec.push_back(std::move(fld));
         }

+ 9 - 0
libmandel/src/IterationIR.cpp

@@ -513,4 +513,13 @@ void mnd::ir::Formula::optimize(void)
 }
 
 
+void mnd::ir::Formula::clearNodeData(void)
+{
+    nodeArena.forAll([] (Node& n) {
+        std::visit([] (auto& x) {
+                x.nodeData.reset();
+        }, n);
+    });
+}
+