From 9c018d2fe2f7bca417b63cf2400eca1f18e01fc4 Mon Sep 17 00:00:00 2001 From: ishanjain28 Date: Tue, 5 Feb 2019 14:27:20 +0530 Subject: [PATCH] Added REPL --- src/main.rs | 5 ++++- src/repl/mod.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/repl/mod.rs diff --git a/src/main.rs b/src/main.rs index eb2fb5d..b0c3820 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,8 +2,11 @@ extern crate lazy_static; mod lexer; +mod repl; -fn main() {} +fn main() { + repl::init(); +} #[cfg(test)] mod tests { diff --git a/src/repl/mod.rs b/src/repl/mod.rs new file mode 100644 index 0000000..ae1b975 --- /dev/null +++ b/src/repl/mod.rs @@ -0,0 +1,27 @@ +use crate::lexer::Lexer; +use std::io::{self, BufRead, Write}; + +const PROMPT: &'static str = ">> "; + +pub fn init() { + let stdin = io::stdin(); + let read_handle = stdin.lock(); + let stdout = io::stdout(); + let write_handle = stdout.lock(); + + start(read_handle, write_handle); +} + +fn start(mut ip: R, mut out: W) { + loop { + out.write_fmt(format_args!("{}", PROMPT)).unwrap(); + out.flush().unwrap(); + let mut s = String::new(); + ip.read_line(&mut s).unwrap(); + let tokens = Lexer::new(&s); + + for token in tokens { + println!("{:?}", token); + } + } +}