From 1e5159f84fa66af3b0bbc0252deef0142d834c23 Mon Sep 17 00:00:00 2001 From: PaddiM8 Date: Thu, 4 Jun 2020 18:55:08 +0200 Subject: [PATCH] Separated some functionality in main.rs into output.rs and repl.rs. --- lek_cli/src/main.rs | 53 ++++++++----------------------------------- lek_cli/src/output.rs | 16 +++++++++++++ lek_cli/src/repl.rs | 32 ++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 44 deletions(-) create mode 100644 lek_cli/src/output.rs create mode 100644 lek_cli/src/repl.rs diff --git a/lek_cli/src/main.rs b/lek_cli/src/main.rs index a99272d..a331ed7 100644 --- a/lek_cli/src/main.rs +++ b/lek_cli/src/main.rs @@ -1,56 +1,21 @@ -use ansi_term::Colour::{Cyan, Red}; -use lek::parser::{self, Unit}; -use rustyline::error::ReadlineError; -use rustyline::Editor; -use std::{env, process}; +mod output; +mod repl; + +use lek::parser::Unit; +use lek::parser::{self}; +use std::env; fn main() { - let mut parser = parser::Context::new(); + let mut parser = parser::Context::new().set_angle_unit(get_angle_unit()); // Command line argument input, execute it and exit. if let Some(expr) = env::args().skip(1).next() { - eval(&mut parser, &expr); + output::eval(&mut parser, &expr); return; } // REPL - let mut rl = Editor::<()>::new(); - - loop { - let readline = rl.readline(&Cyan.paint(">> ").to_string()); - - match readline { - Ok(input) => { - rl.add_history_entry(input.as_str()); - eval_repl(&mut parser, &input); - } - Err(ReadlineError::Interrupted) => break, - _ => break, - } - } -} - -fn eval_repl(parser: &mut parser::Context, input: &str) { - match input { - "" => eprint!(""), - "clear" => print!("\x1B[2J"), - "exit" => process::exit(0), - _ => eval(parser, input), - } -} - -fn eval(parser: &mut parser::Context, input: &str) { - match parser::parse(parser, input, get_angle_unit(), 53) { - Ok(Some(result)) => { - if result.clone().fract() == 0 { - println!("{}", result.to_integer().unwrap()); - } else { - println!("{}", result); - } - } - Ok(None) => print!(""), - Err(err) => println!("{}", Red.paint(err)), - } + repl::start(&mut parser); } fn get_angle_unit() -> Unit { diff --git a/lek_cli/src/output.rs b/lek_cli/src/output.rs new file mode 100644 index 0000000..7f13eda --- /dev/null +++ b/lek_cli/src/output.rs @@ -0,0 +1,16 @@ +use ansi_term::Colour::Red; +use lek::parser::{self}; + +pub fn eval(parser: &mut parser::Context, input: &str) { + match parser::parse(parser, input, 53) { + Ok(Some(result)) => { + if result.clone().fract() == 0 { + println!("{}", result.to_integer().unwrap()); + } else { + println!("{}", result); + } + } + Ok(None) => print!(""), + Err(err) => println!("{}", Red.paint(err)), + } +} diff --git a/lek_cli/src/repl.rs b/lek_cli/src/repl.rs new file mode 100644 index 0000000..c822cec --- /dev/null +++ b/lek_cli/src/repl.rs @@ -0,0 +1,32 @@ +use crate::output; +use ansi_term::Colour::Cyan; +use lek::parser; +use rustyline::error::ReadlineError; +use rustyline::Editor; +use std::process; + +pub fn start(mut parser: &mut parser::Context) { + let mut rl = Editor::<()>::new(); + + loop { + let readline = rl.readline(&Cyan.paint(">> ").to_string()); + + match readline { + Ok(input) => { + rl.add_history_entry(input.as_str()); + eval_repl(&mut parser, &input); + } + Err(ReadlineError::Interrupted) => break, + _ => break, + } + } +} + +fn eval_repl(parser: &mut parser::Context, input: &str) { + match input { + "" => eprint!(""), + "clear" => print!("\x1B[2J"), + "exit" => process::exit(0), + _ => output::eval(parser, input), + } +}