From 3bca8931e8861fa4694b1ca31ecc023149e7aa81 Mon Sep 17 00:00:00 2001 From: Michael Raskin <7c6f434c@mail.ru> Date: Tue, 25 Aug 2009 16:06:46 +0000 Subject: [PATCH] Adding tryEval builtin. It allows to catch presence of errors in an expression. --- src/libexpr/primops.cc | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 016e6abdb..783d26c44 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);