1
1
mirror of https://github.com/tweag/nickel.git synced 2024-10-04 15:17:34 +03:00

Refactor + introduce eval_deep

This commit is contained in:
Avi Dessauer 2022-01-05 10:14:48 -05:00
parent 2d0d9b80d7
commit 3fe998a48e
3 changed files with 31 additions and 2 deletions

View File

@ -665,6 +665,29 @@ pub fn eval_full<R>(
global_env: &Environment,
resolver: &mut R,
) -> Result<RichTerm, EvalError>
where
R: ImportResolver,
{
eval_internal(t0, global_env, resolver).map(|(term, env)| subst(term, global_env, &env))
}
/// Fully evaluates a Nickel term like `eval_full`, but does not substitute all variables.
pub fn eval_deep<R>(
t0: RichTerm,
global_env: &Environment,
resolver: &mut R,
) -> Result<RichTerm, EvalError>
where
R: ImportResolver,
{
eval_internal(t0, global_env, resolver).map(|(term, _)| term)
}
fn eval_internal<R>(
t0: RichTerm,
global_env: &Environment,
resolver: &mut R,
) -> Result<(RichTerm, Environment), EvalError>
where
R: ImportResolver,
{
@ -681,7 +704,6 @@ where
),
);
eval_closure(Closure::atomic_closure(wrapper), global_env, resolver, true)
.map(|(term, env)| subst(term, global_env, &env))
}
/// Evaluate a Nickel Term, stopping when a meta value is encountered at the top-level without

View File

@ -89,6 +89,13 @@ impl Program {
eval::eval_full(t, &global_env, &mut self.cache).map_err(|e| e.into())
}
/// Same as `eval`, but proceeds to a full evaluation.
/// Same as `eval_full`, but does not substitute all variables.
pub fn eval_deep(&mut self) -> Result<RichTerm, Error> {
let (t, global_env) = self.prepare_eval()?;
eval::eval_deep(t, &global_env, &mut self.cache).map_err(|e| e.into())
}
/// Wrapper for [`query`](./fn.query.html).
pub fn query(&mut self, path: Option<String>) -> Result<Term, Error> {
let global_env = self.cache.prepare_stdlib()?;

View File

@ -8,7 +8,7 @@ fn eval_file(file: &str) -> Result<RichTerm, Error> {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push(format!("examples/{}", file));
let mut p = Program::new_from_file(path).expect("could not load file as a program");
p.eval_full()
p.eval_deep()
}
#[test]