1
1
mirror of https://github.com/tweag/nickel.git synced 2024-09-21 00:19:57 +03:00

Cargo fmt

This commit is contained in:
Daniele 2022-10-10 10:11:24 +02:00
parent 7bd496d7b8
commit 844e5c1215
4 changed files with 62 additions and 35 deletions

View File

@ -148,9 +148,12 @@ fn merge_incompatible_defaults() {
#[test]
fn imports() {
let mut vm = VirtualMachine::new(SimpleResolver::new());
vm.import_resolver_mut().add_source(String::from("two"), String::from("1 + 1"));
vm.import_resolver_mut().add_source(String::from("lib"), String::from("{f = true}"));
vm.import_resolver_mut().add_source(String::from("bad"), String::from("^$*/.23ab 0°@"));
vm.import_resolver_mut()
.add_source(String::from("two"), String::from("1 + 1"));
vm.import_resolver_mut()
.add_source(String::from("lib"), String::from("{f = true}"));
vm.import_resolver_mut()
.add_source(String::from("bad"), String::from("^$*/.23ab 0°@"));
vm.import_resolver_mut().add_source(
String::from("nested"),
String::from("let x = import \"two\" in x + 1"),
@ -168,7 +171,7 @@ fn imports() {
var: &str,
import: &str,
body: RichTerm,
vm: &mut VirtualMachine<R>
vm: &mut VirtualMachine<R>,
) -> Result<RichTerm, ImportError>
where
R: ImportResolver,

View File

@ -43,7 +43,7 @@ pub struct Program {
/// The cache holding the sources and parsed terms of the main source as well as imports.
//cache: Cache,
///
vm: VirtualMachine<Cache>
vm: VirtualMachine<Cache>,
}
impl Program {
@ -80,8 +80,13 @@ impl Program {
eval_env,
type_ctxt,
} = self.vm.import_resolver_mut().prepare_stdlib()?;
self.vm.import_resolver_mut().prepare(self.main_id, &type_ctxt)?;
Ok((self.vm.import_resolver().get(self.main_id).unwrap(), eval_env))
self.vm
.import_resolver_mut()
.prepare(self.main_id, &type_ctxt)?;
Ok((
self.vm.import_resolver().get(self.main_id).unwrap(),
eval_env,
))
}
/// Parse if necessary, typecheck and then evaluate the program.
@ -116,12 +121,14 @@ impl Program {
self.vm.import_resolver_mut().parse(self.main_id)?;
self.vm.import_resolver_mut().load_stdlib()?;
let initial_env = self.vm.import_resolver().mk_type_ctxt().expect("program::typecheck(): stdlib has been loaded but was not found in cache on mk_types_env()");
self.vm.import_resolver_mut()
self.vm
.import_resolver_mut()
.resolve_imports(self.main_id)
.map_err(|cache_err| {
cache_err.unwrap_error("program::typecheck(): expected source to be parsed")
})?;
self.vm.import_resolver_mut()
self.vm
.import_resolver_mut()
.typecheck(self.main_id, &initial_env)
.map_err(|cache_err| {
cache_err.unwrap_error("program::typecheck(): expected source to be parsed")
@ -192,7 +199,8 @@ pub fn query(
initial_env: &Envs,
path: Option<String>,
) -> Result<Term, Error> {
vm.import_resolver_mut().prepare(file_id, &initial_env.type_ctxt)?;
vm.import_resolver_mut()
.prepare(file_id, &initial_env.type_ctxt)?;
let t = if let Some(p) = path {
// Parsing `y.path`. We `seq` it to force the evaluation of the underlying value,
// which can be then showed to the user. The newline gives better messages in case of

View File

@ -79,7 +79,7 @@ pub struct ReplImpl {
/// typecheck imports in a fresh environment.
initial_type_ctxt: typecheck::Context,
///
vm: VirtualMachine<Cache>
vm: VirtualMachine<Cache>,
}
impl ReplImpl {
@ -134,13 +134,21 @@ impl ReplImpl {
id: Option<Ident>,
t: RichTerm,
) -> Result<RichTerm, Error> {
let (t, pending) = import_resolution::resolve_imports(t, repl_impl.vm.import_resolver_mut())?;
let (t, pending) =
import_resolution::resolve_imports(t, repl_impl.vm.import_resolver_mut())?;
for id in &pending {
repl_impl.vm.import_resolver_mut().resolve_imports(*id).unwrap();
repl_impl
.vm
.import_resolver_mut()
.resolve_imports(*id)
.unwrap();
}
let wildcards =
typecheck::type_check(&t, repl_impl.env.type_ctxt.clone(), repl_impl.vm.import_resolver())?;
let wildcards = typecheck::type_check(
&t,
repl_impl.env.type_ctxt.clone(),
repl_impl.vm.import_resolver(),
)?;
if let Some(id) = id {
typecheck::env_add(
@ -160,7 +168,8 @@ impl ReplImpl {
for id in &pending {
repl_impl
.vm.import_resolver_mut()
.vm
.import_resolver_mut()
.typecheck(*id, &repl_impl.initial_type_ctxt)
.map_err(|cache_err| {
cache_err.unwrap_error("repl::eval_(): expected imports to be parsed")
@ -171,7 +180,8 @@ impl ReplImpl {
.map_err(|err| Error::ParseErrors(err.into()))?;
for id in &pending {
repl_impl
.vm.import_resolver_mut()
.vm
.import_resolver_mut()
.transform(*id)
.unwrap_or_else(|_| panic!("repl::eval_(): expected imports to be parsed"));
}
@ -182,12 +192,7 @@ impl ReplImpl {
match term {
ExtendedTerm::RichTerm(t) => {
let t = prepare(self, None, t)?;
Ok(eval_function(
&mut self.vm,
t,
&self.env.eval_env,
)?
.into())
Ok(eval_function(&mut self.vm, t, &self.env.eval_env)?.into())
}
ExtendedTerm::ToplevelLet(id, t) => {
let t = prepare(self, Some(id.clone()), t)?;
@ -210,7 +215,8 @@ impl Repl for ReplImpl {
fn load(&mut self, path: impl AsRef<OsStr>) -> Result<RichTerm, Error> {
let file_id = self
.vm.import_resolver_mut()
.vm
.import_resolver_mut()
.add_file(OsString::from(path.as_ref()))
.map_err(IOError::from)?;
self.vm.import_resolver_mut().parse(file_id)?;
@ -226,12 +232,16 @@ impl Repl for ReplImpl {
)))
}
};
self.vm.import_resolver_mut().transform_inner(file_id).map_err(|err| {
err.unwrap_error("load(): expected term to be parsed before transformation")
})?;
self.vm
.import_resolver_mut()
.transform_inner(file_id)
.map_err(|err| {
err.unwrap_error("load(): expected term to be parsed before transformation")
})?;
let term = self.vm.import_resolver().get_owned(file_id).unwrap();
let (term, pending) = import_resolution::resolve_imports(term, self.vm.import_resolver_mut())?;
let (term, pending) =
import_resolution::resolve_imports(term, self.vm.import_resolver_mut())?;
for id in &pending {
self.vm.import_resolver_mut().resolve_imports(*id).unwrap();
}
@ -248,14 +258,19 @@ impl Repl for ReplImpl {
}
fn typecheck(&mut self, exp: &str) -> Result<Types, Error> {
let file_id = self.vm.import_resolver_mut().add_tmp("<repl-typecheck>", String::from(exp));
let file_id = self
.vm
.import_resolver_mut()
.add_tmp("<repl-typecheck>", String::from(exp));
// We ignore non fatal errors while type checking.
let (term, _) = self.vm.import_resolver().parse_nocache(file_id)?;
let (term, pending) = import_resolution::resolve_imports(term, self.vm.import_resolver_mut())?;
let (term, pending) =
import_resolution::resolve_imports(term, self.vm.import_resolver_mut())?;
for id in &pending {
self.vm.import_resolver_mut().resolve_imports(*id).unwrap();
}
let wildcards = typecheck::type_check(&term, self.env.type_ctxt.clone(), self.vm.import_resolver())?;
let wildcards =
typecheck::type_check(&term, self.env.type_ctxt.clone(), self.vm.import_resolver())?;
// Substitute the wildcard types for their inferred types
// We need to `traverse` the term, in case the type depends on inner terms that also contain wildcards
let term = term
@ -281,7 +296,10 @@ impl Repl for ReplImpl {
fn query(&mut self, exp: &str) -> Result<Term, Error> {
use crate::program;
let file_id = self.vm.import_resolver_mut().add_tmp("<repl-query>", String::from(exp));
let file_id = self
.vm
.import_resolver_mut()
.add_tmp("<repl-query>", String::from(exp));
program::query(&mut self.vm, file_id, &self.env, None)
}

View File

@ -148,9 +148,7 @@ pub fn bench_terms<'r>(rts: Vec<Bench<'r>>) -> Box<dyn Fn(&mut Criterion) + 'r>
c_local.typecheck(id, &type_ctxt).unwrap();
} else {
c_local.prepare(id, &type_ctxt).unwrap();
VirtualMachine::new(c_local)
.eval(t, &eval_env)
.unwrap();
VirtualMachine::new(c_local).eval(t, &eval_env).unwrap();
}
},
criterion::BatchSize::LargeInput,