From 2f8ef99b08cd9835e0f94de3f5671875123dc313 Mon Sep 17 00:00:00 2001 From: greg Date: Sun, 3 Dec 2017 22:20:43 -0800 Subject: [PATCH] Type simplification --- src/rukka_lang/mod.rs | 42 +++++++++++++++++------------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/src/rukka_lang/mod.rs b/src/rukka_lang/mod.rs index 2ed6d62..3688c16 100644 --- a/src/rukka_lang/mod.rs +++ b/src/rukka_lang/mod.rs @@ -48,13 +48,11 @@ impl ProgrammingLanguageInterface for Rukka { impl EvaluatorState { fn new() -> EvaluatorState { EvaluatorState { } } fn eval(&mut self, expr: Sexp) -> Result { - use self::Sexp::*; use self::AtomT::*; + use self::Sexp::*; Ok(match expr { - Atom(atom) => match atom { - Symbol(s) => Atom(Symbol(s)), - LangString(s) => Atom(LangString(s)), - Number(s) => Atom(Number(s)), - }, + SymbolAtom(sym) => unimplemented!(), + expr @ StringAtom(_) => expr, + expr @ NumberAtom(_) => expr, List(items) => unimplemented!(), }) } @@ -82,31 +80,24 @@ enum Token { #[derive(Debug)] enum Sexp { - Atom(AtomT), + SymbolAtom(String), + StringAtom(String), + NumberAtom(u64), List(Vec), } impl Sexp { fn print(&self) -> String { - use self::Sexp::*; use self::AtomT::*; + use self::Sexp::*; match self { - &Atom(ref atom) => match atom { - &Symbol(ref s) => format!("{}", s), - &LangString(ref s) => format!("\"{}\"", s), - &Number(ref n) => format!("{}", n), - }, - _ => format!("") + &SymbolAtom(ref sym) => format!("{}", sym), + &StringAtom(ref s) => format!("\"{}\"", s), + &NumberAtom(ref n) => format!("{}", n), + &List(ref sexprs) => format!(""), } } } -#[derive(Debug)] -enum AtomT { - Symbol(String), - LangString(String), - Number(u64), -} - fn tokenize(input: &mut Peekable) -> Vec { use self::Token::*; let mut tokens = Vec::new(); @@ -154,16 +145,17 @@ fn tokenize(input: &mut Peekable) -> Vec { fn parse(tokens: &mut Peekable>) -> Result { use self::Token::*; + use self::Sexp::*; match tokens.next() { - Some(Word(s)) => Ok(Sexp::Atom(AtomT::Symbol(s))), - Some(StringLiteral(s)) => Ok(Sexp::Atom(AtomT::LangString(s))), + Some(Word(s)) => Ok(SymbolAtom(s)), + Some(StringLiteral(s)) => Ok(StringAtom(s)), Some(LParen) => parse_sexp(tokens), Some(RParen) => Err(format!("Unexpected ')'")), Some(Quote) => { let quoted = parse(tokens)?; - Ok(Sexp::List(vec![Sexp::Atom(AtomT::Symbol(format!("quote"))), quoted])) + Ok(List(vec![SymbolAtom(format!("quote")), quoted])) }, - Some(NumLiteral(n)) => Ok(Sexp::Atom(AtomT::Number(n))), + Some(NumLiteral(n)) => Ok(NumberAtom(n)), None => Err(format!("Unexpected end of input")), } }