aboutsummaryrefslogtreecommitdiff
path: root/src/libexpr
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2006-09-22 15:29:21 +0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2006-09-22 15:29:21 +0000
commit2ab4bc44c780d2e28647f7559664675b756f38b9 (patch)
tree39ecb0e001cf6c031e15b0246559b3f8f7a06ed9 /src/libexpr
parentd315210612a8d5eb52654407903544b72222130b (diff)
* Builtin function `add' to add integers.
* Put common test functions in tests/lang/lib.nix.
Diffstat (limited to 'src/libexpr')
-rw-r--r--src/libexpr/eval.cc10
-rw-r--r--src/libexpr/eval.hh1
-rw-r--r--src/libexpr/primops.cc9
3 files changed, 20 insertions, 0 deletions
diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc
index 02df4a4a3..3334e4bbd 100644
--- a/src/libexpr/eval.cc
+++ b/src/libexpr/eval.cc
@@ -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);
diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh
index b34e91055..a7f4e6943 100644
--- a/src/libexpr/eval.hh
+++ b/src/libexpr/eval.hh
@@ -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);
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc
index 190d58733..1739b6656 100644
--- a/src/libexpr/primops.cc
+++ b/src/libexpr/primops.cc
@@ -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);
}