84 lines
2.0 KiB
Rust
84 lines
2.0 KiB
Rust
use std::fmt::{Debug, Display, Formatter, Result as FmtResult};
|
|
|
|
use derive_more::{Constructor, IsVariant, Unwrap};
|
|
use rpds::{HashTrieMapSync, VectorSync};
|
|
|
|
use super::common::*;
|
|
|
|
mod cnst;
|
|
|
|
pub use cnst::Const;
|
|
|
|
#[derive(Constructor, Clone, PartialEq)]
|
|
pub struct AttrSet {
|
|
data: HashTrieMapSync<Symbol, Value>,
|
|
}
|
|
|
|
impl Debug for AttrSet {
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
|
|
use Value::*;
|
|
write!(f, "{{ ")?;
|
|
for (k, v) in self.data.iter() {
|
|
match v {
|
|
List(_) => write!(f, "{k:?} = [ ... ]; ")?,
|
|
AttrSet(_) => write!(f, "{k:?} = {{ ... }}; ")?,
|
|
v => write!(f, "{k:?} = {v:?}; ")?,
|
|
}
|
|
}
|
|
write!(f, "}}")
|
|
}
|
|
}
|
|
|
|
impl Display 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<Value>,
|
|
}
|
|
|
|
impl Display for List {
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
|
|
write!(f, "[ ")?;
|
|
for v in self.data.iter() {
|
|
write!(f, "{v} ")?;
|
|
}
|
|
write!(f, "]")
|
|
}
|
|
}
|
|
|
|
#[derive(IsVariant, Unwrap, Clone, Debug, PartialEq)]
|
|
pub enum Value {
|
|
Const(Const),
|
|
AttrSet(AttrSet),
|
|
List(List),
|
|
Catchable(Catchable),
|
|
Thunk,
|
|
Func,
|
|
PrimOp(&'static str),
|
|
PartialPrimOp(&'static str),
|
|
}
|
|
|
|
impl Display for Value {
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
|
|
use Value::*;
|
|
match self {
|
|
Const(x) => write!(f, "{x}"),
|
|
AttrSet(x) => write!(f, "{x}"),
|
|
List(x) => write!(f, "{x}"),
|
|
Catchable(x) => write!(f, "{x}"),
|
|
Thunk => write!(f, "<CODE>"),
|
|
Func => write!(f, "<LAMBDA>"),
|
|
PrimOp(x) => write!(f, "<PRIMOP {x}>"),
|
|
PartialPrimOp(x) => write!(f, "<PARTIAL PRIMOP {x}>"),
|
|
}
|
|
}
|
|
}
|