use std::fmt::{Debug, Display, Formatter, Result as FmtResult}; use std::ops::Deref; use std::sync::Arc; use derive_more::{Constructor, IsVariant, Unwrap}; use ecow::EcoString; use rpds::{HashTrieMapSync, VectorSync}; use crate::bytecode::{Args, Const, OpCodes}; #[derive(Debug, Clone, Hash, PartialEq, Eq, Constructor)] pub struct Symbol(EcoString); impl> From for Symbol { fn from(value: T) -> Self { Symbol(value.into()) } } impl Deref for Symbol { type Target = str; fn deref(&self) -> &Self::Target { &self.0 } } #[derive(Debug, Clone, Hash)] pub struct Func { pub args: Args, pub opcodes: OpCodes, } impl PartialEq for Func { fn eq(&self, _: &Self) -> bool { false } } /* #[derive(IsVariant, Unwrap, Clone, Debug, PartialEq)] pub enum Const { Int(i64), Float(f64), Bool(bool), String(EcoString), Func(Arc), } impl Display for Const { fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { write!(f, "Const::")?; match self { Const::Int(int) => write!(f, "Int@{}", int), Const::Float(float) => write!(f, "Float@{}", float), Const::Bool(bool) => write!(f, "Bool@{}", bool), Const::String(string) => write!(f, r#"String@"{}""#, string.as_ref()), Const::Func(func) => write!(f, "Func@{:?}", func.as_ref() as *const Func), } } } impl From for Const { fn from(value: ByteCodeConst) -> Self { use ByteCodeConst::*; match value { Int(int) => Const::Int(int), Float(float) => Const::Float(float), Bool(bool) => Const::Bool(bool), String(string) => Const::String(EcoString::from(string)), Func(func) => Const::Func(Arc::new(func)), } } } */ #[derive(Constructor, Clone, PartialEq)] pub struct AttrSet { data: HashTrieMapSync, } impl Debug for AttrSet { fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { write!(f, "{{ ")?; for (k, v) in self.data.iter() { write!(f, "{k:?} = {v:?}; ")?; } write!(f, "}}") } } #[derive(Constructor, Clone, Debug, PartialEq)] pub struct List { data: VectorSync, } #[derive(Clone, Debug, PartialEq, Constructor)] pub struct Catchable { msg: Option } #[derive(IsVariant, Unwrap, Clone, Debug, PartialEq)] pub enum Value { Const(Const), AttrSet(AttrSet), List(List), Catchable(Catchable), Thunk, PrimOp, PartialPrimOp, }