diff options
author | Eelco Dolstra <e.dolstra@tudelft.nl> | 2007-10-09 12:51:25 +0000 |
---|---|---|
committer | Eelco Dolstra <e.dolstra@tudelft.nl> | 2007-10-09 12:51:25 +0000 |
commit | 27a0662828cb5ac9da198f35754750f12628d546 (patch) | |
tree | 97c126eddaf608e533075af931d75f11816daa1d /src | |
parent | 8a9fe6c11c4cf6ae9f3648ece844a8f392fc1cc0 (diff) | |
download | guix-27a0662828cb5ac9da198f35754750f12628d546.tar.gz |
* listToAttrs: the list now should consist of {name, value} attribute
sets instead of {attr, value}. "name" is better than "attr" because the *combination* of the two forms the attribute.
Diffstat (limited to 'src')
-rw-r--r-- | src/libexpr/primops.cc | 57 |
1 files changed, 29 insertions, 28 deletions
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index d0326a3e56..3d080dec3f 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -755,36 +755,37 @@ static Expr prim_hasAttr(EvalState & state, const ATermVector & args) } -/* takes - * param: list of { attr="attr"; value=value } - * returns an attribute set - * */ +/* Builds an attribute set from a list specifying (name, value) + pairs. To be precise, a list [{name = "name1"; value = value1;} + ... {name = "nameN"; value = valueN;}] is transformed to {name1 = + value1; ... nameN = valueN;}. */ static Expr prim_listToAttrs(EvalState & state, const ATermVector & args) { - try { - ATermMap res = ATermMap(); - - ATermList list; - list = evalList(state, args[0]); - for (ATermIterator i(list); i; ++i){ - // *i should now contain a pointer to the list item expression - ATermList attrs; - Expr evaledExpr = evalExpr(state, *i); - if (matchAttrs(evaledExpr, attrs)){ - Expr e = evalExpr(state, makeSelect(evaledExpr, toATerm("attr"))); - string attr = evalStringNoCtx(state,e); - Expr r = makeSelect(evaledExpr, toATerm("value")); - res.set(toATerm(attr), makeAttrRHS(r, makeNoPos())); - } - else { - throw EvalError(format("passed list item is a %s (value: %s). Set { attr=\"name\"; value=nix expr; } expected.") % showType(evaledExpr) % showValue(evaledExpr)); - } - } // for - return makeAttrs(res); - } catch (Error & e) { - e.addPrefix(format("in `listToAttrs':\n")); - throw; - } + try { + ATermMap res = ATermMap(); + ATermList list; + list = evalList(state, args[0]); + for (ATermIterator i(list); i; ++i){ + // *i should now contain a pointer to the list item expression + ATermList attrs; + Expr evaledExpr = evalExpr(state, *i); + if (matchAttrs(evaledExpr, attrs)){ + Expr e = evalExpr(state, makeSelect(evaledExpr, toATerm("name"))); + string attr = evalStringNoCtx(state,e); + Expr r = makeSelect(evaledExpr, toATerm("value")); + res.set(toATerm(attr), makeAttrRHS(r, makeNoPos())); + } + else + throw TypeError(format("list element in `listToAttrs' is %s, expected a set { name = \"<name>\"; value = <value>; }") + % showType(evaledExpr)); + } + + return makeAttrs(res); + + } catch (Error & e) { + e.addPrefix(format("in `listToAttrs':\n")); + throw; + } } static Expr prim_removeAttrs(EvalState & state, const ATermVector & args) |