about summary refs log tree commit diff
path: root/src/fix-ng/eval.cc
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2003-10-30T16·48+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2003-10-30T16·48+0000
commit403cb9327f5c298cb6a85a87241962df4a90857b (patch)
tree7845c52eef1ce3289cd8a32e083542dcdfceecc8 /src/fix-ng/eval.cc
parent9f8f39aa3cdb54532a85e41f14985fc6a530fb36 (diff)
* Factor out evaluation into a separate file.
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 0000000000..c0f4680a49
--- /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);
+}