chore: cargo fmt

This commit is contained in:
2025-06-17 11:59:53 +08:00
parent 7f6848c9e5
commit 3e9f0a72a0
7 changed files with 65 additions and 38 deletions

View File

@@ -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) => {

View File

@@ -24,7 +24,11 @@ pub struct Engine {
}
pub fn eval(downgraded: Downgraded) -> Result<Value> {
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<i::Value> {
@@ -65,7 +72,7 @@ impl Engine {
let val = self.thunks[idx].clone().eval(self, env)?;
env.insert_cache(idx, val)
}
},
}
Dep::Thunk(idx) => {
let val = self.thunks[idx].clone().eval(self, env)?;
env.insert_cache(idx, val)

View File

@@ -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<Value>) -> Result<Value> {
pub fn lookup_cache(
&mut self,
idx: usize,
f: impl FnOnce(&mut VmEnv) -> Result<Value>,
) -> Result<Value> {
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)
}

View File

@@ -180,8 +180,7 @@ impl Evaluate for ir::If {
impl Evaluate for ir::LoadFunc {
fn eval(self, engine: &mut Engine, env: &mut VmEnv) -> Result<Value> {
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<Value> {
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::<Result<_>>()?, engine, env)?;
func.call(
self.args
.into_iter()
.map(|arg| arg.eval(engine, env))
.collect::<Result<_>>()?,
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<Value> {
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)))
})
}
}

View File

@@ -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(())
@@ -200,9 +201,15 @@ impl DowngradeContext {
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)
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;
@@ -224,9 +231,12 @@ impl DowngradeContext {
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)
},
std::ptr::write(
&mut thunk.0,
Ir::Const(super::Const { val: Const::Null }),
);
return Err(err);
}
}
}
Ok(())

View File

@@ -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)]