From 3496fec48e3747bcfb39af38d4259618bbfa3155 Mon Sep 17 00:00:00 2001 From: Erin Date: Sat, 6 Aug 2022 00:17:47 +0200 Subject: [PATCH] added some conversions --- src/interpreter/value/mod.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/interpreter/value/mod.rs b/src/interpreter/value/mod.rs index 43d5226..1380e50 100644 --- a/src/interpreter/value/mod.rs +++ b/src/interpreter/value/mod.rs @@ -6,6 +6,7 @@ pub use function::Function; pub use pair::DotPair; pub use string::Str; +use crate::syntax::ast::{Expr, Spanned}; use std::{collections::BTreeMap, rc::Rc}; pub type OrderedF64 = ordered_float::OrderedFloat; @@ -23,3 +24,27 @@ pub enum Value<'s> { Macro(Function), Nil, } + +impl<'s> From>> for Value<'s> { + fn from(e: Spanned>) -> Self { + e.item.into() + } +} + +impl<'s> From> 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()), + } + } +}