added some conversions

This commit is contained in:
Erin 2022-08-06 00:17:47 +02:00 committed by ondra05
parent f23266f63c
commit 3496fec48e

View file

@ -6,6 +6,7 @@ pub use function::Function;
pub use pair::DotPair; pub use pair::DotPair;
pub use string::Str; pub use string::Str;
use crate::syntax::ast::{Expr, Spanned};
use std::{collections::BTreeMap, rc::Rc}; use std::{collections::BTreeMap, rc::Rc};
pub type OrderedF64 = ordered_float::OrderedFloat<f64>; pub type OrderedF64 = ordered_float::OrderedFloat<f64>;
@ -23,3 +24,27 @@ pub enum Value<'s> {
Macro(Function), Macro(Function),
Nil, Nil,
} }
impl<'s> From<Spanned<Expr<'s>>> for Value<'s> {
fn from(e: Spanned<Expr<'s>>) -> Self {
e.item.into()
}
}
impl<'s> From<Expr<'s>> for Value<'s> {
fn from(e: Expr<'s>) -> Self {
match e {
Expr::List(_) => todo!(),
Expr::Vector(v) => Self::Vector(Rc::new(v.into_iter().map(Into::into).collect())),
Expr::Pair((l, r)) => Self::DotPair(Rc::new(DotPair((*l).into(), (*r).into()))),
Expr::Quote(e) => Self::DotPair(Rc::new(DotPair(
Self::Symbol("quote".into()),
Self::DotPair(Rc::new(DotPair((*e).into(), Self::Nil))),
))),
Expr::Symbol(s) => Self::Symbol(s.into()),
Expr::Keyword(s) => Self::Keyword(s.into()),
Expr::Number(n) => Self::Number(n),
Expr::String(s) => Self::String(s.into()),
}
}
}