This commit is contained in:
Erin 2022-07-21 01:42:25 +02:00 committed by ondra05
parent d52e9a2800
commit ea7ccfffb5
3 changed files with 19 additions and 16 deletions

View file

@ -2,14 +2,14 @@ use crate::value::Value;
/// Single-linked list /// Single-linked list
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Default, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum List { pub enum List<'a> {
Cons(Value, Box<Self>), Cons(Value<'a>, Box<Self>),
#[default] #[default]
Nil, Nil,
} }
impl List { impl<'a> List<'a> {
pub fn from_vec(vec: Vec<Value>) -> Self { pub fn from_vec(vec: Vec<Value<'a>>) -> Self {
vec.into_iter() vec.into_iter()
.rev() .rev()
.fold(Self::Nil, |list, next| Self::Cons(next, Box::new(list))) .fold(Self::Nil, |list, next| Self::Cons(next, Box::new(list)))

View file

@ -7,15 +7,15 @@ pub fn read(src: &str) -> Result<Vec<Value>, Vec<Simple<Token<'_>>>> {
parser().parse(stream_of_lexer(Token::lexer(src))) parser().parse(stream_of_lexer(Token::lexer(src)))
} }
fn parser<'a>() -> impl Parser<Token<'a>, Vec<Value>, Error = Simple<Token<'a>>> { fn parser<'a>() -> impl Parser<Token<'a>, Vec<Value<'a>>, Error = Simple<Token<'a>>> {
recursive(|value| { recursive(|value| {
let atom = select! { let atom = select! {
Token::Symbol("true") => Value::Bool(true), Token::Symbol("true") => Value::Bool(true),
Token::Symbol("false") => Value::Bool(false), Token::Symbol("false") => Value::Bool(false),
Token::Symbol("nil") => Value::Nil, Token::Symbol("nil") => Value::Nil,
Token::Symbol(s) => Value::Symbol(s.to_owned()), Token::Symbol(s) => Value::Symbol(s.into()),
Token::Keyword(k) => Value::Keyword(k.to_owned()), Token::Keyword(k) => Value::Keyword(k.into()),
Token::String(s) => Value::String(s.to_owned()), Token::String(s) => Value::String(s.into()),
Token::Number(n) => Value::Number(n), Token::Number(n) => Value::Number(n),
}; };
@ -44,7 +44,7 @@ fn parser<'a>() -> impl Parser<Token<'a>, Vec<Value>, Error = Simple<Token<'a>>>
let quote = just(Token::Quote).ignore_then(value).map(|value| { let quote = just(Token::Quote).ignore_then(value).map(|value| {
Value::List(Box::new(List::Cons( Value::List(Box::new(List::Cons(
Value::Symbol("quote".to_owned()), Value::Symbol("quote".into()),
Box::new(List::Cons(value, Box::new(List::Nil))), Box::new(List::Cons(value, Box::new(List::Nil))),
))) )))
}); });

View file

@ -1,18 +1,21 @@
use crate::list::List; use crate::list::List;
use ordered_float::OrderedFloat; use ordered_float::OrderedFloat;
use std::collections::BTreeMap; use std::{borrow::Cow, collections::BTreeMap};
/// A Wisp value /// A Wisp value
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum Value { pub enum Value<'a> {
List(Box<List>), List(Box<List<'a>>),
Vector(Vec<Self>), Vector(Vec<Self>),
Map(BTreeMap<Self, Self>), Map(BTreeMap<Self, Self>),
Symbol(String), Symbol(Cow<'a, str>),
Keyword(String), Keyword(Cow<'a, str>),
Function { args: Vec<Value>, body: Box<Value> }, Function {
args: Vec<Value<'a>>,
body: Box<Value<'a>>,
},
Bool(bool), Bool(bool),
Number(OrderedFloat<f64>), Number(OrderedFloat<f64>),
String(String), String(Cow<'a, str>),
Nil, Nil,
} }