mirror of
https://github.com/PaddiM8/kalker.git
synced 2025-01-07 14:09:02 +01:00
Separated some functionality in main.rs into output.rs and repl.rs.
This commit is contained in:
parent
36ae7a0b90
commit
1e5159f84f
@ -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 {
|
||||
|
16
lek_cli/src/output.rs
Normal file
16
lek_cli/src/output.rs
Normal file
@ -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)),
|
||||
}
|
||||
}
|
32
lek_cli/src/repl.rs
Normal file
32
lek_cli/src/repl.rs
Normal file
@ -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),
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user