pub fn compile_program(source: &str) -> Result<Instructions, Error>Expand description
Compiles a statement program’s source, one statement or more, to the instruction list it runs as.
A statement is an assignment (location = value, the location a
variable under any run of .name and [key] steps), an if with an
optional else or else if, a while, or an expression. Statements
are separated by ;, and a statement ending in a closing brace needs
none before the next. Each compiles as the reference compiles it: an
assignment to its location’s segments, its value and a store; an
expression to its instructions and a pop; an if and a while to
their condition, a jump over their blocks, and a back edge for the
while. execute and
execute_value run the list.
A source that does not scan or parse is refused as compile refuses
one, with an Error::Parse.
use predicator_rs::{Context, Key, Options, Value, compile_program, execute};
let program = compile_program("x = 1; if x > 0 { y = x + 1 }")?;
let execution = execute(&program, &Context::new(), &Options::new())?;
assert_eq!(execution.context().get(&Key::from("y")), Some(&Value::from(2)));
let refused = compile_program("x =").unwrap_err();
assert_eq!(refused.type_name(), "ParseError");