feat: initial parallel impl

This commit is contained in:
2025-06-08 17:27:43 +08:00
parent 3797544fc2
commit 7293cb9f75
18 changed files with 529 additions and 934 deletions

View File

@@ -1,29 +1,61 @@
use hashbrown::HashSet;
use std::sync::RwLock;
use std::rc::Rc;
use hashbrown::HashSet;
use priority_queue::PriorityQueue;
use crate::env::VmEnv;
use crate::eval::Evaluate;
use crate::ir::{Downgraded, Ir};
use crate::ty::public::Value;
use crate::ty::internal as i;
use crate::error::Result;
#[cfg(test)]
mod test;
pub struct Engine {
thunks: Box<[RwLock<Thunk>]>,
func_offset: usize,
tasks: PriorityQueue<CompileTask, usize>
}
pub fn eval(downgraded: Downgraded) -> Result<Value> {
let mut engine = Engine::new();
engine.eval(downgraded.top_level)
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())))?
}
impl Engine {
pub fn new() -> Self {
Self {}
pub fn new(thunks: Box<[Ir]>, func_offset: usize) -> Self {
Self {
thunks: thunks.into_iter().map(Thunk::new).map(RwLock::new).collect(),
func_offset,
tasks: PriorityQueue::new()
}
}
pub fn eval(&mut self, expr: Ir) -> Result<Value> {
expr.eval(self).map(|val| val.to_public(self, &mut HashSet::new()))
pub fn eval(&mut self, expr: Ir, env: &VmEnv) -> Result<i::Value> {
expr.eval(self, env)
}
pub fn eval_thunk(&self, idx: usize, env: &VmEnv) -> Result<i::Value> {
todo!()
}
}
enum Thunk {
Expr(Ir),
Compiling,
Compiled(fn(Rc<VmEnv>) -> Value)
}
impl Thunk {
fn new(ir: Ir) -> Thunk {
Thunk::Expr(ir)
}
}
#[derive(Hash, PartialEq, Eq)]
struct CompileTask {
idx: usize
}