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
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum List {
Cons(Value, Box<Self>),
pub enum List<'a> {
Cons(Value<'a>, Box<Self>),
#[default]
Nil,
}
impl List {
pub fn from_vec(vec: Vec<Value>) -> Self {
impl<'a> List<'a> {
pub fn from_vec(vec: Vec<Value<'a>>) -> Self {
vec.into_iter()
.rev()
.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)))
}
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| {
let atom = select! {
Token::Symbol("true") => Value::Bool(true),
Token::Symbol("false") => Value::Bool(false),
Token::Symbol("nil") => Value::Nil,
Token::Symbol(s) => Value::Symbol(s.to_owned()),
Token::Keyword(k) => Value::Keyword(k.to_owned()),
Token::String(s) => Value::String(s.to_owned()),
Token::Symbol(s) => Value::Symbol(s.into()),
Token::Keyword(k) => Value::Keyword(k.into()),
Token::String(s) => Value::String(s.into()),
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| {
Value::List(Box::new(List::Cons(
Value::Symbol("quote".to_owned()),
Value::Symbol("quote".into()),
Box::new(List::Cons(value, Box::new(List::Nil))),
)))
});

View file

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