diff --git a/src/bin/repl.rs b/src/bin/repl.rs index 124feef..aac8dd2 100644 --- a/src/bin/repl.rs +++ b/src/bin/repl.rs @@ -2,9 +2,9 @@ use itertools::Itertools; use rustyline::error::ReadlineError; use rustyline::{DefaultEditor, Result}; -use nixjit::ir::downgrade; -use nixjit::error::Error; use nixjit::engine::eval; +use nixjit::error::Error; +use nixjit::ir::downgrade; macro_rules! unwrap { ($e:expr) => { diff --git a/src/engine/mod.rs b/src/engine/mod.rs index ad5e744..4527719 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -24,7 +24,11 @@ pub struct Engine { } pub fn eval(downgraded: Downgraded) -> Result { - let mut engine = Engine::new(downgraded.thunks, downgraded.func_offset, downgraded.func_deps); + let mut engine = Engine::new( + downgraded.thunks, + downgraded.func_offset, + downgraded.func_deps, + ); engine.eval(downgraded.graph) } @@ -34,7 +38,7 @@ impl Engine { tasks: PriorityQueue::new(), thunks, func_offset, - func_deps + func_deps, } } @@ -49,8 +53,11 @@ impl Engine { env.insert_cache(member, val); } } - env.lookup_cache(last, |_| unreachable!()) - .map(|mut val| Ok(val.force(self, &mut env)?.to_public(self, &mut HashSet::new())))? + env.lookup_cache(last, |_| unreachable!()).map(|mut val| { + Ok(val + .force(self, &mut env)? + .to_public(self, &mut HashSet::new())) + })? } pub fn eval_thunk(&mut self, idx: usize, env: &mut VmEnv) -> Result { @@ -65,8 +72,8 @@ impl Engine { let val = self.thunks[idx].clone().eval(self, env)?; env.insert_cache(idx, val) } - }, - Dep::Thunk(idx) =>{ + } + Dep::Thunk(idx) => { let val = self.thunks[idx].clone().eval(self, env)?; env.insert_cache(idx, val) } diff --git a/src/env.rs b/src/env.rs index 1465911..3463f6b 100644 --- a/src/env.rs +++ b/src/env.rs @@ -4,9 +4,9 @@ use std::rc::{Rc, Weak}; use ecow::EcoString; use hashbrown::HashMap; +use crate::error::{Error, Result}; use crate::stack::Stack; use crate::ty::internal::{EnvRef, Value}; -use crate::error::{Error, Result}; #[derive(Clone)] pub struct VmEnv { @@ -63,7 +63,11 @@ impl VmEnv { self.cache.last_mut().unwrap().insert(idx, val); } - pub fn lookup_cache(&mut self, idx: usize, f: impl FnOnce(&mut VmEnv) -> Result) -> Result { + pub fn lookup_cache( + &mut self, + idx: usize, + f: impl FnOnce(&mut VmEnv) -> Result, + ) -> Result { for level in self.cache.iter().rev() { if let Some(ret) = level.get(&idx) { return ret.clone().ok(); @@ -87,7 +91,6 @@ impl VmEnv { None } - pub fn enter_arg(&mut self, arg: Value) { self.args.push(arg) } diff --git a/src/eval/mod.rs b/src/eval/mod.rs index 0cef6cf..be8bdb5 100644 --- a/src/eval/mod.rs +++ b/src/eval/mod.rs @@ -180,8 +180,7 @@ impl Evaluate for ir::If { impl Evaluate for ir::LoadFunc { fn eval(self, engine: &mut Engine, env: &mut VmEnv) -> Result { let idx = engine.func_offset + self.idx; - Value::Func(idx) - .ok() + Value::Func(idx).ok() } } @@ -189,7 +188,14 @@ impl Evaluate for ir::Call { fn eval(self, engine: &mut Engine, env: &mut VmEnv) -> Result { let mut func = self.func.eval(engine, env)?; func.force(engine, env)?; - func.call(self.args.into_iter().map(|arg| arg.eval(engine, env)).collect::>()?, engine, env)?; + func.call( + self.args + .into_iter() + .map(|arg| arg.eval(engine, env)) + .collect::>()?, + engine, + env, + )?; // FIXME: Modify Value::call // for arg in self.args { // func.call(arg.eval(engine, env)?, engine, env)?; @@ -263,8 +269,9 @@ impl Evaluate for ir::Const { impl Evaluate for ir::Var { fn eval(self, _: &mut Engine, env: &mut VmEnv) -> Result { - env.lookup_with(&self.sym) - .ok_or_else(|| Error::EvalError(format!("variable {} not found", Symbol::from(self.sym)))) + env.lookup_with(&self.sym).ok_or_else(|| { + Error::EvalError(format!("variable {} not found", Symbol::from(self.sym))) + }) } } diff --git a/src/ir/ctx.rs b/src/ir/ctx.rs index 3126113..da40f17 100644 --- a/src/ir/ctx.rs +++ b/src/ir/ctx.rs @@ -2,7 +2,10 @@ use derive_more::Unwrap; use ecow::EcoString; use hashbrown::{HashMap, HashSet}; -use crate::{error::{Error, Result}, ty::common::Const}; +use crate::{ + error::{Error, Result}, + ty::common::Const, +}; use super::{Dep, Func, Ir, LoadFunc, MaybeThunk, SccAnalyzer, SccNode, Thunk}; @@ -107,9 +110,7 @@ impl<'a, 'env> Env<'a, 'env> { } SingleArg(arg) => { if arg == ident { - return Ok(LookupResult::SingleArg { - level: arg_level, - }); + return Ok(LookupResult::SingleArg { level: arg_level }); } else { arg_level += 1; } @@ -121,9 +122,7 @@ impl<'a, 'env> Env<'a, 'env> { default: default.clone(), }); } else if alias.as_ref() == Some(ident) { - return Ok(LookupResult::SingleArg { - level: arg_level, - }); + return Ok(LookupResult::SingleArg { level: arg_level }); } else { arg_level += 1; } @@ -173,10 +172,12 @@ impl DowngradeContext { match this { Index::Thunk(idx) => { if dep == Dep::Thunk(idx) { - return Err(Error::DowngradeError("infinite recursion encountered".into())) + return Err(Error::DowngradeError( + "infinite recursion encountered".into(), + )); } self.thunk_deps[idx].insert(dep.unwrap_thunk()) - }, + } Index::Func(idx) => self.func_deps[idx].insert(dep), }; Ok(()) @@ -198,11 +199,17 @@ impl DowngradeContext { unsafe { let old = std::ptr::read(func); match old.resolve(Index::Func(idx), self_ptr.as_mut().unwrap(), env) { - Ok(ok) => std::ptr::write(func, ok), - Err(err) => { - std::ptr::write(func, Func { param: crate::ir::Param::Ident(EcoString::new()), body: super::Const { val: Const::Null }.ir().boxed() }); - return Err(err) - }, + Ok(ok) => std::ptr::write(func, ok), + Err(err) => { + std::ptr::write( + func, + Func { + param: crate::ir::Param::Ident(EcoString::new()), + body: super::Const { val: Const::Null }.ir().boxed(), + }, + ); + return Err(err); + } } } Ok(()) @@ -212,7 +219,7 @@ impl DowngradeContext { pub fn resolve_thunk(&mut self, idx: usize, env: &Env) -> Result<()> { if self.thunks[idx].1 { - return Ok(()) + return Ok(()); } self.thunks[idx].1 = true; let self_ptr = self as *mut Self; @@ -222,11 +229,14 @@ impl DowngradeContext { unsafe { let (old, _) = std::ptr::read(thunk); match old.resolve(Index::Thunk(idx), self_ptr.as_mut().unwrap(), env) { - Ok(ok) => std::ptr::write(&mut thunk.0, ok), - Err(err) => { - std::ptr::write(&mut thunk.0, Ir::Const(super::Const { val: Const::Null })); - return Err(err) - }, + Ok(ok) => std::ptr::write(&mut thunk.0, ok), + Err(err) => { + std::ptr::write( + &mut thunk.0, + Ir::Const(super::Const { val: Const::Null }), + ); + return Err(err); + } } } Ok(()) diff --git a/src/ir/scc.rs b/src/ir/scc.rs index 1066654..90a17bc 100644 --- a/src/ir/scc.rs +++ b/src/ir/scc.rs @@ -12,7 +12,7 @@ pub struct SccNode { #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Unwrap)] pub enum Dep { Thunk(usize), - Arg(usize) + Arg(usize), } #[derive(Default, Debug)] diff --git a/src/ty/internal/mod.rs b/src/ty/internal/mod.rs index 2a65d74..1ed3666 100644 --- a/src/ty/internal/mod.rs +++ b/src/ty/internal/mod.rs @@ -208,7 +208,7 @@ impl Value { let len = args.len() + old.len(); env.enter_args(std::mem::take(old)); env.enter_cache_level(|env| { - let mut args = args.into_iter().peekable(); + let mut args = args.into_iter().peekable(); env.enter_arg(args.next().unwrap()); let mut ret = engine.eval_thunk(idx, env)?; while args.peek().is_some() {