use std::collections::BTreeMap;
use std::fmt;
use serde::Serialize;
use crate::compiler::instructions::Instructions;
use crate::environment::Environment;
use crate::error::Error;
use crate::output::Output;
use crate::value::Value;
use crate::vm::Vm;
pub struct Expression<'env, 'source> {
env: &'env Environment<'source>,
instructions: Instructions<'source>,
}
impl<'env, 'source> fmt::Debug for Expression<'env, 'source> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Expression")
.field("env", &self.env)
.finish()
}
}
impl<'env, 'source> Expression<'env, 'source> {
pub(crate) fn new(
env: &'env Environment<'source>,
instructions: Instructions<'source>,
) -> Expression<'env, 'source> {
Expression { env, instructions }
}
pub fn eval<S: Serialize>(&self, ctx: S) -> Result<Value, Error> {
self._eval(Value::from_serializable(&ctx))
}
fn _eval(&self, root: Value) -> Result<Value, Error> {
Ok(ok!(Vm::new(self.env).eval(
&self.instructions,
root,
&BTreeMap::new(),
&mut Output::null(),
crate::AutoEscape::None,
))
.expect("expression evaluation did not leave value on stack"))
}
}