From 8a1ab709a47f0896cb5b47521e31c8c27f88b2b3 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 22 Sep 2006 14:31:55 +0000 Subject: * New builtin functions builtins.{hasAttr, getAttr} to check for attribute existence and to return an attribute from an attribute set, respectively. Example: `hasAttr "foo" {foo = 1;}'. They differ from the `?' and `.' operators in that the attribute name is an arbitrary expression. (NIX-61) --- src/libexpr/primops.cc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src/libexpr/primops.cc') diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 93a699b4170b..f08ad0464feb 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -703,6 +703,20 @@ static Expr primCurrentTime(EvalState & state, const ATermVector & args) } +static Expr primGetAttr(EvalState & state, const ATermVector & args) +{ + string attr = evalString(state, args[0]); + return evalExpr(state, makeSelect(args[1], toATerm(attr))); +} + + +static Expr primHasAttr(EvalState & state, const ATermVector & args) +{ + string attr = evalString(state, args[0]); + return evalExpr(state, makeOpHasAttr(args[1], toATerm(attr))); +} + + static Expr primRemoveAttrs(EvalState & state, const ATermVector & args) { ATermMap attrs(128); /* !!! */ @@ -749,6 +763,8 @@ void EvalState::addPrimOps() addPrimOp("abort", 1, primAbort); addPrimOp("map", 2, primMap); + addPrimOp("__getAttr", 2, primGetAttr); + addPrimOp("__hasAttr", 2, primHasAttr); addPrimOp("removeAttrs", 2, primRemoveAttrs); addPrimOp("relativise", 2, primRelativise); } -- cgit 1.4.1