feat: better builtins implementaion
get rid of circular references
This commit is contained in:
@@ -3,12 +3,12 @@ use std::rc::Rc;
|
||||
|
||||
use rpds::HashTrieMap;
|
||||
|
||||
use crate::ty::internal::Value;
|
||||
use crate::ty::internal::{AttrSet, Value};
|
||||
|
||||
#[derive(Debug, Default, PartialEq)]
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Env<'vm> {
|
||||
last: RefCell<Option<Rc<Env<'vm>>>>,
|
||||
pub map: RefCell<HashTrieMap<usize, Value<'vm>>>,
|
||||
map: RefCell<HashTrieMap<usize, Value<'vm>>>,
|
||||
}
|
||||
|
||||
impl Clone for Env<'_> {
|
||||
@@ -38,10 +38,10 @@ impl<'vm> Env<'vm> {
|
||||
self.map.borrow_mut().insert_mut(symbol, value);
|
||||
}
|
||||
|
||||
pub fn enter(&self, new: HashTrieMap<usize, Value<'vm>>) {
|
||||
pub fn enter(&self, new: impl Iterator<Item = (usize, Value<'vm>)>) {
|
||||
let mut map = self.map.borrow().clone();
|
||||
for (k, v) in new.iter() {
|
||||
map.insert_mut(k.clone(), v.clone());
|
||||
for (k, v) in new {
|
||||
map.insert_mut(k, v);
|
||||
}
|
||||
let last = Env {
|
||||
last: self.last.clone(),
|
||||
@@ -51,19 +51,22 @@ impl<'vm> Env<'vm> {
|
||||
*self.map.borrow_mut() = map;
|
||||
}
|
||||
|
||||
pub fn enter_rec(self: &mut Rc<Self>, new: Rc<Env<'vm>>) {
|
||||
let last = (*self.last.borrow_mut()).take();
|
||||
*self = new;
|
||||
*self.last.borrow_mut() = last;
|
||||
}
|
||||
|
||||
pub fn new_rec(self: Rc<Self>) -> Rc<Self> {
|
||||
pub fn enter_with(&self, new: Rc<AttrSet<'vm>>) {
|
||||
let mut map = self.map.borrow().clone();
|
||||
for (k, v) in new.as_inner().iter() {
|
||||
let v = if let Value::Builtins = v {
|
||||
Value::AttrSet(new.clone())
|
||||
} else {
|
||||
v.clone()
|
||||
};
|
||||
map.insert_mut(k.clone(), v);
|
||||
}
|
||||
let last = Env {
|
||||
last: self.last.clone(),
|
||||
map: self.map.clone(),
|
||||
};
|
||||
*self.last.borrow_mut() = Some(Rc::new(last));
|
||||
self.clone()
|
||||
*self.map.borrow_mut() = map;
|
||||
}
|
||||
|
||||
pub fn leave(&self) {
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
use std::pin::Pin;
|
||||
|
||||
use inkwell::types::{BasicType, BasicTypeEnum, FunctionType, StructType};
|
||||
use inkwell::values::{BasicValueEnum, FunctionValue, IntValue};
|
||||
use inkwell::{AddressSpace, OptimizationLevel};
|
||||
use inkwell::builder::Builder;
|
||||
use inkwell::context::Context;
|
||||
use inkwell::execution_engine::ExecutionEngine;
|
||||
use inkwell::module::Module;
|
||||
use inkwell::types::{BasicType, BasicTypeEnum, FunctionType, StructType};
|
||||
use inkwell::values::{BasicValueEnum, FunctionValue, IntValue};
|
||||
use inkwell::{AddressSpace, OptimizationLevel};
|
||||
|
||||
use crate::stack::Stack;
|
||||
|
||||
@@ -21,13 +21,13 @@ pub enum ValueTag {
|
||||
List,
|
||||
Function,
|
||||
Thunk,
|
||||
Path
|
||||
Path,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct JITValue {
|
||||
tag: ValueTag,
|
||||
data: u64
|
||||
data: u64,
|
||||
}
|
||||
|
||||
pub struct JITContext<'ctx> {
|
||||
@@ -50,7 +50,10 @@ impl<'ctx> JITContext<'ctx> {
|
||||
let int_type = context.i64_type();
|
||||
let pointer_type = context.ptr_type(AddressSpace::default());
|
||||
let value_type = context.struct_type(&[int_type.into(), int_type.into()], false);
|
||||
let func_type = value_type.fn_type(&[pointer_type.into(), pointer_type.into(), value_type.into()], false);
|
||||
let func_type = value_type.fn_type(
|
||||
&[pointer_type.into(), pointer_type.into(), value_type.into()],
|
||||
false,
|
||||
);
|
||||
|
||||
Pin::new(Box::new(JITContext {
|
||||
execution_engine: module
|
||||
@@ -71,7 +74,5 @@ impl<'ctx> JITContext<'ctx> {
|
||||
self.context.i64_type().const_int(int as u64, false)
|
||||
}
|
||||
|
||||
pub fn start_trace(&mut self) {
|
||||
|
||||
}
|
||||
pub fn start_trace(&mut self) {}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ use std::pin::Pin;
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::builtins::env;
|
||||
use crate::bytecode::{BinOp, OpCode, OpCodes, Program, UnOp, Func as F};
|
||||
use crate::bytecode::{BinOp, Func as F, OpCode, OpCodes, Program, UnOp};
|
||||
use crate::error::*;
|
||||
use crate::ty::internal::*;
|
||||
use crate::ty::public::{self as p, Symbol};
|
||||
@@ -31,11 +31,13 @@ pub fn run(prog: Program, jit: Pin<Box<JITContext<'_>>>) -> Result<p::Value> {
|
||||
RefCell::new(prog.symbols),
|
||||
RefCell::new(prog.symmap),
|
||||
prog.consts,
|
||||
jit
|
||||
jit,
|
||||
);
|
||||
let env = env(&vm);
|
||||
let mut seen = HashSet::new();
|
||||
let value = vm.eval(prog.top_level.into_iter(), env)?.to_public(&vm, &mut seen);
|
||||
let value = vm
|
||||
.eval(prog.top_level.into_iter(), env)?
|
||||
.to_public(&vm, &mut seen);
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
@@ -46,7 +48,7 @@ pub struct VM<'jit> {
|
||||
symbols: RefCell<Vec<EcoString>>,
|
||||
symmap: RefCell<HashMap<EcoString, usize>>,
|
||||
consts: Box<[Const]>,
|
||||
jit: Pin<Box<JITContext<'jit>>>
|
||||
jit: Pin<Box<JITContext<'jit>>>,
|
||||
}
|
||||
|
||||
impl<'vm, 'jit: 'vm> VM<'jit> {
|
||||
@@ -58,7 +60,7 @@ impl<'vm, 'jit: 'vm> VM<'jit> {
|
||||
&self.funcs[idx]
|
||||
}
|
||||
|
||||
pub fn get_sym(&self, idx: usize) -> Symbol{
|
||||
pub fn get_sym(&self, idx: usize) -> Symbol {
|
||||
self.symbols.borrow()[idx].clone().into()
|
||||
}
|
||||
|
||||
@@ -67,13 +69,19 @@ impl<'vm, 'jit: 'vm> VM<'jit> {
|
||||
if let Some(&idx) = self.symmap.borrow().get(&sym) {
|
||||
idx
|
||||
} else {
|
||||
self.symmap.borrow_mut().insert(sym.clone(), self.symbols.borrow().len());
|
||||
self.symmap
|
||||
.borrow_mut()
|
||||
.insert(sym.clone(), self.symbols.borrow().len());
|
||||
self.symbols.borrow_mut().push(sym);
|
||||
self.symbols.borrow().len() - 1
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eval(&'vm self, opcodes: impl Iterator<Item = OpCode>, env: Rc<Env<'vm>>) -> Result<Value<'vm>> {
|
||||
pub fn eval(
|
||||
&'vm self,
|
||||
opcodes: impl Iterator<Item = OpCode>,
|
||||
env: Rc<Env<'vm>>,
|
||||
) -> Result<Value<'vm>> {
|
||||
let mut stack = Stack::<_, STACK_SIZE>::new();
|
||||
let mut iter = opcodes.into_iter();
|
||||
while let Some(opcode) = iter.next() {
|
||||
@@ -101,12 +109,10 @@ impl<'vm, 'jit: 'vm> VM<'jit> {
|
||||
OpCode::LoadThunk { idx } => {
|
||||
stack.push(Value::Thunk(Thunk::new(self.get_thunk(idx)).into()))?
|
||||
}
|
||||
OpCode::CaptureEnv => {
|
||||
match stack.tos()? {
|
||||
Value::Thunk(thunk) => thunk.capture(env.clone()),
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
OpCode::CaptureEnv => match stack.tos()? {
|
||||
Value::Thunk(thunk) => thunk.capture(env.clone()),
|
||||
_ => (),
|
||||
},
|
||||
OpCode::ForceValue => {
|
||||
stack.tos_mut()?.force(self)?;
|
||||
}
|
||||
@@ -181,12 +187,16 @@ impl<'vm, 'jit: 'vm> VM<'jit> {
|
||||
stack.push(Value::AttrSet(AttrSet::empty().into()))?;
|
||||
}
|
||||
OpCode::FinalizeRec => {
|
||||
env.enter(stack.tos()?.clone().unwrap_attr_set().as_inner().clone());
|
||||
stack
|
||||
.tos_mut()?
|
||||
.as_mut()
|
||||
.unwrap_attr_set()
|
||||
.capture(env);
|
||||
env.enter(
|
||||
stack
|
||||
.tos()?
|
||||
.clone()
|
||||
.unwrap_attr_set()
|
||||
.as_inner()
|
||||
.iter()
|
||||
.map(|(k, v)| (k.clone(), v.clone())),
|
||||
);
|
||||
stack.tos_mut()?.as_mut().unwrap_attr_set().capture(env);
|
||||
}
|
||||
OpCode::PushStaticAttr { name } => {
|
||||
let val = stack.pop();
|
||||
@@ -237,13 +247,12 @@ impl<'vm, 'jit: 'vm> VM<'jit> {
|
||||
stack.tos_mut()?.force(self)?.has_attr(sym);
|
||||
}
|
||||
OpCode::LookUp { sym } => {
|
||||
stack.push(
|
||||
env.lookup(sym)
|
||||
.ok_or_else(|| Error::EvalError(format!("{} not found", self.get_sym(sym))))?,
|
||||
)?;
|
||||
stack.push(env.lookup(sym).ok_or_else(|| {
|
||||
Error::EvalError(format!("{} not found", self.get_sym(sym)))
|
||||
})?)?;
|
||||
}
|
||||
OpCode::EnterEnv => match stack.pop() {
|
||||
Value::AttrSet(attrs) => env.enter(attrs.as_inner().clone()),
|
||||
Value::AttrSet(attrs) => env.enter_with(attrs),
|
||||
|
||||
_ => unreachable!(),
|
||||
},
|
||||
|
||||
@@ -141,7 +141,7 @@ fn test_attrs() {
|
||||
test_expr(
|
||||
"{ a = 1; }",
|
||||
attrs! {
|
||||
symbol!("a") => thunk!()
|
||||
symbol!("a") => int!(1)
|
||||
},
|
||||
);
|
||||
test_expr("{ a = 1; }.a", int!(1));
|
||||
@@ -149,18 +149,18 @@ fn test_attrs() {
|
||||
test_expr(
|
||||
"{ a = { a = 1; }; }.a",
|
||||
attrs! {
|
||||
symbol!("a") => thunk!()
|
||||
symbol!("a") => int!(1)
|
||||
},
|
||||
);
|
||||
test_expr("{ a.b = 1; }.a.b", int!(1));
|
||||
test_expr(
|
||||
"{ a.b = 1; a.c = 2; }",
|
||||
attrs! { symbol!("a") => attrs!{ symbol!("b") => thunk!(), symbol!("c") => thunk!() } },
|
||||
attrs! { symbol!("a") => attrs!{ symbol!("b") => int!(1), symbol!("c") => int!(2) } },
|
||||
);
|
||||
test_expr("{ a.b = 1; } ? a.b", boolean!(true));
|
||||
test_expr(
|
||||
"{ a.b = 1; } // { a.c = 2; }",
|
||||
attrs! { symbol!("a") => attrs!{ symbol!("c") => thunk!() } },
|
||||
attrs! { symbol!("a") => attrs!{ symbol!("c") => int!(2) } },
|
||||
);
|
||||
}
|
||||
|
||||
@@ -181,7 +181,7 @@ fn test_let() {
|
||||
test_expr(r#"let a = { a = 1; }; b = "a"; in a.${b}"#, int!(1));
|
||||
test_expr(
|
||||
r#"let b = "c"; in { a.b = 1; } // { a."a${b}" = 2; }"#,
|
||||
attrs! { symbol!("a") => attrs!{ symbol!("ac") => thunk!() } },
|
||||
attrs! { symbol!("a") => attrs!{ symbol!("ac") => int!(2) } },
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user