diff options
author | Michael Raskin <7c6f434c@mail.ru> | 2009-08-25 16:06:46 +0000 |
---|---|---|
committer | Michael Raskin <7c6f434c@mail.ru> | 2009-08-25 16:06:46 +0000 |
commit | 3bca8931e8861fa4694b1ca31ecc023149e7aa81 (patch) | |
tree | b8c4b3d03b92ea9041d93a17646b9be2659f585e /src | |
parent | 5e9a4e5101a76655dd7f1bcd2c1c3afc200552b1 (diff) | |
download | guix-3bca8931e8861fa4694b1ca31ecc023149e7aa81.tar.gz |
Adding tryEval builtin. It allows to catch presence of errors in an expression.
Diffstat (limited to 'src')
-rw-r--r-- | src/libexpr/primops.cc | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 016e6abdb2..783d26c448 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -223,6 +223,23 @@ static Expr prim_addErrorContext(EvalState & state, const ATermVector & args) } } +/* Try evaluating the argument. Success => {success=true; value=something;}, + * else => {success=false; value=false;} */ +static Expr prim_tryEval(EvalState & state, const ATermVector & args) +{ + ATermMap res = ATermMap(); + try { + Expr val = evalExpr(state, args[0]); + res.set(toATerm("value"), makeAttrRHS(val, makeNoPos())); + res.set(toATerm("success"), makeAttrRHS(eTrue, makeNoPos())); + } catch (Error & e) { + printMsg(lvlInfo, format("tryEval caught an error: %1%: %2%") % e.prefix() % e.msg()); + res.set(toATerm("value"), makeAttrRHS(eFalse, makeNoPos())); + res.set(toATerm("success"), makeAttrRHS(eFalse, makeNoPos())); + } + return makeAttrs(res); +} + /* Return an environment variable. Use with care. */ static Expr prim_getEnv(EvalState & state, const ATermVector & args) @@ -1020,6 +1037,7 @@ void EvalState::addPrimOps() addPrimOp("abort", 1, prim_abort); addPrimOp("throw", 1, prim_throw); addPrimOp("__addErrorContext", 2, prim_addErrorContext); + addPrimOp("__tryEval", 1, prim_tryEval); addPrimOp("__getEnv", 1, prim_getEnv); addPrimOp("__trace", 2, prim_trace); |