about summary refs log tree commit diff
path: root/src/fix-ng/eval.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/fix-ng/eval.cc')
-rw-r--r--src/fix-ng/eval.cc45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/fix-ng/eval.cc b/src/fix-ng/eval.cc
new file mode 100644
index 000000000000..c0f4680a49a6
--- /dev/null
+++ b/src/fix-ng/eval.cc
@@ -0,0 +1,45 @@
+#include "eval.hh"
+#include "expr.hh"
+#include "parser.hh"
+
+
+EvalState::EvalState()
+{
+    blackHole = ATmake("BlackHole()");
+    if (!blackHole) throw Error("cannot build black hole");
+}
+
+
+Expr evalExpr2(EvalState & state, Expr e)
+{
+    return e;
+}
+
+
+Expr evalExpr(EvalState & state, Expr e)
+{
+    Nest nest(lvlVomit, format("evaluating expression: %1%") % printTerm(e));
+
+    /* Consult the memo table to quickly get the normal form of
+       previously evaluated expressions. */
+    NormalForms::iterator i = state.normalForms.find(e);
+    if (i != state.normalForms.end()) {
+        if (i->second == state.blackHole)
+            throw badTerm("infinite recursion", e);
+        return i->second;
+    }
+
+    /* Otherwise, evaluate and memoize. */
+    state.normalForms[e] = state.blackHole;
+    Expr nf = evalExpr2(state, e);
+    state.normalForms[e] = nf;
+    return nf;
+}
+
+
+Expr evalFile(EvalState & state, const Path & path)
+{
+    Nest nest(lvlTalkative, format("evaluating file `%1%'") % path);
+    Expr e = parseExprFromFile(path);
+    return evalExpr(state, e);
+}