aboutsummaryrefslogtreecommitdiff
path: root/src/libstore/expr.cc
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2003-11-18 10:55:27 +0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2003-11-18 10:55:27 +0000
commit9f0f020929c9e093405cc6193d2f227cab763912 (patch)
tree833aad12f6db91e77ff2fb47a2bcb54a0ea9d89f /src/libstore/expr.cc
parent8798fae30450a88c339c8f23d7e0c75f5df2ef1c (diff)
* libnix -> libstore.
Diffstat (limited to 'src/libstore/expr.cc')
-rw-r--r--src/libstore/expr.cc210
1 files changed, 210 insertions, 0 deletions
diff --git a/src/libstore/expr.cc b/src/libstore/expr.cc
new file mode 100644
index 000000000..7bb1f5306
--- /dev/null
+++ b/src/libstore/expr.cc
@@ -0,0 +1,210 @@
+#include "expr.hh"
+#include "globals.hh"
+#include "store.hh"
+
+
+Error badTerm(const format & f, ATerm t)
+{
+ char * s = ATwriteToString(t);
+ if (!s) throw Error("cannot print term");
+ if (strlen(s) > 1000) {
+ int len;
+ s = ATwriteToSharedString(t, &len);
+ if (!s) throw Error("cannot print term");
+ }
+ return Error(format("%1%, in `%2%'") % f.str() % (string) s);
+}
+
+
+Hash hashTerm(ATerm t)
+{
+ return hashString(atPrint(t));
+}
+
+
+Path writeTerm(ATerm t, const string & suffix)
+{
+ /* The id of a term is its hash. */
+ Hash h = hashTerm(t);
+
+ Path path = canonPath(nixStore + "/" +
+ (string) h + suffix + ".nix");
+
+ if (!isValidPath(path)) {
+ char * s = ATwriteToString(t);
+ if (!s) throw Error(format("cannot write aterm to `%1%'") % path);
+ addTextToStore(path, string(s));
+ }
+
+ return path;
+}
+
+
+static void parsePaths(ATermList paths, PathSet & out)
+{
+ ATMatcher m;
+ for (ATermIterator i(paths); i; ++i) {
+ string s;
+ if (!(atMatch(m, *i) >> s))
+ throw badTerm("not a path", *i);
+ out.insert(s);
+ }
+}
+
+
+static void checkClosure(const Closure & closure)
+{
+ if (closure.elems.size() == 0)
+ throw Error("empty closure");
+
+ PathSet decl;
+ for (ClosureElems::const_iterator i = closure.elems.begin();
+ i != closure.elems.end(); i++)
+ decl.insert(i->first);
+
+ for (PathSet::const_iterator i = closure.roots.begin();
+ i != closure.roots.end(); i++)
+ if (decl.find(*i) == decl.end())
+ throw Error(format("undefined root path `%1%'") % *i);
+
+ for (ClosureElems::const_iterator i = closure.elems.begin();
+ i != closure.elems.end(); i++)
+ for (PathSet::const_iterator j = i->second.refs.begin();
+ j != i->second.refs.end(); j++)
+ if (decl.find(*j) == decl.end())
+ throw Error(
+ format("undefined path `%1%' referenced by `%2%'")
+ % *j % i->first);
+}
+
+
+/* Parse a closure. */
+static bool parseClosure(ATerm t, Closure & closure)
+{
+ ATermList roots, elems;
+ ATMatcher m;
+
+ if (!(atMatch(m, t) >> "Closure" >> roots >> elems))
+ return false;
+
+ parsePaths(roots, closure.roots);
+
+ for (ATermIterator i(elems); i; ++i) {
+ string path;
+ ATermList refs;
+ if (!(atMatch(m, *i) >> "" >> path >> refs))
+ throw badTerm("not a closure element", *i);
+ ClosureElem elem;
+ parsePaths(refs, elem.refs);
+ closure.elems[path] = elem;
+ }
+
+ checkClosure(closure);
+ return true;
+}
+
+
+static bool parseDerivation(ATerm t, Derivation & derivation)
+{
+ ATMatcher m;
+ ATermList outs, ins, args, bnds;
+ string builder, platform;
+
+ if (!(atMatch(m, t) >> "Derive" >> outs >> ins >> platform
+ >> builder >> args >> bnds))
+ return false;
+
+ parsePaths(outs, derivation.outputs);
+ parsePaths(ins, derivation.inputs);
+
+ derivation.builder = builder;
+ derivation.platform = platform;
+
+ for (ATermIterator i(args); i; ++i) {
+ string s;
+ if (!(atMatch(m, *i) >> s))
+ throw badTerm("string expected", *i);
+ derivation.args.push_back(s);
+ }
+
+ for (ATermIterator i(bnds); i; ++i) {
+ string s1, s2;
+ if (!(atMatch(m, *i) >> "" >> s1 >> s2))
+ throw badTerm("tuple of strings expected", *i);
+ derivation.env[s1] = s2;
+ }
+
+ return true;
+}
+
+
+NixExpr parseNixExpr(ATerm t)
+{
+ NixExpr ne;
+ if (parseClosure(t, ne.closure))
+ ne.type = NixExpr::neClosure;
+ else if (parseDerivation(t, ne.derivation))
+ ne.type = NixExpr::neDerivation;
+ else throw badTerm("not a Nix expression", t);
+ return ne;
+}
+
+
+static ATermList unparsePaths(const PathSet & paths)
+{
+ ATermList l = ATempty;
+ for (PathSet::const_iterator i = paths.begin();
+ i != paths.end(); i++)
+ l = ATinsert(l, ATmake("<str>", i->c_str()));
+ return ATreverse(l);
+}
+
+
+static ATerm unparseClosure(const Closure & closure)
+{
+ ATermList roots = unparsePaths(closure.roots);
+
+ ATermList elems = ATempty;
+ for (ClosureElems::const_iterator i = closure.elems.begin();
+ i != closure.elems.end(); i++)
+ elems = ATinsert(elems,
+ ATmake("(<str>, <term>)",
+ i->first.c_str(),
+ unparsePaths(i->second.refs)));
+
+ return ATmake("Closure(<term>, <term>)", roots, elems);
+}
+
+
+static ATerm unparseDerivation(const Derivation & derivation)
+{
+ ATermList args = ATempty;
+ for (Strings::const_iterator i = derivation.args.begin();
+ i != derivation.args.end(); i++)
+ args = ATinsert(args, ATmake("<str>", i->c_str()));
+
+ ATermList env = ATempty;
+ for (StringPairs::const_iterator i = derivation.env.begin();
+ i != derivation.env.end(); i++)
+ env = ATinsert(env,
+ ATmake("(<str>, <str>)",
+ i->first.c_str(), i->second.c_str()));
+
+ return ATmake("Derive(<term>, <term>, <str>, <str>, <term>, <term>)",
+ unparsePaths(derivation.outputs),
+ unparsePaths(derivation.inputs),
+ derivation.platform.c_str(),
+ derivation.builder.c_str(),
+ ATreverse(args),
+ ATreverse(env));
+}
+
+
+ATerm unparseNixExpr(const NixExpr & ne)
+{
+ if (ne.type == NixExpr::neClosure)
+ return unparseClosure(ne.closure);
+ else if (ne.type == NixExpr::neDerivation)
+ return unparseDerivation(ne.derivation);
+ else abort();
+}