feat: better builtins implementaion

get rid of circular references
This commit is contained in:
2025-05-17 18:31:36 +08:00
parent 8480e0891b
commit ff9afd0cc1
19 changed files with 191 additions and 244 deletions

View File

@@ -22,6 +22,7 @@
"rustc"
"rustfmt"
"rust-analyzer"
"miri"
])
llvm_18
libffi

View File

@@ -1,12 +1,12 @@
use itertools::Itertools;
use inkwell::context::Context;
use itertools::Itertools;
use rustyline::error::ReadlineError;
use rustyline::{DefaultEditor, Result};
use nixjit::compile::compile;
use nixjit::ir::downgrade;
use nixjit::vm::{run, JITContext};
use nixjit::error::Error;
use nixjit::ir::downgrade;
use nixjit::vm::{JITContext, run};
macro_rules! unwrap {
($e:expr) => {
@@ -32,7 +32,12 @@ fn main() -> Result<()> {
rl.add_history_entry(expr.as_str())?;
let root = rnix::Root::parse(&expr);
if !root.errors().is_empty() {
println!("{}", Error::ParseError(root.errors().iter().map(|err| err.to_string()).join(";")));
println!(
"{}",
Error::ParseError(
root.errors().iter().map(|err| err.to_string()).join(";")
)
);
continue;
}
let expr = root.tree().expr().unwrap();

View File

@@ -1,6 +1,8 @@
use std::rc::Rc;
use crate::ty::internal::{Const, PrimOp, RecAttrSet, Value};
use rpds::HashTrieMap;
use crate::ty::internal::{AttrSet, Const, PrimOp, Value};
use crate::vm::{Env, VM};
pub fn env<'vm>(vm: &'vm VM) -> Rc<Env<'vm>> {
@@ -41,18 +43,18 @@ pub fn env<'vm>(vm: &'vm VM) -> Rc<Env<'vm>> {
}),
];
let builtins_env = Rc::new(Env::empty());
let map = builtins_env.clone().new_rec();
let mut map = HashTrieMap::new();
for primop in primops {
let primop = Rc::new(primop);
env.insert(
vm.new_sym(format!("__{}", primop.name)),
Value::PrimOp(primop.clone()),
);
map.insert(vm.new_sym(primop.name), Value::PrimOp(primop));
map.insert_mut(vm.new_sym(primop.name), Value::PrimOp(primop));
}
let builtins = Value::RecAttrSet(RecAttrSet::from_inner(map.clone()).into());
map.insert(vm.new_sym("builtins"), builtins.clone());
let attrs: Rc<_> = AttrSet::from_inner(map).into();
let mut builtins = Value::AttrSet(attrs);
builtins.push_attr(vm.new_sym("builtins"), Value::Builtins);
env.insert(vm.new_sym("builtins"), builtins);
env

View File

@@ -114,5 +114,5 @@ pub struct Program {
pub funcs: Slice<Func>,
pub symbols: Vec<EcoString>,
pub symmap: HashMap<EcoString, usize>,
pub consts: Box<[Const]>
pub consts: Box<[Const]>,
}

View File

@@ -23,7 +23,7 @@ pub fn compile(downgraded: ir::Downgraded) -> Program {
.collect(),
symbols: downgraded.symbols,
symmap: downgraded.symmap,
consts: downgraded.consts
consts: downgraded.consts,
}
}

View File

@@ -164,7 +164,9 @@ impl DowngradeContext {
} else {
self.constmap.insert(cnst.clone(), self.consts.len());
self.consts.push(cnst);
Const { idx: self.consts.len() - 1 }
Const {
idx: self.consts.len() - 1,
}
}
}
@@ -425,9 +427,7 @@ impl Downgrade for ast::Path {
let parts = self
.parts()
.map(|part| match part {
ast::InterpolPart::Literal(lit) => ctx.new_const(lit.to_string().into())
.ir()
.ok(),
ast::InterpolPart::Literal(lit) => ctx.new_const(lit.to_string().into()).ir().ok(),
ast::InterpolPart::Interpolation(interpol) => {
interpol.expr().unwrap().downgrade(ctx)
}
@@ -472,7 +472,7 @@ impl Downgrade for ast::Literal {
match self.kind() {
ast::LiteralKind::Integer(int) => ctx.new_const(int.value().unwrap().into()),
ast::LiteralKind::Float(float) => ctx.new_const(float.value().unwrap().into()),
ast::LiteralKind::Uri(uri) => ctx.new_const(uri.to_string().into())
ast::LiteralKind::Uri(uri) => ctx.new_const(uri.to_string().into()),
}
.ir()
.ok()
@@ -754,8 +754,8 @@ fn downgrade_attrpathvalue(
let path = downgrade_attrpath(value.attrpath().unwrap(), ctx)?;
let value = value.value().unwrap().downgrade(ctx)?;
let value = match value {
x @ Ir::Const(_) | x @ Ir::Var(_) => x,
x => ctx.new_thunk(x).ir()
x @ Ir::Const(_) => x,
x => ctx.new_thunk(x).ir(),
};
attrs.insert(path, value)
}

View File

@@ -1,11 +1,10 @@
#![cfg_attr(test, feature(test))]
#![allow(dead_code)]
mod builtins;
mod bytecode;
mod ty;
mod stack;
mod ty;
pub mod compile;
pub mod error;

View File

@@ -2,7 +2,6 @@ use std::fmt::{Display, Formatter, Result as FmtResult};
use derive_more::Constructor;
#[derive(Clone, Debug, PartialEq, Constructor, Hash)]
pub struct Catchable {
msg: String,
@@ -13,4 +12,3 @@ impl Display for Catchable {
write!(f, "<error: {}>", self.msg)
}
}

View File

@@ -1,11 +1,13 @@
use std::rc::Rc;
use std::collections::HashSet;
use std::rc::Rc;
use derive_more::Constructor;
use itertools::Itertools;
use rpds::HashTrieMap;
use crate::error::Result;
use crate::vm::{Env, VM};
use super::super::public as p;
use super::Value;
@@ -27,7 +29,7 @@ impl<'vm> AttrSet<'vm> {
}
pub fn push_attr(&mut self, sym: usize, val: Value<'vm>) {
if self.data.get_mut(&sym).is_some() {
if self.data.get(&sym).is_some() {
todo!()
}
self.data.insert_mut(sym, val);
@@ -42,16 +44,11 @@ impl<'vm> AttrSet<'vm> {
}
pub fn capture(&mut self, env: &Rc<Env<'vm>>) {
self
.data
.iter()
.for_each(|(_, v)| {
match v.clone() {
self.data.iter().for_each(|(_, v)| match v.clone() {
Value::Thunk(ref thunk) => {
thunk.capture(env.clone());
}
_ => ()
}
_ => (),
})
}
@@ -61,20 +58,18 @@ impl<'vm> AttrSet<'vm> {
}
}
pub fn update_rec(&mut self, other: &RecAttrSet<'vm>) {
for (k, v) in other.data.map.borrow().iter() {
self.push_attr_force(k.clone(), v.clone())
}
}
pub fn as_inner(&self) -> &HashTrieMap<usize, Value<'vm>> {
&self.data
}
pub fn from_inner(data: HashTrieMap<usize, Value<'vm>>) -> Self {
Self { data }
}
pub fn force_deep(&mut self, vm: &'vm VM<'_>) -> Result<()> {
let mut map: Vec<_> = self
.data
.into_iter()
.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect();
for (_, v) in map.iter_mut() {
@@ -84,99 +79,20 @@ impl<'vm> AttrSet<'vm> {
Ok(())
}
pub fn to_public(&self, vm: &'vm VM, seen: &mut HashSet<Value<'vm>>) -> p::Value {
p::Value::AttrSet(p::AttrSet::new(
self.data
.iter()
.map(|(&sym, value)| (vm.get_sym(sym), value.clone().to_public(vm, seen)))
.collect(),
))
}
}
#[derive(Debug, Constructor, Clone, PartialEq)]
pub struct RecAttrSet<'vm> {
data: Rc<Env<'vm>>,
}
impl<'vm> RecAttrSet<'vm> {
pub fn empty() -> Self {
RecAttrSet {
data: Rc::default(),
}
}
pub fn push_attr_force(&mut self, sym: usize, val: Value<'vm>) {
self.data.insert(sym, val);
}
pub fn push_attr(&mut self, sym: usize, val: Value<'vm>) {
if self.data.lookup(sym).is_some() {
todo!()
}
self.data.insert(sym, val);
}
pub fn select(&self, sym: usize) -> Option<Value<'vm>> {
self.data.lookup(sym)
}
pub fn has_attr(&self, sym: usize) -> bool {
self.data.lookup(sym).is_some()
}
pub fn update(&mut self, other: RecAttrSet<'vm>) {
for (k, v) in other.data.map.borrow().iter() {
self.push_attr_force(k.clone(), v.clone())
}
}
pub fn update_normal(&self, other: &AttrSet<'vm>) -> AttrSet<'vm> {
let map = self
.data
.map
.borrow()
.into_iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect();
let mut new = AttrSet::new(map);
for (k, v) in other.data.iter() {
new.push_attr_force(k.clone(), v.clone())
}
new
}
pub fn into_inner(self) -> Rc<Env<'vm>> {
self.data
}
pub fn from_inner(data: Rc<Env<'vm>>) -> Self {
RecAttrSet { data }
}
pub fn force_deep(&mut self, vm: &'vm VM<'_>) -> Result<()> {
let mut map: Vec<_> = self
.data
.map
.borrow()
.into_iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect();
map.iter_mut()
.map(|(_, v)| v.force_deep(vm).map(|_| ()))
.find(|v| v.is_err())
.map_or(Ok(()), |err| err)?;
*self.data.map.borrow_mut() = map.into_iter().collect();
Ok(())
pub fn eq_impl(&self, other: &AttrSet<'vm>, vm: &'vm VM<'_>) -> bool {
self.data.iter().len() == other.data.iter().len()
&& std::iter::zip(
self.data.iter().sorted_by_key(|(k, _)| **k),
self.data.iter().sorted_by_key(|(k, _)| **k),
)
.all(|((_, v1), (_, v2))| v1.eq_impl(v2, vm))
}
pub fn to_public(&self, vm: &'vm VM, seen: &mut HashSet<Value<'vm>>) -> p::Value {
p::Value::AttrSet(p::AttrSet::new(
self.data
.map
.borrow()
.iter()
.map(|(&sym, value)| (vm.get_sym(sym), value.clone().to_public(vm, seen)))
.map(|(&sym, value)| (vm.get_sym(sym), value.to_public(vm, seen)))
.collect(),
))
}

View File

@@ -9,7 +9,7 @@ pub enum Const {
Int(i64),
Float(f64),
String(EcoString),
Null
Null,
}
impl Hash for Const {
@@ -68,6 +68,8 @@ impl PartialEq for Const {
(Bool(a), Bool(b)) => a == b,
(Int(a), Int(b)) => a == b,
(Float(a), Float(b)) => a == b,
(Int(a), Float(b)) => *a as f64 == *b,
(Float(a), Int(b)) => *b as f64 == *a,
(String(a), String(b)) => a == b,
_ => false,
}

View File

@@ -1,8 +1,8 @@
use std::collections::HashMap;
use std::rc::Rc;
use itertools::Itertools;
use rpds::HashTrieMap;
use derive_more::Constructor;
use itertools::Itertools;
use crate::bytecode::Func as BFunc;
use crate::error::Result;
@@ -40,13 +40,14 @@ impl From<ir::Param> for Param {
}
}
pub type JITFunc<'vm> = unsafe extern "C" fn(vm: *mut VM<'_>, *mut Env<'vm>, *mut Value<'vm>) -> Value<'vm>;
pub type JITFunc<'vm> =
unsafe extern "C" fn(vm: *mut VM<'_>, *mut Env<'vm>, *mut Value<'vm>) -> Value<'vm>;
#[derive(Debug, Clone, Constructor)]
pub struct Func<'vm> {
pub func: &'vm BFunc,
pub env: Rc<Env<'vm>>,
pub compiled: Option<JITFunc<'vm>>
pub compiled: Option<JITFunc<'vm>>,
}
impl<'vm> Func<'vm> {
@@ -56,14 +57,14 @@ impl<'vm> Func<'vm> {
let env = Rc::new(self.env.as_ref().clone());
match self.func.param.clone() {
Ident(ident) => env.enter(HashTrieMap::new().insert(ident.into(), arg)),
Ident(ident) => env.enter([(ident.into(), arg)].into_iter()),
Formals {
formals,
ellipsis,
alias,
} => {
let arg = arg.unwrap_attr_set();
let mut new = HashTrieMap::new();
let mut new = Vec::with_capacity(formals.len() + alias.iter().len());
if !ellipsis
&& arg
.as_inner()
@@ -78,14 +79,16 @@ impl<'vm> Func<'vm> {
let formal = formal.clone().into();
let arg = arg
.select(formal)
.or_else(|| default.map(|idx| Value::Thunk(Thunk::new(vm.get_thunk(idx)).into())))
.or_else(|| {
default.map(|idx| Value::Thunk(Thunk::new(vm.get_thunk(idx)).into()))
})
.unwrap();
new.insert_mut(formal, arg);
new.push((formal, arg));
}
if let Some(alias) = alias {
new.insert_mut(alias.clone().into(), Value::AttrSet(arg));
new.push((alias.clone().into(), Value::AttrSet(arg)));
}
env.enter(new);
env.enter(new.into_iter());
}
}

View File

@@ -32,28 +32,43 @@ pub enum Value<'vm> {
Thunk(Rc<Thunk<'vm>>),
ThunkRef(&'vm Thunk<'vm>),
AttrSet(Rc<AttrSet<'vm>>),
RecAttrSet(Rc<RecAttrSet<'vm>>),
List(Rc<List<'vm>>),
Catchable(c::Catchable),
PrimOp(Rc<PrimOp<'vm>>),
PartialPrimOp(Rc<PartialPrimOp<'vm>>),
Func(Rc<Func<'vm>>),
Builtins,
}
impl Hash for Value<'_> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
use Value::*;
std::mem::discriminant(self).hash(state);
match self {
Const(x) => x.hash(state),
Thunk(x) => (x.as_ref() as *const self::Thunk).hash(state),
ThunkRef(x) => (*x as *const self::Thunk).hash(state),
AttrSet(x) => (x.as_ref() as *const self::AttrSet).hash(state),
RecAttrSet(x) => (x.as_ref() as *const self::RecAttrSet).hash(state),
List(x) => (x.as_ref() as *const self::List).hash(state),
Catchable(x) => x.hash(state),
PrimOp(x) => (x.as_ref() as *const self::PrimOp).hash(state),
PartialPrimOp(x) => (x.as_ref() as *const self::PartialPrimOp).hash(state),
Func(x) => (x.as_ref() as *const self::Func).hash(state),
Builtins => (),
}
}
}
impl<'vm> Value<'vm> {
fn eq_impl(&self, other: &Self, vm: &'vm VM<'_>) -> bool {
use Value::*;
match (self, other) {
(Const(a), Const(b)) => a.eq(b),
(AttrSet(a), AttrSet(b)) => a.eq_impl(b, vm),
(List(a), List(b)) => a.eq(b),
(Builtins, AttrSet(attrs)) => attrs.has_attr(vm.new_sym("builtins")),
(AttrSet(attrs), Builtins) => attrs.has_attr(vm.new_sym("builtins")),
_ => false,
}
}
}
@@ -65,7 +80,6 @@ pub enum ValueAsRef<'v, 'vm: 'v> {
Const(&'v Const),
Thunk(&'v Thunk<'vm>),
AttrSet(&'v AttrSet<'vm>),
RecAttrSet(&'v RecAttrSet<'vm>),
List(&'v List<'vm>),
Catchable(&'v c::Catchable),
PrimOp(&'v PrimOp<'vm>),
@@ -78,7 +92,6 @@ pub enum ValueAsMut<'v, 'vm: 'v> {
Const(&'v mut Const),
Thunk(&'v Thunk<'vm>),
AttrSet(&'v mut AttrSet<'vm>),
RecAttrSet(&'v mut RecAttrSet<'vm>),
List(&'v mut List<'vm>),
Catchable(&'v mut c::Catchable),
PrimOp(&'v mut PrimOp<'vm>),
@@ -95,12 +108,12 @@ impl<'v, 'vm: 'v> Value<'vm> {
Thunk(x) => R::Thunk(x),
ThunkRef(x) => R::Thunk(x),
AttrSet(x) => R::AttrSet(x),
RecAttrSet(x) => R::RecAttrSet(x),
List(x) => R::List(x),
Catchable(x) => R::Catchable(x),
PrimOp(x) => R::PrimOp(x),
PartialPrimOp(x) => R::PartialPrimOp(x),
Func(x) => R::Func(x),
Builtins => unreachable!(),
}
}
@@ -112,12 +125,12 @@ impl<'v, 'vm: 'v> Value<'vm> {
Thunk(x) => M::Thunk(x),
ThunkRef(x) => M::Thunk(x),
AttrSet(x) => M::AttrSet(Rc::make_mut(x)),
RecAttrSet(x) => M::RecAttrSet(Rc::make_mut(x)),
List(x) => M::List(Rc::make_mut(x)),
Catchable(x) => M::Catchable(x),
PrimOp(x) => M::PrimOp(Rc::make_mut(x)),
PartialPrimOp(x) => M::PartialPrimOp(Rc::make_mut(x)),
Func(x) => M::Func(x),
Builtins => unreachable!(),
}
}
}
@@ -135,19 +148,19 @@ impl<'vm> Value<'vm> {
Thunk(_) => "thunk",
ThunkRef(_) => "thunk",
AttrSet(_) => "set",
RecAttrSet(_) => "set",
List(_) => "list",
Catchable(_) => unreachable!(),
PrimOp(_) => "lambda",
PartialPrimOp(_) => "lambda",
Func(_) => "lambda",
Builtins => "set",
}
}
pub fn callable(&self) -> bool {
match self {
Value::PrimOp(_) | Value::PartialPrimOp(_) | Value::Func(_) => true,
Value::AttrSet(_) | Value::RecAttrSet(_) => todo!(),
Value::AttrSet(_) => todo!(),
_ => false,
}
}
@@ -318,8 +331,6 @@ impl<'vm> Value<'vm> {
pub fn push_attr(&mut self, sym: usize, val: Self) -> &mut Self {
if let Value::AttrSet(attrs) = self {
Rc::make_mut(attrs).push_attr(sym, val)
} else if let Value::RecAttrSet(attrs) = self {
Rc::make_mut(attrs).push_attr(sym, val)
} else if let Value::Catchable(_) = self {
} else if let Value::Catchable(_) = val {
*self = val
@@ -335,13 +346,6 @@ impl<'vm> Value<'vm> {
Rc::make_mut(&mut a).update(b.as_ref());
Value::AttrSet(a)
}
(Value::RecAttrSet(a), Value::AttrSet(b)) => {
Value::AttrSet(a.update_normal(b.as_ref()).into())
}
(Value::AttrSet(mut a), Value::RecAttrSet(b)) => {
Rc::make_mut(&mut a).update_rec(b.as_ref());
Value::AttrSet(a)
}
(x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => x,
_ => todo!(),
}
@@ -352,23 +356,22 @@ impl<'vm> Value<'vm> {
Value::AttrSet(attrs) => attrs
.select(sym)
.ok_or_else(|| Error::EvalError(format!("{} not found", vm.get_sym(sym)))),
Value::RecAttrSet(attrs) => attrs
.select(sym)
.ok_or_else(|| Error::EvalError(format!("{} not found", vm.get_sym(sym)))),
Value::Catchable(_) => return Ok(self),
_ => Err(Error::EvalError(format!(
"cannot select from {:?}",
self.typename()
))),
}?;
if let Value::Builtins = val {
} else {
*self = val;
}
Ok(self)
}
pub fn select_with_default(&mut self, sym: usize, default: Self) -> Result<&mut Self> {
let val = match self {
Value::AttrSet(attrs) => attrs.select(sym).unwrap_or(default),
Value::RecAttrSet(attrs) => attrs.select(sym).unwrap_or(default),
Value::Catchable(_) => return Ok(self),
_ => {
return Err(Error::EvalError(format!(
@@ -377,7 +380,10 @@ impl<'vm> Value<'vm> {
)));
}
};
if let Value::Builtins = val {
} else {
*self = val;
}
Ok(self)
}
@@ -385,9 +391,6 @@ impl<'vm> Value<'vm> {
if let Value::AttrSet(attrs) = self {
let val = VmConst(Const::Bool(attrs.has_attr(sym)));
*self = val;
} else if let Value::RecAttrSet(attrs) = self {
let val = VmConst(Const::Bool(attrs.has_attr(sym)));
*self = val;
} else if let Value::Catchable(_) = self {
} else {
*self = VmConst(Const::Bool(false));
@@ -429,28 +432,29 @@ impl<'vm> Value<'vm> {
}
Value::List(list) => Rc::make_mut(list).force_deep(vm)?,
Value::AttrSet(attrs) => Rc::make_mut(attrs).force_deep(vm)?,
Value::RecAttrSet(attrs) => Rc::make_mut(attrs).force_deep(vm)?,
_ => (),
}
Ok(self)
}
pub fn to_public(&self, vm: &'vm VM, seen: &mut HashSet<Value<'vm>>) -> p::Value {
use self::Value::*;
use p::Value;
if seen.contains(self) {
return p::Value::Repeated;
return Value::Repeated;
}
seen.insert(self.clone());
match self {
Value::AttrSet(attrs) => attrs.to_public(vm, seen),
Value::RecAttrSet(attrs) => attrs.to_public(vm, seen),
Value::List(list) => list.to_public(vm, seen),
Value::Catchable(catchable) => p::Value::Catchable(catchable.clone()),
Value::Const(cnst) => p::Value::Const(cnst.clone().into()),
Value::Thunk(_) => p::Value::Thunk,
Value::ThunkRef(_) => p::Value::Thunk,
Value::PrimOp(primop) => p::Value::PrimOp(primop.name),
Value::PartialPrimOp(primop) => p::Value::PartialPrimOp(primop.name),
Value::Func(_) => p::Value::Func,
AttrSet(attrs) => attrs.to_public(vm, seen),
List(list) => list.to_public(vm, seen),
Catchable(catchable) => Value::Catchable(catchable.clone()),
Const(cnst) => Value::Const(cnst.clone().into()),
Thunk(_) => Value::Thunk,
ThunkRef(_) => Value::Thunk,
PrimOp(primop) => Value::PrimOp(primop.name),
PartialPrimOp(primop) => Value::PartialPrimOp(primop.name),
Func(_) => Value::Func,
Builtins => Value::Repeated,
}
}
}

View File

@@ -2,8 +2,8 @@ use std::rc::Rc;
use derive_more::Constructor;
use crate::vm::VM;
use crate::error::Result;
use crate::vm::VM;
use super::Value;
@@ -23,12 +23,16 @@ impl PartialEq for PrimOp<'_> {
impl<'vm> PrimOp<'vm> {
pub fn call(&self, vm: &'vm VM<'_>, args: Vec<Value<'vm>>) -> Result<Value<'vm>> {
if (args.len()) < self.arity {
Value::PartialPrimOp(PartialPrimOp {
Value::PartialPrimOp(
PartialPrimOp {
name: self.name,
arity: self.arity - args.len(),
args,
func: self.func,
}.into()).ok()
}
.into(),
)
.ok()
} else if args.len() == self.arity {
(self.func)(vm, args)
} else {

View File

@@ -13,7 +13,7 @@ pub enum Const {
Int(i64),
Float(f64),
String(EcoString),
Null
Null,
}
impl Display for Const {
@@ -24,7 +24,7 @@ impl Display for Const {
Int(i) => write!(f, "{i}"),
Float(float) => write!(f, "{float}"),
String(s) => write!(f, "{s}"),
Null => write!(f, "null")
Null => write!(f, "null"),
}
}
}
@@ -37,7 +37,7 @@ impl From<i::Const> for Const {
Int(int) => Const::Int(int),
Float(float) => Const::Float(float),
String(string) => Const::String(string),
Null => Const::Null
Null => Const::Null,
}
}
}

View File

@@ -3,9 +3,9 @@ use std::ops::Deref;
use std::sync::LazyLock;
use derive_more::{Constructor, IsVariant, Unwrap};
use rpds::{HashTrieMap, VectorSync};
use ecow::EcoString;
use regex::Regex;
use rpds::{HashTrieMap, VectorSync};
use super::common::*;
@@ -112,7 +112,7 @@ pub enum Value {
Func,
PrimOp(&'static str),
PartialPrimOp(&'static str),
Repeated
Repeated,
}
impl Display for Value {
@@ -127,7 +127,7 @@ impl Display for Value {
Func => write!(f, "<LAMBDA>"),
PrimOp(x) => write!(f, "<PRIMOP {x}>"),
PartialPrimOp(x) => write!(f, "<PARTIAL PRIMOP {x}>"),
Repeated => write!(f, "<REPEATED>")
Repeated => write!(f, "<REPEATED>"),
}
}
}

View File

@@ -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 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);
}
pub fn new_rec(self: Rc<Self>) -> Rc<Self> {
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) {

View File

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

View File

@@ -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()? {
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());
env.enter(
stack
.tos_mut()?
.as_mut()
.tos()?
.clone()
.unwrap_attr_set()
.capture(env);
.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!(),
},

View File

@@ -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) } },
);
}