This commit is contained in:
greg 2017-12-03 06:04:53 -08:00
parent d235b47bc5
commit 6e188976f9

View File

@ -58,6 +58,7 @@ fn read(input: &str) -> Result<Vec<Sexp>, String> {
enum Token { enum Token {
LParen, LParen,
RParen, RParen,
Quote,
Word(String) Word(String)
} }
@ -81,6 +82,7 @@ fn tokenize(input: &mut Peekable<Chars>) -> Vec<Token> {
None => break, None => break,
Some('(') => tokens.push(LParen), Some('(') => tokens.push(LParen),
Some(')') => tokens.push(RParen), Some(')') => tokens.push(RParen),
Some('\'') => tokens.push(Quote),
Some(c) if c.is_whitespace() => continue, Some(c) if c.is_whitespace() => continue,
Some(c) => { Some(c) => {
let sym: String = input.peeking_take_while(|next| { let sym: String = input.peeking_take_while(|next| {
@ -103,6 +105,10 @@ fn parse(tokens: &mut Peekable<IntoIter<Token>>) -> Result<Sexp, String> {
Some(Word(s)) => Ok(Sexp::Atom(AtomT::Symbol(s))), Some(Word(s)) => Ok(Sexp::Atom(AtomT::Symbol(s))),
Some(LParen) => parse_sexp(tokens), Some(LParen) => parse_sexp(tokens),
Some(RParen) => Err(format!("Unexpected ')'")), Some(RParen) => Err(format!("Unexpected ')'")),
Some(Quote) => {
let quoted = parse(tokens)?;
Ok(Sexp::List(vec![Sexp::Atom(AtomT::Symbol(format!("quote"))), quoted]))
},
None => Err(format!("Unexpected end of input")), None => Err(format!("Unexpected end of input")),
} }
} }