From 42b9507af009670a155c424c75c7c6b7fb335f4a Mon Sep 17 00:00:00 2001 From: greg Date: Wed, 29 Nov 2017 01:45:29 -0800 Subject: [PATCH] Parses ( --- src/rukka_lang/mod.rs | 50 +++++++++++++------------------------------ 1 file changed, 15 insertions(+), 35 deletions(-) diff --git a/src/rukka_lang/mod.rs b/src/rukka_lang/mod.rs index 55a3ec2..baec613 100644 --- a/src/rukka_lang/mod.rs +++ b/src/rukka_lang/mod.rs @@ -1,5 +1,7 @@ use itertools::Itertools; use schala_lib::{ProgrammingLanguageInterface, EvalOptions, ReplOutput}; +use std::iter::Peekable; +use std::str::Chars; pub struct Rukka { } @@ -18,7 +20,10 @@ impl ProgrammingLanguageInterface for Rukka { fn evaluate_in_repl(&mut self, input: &str, _eval_options: &EvalOptions) -> ReplOutput { let mut output = ReplOutput::default(); - output.add_output(parse(input).and_then(|x| eval(x)).unwrap()); + match parse(input).and_then(|x| eval(x)) { + Ok(s) => output.add_output(s), + Err(e) => output.add_output(format!("Error: {}", e)) + }; output } } @@ -27,41 +32,16 @@ fn eval(ast: Sexp) -> Result { Ok(format!("Everything is ()")) } -enum Token { - LParen, - RParen, - Symbol(String) -} - -fn tokenize(input: &str) -> Vec { - let mut iter = input.chars().peekable(); - let mut tokens = Vec::new(); - loop { - match iter.next() { - None => break, - Some('(') => tokens.push(LParen), - Some(')') => tokens.push(RParen), - Some(c) if c.is_whitespace() => continue, - Some(c) => { - let mut sym = String::new(); - loop { - match iter.peek() { - None | Some('(') | Some(')') => break, - Some(c) if c.is_whitespace() => break, - Some(c) => sym.push_char(c), - } - iter.next(); - } - tokens.push(Symbol(sym)); - } - } - } - tokens -} - fn parse(input: &str) -> Result { - let _tokens = tokenize(input); - Ok(Sexp::Atom(AtomT::Number(1))) + let mut iter: Peekable = input.chars().peekable(); + read_sexp(iter) +} + +fn read_sexp(mut input: Peekable) -> Result { + if input.next() != Some('(') { + return Err(format!("Expected '('")); + } + Ok(Sexp::Atom(AtomT::Number(4))) } #[derive(Debug)]