From 9ec983dc203f4cc0b5dce4bfb5922eff21fa1566 Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 30 Dec 2017 23:19:42 -0800 Subject: [PATCH] unified BoolAtom --- src/rukka_lang/mod.rs | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/src/rukka_lang/mod.rs b/src/rukka_lang/mod.rs index ad4b7a1..499fb24 100644 --- a/src/rukka_lang/mod.rs +++ b/src/rukka_lang/mod.rs @@ -104,8 +104,7 @@ impl EvaluatorState { expr @ FnLiteral { .. } => expr, expr @ StringAtom(_) => expr, expr @ NumberAtom(_) => expr, - True => True, - False => False, + expr @ BoolAtom(_) => expr, Cons(box operator, box operands) => match operator { SymbolAtom(ref sym) if match &sym[..] { "quote" | "eq?" | "cons" | "car" | "cdr" | "atom?" | "define" | "lambda" | "if" | "cond" => true, _ => false @@ -125,14 +124,9 @@ impl EvaluatorState { Cons(box quoted, box Nil) => quoted, _ => return Err(format!("Bad syntax in quote")), }, - "eq?" => match operands { - Cons(box lhs, box Cons(box rhs, _)) => { - match lhs == rhs { - true => True, - false => False, - } - }, - _ => True, + "eq?" => match operands {//TODO make correct + Cons(box lhs, box Cons(box rhs, _)) => BoolAtom(lhs == rhs), + _ => BoolAtom(true), }, "cons" => match operands { 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")), }, "atom?" => match operands { - Cons(_, _) => False, - _ => True, + Cons(_, _) => BoolAtom(false), + _ => BoolAtom(true), }, "define" => match operands { Cons(box SymbolAtom(sym), box Cons(box expr, box Nil)) => { @@ -270,8 +264,7 @@ enum Sexp { SymbolAtom(String), StringAtom(String), NumberAtom(u64), - True, - False, + BoolAtom(bool), Cons(Box, Box), Nil, FnLiteral { @@ -323,8 +316,8 @@ impl Sexp { fn print(&self) -> String { use self::Sexp::*; match self { - &True => format!("#t"), - &False => format!("#f"), + &BoolAtom(true) => format!("#t"), + &BoolAtom(false) => format!("#f"), &SymbolAtom(ref sym) => format!("{}", sym), &StringAtom(ref s) => format!("\"{}\"", s), &NumberAtom(ref n) => format!("{}", n), @@ -338,7 +331,7 @@ impl Sexp { fn truthy(&self) -> bool { use self::Sexp::*; match self { - &False => false, + &BoolAtom(false) => false, _ => true } } @@ -390,8 +383,8 @@ fn parse(tokens: &mut Peekable>) -> Result { use self::Token::*; use self::Sexp::*; match tokens.next() { - Some(Word(ref s)) if s == "#f" => Ok(False), - Some(Word(ref s)) if s == "#t" => Ok(True), + Some(Word(ref s)) if s == "#f" => Ok(BoolAtom(false)), + Some(Word(ref s)) if s == "#t" => Ok(BoolAtom(true)), Some(Word(s)) => Ok(SymbolAtom(s)), Some(StringLiteral(s)) => Ok(StringAtom(s)), Some(LParen) => parse_sexp(tokens),