feat: stash

This commit is contained in:
2025-05-10 16:29:55 +08:00
parent 14045f7924
commit f86c088e97
21 changed files with 222 additions and 219 deletions

View File

@@ -1,15 +1,15 @@
use std::cell::RefCell;
use std::sync::Arc;
use anyhow::Result;
use derive_more::Constructor;
use rpds::HashTrieMapSync;
use crate::error::Result;
use crate::vm::VM;
use super::super::common::Symbol;
use super::super::public as p;
use super::{ToPublic, Value};
use crate::vm::VM;
#[derive(Debug, Constructor, Clone, PartialEq)]
pub struct AttrSet {
@@ -28,7 +28,7 @@ impl AttrSet {
}
pub fn push_attr(&mut self, sym: Symbol, val: Value) {
if self.data.get(&sym).is_some() {
if let Some(_) = self.data.get_mut(&sym) {
todo!()
}
self.data.insert_mut(sym, val);

View File

@@ -1,6 +1,4 @@
use anyhow::Error;
use derive_more::{IsVariant, Unwrap};
use ecow::EcoString;
#[derive(Debug, Clone, IsVariant, Unwrap)]
@@ -47,49 +45,6 @@ impl From<&str> for Const {
}
}
impl<'a> TryFrom<&'a Const> for &'a bool {
type Error = Error;
fn try_from(value: &'a Const) -> Result<Self, Self::Error> {
match value {
Const::Bool(b) => Ok(b),
_ => panic!(),
}
}
}
impl<'a> TryFrom<&'a Const> for &'a i64 {
type Error = Error;
fn try_from(value: &'a Const) -> Result<Self, Self::Error> {
match value {
Const::Int(int) => Ok(int),
_ => panic!(),
}
}
}
impl<'a> TryFrom<&'a Const> for &'a f64 {
type Error = Error;
fn try_from(value: &'a Const) -> Result<Self, Self::Error> {
match value {
Const::Float(float) => Ok(float),
_ => panic!(),
}
}
}
impl<'a> TryFrom<&'a Const> for &'a str {
type Error = Error;
fn try_from(value: &'a Const) -> Result<Self, Self::Error> {
match value {
Const::String(string) => Ok(string),
_ => panic!(),
}
}
}
impl PartialEq for Const {
fn eq(&self, other: &Self) -> bool {
use Const::*;

View File

@@ -3,8 +3,8 @@ use itertools::Itertools;
use rpds::HashTrieMap;
use crate::bytecode::OpCodes;
use crate::error::Result;
use crate::ty::internal::{Thunk, Value};
use crate::vm::{CapturedEnv, VM};
#[derive(Debug, Clone)]
@@ -33,7 +33,7 @@ impl Func {
}
}
pub fn call(self, vm: &VM, arg: Value) -> Value {
pub fn call(self, vm: &VM, arg: Value) -> Result<Value> {
use Param::*;
let env = self.env.released();
@@ -71,7 +71,7 @@ impl Func {
}
}
vm.eval(self.opcodes, env).unwrap()
vm.eval(self.opcodes, env)
}
pub fn push_ident_param(&mut self, param: EcoString) {

View File

@@ -1,11 +1,11 @@
use anyhow::Result;
use derive_more::Constructor;
use rpds::VectorSync;
use crate::error::Result;
use crate::ty::public as p;
use crate::vm::VM;
use super::{ToPublic, Value};
use crate::vm::VM;
#[derive(Debug, Constructor, Clone, PartialEq)]
pub struct List {

View File

@@ -1,7 +1,6 @@
use std::cell::RefCell;
use std::sync::Arc;
use anyhow::{Result, anyhow};
use derive_more::{IsVariant, Unwrap};
use super::common as c;
@@ -11,6 +10,7 @@ use super::public as p;
use c::Symbol;
use crate::bytecode::OpCodes;
use crate::error::*;
use crate::vm::{Env, VM};
mod attrset;
@@ -65,9 +65,10 @@ impl Thunk {
match &*self.thunk.borrow() {
_Thunk::Value(value) => return Ok(value.as_ref().clone()),
_Thunk::SuspendedFrom(from) => {
return Err(anyhow!(
"already suspended from {from:p} (infinite recursion encountered)"
));
return Err(Error::EvalError(format!(
"thunk {:p} already suspended from {from:p} (infinite recursion encountered)",
self as *const Thunk
)));
}
_Thunk::Code(_) => (),
}
@@ -77,9 +78,7 @@ impl Thunk {
_Thunk::SuspendedFrom(self as *const Thunk),
)
.unwrap_code();
let value = vm
.eval(opcodes, self.env.borrow().clone().unwrap())
.unwrap();
let value = vm.eval(opcodes, self.env.borrow().clone().unwrap())?;
let _ = std::mem::replace(
&mut *self.thunk.borrow_mut(),
_Thunk::Value(value.clone().into()),
@@ -199,9 +198,9 @@ impl Value {
}
}
pub fn call(self, vm: &VM, args: Vec<Value>) -> Value {
pub fn call(self, vm: &VM, args: Vec<Value>) -> Result<Value> {
use Value::*;
match self {
Ok(match self {
PrimOp(func) => func.call(vm, args),
PartialPrimOp(func) => func.call(vm, args),
mut func @ Value::Func(_) => {
@@ -209,12 +208,12 @@ impl Value {
while let Some(arg) = iter.next() {
func = match func {
PrimOp(func) => {
return func.call(vm, [arg].into_iter().chain(iter).collect());
return Ok(func.call(vm, [arg].into_iter().chain(iter).collect()));
}
PartialPrimOp(func) => {
return func.call(vm, [arg].into_iter().chain(iter).collect());
return Ok(func.call(vm, [arg].into_iter().chain(iter).collect()));
}
Func(func) => func.call(vm, arg),
Func(func) => func.call(vm, arg)?,
_ => todo!(),
}
}
@@ -222,7 +221,7 @@ impl Value {
}
x @ Catchable(_) => x,
_ => todo!(),
}
})
}
pub fn not(self) -> Value {

View File

@@ -1,9 +1,10 @@
use std::fmt::{Display, Formatter, Result as FmtResult};
use anyhow::Error;
use derive_more::{IsVariant, Unwrap};
use ecow::EcoString;
use crate::error::Error;
use super::super::internal as i;
#[derive(Debug, Clone, IsVariant, Unwrap)]