* Implemented tryEval, the last missing primop in the fast-eval

branch.  Also added a test for tryEval.
This commit is contained in:
Eelco Dolstra 2010-05-12 11:23:44 +00:00
parent 1a8eb6e3ec
commit 81a4b4e49b
3 changed files with 12 additions and 12 deletions

View file

@ -202,24 +202,20 @@ static void prim_addErrorContext(EvalState & state, Value * * args, Value & v)
} }
#if 0
/* Try evaluating the argument. Success => {success=true; value=something;}, /* Try evaluating the argument. Success => {success=true; value=something;},
* else => {success=false; value=false;} */ * else => {success=false; value=false;} */
static void prim_tryEval(EvalState & state, Value * * args, Value & v) static void prim_tryEval(EvalState & state, Value * * args, Value & v)
{ {
ATermMap res = ATermMap(); state.mkAttrs(v);
try { try {
Expr val = evalExpr(state, args[0]); state.forceValue(*args[0]);
res.set(toATerm("value"), makeAttrRHS(val, makeNoPos())); (*v.attrs)[state.symbols.create("value")].value = *args[0];
res.set(toATerm("success"), makeAttrRHS(eTrue, makeNoPos())); mkBool((*v.attrs)[state.symbols.create("success")].value, true);
} catch (AssertionError & e) { } catch (AssertionError & e) {
printMsg(lvlDebug, format("tryEval caught an error: %1%: %2%") % e.prefix() % e.msg()); mkBool((*v.attrs)[state.symbols.create("value")].value, false);
res.set(toATerm("value"), makeAttrRHS(eFalse, makeNoPos())); mkBool((*v.attrs)[state.symbols.create("success")].value, false);
res.set(toATerm("success"), makeAttrRHS(eFalse, makeNoPos()));
} }
return makeAttrs(res);
} }
#endif
/* Return an environment variable. Use with care. */ /* Return an environment variable. Use with care. */
@ -1062,9 +1058,7 @@ void EvalState::createBaseEnv()
addPrimOp("abort", 1, prim_abort); addPrimOp("abort", 1, prim_abort);
addPrimOp("throw", 1, prim_throw); addPrimOp("throw", 1, prim_throw);
addPrimOp("__addErrorContext", 2, prim_addErrorContext); addPrimOp("__addErrorContext", 2, prim_addErrorContext);
#if 0
addPrimOp("__tryEval", 1, prim_tryEval); addPrimOp("__tryEval", 1, prim_tryEval);
#endif
addPrimOp("__getEnv", 1, prim_getEnv); addPrimOp("__getEnv", 1, prim_getEnv);
addPrimOp("__trace", 2, prim_trace); addPrimOp("__trace", 2, prim_trace);

View file

@ -0,0 +1 @@
{ x = { value = "x"; success = true; }; y = { value = false; success = false; }; z = { value = false; success = false; }; }

View file

@ -0,0 +1,5 @@
{
x = builtins.tryEval "x";
y = builtins.tryEval (assert false; "y");
z = builtins.tryEval (throw "bla");
}