pub fn execute<P: Program>(
program: P,
context: &Context,
options: &Options<'_>,
) -> Result<Execution, Stopped>Expand description
Runs program over context as a statement program and answers the
context it leaves, with the roots it stored into.
The program is an &Instructions, or a &str source compiled as a
statement program (compile_program); a
source that does not compile stops before anything runs, the Stopped
holding its Error::Parse and the context passed in. It runs in
statement mode, as the reference’s instruction-set text states it: from its first instruction
to its end, its stores writing a copy of context (the one passed in
is never changed), and what is left on the stack at the end is
discarded. A run answers an Execution: the context it left and
Execution::stored_roots, the roots it stored into in the order of
its first store.
A run that stops answers a Stopped: the Error it stopped on, the
context as of the last statement it completed (every earlier write kept,
the failing write not made) and the roots it had stored into. A
Stopped converts into its Error, so ? takes it where only the
error matters.
The Options are read as evaluate reads them, and two more:
Options::loop_budget bounds the back edges a run takes, as it does
for evaluate, and Options::protected_roots names the roots a
store is refused for (reason protected_root). A variable the context
does not bind is read as evaluate reads it, and a refused undefined
operand is reported as the first unbound variable the run loaded; the
run’s answer is the context, so an undefined value is never one.
Before anything runs, every value the context binds is checked against
the value domain (Context).
use predicator_rs::{Context, Instructions, Key, Options, Value, execute};
// x = 1; y = x + 2
let program = Instructions::from_json(
r#"[["lit","x"],["lit",1],["store",1],["lit","y"],["load","x"],["lit",2],["add"],["store",1]]"#,
)?;
let given = Context::new();
let execution = execute(&program, &given, &Options::new())?;
assert_eq!(execution.context().get(&Key::from("y")), Some(&Value::from(3)));
assert_eq!(execution.stored_roots(), [Key::from("x"), Key::from("y")]);
assert_eq!(given, Context::new());