2022-07-20 17:56:00 -05:00
|
|
|
use crate::list::List;
|
|
|
|
use ordered_float::OrderedFloat;
|
2022-07-21 05:32:24 -05:00
|
|
|
use std::{
|
|
|
|
borrow::Cow,
|
|
|
|
collections::BTreeMap,
|
|
|
|
fmt::{Display, Write},
|
2022-07-21 14:54:28 -05:00
|
|
|
rc::Rc,
|
2022-07-21 05:32:24 -05:00
|
|
|
};
|
2022-07-20 17:56:00 -05:00
|
|
|
|
|
|
|
/// A Wisp value
|
|
|
|
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
2022-07-20 18:42:25 -05:00
|
|
|
pub enum Value<'a> {
|
|
|
|
List(Box<List<'a>>),
|
2022-07-20 17:56:00 -05:00
|
|
|
Vector(Vec<Self>),
|
2022-07-20 18:08:40 -05:00
|
|
|
Map(BTreeMap<Self, Self>),
|
2022-07-21 11:02:30 -05:00
|
|
|
Module(Module<'a>),
|
2022-07-21 14:54:28 -05:00
|
|
|
Function(Function<'a>),
|
2022-07-21 06:22:25 -05:00
|
|
|
Symbol(Symbol<'a>),
|
2022-07-20 18:42:25 -05:00
|
|
|
Keyword(Cow<'a, str>),
|
2022-07-20 17:56:00 -05:00
|
|
|
Bool(bool),
|
|
|
|
Number(OrderedFloat<f64>),
|
2022-07-20 18:42:25 -05:00
|
|
|
String(Cow<'a, str>),
|
2022-07-20 17:56:00 -05:00
|
|
|
Nil,
|
|
|
|
}
|
2022-07-21 05:32:24 -05:00
|
|
|
|
2022-07-21 12:25:33 -05:00
|
|
|
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
pub struct Module<'a> {
|
2022-07-21 14:54:28 -05:00
|
|
|
members: BTreeMap<u64, Rc<Value<'a>>>,
|
2022-07-21 12:25:33 -05:00
|
|
|
symbol_table: BTreeMap<String, u64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
2022-07-21 14:54:28 -05:00
|
|
|
pub struct Function<'a> {
|
|
|
|
// TODO: bytecode
|
|
|
|
captures: Box<[Capture<'a>]>,
|
|
|
|
symbol_table: BTreeMap<Symbol<'a>, SymbolMapping>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
pub enum Capture<'a> {
|
|
|
|
Reference(Rc<Value<'a>>),
|
|
|
|
Owned(Value<'a>),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
pub enum SymbolMapping {
|
|
|
|
Local(u64),
|
|
|
|
Capture(u64),
|
|
|
|
}
|
2022-07-21 12:25:33 -05:00
|
|
|
|
2022-07-21 06:22:25 -05:00
|
|
|
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
pub enum Symbol<'a> {
|
|
|
|
Interned(u64),
|
|
|
|
String(Cow<'a, str>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<&'a str> for Symbol<'a> {
|
|
|
|
fn from(s: &'a str) -> Self {
|
|
|
|
Self::String(Cow::Borrowed(s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Display for Symbol<'a> {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
Symbol::Interned(n) => write!(f, "#{n}"),
|
|
|
|
Symbol::String(s) => write!(f, "{s}"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 05:32:24 -05:00
|
|
|
impl<'a> Display for Value<'a> {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
Value::List(list) => write!(f, "{list}"),
|
|
|
|
Value::Vector(vec) => write_seq(f, vec, "[", "]"),
|
|
|
|
Value::Map(map) => write_seq(
|
|
|
|
f,
|
|
|
|
map.into_iter().map(|(k, v)| format!("{k} {v}")),
|
|
|
|
"[",
|
|
|
|
"]",
|
|
|
|
),
|
2022-07-21 11:02:30 -05:00
|
|
|
Value::Module(_) => write!(f, "#module#"),
|
2022-07-21 05:32:24 -05:00
|
|
|
Value::Symbol(sym) => write!(f, "{sym}"),
|
|
|
|
Value::Keyword(kw) => write!(f, ":{kw}"),
|
|
|
|
Value::Function { .. } => write!(f, "fn"),
|
|
|
|
Value::Bool(b) => write!(f, "{b}"),
|
|
|
|
Value::Number(n) => write!(f, "{n}"),
|
|
|
|
Value::String(s) => write!(f, "\"{s}\""),
|
|
|
|
Value::Nil => write!(f, "nil"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn write_seq(
|
|
|
|
f: &mut impl Write,
|
|
|
|
iterable: impl IntoIterator<Item = impl Display>,
|
|
|
|
delimiter_left: &str,
|
|
|
|
delimiter_right: &str,
|
|
|
|
) -> std::fmt::Result {
|
|
|
|
let mut iter = iterable.into_iter();
|
|
|
|
write!(f, "{delimiter_left}")?;
|
|
|
|
if let Some(x) = iter.next() {
|
|
|
|
write!(f, "{x}")?;
|
|
|
|
}
|
|
|
|
for x in iter {
|
|
|
|
write!(f, " {x}")?;
|
|
|
|
}
|
|
|
|
write!(f, "{delimiter_right}")
|
|
|
|
}
|