From ecdcb7ff3df2abbbe5e7281205d7cdb7d8e6c603 Mon Sep 17 00:00:00 2001 From: greg Date: Sun, 3 Dec 2017 19:21:56 -0800 Subject: [PATCH] Numbers --- src/rukka_lang/mod.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/rukka_lang/mod.rs b/src/rukka_lang/mod.rs index 4fcf87b..6659e3c 100644 --- a/src/rukka_lang/mod.rs +++ b/src/rukka_lang/mod.rs @@ -46,6 +46,7 @@ fn eval(expr: Sexp) -> Result { Atom(atom) => match atom { Symbol(s) => Atom(Symbol(s)), LangString(s) => Atom(LangString(s)), + Number(s) => Atom(Number(s)), }, List(items) => unimplemented!(), }) @@ -68,6 +69,7 @@ enum Token { Quote, Word(String), StringLiteral(String), + NumLiteral(u64), } #[derive(Debug)] @@ -83,6 +85,7 @@ impl Sexp { &Atom(ref atom) => match atom { &Symbol(ref s) => format!("{}", s), &LangString(ref s) => format!("\"{}\"", s), + &Number(ref n) => format!("{}", n), }, _ => format!("") } @@ -93,7 +96,7 @@ impl Sexp { enum AtomT { Symbol(String), LangString(String), - //Number(u64), + Number(u64), } fn tokenize(input: &mut Peekable) -> Vec { @@ -106,6 +109,11 @@ fn tokenize(input: &mut Peekable) -> Vec { Some(')') => tokens.push(RParen), Some('\'') => tokens.push(Quote), Some(c) if c.is_whitespace() => continue, + Some(c) if c.is_numeric() => { + let tok: String = input.peeking_take_while(|next| next.is_numeric()).collect(); + let n: u64 = format!("{}{}", c, tok).parse().unwrap(); + tokens.push(NumLiteral(n)); + }, Some('"') => { let string: String = input.scan(false, |escape, cur_char| { let seen_escape = *escape; @@ -147,6 +155,7 @@ fn parse(tokens: &mut Peekable>) -> Result { let quoted = parse(tokens)?; Ok(Sexp::List(vec![Sexp::Atom(AtomT::Symbol(format!("quote"))), quoted])) }, + Some(NumLiteral(n)) => Ok(Sexp::Atom(AtomT::Number(n))), None => Err(format!("Unexpected end of input")), } }