mirror of
https://github.com/NixOS/nix.git
synced 2024-11-25 08:12:29 +00:00
* Builtin function `add' to add integers.
* Put common test functions in tests/lang/lib.nix.
This commit is contained in:
parent
d315210612
commit
2ab4bc44c7
@ -176,6 +176,16 @@ Path evalPath(EvalState & state, Expr e)
|
||||
}
|
||||
|
||||
|
||||
int evalInt(EvalState & state, Expr e)
|
||||
{
|
||||
e = evalExpr(state, e);
|
||||
int i;
|
||||
if (!matchInt(e, i))
|
||||
throw TypeError(format("value is %1% while an integer was expected") % showType(e));
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
bool evalBool(EvalState & state, Expr e)
|
||||
{
|
||||
e = evalExpr(state, e);
|
||||
|
@ -62,6 +62,7 @@ Expr strictEvalExpr(EvalState & state, Expr e,
|
||||
/* Specific results. */
|
||||
string evalString(EvalState & state, Expr e);
|
||||
Path evalPath(EvalState & state, Expr e);
|
||||
int evalInt(EvalState & state, Expr e);
|
||||
bool evalBool(EvalState & state, Expr e);
|
||||
ATermList evalList(EvalState & state, Expr e);
|
||||
ATerm coerceToString(Expr e);
|
||||
|
@ -771,6 +771,14 @@ static Expr primRelativise(EvalState & state, const ATermVector & args)
|
||||
}
|
||||
|
||||
|
||||
static Expr primAdd(EvalState & state, const ATermVector & args)
|
||||
{
|
||||
int i1 = evalInt(state, args[0]);
|
||||
int i2 = evalInt(state, args[1]);
|
||||
return makeInt(i1 + i2);
|
||||
}
|
||||
|
||||
|
||||
void EvalState::addPrimOps()
|
||||
{
|
||||
addPrimOp("builtins", 0, primBuiltins);
|
||||
@ -801,6 +809,7 @@ void EvalState::addPrimOps()
|
||||
addPrimOp("__hasAttr", 2, primHasAttr);
|
||||
addPrimOp("removeAttrs", 2, primRemoveAttrs);
|
||||
addPrimOp("relativise", 2, primRelativise);
|
||||
addPrimOp("__add", 2, primAdd);
|
||||
}
|
||||
|
||||
|
||||
|
@ -27,7 +27,7 @@ done
|
||||
for i in lang/eval-fail-*.nix; do
|
||||
echo "evaluating $i (should fail)";
|
||||
i=$(basename $i .nix)
|
||||
if $nixinstantiate --eval-only - < lang/$i.nix; then
|
||||
if $nixinstantiate --eval-only lang/$i.nix; then
|
||||
echo "FAIL: $i shouldn't evaluate"
|
||||
fail=1
|
||||
fi
|
||||
@ -38,7 +38,7 @@ for i in lang/eval-okay-*.nix; do
|
||||
i=$(basename $i .nix)
|
||||
|
||||
if test -e lang/$i.exp; then
|
||||
if ! $nixinstantiate --eval-only - < lang/$i.nix > lang/$i.out; then
|
||||
if ! $nixinstantiate --eval-only lang/$i.nix > lang/$i.out; then
|
||||
echo "FAIL: $i should evaluate"
|
||||
fail=1
|
||||
fi
|
||||
@ -49,7 +49,7 @@ for i in lang/eval-okay-*.nix; do
|
||||
fi
|
||||
|
||||
if test -e lang/$i.exp.xml; then
|
||||
if ! $nixinstantiate --eval-only --xml --strict - < lang/$i.nix > lang/$i.out.xml; then
|
||||
if ! $nixinstantiate --eval-only --xml --strict lang/$i.nix > lang/$i.out.xml; then
|
||||
echo "FAIL: $i should evaluate"
|
||||
fail=1
|
||||
fi
|
||||
|
1
tests/lang/eval-okay-arithmetic.exp
Normal file
1
tests/lang/eval-okay-arithmetic.exp
Normal file
@ -0,0 +1 @@
|
||||
Int(1275)
|
18
tests/lang/eval-okay-arithmetic.nix
Normal file
18
tests/lang/eval-okay-arithmetic.nix
Normal file
@ -0,0 +1,18 @@
|
||||
with import ./lib.nix;
|
||||
|
||||
let {
|
||||
|
||||
range = first: last: [first] ++ (if first == last then [] else range (builtins.add first 1) last);
|
||||
|
||||
/* Supposedly tail recursive version:
|
||||
|
||||
range_ = accum: first: last:
|
||||
if first == last then ([first] ++ accum)
|
||||
else range_ ([first] ++ accum) (builtins.add first 1) last;
|
||||
|
||||
range = range_ [];
|
||||
*/
|
||||
|
||||
body = sum (range 1 50);
|
||||
|
||||
}
|
@ -1,18 +1,7 @@
|
||||
with import ./lib.nix;
|
||||
|
||||
let {
|
||||
|
||||
fold = op: nul: list:
|
||||
if list == []
|
||||
then nul
|
||||
else op (builtins.head list) (fold op nul (builtins.tail list));
|
||||
|
||||
concat =
|
||||
fold (x: y: x + y) "";
|
||||
|
||||
flatten = x:
|
||||
if builtins.isList x
|
||||
then fold (x: y: (flatten x) ++ y) [] x
|
||||
else [x];
|
||||
|
||||
l = ["1" "2" ["3" ["4"] ["5" "6"]] "7"];
|
||||
|
||||
body = concat (flatten l);
|
||||
|
@ -1,13 +1,7 @@
|
||||
with import ./lib.nix;
|
||||
|
||||
let {
|
||||
|
||||
fold = op: nul: list:
|
||||
if list == []
|
||||
then nul
|
||||
else op (builtins.head list) (fold op nul (builtins.tail list));
|
||||
|
||||
concat =
|
||||
fold (x: y: x + y) "";
|
||||
|
||||
body = concat ["foo" "bar" "bla" "test"];
|
||||
|
||||
}
|
@ -1 +1 @@
|
||||
List([Call(Function1("x",OpPlus(Var("x"),Str("bar")),Pos("(string)",1,7)),Str("foo")),Call(Function1("x",OpPlus(Var("x"),Str("bar")),Pos("(string)",1,7)),Str("bla")),Call(Function1("x",OpPlus(Var("x"),Str("bar")),Pos("(string)",1,7)),Str("xyzzy"))])
|
||||
Str("foobarblabarxyzzybar")
|
||||
|
@ -1 +1,3 @@
|
||||
map (x: x + "bar") [ "foo" "bla" "xyzzy" ]
|
||||
with import ./lib.nix;
|
||||
|
||||
concat (map (x: x + "bar") [ "foo" "bla" "xyzzy" ])
|
18
tests/lang/lib.nix
Normal file
18
tests/lang/lib.nix
Normal file
@ -0,0 +1,18 @@
|
||||
rec {
|
||||
|
||||
fold = op: nul: list:
|
||||
if list == []
|
||||
then nul
|
||||
else op (builtins.head list) (fold op nul (builtins.tail list));
|
||||
|
||||
concat =
|
||||
fold (x: y: x + y) "";
|
||||
|
||||
flatten = x:
|
||||
if builtins.isList x
|
||||
then fold (x: y: (flatten x) ++ y) [] x
|
||||
else [x];
|
||||
|
||||
sum = fold (x: y: builtins.add x y) 0;
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user