unified BoolAtom

This commit is contained in:
greg 2017-12-30 23:19:42 -08:00
parent 540ffde4bc
commit 559306ffc8
1 changed files with 12 additions and 19 deletions

View File

@ -104,8 +104,7 @@ impl EvaluatorState {
expr @ FnLiteral { .. } => expr, expr @ FnLiteral { .. } => expr,
expr @ StringAtom(_) => expr, expr @ StringAtom(_) => expr,
expr @ NumberAtom(_) => expr, expr @ NumberAtom(_) => expr,
True => True, expr @ BoolAtom(_) => expr,
False => False,
Cons(box operator, box operands) => match operator { Cons(box operator, box operands) => match operator {
SymbolAtom(ref sym) if match &sym[..] { SymbolAtom(ref sym) if match &sym[..] {
"quote" | "eq?" | "cons" | "car" | "cdr" | "atom?" | "define" | "lambda" | "if" | "cond" => true, _ => false "quote" | "eq?" | "cons" | "car" | "cdr" | "atom?" | "define" | "lambda" | "if" | "cond" => true, _ => false
@ -125,14 +124,9 @@ impl EvaluatorState {
Cons(box quoted, box Nil) => quoted, Cons(box quoted, box Nil) => quoted,
_ => return Err(format!("Bad syntax in quote")), _ => return Err(format!("Bad syntax in quote")),
}, },
"eq?" => match operands { "eq?" => match operands {//TODO make correct
Cons(box lhs, box Cons(box rhs, _)) => { Cons(box lhs, box Cons(box rhs, _)) => BoolAtom(lhs == rhs),
match lhs == rhs { _ => BoolAtom(true),
true => True,
false => False,
}
},
_ => True,
}, },
"cons" => match operands { "cons" => match operands {
Cons(box cadr, box Cons(box caddr, box Nil)) => { Cons(box cadr, box Cons(box caddr, box Nil)) => {
@ -151,8 +145,8 @@ impl EvaluatorState {
_ => return Err(format!("called cdr with a non-pair argument")), _ => return Err(format!("called cdr with a non-pair argument")),
}, },
"atom?" => match operands { "atom?" => match operands {
Cons(_, _) => False, Cons(_, _) => BoolAtom(false),
_ => True, _ => BoolAtom(true),
}, },
"define" => match operands { "define" => match operands {
Cons(box SymbolAtom(sym), box Cons(box expr, box Nil)) => { Cons(box SymbolAtom(sym), box Cons(box expr, box Nil)) => {
@ -270,8 +264,7 @@ enum Sexp {
SymbolAtom(String), SymbolAtom(String),
StringAtom(String), StringAtom(String),
NumberAtom(u64), NumberAtom(u64),
True, BoolAtom(bool),
False,
Cons(Box<Sexp>, Box<Sexp>), Cons(Box<Sexp>, Box<Sexp>),
Nil, Nil,
FnLiteral { FnLiteral {
@ -323,8 +316,8 @@ impl Sexp {
fn print(&self) -> String { fn print(&self) -> String {
use self::Sexp::*; use self::Sexp::*;
match self { match self {
&True => format!("#t"), &BoolAtom(true) => format!("#t"),
&False => format!("#f"), &BoolAtom(false) => format!("#f"),
&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),
@ -338,7 +331,7 @@ impl Sexp {
fn truthy(&self) -> bool { fn truthy(&self) -> bool {
use self::Sexp::*; use self::Sexp::*;
match self { match self {
&False => false, &BoolAtom(false) => false,
_ => true _ => true
} }
} }
@ -390,8 +383,8 @@ fn parse(tokens: &mut Peekable<IntoIter<Token>>) -> Result<Sexp, String> {
use self::Token::*; use self::Token::*;
use self::Sexp::*; use self::Sexp::*;
match tokens.next() { match tokens.next() {
Some(Word(ref s)) if s == "#f" => Ok(False), Some(Word(ref s)) if s == "#f" => Ok(BoolAtom(false)),
Some(Word(ref s)) if s == "#t" => Ok(True), Some(Word(ref s)) if s == "#t" => Ok(BoolAtom(true)),
Some(Word(s)) => Ok(SymbolAtom(s)), Some(Word(s)) => Ok(SymbolAtom(s)),
Some(StringLiteral(s)) => Ok(StringAtom(s)), Some(StringLiteral(s)) => Ok(StringAtom(s)),
Some(LParen) => parse_sexp(tokens), Some(LParen) => parse_sexp(tokens),