about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2013-08-02T15·35+0200
committerEelco Dolstra <eelco.dolstra@logicblox.com>2013-08-02T15·43+0000
commit5d147e125cea69e9a3b12f0ef64c64f42985c65e (patch)
tree0e9611fb64739f094fe0652ff71586c9ad7fd368
parent159e621d1a9c4391b53f3d822109c36931934698 (diff)
Add a unary integer negation operator
This allows saying "-1" instead of "builtins.sub 0 1".
-rw-r--r--src/libexpr/parser.y6
-rw-r--r--tests/lang/eval-okay-arithmetic.exp2
-rw-r--r--tests/lang/eval-okay-arithmetic.nix7
3 files changed, 11 insertions, 4 deletions
diff --git a/src/libexpr/parser.y b/src/libexpr/parser.y
index 964527be6763..4a59a0875eb8 100644
--- a/src/libexpr/parser.y
+++ b/src/libexpr/parser.y
@@ -270,11 +270,12 @@ void yyerror(YYLTYPE * loc, yyscan_t scanner, ParseData * data, const char * err
 %left AND
 %nonassoc EQ NEQ
 %right UPDATE
-%left NEG
+%left NOT
 %left '+'
 %right CONCAT
 %nonassoc '?'
 %nonassoc '~'
+%nonassoc NEGATE
 
 %%
 
@@ -306,7 +307,8 @@ expr_if
   ;
 
 expr_op
-  : '!' expr_op %prec NEG { $$ = new ExprOpNot($2); }
+  : '!' expr_op %prec NOT { $$ = new ExprOpNot($2); }
+  | '-' expr_op %prec NEGATE { $$ = new ExprApp(new ExprApp(new ExprVar(data->symbols.create("__sub")), new ExprInt(0)), $2); }
   | expr_op EQ expr_op { $$ = new ExprOpEq($1, $3); }
   | expr_op NEQ expr_op { $$ = new ExprOpNEq($1, $3); }
   | expr_op AND expr_op { $$ = new ExprOpAnd($1, $3); }
diff --git a/tests/lang/eval-okay-arithmetic.exp b/tests/lang/eval-okay-arithmetic.exp
index 861c12661a83..f625eb6ab6ab 100644
--- a/tests/lang/eval-okay-arithmetic.exp
+++ b/tests/lang/eval-okay-arithmetic.exp
@@ -1 +1 @@
-1854
+1843
diff --git a/tests/lang/eval-okay-arithmetic.nix b/tests/lang/eval-okay-arithmetic.nix
index 1f45b122a2bb..76bef919bdab 100644
--- a/tests/lang/eval-okay-arithmetic.nix
+++ b/tests/lang/eval-okay-arithmetic.nix
@@ -16,6 +16,11 @@ let {
   range = range_ [];
   */
 
-  body = sum (range 1 50) + 123 + 456;
+  x = 12;
 
+  body = sum
+    [ (sum (range 1 50))
+      (123 + 456)
+      (0 + -10 + -(-11) + -x)
+    ];
 }