feat: at least it compiles, right?

This commit is contained in:
2025-06-12 20:12:31 +08:00
parent 7293cb9f75
commit 49255948ff
22 changed files with 383 additions and 251 deletions

View File

@@ -1,52 +1,63 @@
use std::sync::RwLock;
use std::rc::Rc;
use hashbrown::HashSet;
use lru::LruCache;
use priority_queue::PriorityQueue;
use crate::env::VmEnv;
use crate::error::Result;
use crate::eval::Evaluate;
use crate::ir::{Downgraded, Ir};
use crate::ty::public::Value;
use crate::ty::internal as i;
use crate::error::Result;
use crate::ty::public::Value;
#[cfg(test)]
mod test;
type ThunkIdx = usize;
type EnvIdx = usize;
pub struct Engine {
thunks: Box<[RwLock<Thunk>]>,
func_offset: usize,
tasks: PriorityQueue<CompileTask, usize>
thunks: Box<[Ir]>,
pub func_offset: usize,
tasks: PriorityQueue<CompileTask, usize>,
lru: LruCache<(ThunkIdx, EnvIdx), i::Value>,
}
pub fn eval(downgraded: Downgraded) -> Result<Value> {
let mut engine = Engine::new(downgraded.thunks, downgraded.func_offset);
engine.eval(downgraded.top_level, &VmEnv::new(vec![])).map(|mut val| Ok(val.force(&engine)?.to_public(&engine, &mut HashSet::new())))?
engine
.eval(downgraded.top_level, &mut VmEnv::new())
.map(|mut val| {
Ok(val
.force(&mut engine)?
.to_public(&engine, &mut HashSet::new()))
})?
}
impl Engine {
pub fn new(thunks: Box<[Ir]>, func_offset: usize) -> Self {
Self {
thunks: thunks.into_iter().map(Thunk::new).map(RwLock::new).collect(),
lru: LruCache::new(thunks.len().clamp(1, usize::MAX).try_into().unwrap()),
thunks,
func_offset,
tasks: PriorityQueue::new()
tasks: PriorityQueue::new(),
}
}
pub fn eval(&mut self, expr: Ir, env: &VmEnv) -> Result<i::Value> {
pub fn eval(&mut self, expr: Ir, env: &mut VmEnv) -> Result<i::Value> {
expr.eval(self, env)
}
pub fn eval_thunk(&self, idx: usize, env: &VmEnv) -> Result<i::Value> {
todo!()
pub fn eval_thunk(&mut self, idx: usize, env: &mut VmEnv) -> Result<i::Value> {
self.thunks[idx].clone().eval(self, env)
}
}
enum Thunk {
Expr(Ir),
Compiling,
Compiled(fn(Rc<VmEnv>) -> Value)
Compiled(fn(Rc<VmEnv>) -> Value),
}
impl Thunk {
@@ -57,5 +68,5 @@ impl Thunk {
#[derive(Hash, PartialEq, Eq)]
struct CompileTask {
idx: usize
idx: usize,
}