aboutsummaryrefslogtreecommitdiff
path: root/src/libexpr
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2007-01-13 15:41:54 +0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2007-01-13 15:41:54 +0000
commit05879db628a31f53c69a0fc29311c840c80837e0 (patch)
tree9573ec43e89a467a9bd0bb1980fc7771efa4fcab /src/libexpr
parent501158845919c8bdf4297a8a76a916dc5b9a7943 (diff)
* Memoize strict evaluation.
Diffstat (limited to 'src/libexpr')
-rw-r--r--src/libexpr/eval.cc39
-rw-r--r--src/libexpr/eval.hh3
2 files changed, 29 insertions, 13 deletions
diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc
index e6ea73efc..cccd40404 100644
--- a/src/libexpr/eval.cc
+++ b/src/libexpr/eval.cc
@@ -672,7 +672,10 @@ Expr evalFile(EvalState & state, const Path & path)
}
-Expr strictEvalExpr(EvalState & state, Expr e, bool canonicalise)
+static Expr strictEvalExpr(EvalState & state, Expr e, ATermMap & nfs);
+
+
+static Expr strictEvalExpr_(EvalState & state, Expr e, ATermMap & nfs)
{
e = evalExpr(state, e);
@@ -682,13 +685,8 @@ Expr strictEvalExpr(EvalState & state, Expr e, bool canonicalise)
for (ATermIterator i(as); i; ++i) {
ATerm name; Expr e; ATerm pos;
if (!matchBind(*i, name, e, pos)) abort(); /* can't happen */
- as2 = ATinsert(as2, makeBind(name, strictEvalExpr(state, e, canonicalise),
- canonicalise ? makeNoPos() : pos));
+ as2 = ATinsert(as2, makeBind(name, strictEvalExpr(state, e, nfs), pos));
}
- if (canonicalise) {
-
- }
- /* !!! sort attributes if canonicalise == true */
return makeAttrs(ATreverse(as2));
}
@@ -696,7 +694,7 @@ Expr strictEvalExpr(EvalState & state, Expr e, bool canonicalise)
if (matchList(e, es)) {
ATermList es2 = ATempty;
for (ATermIterator i(es); i; ++i)
- es2 = ATinsert(es2, strictEvalExpr(state, *i, canonicalise));
+ es2 = ATinsert(es2, strictEvalExpr(state, *i, nfs));
return makeList(ATreverse(es2));
}
@@ -713,21 +711,40 @@ Expr strictEvalExpr(EvalState & state, Expr e, bool canonicalise)
if (matchValidValues(valids, valids2)) {
ATermList valids3 = ATempty;
for (ATermIterator j(valids2); j; ++j)
- valids3 = ATinsert(valids3, strictEvalExpr(state, *j, canonicalise));
+ valids3 = ATinsert(valids3, strictEvalExpr(state, *j, nfs));
valids = makeValidValues(ATreverse(valids3));
}
formals2 = ATinsert(formals2, makeFormal(name, valids, dummy));
}
- return makeFunction(ATreverse(formals2), body,
- canonicalise ? makeNoPos() : pos);
+ return makeFunction(ATreverse(formals2), body, pos);
}
return e;
}
+static Expr strictEvalExpr(EvalState & state, Expr e, ATermMap & nfs)
+{
+ Expr nf = nfs.get(e);
+ if (nf) return nf;
+
+ nf = strictEvalExpr_(state, e, nfs);
+
+ nfs.set(e, nf);
+
+ return nf;
+}
+
+
+Expr strictEvalExpr(EvalState & state, Expr e)
+{
+ ATermMap strictNormalForms;
+ return strictEvalExpr(state, e, strictNormalForms);
+}
+
+
/* Yes, this is a really bad idea... */
extern "C" {
unsigned long AT_calcAllocatedSize();
diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh
index 323a82873..5699d455c 100644
--- a/src/libexpr/eval.hh
+++ b/src/libexpr/eval.hh
@@ -56,8 +56,7 @@ Expr evalFile(EvalState & state, const Path & path);
attributes. If `canonicalise' is true, we remove things like
position information and make sure that attribute sets are in
sorded order. */
-Expr strictEvalExpr(EvalState & state, Expr e,
- bool canonicalise = false);
+Expr strictEvalExpr(EvalState & state, Expr e);
/* Specific results. */
string evalString(EvalState & state, Expr e, PathSet & context);