diff options
author | Eelco Dolstra <e.dolstra@tudelft.nl> | 2010-05-12T11·23+0000 |
---|---|---|
committer | Eelco Dolstra <e.dolstra@tudelft.nl> | 2010-05-12T11·23+0000 |
commit | 81a4b4e49bf82f17eef20d78c4f505874cf5532e (patch) | |
tree | e5fa0c53c248cc341b964332911f4a44be4a0127 | |
parent | 1a8eb6e3ec9329ee7b61ac2345c6e1d994905813 (diff) |
* Implemented tryEval, the last missing primop in the fast-eval
branch. Also added a test for tryEval.
-rw-r--r-- | src/libexpr/primops.cc | 18 | ||||
-rw-r--r-- | tests/lang/eval-okay-tryeval.exp | 1 | ||||
-rw-r--r-- | tests/lang/eval-okay-tryeval.nix | 5 |
3 files changed, 12 insertions, 12 deletions
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index a7e7bcdc8615..9d36fb6a05d7 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -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;}, * else => {success=false; value=false;} */ static void prim_tryEval(EvalState & state, Value * * args, Value & v) { - ATermMap res = ATermMap(); + state.mkAttrs(v); try { - Expr val = evalExpr(state, args[0]); - res.set(toATerm("value"), makeAttrRHS(val, makeNoPos())); - res.set(toATerm("success"), makeAttrRHS(eTrue, makeNoPos())); + state.forceValue(*args[0]); + (*v.attrs)[state.symbols.create("value")].value = *args[0]; + mkBool((*v.attrs)[state.symbols.create("success")].value, true); } catch (AssertionError & e) { - printMsg(lvlDebug, 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())); + mkBool((*v.attrs)[state.symbols.create("value")].value, false); + mkBool((*v.attrs)[state.symbols.create("success")].value, false); } - return makeAttrs(res); } -#endif /* Return an environment variable. Use with care. */ @@ -1062,9 +1058,7 @@ void EvalState::createBaseEnv() addPrimOp("abort", 1, prim_abort); addPrimOp("throw", 1, prim_throw); addPrimOp("__addErrorContext", 2, prim_addErrorContext); -#if 0 addPrimOp("__tryEval", 1, prim_tryEval); -#endif addPrimOp("__getEnv", 1, prim_getEnv); addPrimOp("__trace", 2, prim_trace); diff --git a/tests/lang/eval-okay-tryeval.exp b/tests/lang/eval-okay-tryeval.exp new file mode 100644 index 000000000000..c2788b412288 --- /dev/null +++ b/tests/lang/eval-okay-tryeval.exp @@ -0,0 +1 @@ +{ x = { value = "x"; success = true; }; y = { value = false; success = false; }; z = { value = false; success = false; }; } diff --git a/tests/lang/eval-okay-tryeval.nix b/tests/lang/eval-okay-tryeval.nix new file mode 100644 index 000000000000..629bc440a85a --- /dev/null +++ b/tests/lang/eval-okay-tryeval.nix @@ -0,0 +1,5 @@ +{ + x = builtins.tryEval "x"; + y = builtins.tryEval (assert false; "y"); + z = builtins.tryEval (throw "bla"); +} |