Added map support

This commit is contained in:
Erin 2022-07-21 01:08:40 +02:00 committed by ondra05
parent a5e5043c87
commit 8c47a30f8e
3 changed files with 22 additions and 1 deletions

View file

@ -16,9 +16,18 @@ pub enum Token<'a> {
#[token("]")] #[token("]")]
RightBracket, RightBracket,
#[token("{")]
LeftCurly,
#[token["}"]]
RightCurly,
#[token("'")] #[token("'")]
Quote, Quote,
#[token(",")]
Comma,
// Values // Values
#[regex("\"(\\.|[^\"])*\"", (lex_slice::<1, 1>))] #[regex("\"(\\.|[^\"])*\"", (lex_slice::<1, 1>))]
String(&'a str), String(&'a str),

View file

@ -33,6 +33,15 @@ fn parser<'a>() -> impl Parser<Token<'a>, Vec<Value>, Error = Simple<Token<'a>>>
.map(Value::Vector) .map(Value::Vector)
.delimited_by(just(Token::LeftBracket), just(Token::RightBracket)); .delimited_by(just(Token::LeftBracket), just(Token::RightBracket));
let map = value
.clone()
.then(value.clone())
.separated_by(just(Token::Comma))
.allow_trailing()
.collect()
.map(Value::Map)
.delimited_by(just(Token::LeftCurly), just(Token::RightCurly));
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".to_owned()),
@ -40,9 +49,10 @@ fn parser<'a>() -> impl Parser<Token<'a>, Vec<Value>, Error = Simple<Token<'a>>>
))) )))
}); });
atom.or(list).or(vector).or(quote) atom.or(list).or(vector).or(map).or(quote)
}) })
.repeated() .repeated()
.then_ignore(end())
} }
/// Convert Logos' Lexer into Chumsky'a Stream /// Convert Logos' Lexer into Chumsky'a Stream

View file

@ -1,11 +1,13 @@
use crate::list::List; use crate::list::List;
use ordered_float::OrderedFloat; use ordered_float::OrderedFloat;
use std::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 {
List(Box<List>), List(Box<List>),
Vector(Vec<Self>), Vector(Vec<Self>),
Map(BTreeMap<Self, Self>),
Symbol(String), Symbol(String),
Keyword(String), Keyword(String),
Function { args: Vec<Value>, body: Box<Value> }, Function { args: Vec<Value>, body: Box<Value> },