Skip to content

Commit

Permalink
* Added an operator `?' to test for attribute existence, e.g.,
Browse files Browse the repository at this point in the history
  `attrs ? x' yields true iff `attrs' has an attribute named `x'.
  • Loading branch information
edolstra committed Mar 28, 2004
1 parent f958bcd commit ac4d39f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/libexpr/eval.cc
Expand Up @@ -288,10 +288,17 @@ Expr evalExpr2(EvalState & state, Expr e)
if (atMatch(m, e) >> "OpOr" >> e1 >> e2)
return makeBool(evalBool(state, e1) || evalBool(state, e2));

/* Attribut set update (//). */
/* Attribute set update (//). */
if (atMatch(m, e) >> "OpUpdate" >> e1 >> e2)
return updateAttrs(evalExpr(state, e1), evalExpr(state, e2));

/* Attribute existence test (?). */
if (atMatch(m, e) >> "OpHasAttr" >> e1 >> name) {
ATermMap attrs;
queryAllAttrs(evalExpr(state, e1), attrs);
return makeBool(attrs.get(name) != 0);
}

/* Barf. */
throw badTerm("invalid expression", e);
}
Expand Down
2 changes: 2 additions & 0 deletions src/libexpr/parser.y
Expand Up @@ -44,6 +44,7 @@ void yyerror(YYLTYPE * loc, yyscan_t scanner, void * data, char * s)
%nonassoc EQ NEQ
%right UPDATE
%left NEG
%nonassoc '?'
%nonassoc '~'

%%
Expand Down Expand Up @@ -81,6 +82,7 @@ expr_op
| expr_op IMPL expr_op { $$ = ATmake("OpImpl(<term>, <term>)", $1, $3); }
| expr_op UPDATE expr_op { $$ = ATmake("OpUpdate(<term>, <term>)", $1, $3); }
| expr_op '~' expr_op { $$ = ATmake("SubPath(<term>, <term>)", $1, $3); }
| expr_op '?' ID { $$ = ATmake("OpHasAttr(<term>, <term>)", $1, $3); }
| expr_app
;

Expand Down

1 comment on commit ac4d39f

@nixos-discourse
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/pre-rfc-visually-differentiate-the-symbol-in-nixpkgs/29929/1

Please sign in to comment.