Type simplification

This commit is contained in:
greg 2017-12-03 22:20:43 -08:00
parent bf42b58ca5
commit 559eaf54de

View File

@ -48,13 +48,11 @@ impl ProgrammingLanguageInterface for Rukka {
impl EvaluatorState { impl EvaluatorState {
fn new() -> EvaluatorState { EvaluatorState { } } fn new() -> EvaluatorState { EvaluatorState { } }
fn eval(&mut self, expr: Sexp) -> Result<Sexp, String> { fn eval(&mut self, expr: Sexp) -> Result<Sexp, String> {
use self::Sexp::*; use self::AtomT::*; use self::Sexp::*;
Ok(match expr { Ok(match expr {
Atom(atom) => match atom { SymbolAtom(sym) => unimplemented!(),
Symbol(s) => Atom(Symbol(s)), expr @ StringAtom(_) => expr,
LangString(s) => Atom(LangString(s)), expr @ NumberAtom(_) => expr,
Number(s) => Atom(Number(s)),
},
List(items) => unimplemented!(), List(items) => unimplemented!(),
}) })
} }
@ -82,31 +80,24 @@ enum Token {
#[derive(Debug)] #[derive(Debug)]
enum Sexp { enum Sexp {
Atom(AtomT), SymbolAtom(String),
StringAtom(String),
NumberAtom(u64),
List(Vec<Sexp>), List(Vec<Sexp>),
} }
impl Sexp { impl Sexp {
fn print(&self) -> String { fn print(&self) -> String {
use self::Sexp::*; use self::AtomT::*; use self::Sexp::*;
match self { match self {
&Atom(ref atom) => match atom { &SymbolAtom(ref sym) => format!("{}", sym),
&Symbol(ref s) => format!("{}", s), &StringAtom(ref s) => format!("\"{}\"", s),
&LangString(ref s) => format!("\"{}\"", s), &NumberAtom(ref n) => format!("{}", n),
&Number(ref n) => format!("{}", n), &List(ref sexprs) => format!("<unprintable>"),
},
_ => format!("<unprintable>")
} }
} }
} }
#[derive(Debug)]
enum AtomT {
Symbol(String),
LangString(String),
Number(u64),
}
fn tokenize(input: &mut Peekable<Chars>) -> Vec<Token> { fn tokenize(input: &mut Peekable<Chars>) -> Vec<Token> {
use self::Token::*; use self::Token::*;
let mut tokens = Vec::new(); let mut tokens = Vec::new();
@ -154,16 +145,17 @@ fn tokenize(input: &mut Peekable<Chars>) -> Vec<Token> {
fn parse(tokens: &mut Peekable<IntoIter<Token>>) -> Result<Sexp, String> { fn parse(tokens: &mut Peekable<IntoIter<Token>>) -> Result<Sexp, String> {
use self::Token::*; use self::Token::*;
use self::Sexp::*;
match tokens.next() { match tokens.next() {
Some(Word(s)) => Ok(Sexp::Atom(AtomT::Symbol(s))), Some(Word(s)) => Ok(SymbolAtom(s)),
Some(StringLiteral(s)) => Ok(Sexp::Atom(AtomT::LangString(s))), Some(StringLiteral(s)) => Ok(StringAtom(s)),
Some(LParen) => parse_sexp(tokens), Some(LParen) => parse_sexp(tokens),
Some(RParen) => Err(format!("Unexpected ')'")), Some(RParen) => Err(format!("Unexpected ')'")),
Some(Quote) => { Some(Quote) => {
let quoted = parse(tokens)?; 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")), None => Err(format!("Unexpected end of input")),
} }
} }