about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2007-01-13T16·17+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2007-01-13T16·17+0000
commitf23dcdd60330c3b2f83b5b4278e38c245c397468 (patch)
tree0c0386b4b42d7974dff18e93485fdda7b9ad71eb /src
parent05879db628a31f53c69a0fc29311c840c80837e0 (diff)
* Canonicalise ASTs in `nix-instantiate --eval': remove position
  info, sort attribute sets.

Diffstat (limited to 'src')
-rw-r--r--src/libexpr/nixexpr.cc39
-rw-r--r--src/libexpr/nixexpr.hh5
-rw-r--r--src/nix-instantiate/nix-instantiate.cc2
3 files changed, 45 insertions, 1 deletions
diff --git a/src/libexpr/nixexpr.cc b/src/libexpr/nixexpr.cc
index a6cde127c3b2..7502fd1663f5 100644
--- a/src/libexpr/nixexpr.cc
+++ b/src/libexpr/nixexpr.cc
@@ -287,6 +287,45 @@ void checkVarDefs(const ATermMap & defs, Expr e)
 }
 
 
+struct Canonicalise : TermFun
+{
+    ATerm operator () (ATerm e)
+    {
+        /* Remove position info. */
+        ATerm path;
+        int line, column;
+        if (matchPos(e, path, line, column))
+            return makeNoPos();
+
+        /* Sort attribute sets. */
+        ATermList _;
+        if (matchAttrs(e, _)) {
+            ATermMap attrs;
+            queryAllAttrs(e, attrs);
+            StringSet names;
+            for (ATermMap::const_iterator i = attrs.begin(); i != attrs.end(); ++i)
+                names.insert(aterm2String(i->key));
+
+            ATermList attrs2 = ATempty;
+            for (StringSet::reverse_iterator i = names.rbegin(); i != names.rend(); ++i)
+                attrs2 = ATinsert(attrs2,
+                    makeBind(toATerm(*i), attrs.get(toATerm(*i)), makeNoPos()));
+
+            return makeAttrs(attrs2);
+        }
+        
+        return e;
+    }
+};
+
+
+Expr canonicaliseExpr(Expr & e)
+{
+    Canonicalise canonicalise;
+    return bottomupRewrite(canonicalise, e);
+}
+
+
 Expr makeBool(bool b)
 {
     return b ? eTrue : eFalse;
diff --git a/src/libexpr/nixexpr.hh b/src/libexpr/nixexpr.hh
index 2fdad73b78a3..d76bc1060d33 100644
--- a/src/libexpr/nixexpr.hh
+++ b/src/libexpr/nixexpr.hh
@@ -94,6 +94,11 @@ Expr substitute(const Substitution & subs, Expr e);
 void checkVarDefs(const ATermMap & def, Expr e);
 
 
+/* Canonicalise a Nix expression by sorting attributes and removing
+   location information. */
+Expr canonicaliseExpr(Expr & e);
+
+
 /* Create an expression representing a boolean. */
 Expr makeBool(bool b);
 
diff --git a/src/nix-instantiate/nix-instantiate.cc b/src/nix-instantiate/nix-instantiate.cc
index ffd8a2e71f40..41015c0eee54 100644
--- a/src/nix-instantiate/nix-instantiate.cc
+++ b/src/nix-instantiate/nix-instantiate.cc
@@ -45,7 +45,7 @@ static void printResult(EvalState & state, Expr e,
         if (xmlOutput)
             printTermAsXML(e, std::cout, context);
         else
-            std::cout << format("%1%\n") % e;
+            std::cout << format("%1%\n") % canonicaliseExpr(e);
     
     else {
         DrvInfos drvs;