feat: ref
This commit is contained in:
@@ -5,7 +5,7 @@ use crate::ty::common::Symbol;
|
|||||||
use crate::ty::internal::{_Thunk, Const, PrimOp, RecAttrSet, Thunk, Value};
|
use crate::ty::internal::{_Thunk, Const, PrimOp, RecAttrSet, Thunk, Value};
|
||||||
use crate::vm::Env;
|
use crate::vm::Env;
|
||||||
|
|
||||||
pub fn env() -> Arc<Env> {
|
pub fn env<'vm>() -> Arc<Env<'vm>> {
|
||||||
let env = Arc::new(Env::empty());
|
let env = Arc::new(Env::empty());
|
||||||
env.insert(Symbol::from("true"), Value::Const(Const::Bool(true)));
|
env.insert(Symbol::from("true"), Value::Const(Const::Bool(true)));
|
||||||
env.insert(Symbol::from("false"), Value::Const(Const::Bool(false)));
|
env.insert(Symbol::from("false"), Value::Const(Const::Bool(false)));
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use ecow::EcoString;
|
use ecow::EcoString;
|
||||||
|
|
||||||
use crate::ty::internal::{Const, Func};
|
use crate::ty::internal::{Const, Param};
|
||||||
|
|
||||||
type Slice<T> = Box<[T]>;
|
type Slice<T> = Box<[T]>;
|
||||||
|
|
||||||
@@ -94,6 +94,12 @@ pub enum UnOp {
|
|||||||
Not,
|
Not,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Func {
|
||||||
|
pub param: Param,
|
||||||
|
pub opcodes: OpCodes,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Program {
|
pub struct Program {
|
||||||
pub top_level: OpCodes,
|
pub top_level: OpCodes,
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
use std::cell::OnceCell;
|
|
||||||
|
|
||||||
use crate::bytecode::*;
|
use crate::bytecode::*;
|
||||||
use crate::ir;
|
use crate::ir;
|
||||||
use crate::ty::internal::Const;
|
use crate::ty::internal::Const;
|
||||||
use crate::ty::internal::Func;
|
|
||||||
|
|
||||||
pub struct Compiler {
|
pub struct Compiler {
|
||||||
opcodes: Vec<OpCode>,
|
opcodes: Vec<OpCode>,
|
||||||
@@ -21,7 +18,6 @@ pub fn compile(downgraded: ir::Downgraded) -> Program {
|
|||||||
.funcs
|
.funcs
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|func| Func {
|
.map(|func| Func {
|
||||||
env: OnceCell::new(),
|
|
||||||
param: func.param.into(),
|
param: func.param.into(),
|
||||||
opcodes: Compiler::new().compile(*func.body),
|
opcodes: Compiler::new().compile(*func.body),
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -12,29 +12,29 @@ use super::super::public as p;
|
|||||||
use super::{ToPublic, Value};
|
use super::{ToPublic, Value};
|
||||||
|
|
||||||
#[derive(Debug, Constructor, Clone, PartialEq)]
|
#[derive(Debug, Constructor, Clone, PartialEq)]
|
||||||
pub struct AttrSet {
|
pub struct AttrSet<'vm> {
|
||||||
data: HashTrieMapSync<Symbol, Value>,
|
data: HashTrieMapSync<Symbol, Value<'vm>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AttrSet {
|
impl<'vm> AttrSet<'vm> {
|
||||||
pub fn empty() -> Self {
|
pub fn empty() -> Self {
|
||||||
AttrSet {
|
AttrSet {
|
||||||
data: HashTrieMapSync::new_sync(),
|
data: HashTrieMapSync::new_sync(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn push_attr_force(&mut self, sym: Symbol, val: Value) {
|
pub fn push_attr_force(&mut self, sym: Symbol, val: Value<'vm>) {
|
||||||
self.data.insert_mut(sym, val);
|
self.data.insert_mut(sym, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn push_attr(&mut self, sym: Symbol, val: Value) {
|
pub fn push_attr(&mut self, sym: Symbol, val: Value<'vm>) {
|
||||||
if self.data.get_mut(&sym).is_some() {
|
if self.data.get_mut(&sym).is_some() {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
self.data.insert_mut(sym, val);
|
self.data.insert_mut(sym, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn select(&self, sym: Symbol) -> Option<Value> {
|
pub fn select(&self, sym: Symbol) -> Option<Value<'vm>> {
|
||||||
self.data.get(&sym).cloned()
|
self.data.get(&sym).cloned()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,29 +42,29 @@ impl AttrSet {
|
|||||||
self.data.get(&sym).is_some()
|
self.data.get(&sym).is_some()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(mut self, other: AttrSet) -> AttrSet {
|
pub fn update(mut self, other: AttrSet<'vm>) -> AttrSet<'vm> {
|
||||||
for (k, v) in other.data.iter() {
|
for (k, v) in other.data.iter() {
|
||||||
self.push_attr_force(k.clone(), v.clone())
|
self.push_attr_force(k.clone(), v.clone())
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_rec(mut self, other: RecAttrSet) -> AttrSet {
|
pub fn update_rec(mut self, other: RecAttrSet<'vm>) -> AttrSet<'vm> {
|
||||||
for (k, v) in other.data.borrow().iter() {
|
for (k, v) in other.data.borrow().iter() {
|
||||||
self.push_attr_force(k.clone(), v.clone())
|
self.push_attr_force(k.clone(), v.clone())
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn into_inner(self) -> HashTrieMapSync<Symbol, Value> {
|
pub fn into_inner(self) -> HashTrieMapSync<Symbol, Value<'vm>> {
|
||||||
self.data
|
self.data
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_inner(&self) -> &HashTrieMapSync<Symbol, Value> {
|
pub fn as_inner(&self) -> &HashTrieMapSync<Symbol, Value<'vm>> {
|
||||||
&self.data
|
&self.data
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn force_deep(&mut self, vm: &VM) -> Result<()> {
|
pub fn force_deep(&mut self, vm: &VM<'vm>) -> Result<()> {
|
||||||
let mut map: Vec<_> = self
|
let mut map: Vec<_> = self
|
||||||
.data
|
.data
|
||||||
.into_iter()
|
.into_iter()
|
||||||
@@ -79,7 +79,7 @@ impl AttrSet {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToPublic for AttrSet {
|
impl ToPublic for AttrSet<'_> {
|
||||||
fn to_public(self, vm: &VM) -> p::Value {
|
fn to_public(self, vm: &VM) -> p::Value {
|
||||||
p::Value::AttrSet(p::AttrSet::new(
|
p::Value::AttrSet(p::AttrSet::new(
|
||||||
self.data
|
self.data
|
||||||
@@ -91,29 +91,29 @@ impl ToPublic for AttrSet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Constructor, Clone, PartialEq)]
|
#[derive(Debug, Constructor, Clone, PartialEq)]
|
||||||
pub struct RecAttrSet {
|
pub struct RecAttrSet<'vm> {
|
||||||
data: Arc<RefCell<HashTrieMapSync<Symbol, Value>>>,
|
data: Arc<RefCell<HashTrieMapSync<Symbol, Value<'vm>>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RecAttrSet {
|
impl<'vm> RecAttrSet<'vm> {
|
||||||
pub fn empty() -> RecAttrSet {
|
pub fn empty() -> Self {
|
||||||
RecAttrSet {
|
RecAttrSet {
|
||||||
data: Arc::default(),
|
data: Arc::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn push_attr_force(&mut self, sym: Symbol, val: Value) {
|
pub fn push_attr_force(&mut self, sym: Symbol, val: Value<'vm>) {
|
||||||
self.data.borrow_mut().insert_mut(sym, val);
|
self.data.borrow_mut().insert_mut(sym, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn push_attr(&mut self, sym: Symbol, val: Value) {
|
pub fn push_attr(&mut self, sym: Symbol, val: Value<'vm>) {
|
||||||
if self.data.borrow().get(&sym).is_some() {
|
if self.data.borrow().get(&sym).is_some() {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
self.data.borrow_mut().insert_mut(sym, val);
|
self.data.borrow_mut().insert_mut(sym, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn select(&self, sym: Symbol) -> Option<Value> {
|
pub fn select(&self, sym: Symbol) -> Option<Value<'vm>> {
|
||||||
self.data.borrow().get(&sym).cloned()
|
self.data.borrow().get(&sym).cloned()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,14 +121,14 @@ impl RecAttrSet {
|
|||||||
self.data.borrow().get(&sym).is_some()
|
self.data.borrow().get(&sym).is_some()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(mut self, other: RecAttrSet) -> RecAttrSet {
|
pub fn update(mut self, other: RecAttrSet<'vm>) -> RecAttrSet<'vm> {
|
||||||
for (k, v) in other.data.borrow().iter() {
|
for (k, v) in other.data.borrow().iter() {
|
||||||
self.push_attr_force(k.clone(), v.clone())
|
self.push_attr_force(k.clone(), v.clone())
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_normal(self, other: AttrSet) -> AttrSet {
|
pub fn update_normal(self, other: AttrSet<'vm>) -> AttrSet<'vm> {
|
||||||
let map = self
|
let map = self
|
||||||
.data
|
.data
|
||||||
.borrow()
|
.borrow()
|
||||||
@@ -142,15 +142,15 @@ impl RecAttrSet {
|
|||||||
new
|
new
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn into_inner(self) -> HashTrieMapSync<Symbol, Value> {
|
pub fn into_inner(self) -> HashTrieMapSync<Symbol, Value<'vm>> {
|
||||||
self.data.borrow().clone()
|
self.data.borrow().clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_inner(data: Arc<RefCell<HashTrieMapSync<Symbol, Value>>>) -> Self {
|
pub fn from_inner(data: Arc<RefCell<HashTrieMapSync<Symbol, Value<'vm>>>>) -> Self {
|
||||||
RecAttrSet { data }
|
RecAttrSet { data }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn force_deep(&mut self, vm: &VM) -> Result<()> {
|
pub fn force_deep(&mut self, vm: &VM<'vm>) -> Result<()> {
|
||||||
let mut map: Vec<_> = self
|
let mut map: Vec<_> = self
|
||||||
.data
|
.data
|
||||||
.borrow()
|
.borrow()
|
||||||
@@ -166,7 +166,7 @@ impl RecAttrSet {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToPublic for RecAttrSet {
|
impl ToPublic for RecAttrSet<'_> {
|
||||||
fn to_public(self, vm: &VM) -> p::Value {
|
fn to_public(self, vm: &VM) -> p::Value {
|
||||||
p::Value::AttrSet(p::AttrSet::new(
|
p::Value::AttrSet(p::AttrSet::new(
|
||||||
self.data
|
self.data
|
||||||
|
|||||||
@@ -41,14 +41,14 @@ impl From<ir::Param> for Param {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Func {
|
pub struct Func<'vm> {
|
||||||
pub env: OnceCell<CapturedEnv>,
|
pub env: OnceCell<CapturedEnv<'vm>>,
|
||||||
pub param: Param,
|
pub param: Param,
|
||||||
pub opcodes: OpCodes,
|
pub opcodes: OpCodes,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Func {
|
impl<'vm> Func<'vm> {
|
||||||
pub fn call(&self, vm: &VM, arg: Value) -> Result<Value> {
|
pub fn call(&'vm self, vm: &VM<'vm>, arg: Value<'vm>) -> Result<Value<'vm>> {
|
||||||
use Param::*;
|
use Param::*;
|
||||||
|
|
||||||
let env = self.env.get().unwrap().clone().released();
|
let env = self.env.get().unwrap().clone().released();
|
||||||
@@ -75,7 +75,7 @@ impl Func {
|
|||||||
for (formal, default) in formals {
|
for (formal, default) in formals {
|
||||||
let arg = arg
|
let arg = arg
|
||||||
.select(formal.clone().into())
|
.select(formal.clone().into())
|
||||||
.or_else(|| default.map(Value::ThunkRef))
|
.or_else(|| default.map(|idx| Value::ThunkRef(vm.get_thunk(idx))))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
new.insert_mut(formal.clone().into(), arg);
|
new.insert_mut(formal.clone().into(), arg);
|
||||||
}
|
}
|
||||||
@@ -90,7 +90,7 @@ impl Func {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq for Func {
|
impl PartialEq for Func<'_> {
|
||||||
fn eq(&self, _: &Self) -> bool {
|
fn eq(&self, _: &Self) -> bool {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,29 +8,29 @@ use crate::vm::VM;
|
|||||||
use super::{ToPublic, Value};
|
use super::{ToPublic, Value};
|
||||||
|
|
||||||
#[derive(Debug, Constructor, Clone, PartialEq)]
|
#[derive(Debug, Constructor, Clone, PartialEq)]
|
||||||
pub struct List {
|
pub struct List<'vm> {
|
||||||
data: VectorSync<Value>,
|
data: VectorSync<Value<'vm>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl List {
|
impl<'vm> List<'vm> {
|
||||||
pub fn empty() -> List {
|
pub fn empty() -> Self {
|
||||||
List {
|
List {
|
||||||
data: VectorSync::new_sync(),
|
data: VectorSync::new_sync(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn push(&mut self, elem: Value) {
|
pub fn push(&mut self, elem: Value<'vm>) {
|
||||||
self.data.push_back_mut(elem);
|
self.data.push_back_mut(elem);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn concat(mut self, other: List) -> List {
|
pub fn concat(mut self, other: List<'vm>) -> List<'vm> {
|
||||||
for elem in other.data.iter() {
|
for elem in other.data.iter() {
|
||||||
self.data.push_back_mut(elem.clone());
|
self.data.push_back_mut(elem.clone());
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn force_deep(&mut self, vm: &VM) -> Result<()> {
|
pub fn force_deep(&mut self, vm: &VM<'vm>) -> Result<()> {
|
||||||
let mut vec: Vec<_> = self.data.iter().cloned().collect();
|
let mut vec: Vec<_> = self.data.iter().cloned().collect();
|
||||||
vec.iter_mut()
|
vec.iter_mut()
|
||||||
.map(|v| v.force_deep(vm).map(|_| ()))
|
.map(|v| v.force_deep(vm).map(|_| ()))
|
||||||
@@ -41,7 +41,7 @@ impl List {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToPublic for List {
|
impl ToPublic for List<'_> {
|
||||||
fn to_public(self, vm: &VM) -> p::Value {
|
fn to_public(self, vm: &VM) -> p::Value {
|
||||||
p::Value::List(p::List::new(
|
p::Value::List(p::List::new(
|
||||||
self.data
|
self.data
|
||||||
|
|||||||
@@ -30,20 +30,20 @@ pub trait ToPublic {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Thunk {
|
pub struct Thunk<'vm> {
|
||||||
pub thunk: RefCell<_Thunk>,
|
pub thunk: RefCell<_Thunk<'vm>>,
|
||||||
pub env: RefCell<Option<Arc<Env>>>,
|
pub env: RefCell<Option<Arc<Env<'vm>>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, IsVariant, Unwrap, Clone)]
|
#[derive(Debug, IsVariant, Unwrap, Clone)]
|
||||||
pub enum _Thunk {
|
pub enum _Thunk<'vm> {
|
||||||
Code(OpCodes),
|
Code(OpCodes),
|
||||||
SuspendedFrom(*const Thunk),
|
SuspendedFrom(*const Thunk<'vm>),
|
||||||
Value(Box<Value>),
|
Value(Box<Value<'vm>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Thunk {
|
impl<'vm> Thunk<'vm> {
|
||||||
pub fn new(opcodes: OpCodes) -> Thunk {
|
pub fn new(opcodes: OpCodes) -> Self {
|
||||||
Thunk {
|
Thunk {
|
||||||
thunk: RefCell::new(_Thunk::Code(opcodes)),
|
thunk: RefCell::new(_Thunk::Code(opcodes)),
|
||||||
env: RefCell::new(None),
|
env: RefCell::new(None),
|
||||||
@@ -54,11 +54,11 @@ impl Thunk {
|
|||||||
self.thunk.borrow().clone().unwrap_code()
|
self.thunk.borrow().clone().unwrap_code()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn capture(&self, env: Arc<Env>) {
|
pub fn capture(&self, env: Arc<Env<'vm>>) {
|
||||||
*self.env.borrow_mut() = Some(env);
|
*self.env.borrow_mut() = Some(env);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn force(&self, vm: &VM) -> Result<Value> {
|
pub fn force(&self, vm: &VM<'vm>) -> Result<Value<'vm>> {
|
||||||
match &*self.thunk.borrow() {
|
match &*self.thunk.borrow() {
|
||||||
_Thunk::Value(value) => return Ok(value.as_ref().clone()),
|
_Thunk::Value(value) => return Ok(value.as_ref().clone()),
|
||||||
_Thunk::SuspendedFrom(from) => {
|
_Thunk::SuspendedFrom(from) => {
|
||||||
@@ -82,7 +82,7 @@ impl Thunk {
|
|||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn value(&self) -> Option<Value> {
|
pub fn value(&'vm self) -> Option<Value<'vm>> {
|
||||||
match &*self.thunk.borrow() {
|
match &*self.thunk.borrow() {
|
||||||
_Thunk::Value(value) => Some(value.as_ref().clone()),
|
_Thunk::Value(value) => Some(value.as_ref().clone()),
|
||||||
_ => None,
|
_ => None,
|
||||||
@@ -90,62 +90,60 @@ impl Thunk {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq for Thunk {
|
impl PartialEq for Thunk<'_> {
|
||||||
fn eq(&self, _: &Self) -> bool {
|
fn eq(&self, _: &Self) -> bool {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, IsVariant, Unwrap, Clone, PartialEq)]
|
#[derive(Debug, IsVariant, Unwrap, Clone, PartialEq)]
|
||||||
pub enum Value {
|
pub enum Value<'vm> {
|
||||||
Const(Const),
|
Const(Const),
|
||||||
Thunk(Thunk),
|
Thunk(Thunk<'vm>),
|
||||||
ThunkRef(usize),
|
ThunkRef(&'vm Thunk<'vm>),
|
||||||
AttrSet(AttrSet),
|
AttrSet(AttrSet<'vm>),
|
||||||
RecAttrSet(RecAttrSet),
|
RecAttrSet(RecAttrSet<'vm>),
|
||||||
List(List),
|
List(List<'vm>),
|
||||||
Catchable(c::Catchable),
|
Catchable(c::Catchable),
|
||||||
PrimOp(PrimOp),
|
PrimOp(PrimOp<'vm>),
|
||||||
PartialPrimOp(PartialPrimOp),
|
PartialPrimOp(PartialPrimOp<'vm>),
|
||||||
Func(usize),
|
Func(&'vm Func<'vm>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, IsVariant, Unwrap, Clone, PartialEq)]
|
#[derive(Debug, IsVariant, Unwrap, Clone, PartialEq)]
|
||||||
pub enum ValueAsRef<'v> {
|
pub enum ValueAsRef<'v, 'vm: 'v> {
|
||||||
Const(&'v Const),
|
Const(&'v Const),
|
||||||
Thunk(&'v Thunk),
|
Thunk(&'v Thunk<'vm>),
|
||||||
ThunkRef(&'v usize),
|
AttrSet(&'v AttrSet<'vm>),
|
||||||
AttrSet(&'v AttrSet),
|
RecAttrSet(&'v RecAttrSet<'vm>),
|
||||||
RecAttrSet(&'v RecAttrSet),
|
List(&'v List<'vm>),
|
||||||
List(&'v List),
|
|
||||||
Catchable(&'v c::Catchable),
|
Catchable(&'v c::Catchable),
|
||||||
PrimOp(&'v PrimOp),
|
PrimOp(&'v PrimOp<'vm>),
|
||||||
PartialPrimOp(&'v PartialPrimOp),
|
PartialPrimOp(&'v PartialPrimOp<'vm>),
|
||||||
Func(&'v usize),
|
Func(&'vm Func<'vm>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, IsVariant, Unwrap, PartialEq)]
|
#[derive(Debug, IsVariant, Unwrap, PartialEq)]
|
||||||
pub enum ValueAsMut<'v> {
|
pub enum ValueAsMut<'v, 'vm: 'v> {
|
||||||
Const(&'v mut Const),
|
Const(&'v mut Const),
|
||||||
Thunk(&'v Thunk),
|
Thunk(&'v Thunk<'vm>),
|
||||||
ThunkRef(&'v usize),
|
AttrSet(&'v mut AttrSet<'vm>),
|
||||||
AttrSet(&'v mut AttrSet),
|
RecAttrSet(&'v mut RecAttrSet<'vm>),
|
||||||
RecAttrSet(&'v mut RecAttrSet),
|
List(&'v mut List<'vm>),
|
||||||
List(&'v mut List),
|
|
||||||
Catchable(&'v mut c::Catchable),
|
Catchable(&'v mut c::Catchable),
|
||||||
PrimOp(&'v mut PrimOp),
|
PrimOp(&'v mut PrimOp<'vm>),
|
||||||
PartialPrimOp(&'v mut PartialPrimOp),
|
PartialPrimOp(&'v mut PartialPrimOp<'vm>),
|
||||||
Func(&'v usize),
|
Func(&'vm Func<'vm>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Value {
|
impl<'v, 'vm: 'v> Value<'vm> {
|
||||||
pub fn as_ref(&self) -> ValueAsRef {
|
pub fn as_ref(&'vm self) -> ValueAsRef<'v, 'vm> {
|
||||||
use Value::*;
|
use Value::*;
|
||||||
use ValueAsRef as R;
|
use ValueAsRef as R;
|
||||||
match self {
|
match self {
|
||||||
Const(x) => R::Const(x),
|
Const(x) => R::Const(x),
|
||||||
Thunk(x) => R::Thunk(x),
|
Thunk(x) => R::Thunk(x),
|
||||||
ThunkRef(x) => R::ThunkRef(x),
|
ThunkRef(x) => R::Thunk(x),
|
||||||
AttrSet(x) => R::AttrSet(x),
|
AttrSet(x) => R::AttrSet(x),
|
||||||
RecAttrSet(x) => R::RecAttrSet(x),
|
RecAttrSet(x) => R::RecAttrSet(x),
|
||||||
List(x) => R::List(x),
|
List(x) => R::List(x),
|
||||||
@@ -156,13 +154,13 @@ impl Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_mut(&mut self) -> ValueAsMut {
|
pub fn as_mut(&'vm mut self) -> ValueAsMut<'v, 'vm> {
|
||||||
use Value::*;
|
use Value::*;
|
||||||
use ValueAsMut as M;
|
use ValueAsMut as M;
|
||||||
match self {
|
match self {
|
||||||
Const(x) => M::Const(x),
|
Const(x) => M::Const(x),
|
||||||
Thunk(x) => M::Thunk(x),
|
Thunk(x) => M::Thunk(x),
|
||||||
ThunkRef(x) => M::ThunkRef(x),
|
ThunkRef(x) => M::Thunk(x),
|
||||||
AttrSet(x) => M::AttrSet(x),
|
AttrSet(x) => M::AttrSet(x),
|
||||||
RecAttrSet(x) => M::RecAttrSet(x),
|
RecAttrSet(x) => M::RecAttrSet(x),
|
||||||
List(x) => M::List(x),
|
List(x) => M::List(x),
|
||||||
@@ -175,7 +173,7 @@ impl Value {
|
|||||||
}
|
}
|
||||||
|
|
||||||
use Value::Const as VmConst;
|
use Value::Const as VmConst;
|
||||||
impl Value {
|
impl<'vm> Value<'vm> {
|
||||||
pub fn typename(&self) -> &'static str {
|
pub fn typename(&self) -> &'static str {
|
||||||
use Value::*;
|
use Value::*;
|
||||||
match self {
|
match self {
|
||||||
@@ -200,7 +198,7 @@ impl Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn call(self, vm: &VM, args: Vec<Value>) -> Result<Value> {
|
pub fn call(self, vm: &VM<'vm>, args: Vec<Value<'vm>>) -> Result<Value<'vm>> {
|
||||||
use Value::*;
|
use Value::*;
|
||||||
Ok(match self {
|
Ok(match self {
|
||||||
PrimOp(func) => func.call(vm, args),
|
PrimOp(func) => func.call(vm, args),
|
||||||
@@ -215,7 +213,7 @@ impl Value {
|
|||||||
PartialPrimOp(func) => {
|
PartialPrimOp(func) => {
|
||||||
return Ok(func.call(vm, [arg].into_iter().chain(iter).collect()));
|
return Ok(func.call(vm, [arg].into_iter().chain(iter).collect()));
|
||||||
}
|
}
|
||||||
Func(func) => vm.get_func(func).call(vm, arg)?,
|
Func(func) => func.call(vm, arg)?,
|
||||||
_ => todo!(),
|
_ => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -226,7 +224,7 @@ impl Value {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn not(self) -> Value {
|
pub fn not(self) -> Value<'vm> {
|
||||||
use Const::*;
|
use Const::*;
|
||||||
match self {
|
match self {
|
||||||
VmConst(Bool(bool)) => VmConst(Bool(!bool)),
|
VmConst(Bool(bool)) => VmConst(Bool(!bool)),
|
||||||
@@ -235,7 +233,7 @@ impl Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn and(self, other: Value) -> Value {
|
pub fn and(self, other: Value<'vm>) -> Value<'vm> {
|
||||||
use Const::*;
|
use Const::*;
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(VmConst(Bool(a)), VmConst(Bool(b))) => VmConst(Bool(a && b)),
|
(VmConst(Bool(a)), VmConst(Bool(b))) => VmConst(Bool(a && b)),
|
||||||
@@ -244,7 +242,7 @@ impl Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn or(self, other: Value) -> Value {
|
pub fn or(self, other: Value<'vm>) -> Value<'vm> {
|
||||||
use Const::*;
|
use Const::*;
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(VmConst(Bool(a)), VmConst(Bool(b))) => VmConst(Bool(a || b)),
|
(VmConst(Bool(a)), VmConst(Bool(b))) => VmConst(Bool(a || b)),
|
||||||
@@ -253,7 +251,7 @@ impl Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn eq(self, other: Value) -> Value {
|
pub fn eq(self, other: Value<'vm>) -> Value<'vm> {
|
||||||
use Const::Bool;
|
use Const::Bool;
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => x,
|
(x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => x,
|
||||||
@@ -261,7 +259,7 @@ impl Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn lt(self, other: Value) -> Value {
|
pub fn lt(self, other: Value<'vm>) -> Value<'vm> {
|
||||||
use Const::*;
|
use Const::*;
|
||||||
VmConst(Bool(match (self, other) {
|
VmConst(Bool(match (self, other) {
|
||||||
(VmConst(Int(a)), VmConst(Int(b))) => a < b,
|
(VmConst(Int(a)), VmConst(Int(b))) => a < b,
|
||||||
@@ -274,7 +272,7 @@ impl Value {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn neg(self) -> Value {
|
pub fn neg(self) -> Value<'vm> {
|
||||||
use Const::*;
|
use Const::*;
|
||||||
match self {
|
match self {
|
||||||
VmConst(Int(int)) => VmConst(Int(-int)),
|
VmConst(Int(int)) => VmConst(Int(-int)),
|
||||||
@@ -284,7 +282,7 @@ impl Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add(self, other: Value) -> Value {
|
pub fn add(self, other: Value<'vm>) -> Value<'vm> {
|
||||||
use Const::*;
|
use Const::*;
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(VmConst(Int(a)), VmConst(Int(b))) => VmConst(Int(a + b)),
|
(VmConst(Int(a)), VmConst(Int(b))) => VmConst(Int(a + b)),
|
||||||
@@ -301,7 +299,7 @@ impl Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mul(self, other: Value) -> Value {
|
pub fn mul(self, other: Value<'vm>) -> Value<'vm> {
|
||||||
use Const::*;
|
use Const::*;
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(VmConst(Int(a)), VmConst(Int(b))) => VmConst(Int(a * b)),
|
(VmConst(Int(a)), VmConst(Int(b))) => VmConst(Int(a * b)),
|
||||||
@@ -313,7 +311,7 @@ impl Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn div(self, other: Value) -> Value {
|
pub fn div(self, other: Value<'vm>) -> Value<'vm> {
|
||||||
use Const::*;
|
use Const::*;
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(_, VmConst(Int(0))) => todo!(),
|
(_, VmConst(Int(0))) => todo!(),
|
||||||
@@ -327,7 +325,7 @@ impl Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn concat_string(&mut self, mut other: Value) -> &mut Self {
|
pub fn concat_string(&mut self, mut other: Value<'vm>) -> &mut Self {
|
||||||
match (self.coerce_to_string(), other.coerce_to_string()) {
|
match (self.coerce_to_string(), other.coerce_to_string()) {
|
||||||
(VmConst(Const::String(a)), VmConst(Const::String(b))) => a.push_str(b.as_str()),
|
(VmConst(Const::String(a)), VmConst(Const::String(b))) => a.push_str(b.as_str()),
|
||||||
(_, Value::Catchable(_)) => *self = other,
|
(_, Value::Catchable(_)) => *self = other,
|
||||||
@@ -337,7 +335,7 @@ impl Value {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn push(&mut self, elem: Value) -> &mut Self {
|
pub fn push(&mut self, elem: Value<'vm>) -> &mut Self {
|
||||||
if let Value::List(list) = self {
|
if let Value::List(list) = self {
|
||||||
list.push(elem);
|
list.push(elem);
|
||||||
} else if let Value::Catchable(_) = self {
|
} else if let Value::Catchable(_) = self {
|
||||||
@@ -349,7 +347,7 @@ impl Value {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn concat(self, other: Value) -> Value {
|
pub fn concat(self, other: Value<'vm>) -> Value<'vm> {
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(Value::List(a), Value::List(b)) => Value::List(a.concat(b)),
|
(Value::List(a), Value::List(b)) => Value::List(a.concat(b)),
|
||||||
(x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => x,
|
(x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => x,
|
||||||
@@ -357,7 +355,7 @@ impl Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn push_attr(&mut self, sym: Symbol, val: Value) -> &mut Self {
|
pub fn push_attr(&mut self, sym: Symbol, val: Value<'vm>) -> &mut Self {
|
||||||
if let Value::AttrSet(attrs) = self {
|
if let Value::AttrSet(attrs) = self {
|
||||||
attrs.push_attr(sym, val)
|
attrs.push_attr(sym, val)
|
||||||
} else if let Value::RecAttrSet(attrs) = self {
|
} else if let Value::RecAttrSet(attrs) = self {
|
||||||
@@ -371,7 +369,7 @@ impl Value {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(self, other: Value) -> Value {
|
pub fn update(self, other: Value<'vm>) -> Value<'vm> {
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(Value::AttrSet(a), Value::AttrSet(b)) => Value::AttrSet(a.update(b)),
|
(Value::AttrSet(a), Value::AttrSet(b)) => Value::AttrSet(a.update(b)),
|
||||||
(Value::RecAttrSet(a), Value::AttrSet(b)) => Value::AttrSet(a.update_normal(b)),
|
(Value::RecAttrSet(a), Value::AttrSet(b)) => Value::AttrSet(a.update_normal(b)),
|
||||||
@@ -399,7 +397,7 @@ impl Value {
|
|||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn select_with_default(&mut self, sym: Symbol, default: Value) -> Result<&mut Self> {
|
pub fn select_with_default(&mut self, sym: Symbol, default: Value<'vm>) -> Result<&mut Self> {
|
||||||
let val = match self {
|
let val = match self {
|
||||||
Value::AttrSet(attrs) => attrs.select(sym.clone()).unwrap_or(default),
|
Value::AttrSet(attrs) => attrs.select(sym.clone()).unwrap_or(default),
|
||||||
Value::RecAttrSet(attrs) => attrs.select(sym.clone()).unwrap_or(default),
|
Value::RecAttrSet(attrs) => attrs.select(sym.clone()).unwrap_or(default),
|
||||||
@@ -438,7 +436,7 @@ impl Value {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn force(&mut self, vm: &VM) -> Result<&mut Self> {
|
pub fn force(&mut self, vm: &VM<'vm>) -> Result<&mut Self> {
|
||||||
if let Value::Thunk(thunk) = self {
|
if let Value::Thunk(thunk) = self {
|
||||||
let value = thunk.force(vm)?;
|
let value = thunk.force(vm)?;
|
||||||
*self = value
|
*self = value
|
||||||
@@ -446,7 +444,7 @@ impl Value {
|
|||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn force_deep(&mut self, vm: &VM) -> Result<&mut Self> {
|
pub fn force_deep(&mut self, vm: &VM<'vm>) -> Result<&mut Self> {
|
||||||
match self {
|
match self {
|
||||||
Value::Thunk(thunk) => {
|
Value::Thunk(thunk) => {
|
||||||
let mut value = thunk.force(vm)?;
|
let mut value = thunk.force(vm)?;
|
||||||
@@ -462,7 +460,7 @@ impl Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToPublic for Value {
|
impl ToPublic for Value<'_> {
|
||||||
fn to_public(self, vm: &VM) -> p::Value {
|
fn to_public(self, vm: &VM) -> p::Value {
|
||||||
match self {
|
match self {
|
||||||
Value::AttrSet(attrs) => attrs.to_public(vm),
|
Value::AttrSet(attrs) => attrs.to_public(vm),
|
||||||
|
|||||||
@@ -5,20 +5,20 @@ use crate::vm::VM;
|
|||||||
use super::Value;
|
use super::Value;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Constructor)]
|
#[derive(Debug, Clone, Constructor)]
|
||||||
pub struct PrimOp {
|
pub struct PrimOp<'vm> {
|
||||||
pub name: &'static str,
|
pub name: &'static str,
|
||||||
arity: u8,
|
arity: u8,
|
||||||
func: fn(&VM, Vec<Value>) -> Value,
|
func: fn(&VM<'vm>, Vec<Value<'vm>>) -> Value<'vm>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq for PrimOp {
|
impl PartialEq for PrimOp<'_> {
|
||||||
fn eq(&self, _: &Self) -> bool {
|
fn eq(&self, _: &Self) -> bool {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PrimOp {
|
impl<'vm> PrimOp<'vm> {
|
||||||
pub fn call(self, vm: &VM, args: Vec<Value>) -> Value {
|
pub fn call(self, vm: &VM<'vm>, args: Vec<Value<'vm>>) -> Value<'vm> {
|
||||||
if (args.len() as u8) < self.arity {
|
if (args.len() as u8) < self.arity {
|
||||||
Value::PartialPrimOp(PartialPrimOp {
|
Value::PartialPrimOp(PartialPrimOp {
|
||||||
name: self.name,
|
name: self.name,
|
||||||
@@ -35,21 +35,21 @@ impl PrimOp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct PartialPrimOp {
|
pub struct PartialPrimOp<'vm> {
|
||||||
pub name: &'static str,
|
pub name: &'static str,
|
||||||
arity: u8,
|
arity: u8,
|
||||||
args: Vec<Value>,
|
args: Vec<Value<'vm>>,
|
||||||
func: fn(&VM, Vec<Value>) -> Value,
|
func: fn(&VM<'vm>, Vec<Value<'vm>>) -> Value<'vm>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq for PartialPrimOp {
|
impl PartialEq for PartialPrimOp<'_> {
|
||||||
fn eq(&self, _: &Self) -> bool {
|
fn eq(&self, _: &Self) -> bool {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialPrimOp {
|
impl<'vm> PartialPrimOp<'vm> {
|
||||||
pub fn call(mut self, vm: &VM, args: Vec<Value>) -> Value {
|
pub fn call(mut self, vm: &VM<'vm>, args: Vec<Value<'vm>>) -> Value<'vm> {
|
||||||
let len = args.len() as u8;
|
let len = args.len() as u8;
|
||||||
self.args.extend(args);
|
self.args.extend(args);
|
||||||
if len < self.arity {
|
if len < self.arity {
|
||||||
|
|||||||
@@ -7,12 +7,12 @@ use crate::ty::common::Symbol;
|
|||||||
use crate::ty::internal::Value;
|
use crate::ty::internal::Value;
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct Env {
|
pub struct Env<'vm> {
|
||||||
last: RefCell<Option<Arc<Env>>>,
|
last: RefCell<Option<Arc<Env<'vm>>>>,
|
||||||
map: Arc<RefCell<HashTrieMapSync<Symbol, Value>>>,
|
map: Arc<RefCell<HashTrieMapSync<Symbol, Value<'vm>>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clone for Env {
|
impl Clone for Env<'_> {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Env {
|
Env {
|
||||||
last: RefCell::new(
|
last: RefCell::new(
|
||||||
@@ -27,24 +27,24 @@ impl Clone for Env {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct CapturedEnv {
|
pub struct CapturedEnv<'vm> {
|
||||||
env: Arc<Env>,
|
env: Arc<Env<'vm>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Env {
|
impl<'vm> Env<'vm> {
|
||||||
pub fn empty() -> Env {
|
pub fn empty() -> Self {
|
||||||
Env::default()
|
Env::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn lookup(&self, symbol: Symbol) -> Option<Value> {
|
pub fn lookup(&self, symbol: Symbol) -> Option<Value<'vm>> {
|
||||||
self.map.borrow().get(&symbol).cloned()
|
self.map.borrow().get(&symbol).cloned()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert(&self, symbol: Symbol, value: Value) {
|
pub fn insert(&self, symbol: Symbol, value: Value<'vm>) {
|
||||||
self.map.borrow_mut().insert_mut(symbol, value);
|
self.map.borrow_mut().insert_mut(symbol, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn enter(&self, new: HashTrieMapSync<Symbol, Value>) {
|
pub fn enter(&self, new: HashTrieMapSync<Symbol, Value<'vm>>) {
|
||||||
let mut map = self.map.borrow().clone();
|
let mut map = self.map.borrow().clone();
|
||||||
for (k, v) in new.iter() {
|
for (k, v) in new.iter() {
|
||||||
map.insert_mut(k.clone(), v.clone());
|
map.insert_mut(k.clone(), v.clone());
|
||||||
@@ -57,7 +57,7 @@ impl Env {
|
|||||||
*self.map.borrow_mut() = map;
|
*self.map.borrow_mut() = map;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn enter_rec(&self) -> Arc<RefCell<HashTrieMapSync<Symbol, Value>>> {
|
pub fn enter_rec(&self) -> Arc<RefCell<HashTrieMapSync<Symbol, Value<'vm>>>> {
|
||||||
let last = Env {
|
let last = Env {
|
||||||
last: self.last.clone(),
|
last: self.last.clone(),
|
||||||
map: self.map.clone(),
|
map: self.map.clone(),
|
||||||
@@ -73,17 +73,17 @@ impl Env {
|
|||||||
*self.map.borrow_mut() = map;
|
*self.map.borrow_mut() = map;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn captured(self: Arc<Self>) -> CapturedEnv {
|
pub fn captured(self: Arc<Self>) -> CapturedEnv<'vm> {
|
||||||
CapturedEnv { env: self }
|
CapturedEnv { env: self }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CapturedEnv {
|
impl<'vm> CapturedEnv<'vm> {
|
||||||
pub fn lookup(&self, symbol: Symbol) -> Option<Value> {
|
pub fn lookup(&self, symbol: Symbol) -> Option<Value<'vm>> {
|
||||||
self.env.lookup(symbol)
|
self.env.lookup(symbol)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn released(self) -> Arc<Env> {
|
pub fn released(self) -> Arc<Env<'vm>> {
|
||||||
Arc::new(self.env.as_ref().clone())
|
Arc::new(self.env.as_ref().clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use std::cell::OnceCell;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use crate::builtins::env;
|
use crate::builtins::env;
|
||||||
@@ -19,17 +20,30 @@ mod stack;
|
|||||||
mod test;
|
mod test;
|
||||||
|
|
||||||
pub fn run(prog: Program) -> Result<p::Value> {
|
pub fn run(prog: Program) -> Result<p::Value> {
|
||||||
let vm = VM::new(prog.thunks, prog.funcs);
|
let vm = VM::new(
|
||||||
Ok(vm.eval(prog.top_level, env())?.to_public(&vm))
|
prog.thunks,
|
||||||
|
prog.funcs
|
||||||
|
.into_iter()
|
||||||
|
.map(|f| Func {
|
||||||
|
env: OnceCell::new(),
|
||||||
|
param: f.param,
|
||||||
|
opcodes: f.opcodes,
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
);
|
||||||
|
let env = env();
|
||||||
|
let temp = vm.eval(prog.top_level, env)?;
|
||||||
|
let temp = temp.to_public(&vm);
|
||||||
|
Ok(temp)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct VM {
|
pub struct VM<'vm> {
|
||||||
thunks: Box<[Thunk]>,
|
thunks: Box<[Thunk<'vm>]>,
|
||||||
funcs: Box<[Func]>,
|
funcs: Box<[Func<'vm>]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VM {
|
impl<'vm> VM<'vm> {
|
||||||
fn new(thunks: Box<[OpCodes]>, funcs: Box<[Func]>) -> Self {
|
fn new(thunks: Box<[OpCodes]>, funcs: Box<[Func<'vm>]>) -> Self {
|
||||||
let thunks = thunks
|
let thunks = thunks
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|opcodes| Thunk::new(opcodes))
|
.map(|opcodes| Thunk::new(opcodes))
|
||||||
@@ -37,15 +51,15 @@ impl VM {
|
|||||||
VM { thunks, funcs }
|
VM { thunks, funcs }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_thunk(&self, idx: usize) -> &Thunk {
|
pub fn get_thunk(&self, idx: usize) -> &'vm Thunk<'vm> {
|
||||||
&self.thunks[idx]
|
unsafe { &*(&self.thunks[idx] as *const _) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_func(&self, idx: usize) -> &Func {
|
pub fn get_func(&self, idx: usize) -> &'vm Func<'vm> {
|
||||||
&self.funcs[idx]
|
unsafe { &*(&self.funcs[idx] as *const _) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn eval(&self, opcodes: OpCodes, env: Arc<Env>) -> Result<Value> {
|
pub fn eval(&self, opcodes: OpCodes, env: Arc<Env<'vm>>) -> Result<Value<'vm>> {
|
||||||
let mut stack = Stack::<STACK_SIZE>::new();
|
let mut stack = Stack::<STACK_SIZE>::new();
|
||||||
let mut iter = opcodes.into_iter();
|
let mut iter = opcodes.into_iter();
|
||||||
while let Some(opcode) = iter.next() {
|
while let Some(opcode) = iter.next() {
|
||||||
@@ -61,12 +75,13 @@ impl VM {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn single_op<const CAP: usize>(
|
fn single_op<'s, const CAP: usize>(
|
||||||
&self,
|
&self,
|
||||||
opcode: OpCode,
|
opcode: OpCode,
|
||||||
stack: &mut Stack<CAP>,
|
stack: &'s mut Stack<'vm, CAP>,
|
||||||
env: Arc<Env>,
|
env: Arc<Env<'vm>>,
|
||||||
) -> Result<usize> {
|
) -> Result<usize>
|
||||||
|
{
|
||||||
match opcode {
|
match opcode {
|
||||||
OpCode::Illegal => panic!("illegal opcode"),
|
OpCode::Illegal => panic!("illegal opcode"),
|
||||||
OpCode::Const { value } => stack.push(Value::Const(value))?,
|
OpCode::Const { value } => stack.push(Value::Const(value))?,
|
||||||
@@ -101,8 +116,9 @@ impl VM {
|
|||||||
stack.push(func.call(self, args)?)?;
|
stack.push(func.call(self, args)?)?;
|
||||||
}
|
}
|
||||||
OpCode::Func { idx } => {
|
OpCode::Func { idx } => {
|
||||||
self.get_func(idx).env.get_or_init(|| env.captured());
|
let func = self.get_func(idx);
|
||||||
stack.push(Value::Func(idx))?;
|
func.env.get_or_init(|| env.captured());
|
||||||
|
stack.push(Value::Func(func))?;
|
||||||
}
|
}
|
||||||
OpCode::UnOp { op } => {
|
OpCode::UnOp { op } => {
|
||||||
use UnOp::*;
|
use UnOp::*;
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ use crate::ty::internal::Value;
|
|||||||
|
|
||||||
pub const STACK_SIZE: usize = 8 * 1024 / size_of::<Value>();
|
pub const STACK_SIZE: usize = 8 * 1024 / size_of::<Value>();
|
||||||
|
|
||||||
pub struct Stack<const CAP: usize> {
|
pub struct Stack<'vm, const CAP: usize> {
|
||||||
items: Box<[MaybeUninit<Value>; CAP]>,
|
items: Box<[MaybeUninit<Value<'vm>>; CAP]>,
|
||||||
top: usize,
|
top: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -17,7 +17,7 @@ macro_rules! into {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const CAP: usize> Stack<CAP> {
|
impl<'vm, const CAP: usize> Stack<'vm, CAP> {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Stack {
|
Stack {
|
||||||
items: (0..CAP)
|
items: (0..CAP)
|
||||||
@@ -29,7 +29,7 @@ impl<const CAP: usize> Stack<CAP> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn push(&mut self, item: Value) -> Result<()> {
|
pub fn push(&mut self, item: Value<'vm>) -> Result<()> {
|
||||||
self.items
|
self.items
|
||||||
.get_mut(self.top)
|
.get_mut(self.top)
|
||||||
.map_or_else(
|
.map_or_else(
|
||||||
@@ -41,14 +41,14 @@ impl<const CAP: usize> Stack<CAP> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pop(&mut self) -> Value {
|
pub fn pop(&mut self) -> Value<'vm> {
|
||||||
self.top -= 1;
|
self.top -= 1;
|
||||||
let item = self.items.get_mut(self.top).unwrap();
|
let item = self.items.get_mut(self.top).unwrap();
|
||||||
|
|
||||||
unsafe { replace(item, MaybeUninit::uninit()).assume_init() }
|
unsafe { replace(item, MaybeUninit::uninit()).assume_init() }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn tos_mut(&mut self) -> Result<&mut Value> {
|
pub fn tos_mut(&mut self) -> Result<&mut Value<'vm>> {
|
||||||
if self.top == 0 {
|
if self.top == 0 {
|
||||||
panic!("stack empty")
|
panic!("stack empty")
|
||||||
} else {
|
} else {
|
||||||
@@ -57,14 +57,14 @@ impl<const CAP: usize> Stack<CAP> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const CAP: usize> Deref for Stack<CAP> {
|
impl<'vm, const CAP: usize> Deref for Stack<'vm, CAP> {
|
||||||
type Target = [Value];
|
type Target = [Value<'vm>];
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
into!(&self.items[0..self.top])
|
into!(&self.items[0..self.top])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const CAP: usize> Drop for Stack<CAP> {
|
impl<const CAP: usize> Drop for Stack<'_, CAP> {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
self.items.as_mut_slice()[0..self.top]
|
self.items.as_mut_slice()[0..self.top]
|
||||||
.iter_mut()
|
.iter_mut()
|
||||||
|
|||||||
@@ -204,7 +204,6 @@ fn test_fib() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
#[ignore]
|
|
||||||
fn bench_fib(b: &mut Bencher) {
|
fn bench_fib(b: &mut Bencher) {
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
black_box(test_expr(
|
black_box(test_expr(
|
||||||
|
|||||||
Reference in New Issue
Block a user