Convert to more lispish Cons
This commit is contained in:
parent
18d8ca7bd5
commit
0e17e45f3e
@ -53,13 +53,9 @@ impl EvaluatorState {
|
|||||||
SymbolAtom(sym) => unimplemented!(),
|
SymbolAtom(sym) => unimplemented!(),
|
||||||
expr @ StringAtom(_) => expr,
|
expr @ StringAtom(_) => expr,
|
||||||
expr @ NumberAtom(_) => expr,
|
expr @ NumberAtom(_) => expr,
|
||||||
List(items) => {
|
Cons(ref car, ref cdr) => {
|
||||||
if items.len() == 0 {
|
match **car {
|
||||||
return Ok(List(items))
|
SymbolAtom(ref sym) => match &sym[..] {
|
||||||
}
|
|
||||||
let ref first = items[0];
|
|
||||||
match first {
|
|
||||||
&SymbolAtom(ref sym) => match &sym[..] {
|
|
||||||
"quote" => unimplemented!(),
|
"quote" => unimplemented!(),
|
||||||
"eq?" => unimplemented!(),
|
"eq?" => unimplemented!(),
|
||||||
"cons" => unimplemented!(),
|
"cons" => unimplemented!(),
|
||||||
@ -73,7 +69,8 @@ impl EvaluatorState {
|
|||||||
},
|
},
|
||||||
_ => unimplemented!()
|
_ => unimplemented!()
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
Nil => Nil,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -103,7 +100,8 @@ enum Sexp {
|
|||||||
SymbolAtom(String),
|
SymbolAtom(String),
|
||||||
StringAtom(String),
|
StringAtom(String),
|
||||||
NumberAtom(u64),
|
NumberAtom(u64),
|
||||||
List(Vec<Sexp>),
|
Cons(Box<Sexp>, Box<Sexp>),
|
||||||
|
Nil
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Sexp {
|
impl Sexp {
|
||||||
@ -113,15 +111,8 @@ impl Sexp {
|
|||||||
&SymbolAtom(ref sym) => format!("{}", sym),
|
&SymbolAtom(ref sym) => format!("{}", sym),
|
||||||
&StringAtom(ref s) => format!("\"{}\"", s),
|
&StringAtom(ref s) => format!("\"{}\"", s),
|
||||||
&NumberAtom(ref n) => format!("{}", n),
|
&NumberAtom(ref n) => format!("{}", n),
|
||||||
&List(ref sexprs) => {
|
&Cons(ref car, ref cdr) => format!("({} . {}", car.print(), cdr.print()),
|
||||||
let mut output = String::new();
|
&Nil => format!("()"),
|
||||||
write!(&mut output, "(").unwrap();
|
|
||||||
for sexpr in sexprs.iter() {
|
|
||||||
write!(&mut output, "{}", sexpr.print()).unwrap();
|
|
||||||
}
|
|
||||||
write!(&mut output, ")").unwrap();
|
|
||||||
output
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -178,7 +169,7 @@ fn parse(tokens: &mut Peekable<IntoIter<Token>>) -> Result<Sexp, String> {
|
|||||||
Some(RParen) => Err(format!("Unexpected ')'")),
|
Some(RParen) => Err(format!("Unexpected ')'")),
|
||||||
Some(Quote) => {
|
Some(Quote) => {
|
||||||
let quoted = parse(tokens)?;
|
let quoted = parse(tokens)?;
|
||||||
Ok(List(vec![SymbolAtom(format!("quote")), quoted]))
|
Ok(Cons(Box::new(SymbolAtom(format!("quote"))), Box::new(quoted)))
|
||||||
},
|
},
|
||||||
Some(NumLiteral(n)) => Ok(NumberAtom(n)),
|
Some(NumLiteral(n)) => Ok(NumberAtom(n)),
|
||||||
None => Err(format!("Unexpected end of input")),
|
None => Err(format!("Unexpected end of input")),
|
||||||
@ -188,14 +179,16 @@ fn parse(tokens: &mut Peekable<IntoIter<Token>>) -> Result<Sexp, String> {
|
|||||||
fn parse_sexp(tokens: &mut Peekable<IntoIter<Token>>) -> Result<Sexp, String> {
|
fn parse_sexp(tokens: &mut Peekable<IntoIter<Token>>) -> Result<Sexp, String> {
|
||||||
use self::Token::*;
|
use self::Token::*;
|
||||||
use self::Sexp::*;
|
use self::Sexp::*;
|
||||||
let mut vec = Vec::new();
|
let mut cell = Nil;
|
||||||
loop {
|
loop {
|
||||||
match tokens.peek() {
|
match tokens.peek() {
|
||||||
None => return Err(format!("Unexpected end of input")),
|
None => return Err(format!("Unexpected end of input")),
|
||||||
Some(&RParen) => { tokens.next(); break},
|
Some(&RParen) => { tokens.next(); break},
|
||||||
_ => vec.push(parse(tokens)?),
|
_ => {
|
||||||
|
cell = Cons(Box::new(parse(tokens)?), Box::new(cell));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(List(vec))
|
}
|
||||||
|
Ok(cell)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user