* Builtin function `add' to add integers.

* Put common test functions in tests/lang/lib.nix.
This commit is contained in:
Eelco Dolstra 2006-09-22 15:29:21 +00:00
parent d315210612
commit 2ab4bc44c7
11 changed files with 68 additions and 26 deletions

View file

@ -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);

View file

@ -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);

View file

@ -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);
}

View file

@ -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

View file

@ -0,0 +1 @@
Int(1275)

View 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);
}

View file

@ -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);

View file

@ -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"];
}

View file

@ -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")

View file

@ -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
View 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;
}