mirror of
https://github.com/PaddiM8/kalker.git
synced 2025-06-23 03:01:22 +02: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};
|
mod output;
|
||||||
use lek::parser::{self, Unit};
|
mod repl;
|
||||||
use rustyline::error::ReadlineError;
|
|
||||||
use rustyline::Editor;
|
use lek::parser::Unit;
|
||||||
use std::{env, process};
|
use lek::parser::{self};
|
||||||
|
use std::env;
|
||||||
|
|
||||||
fn main() {
|
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.
|
// Command line argument input, execute it and exit.
|
||||||
if let Some(expr) = env::args().skip(1).next() {
|
if let Some(expr) = env::args().skip(1).next() {
|
||||||
eval(&mut parser, &expr);
|
output::eval(&mut parser, &expr);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// REPL
|
// REPL
|
||||||
let mut rl = Editor::<()>::new();
|
repl::start(&mut parser);
|
||||||
|
|
||||||
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)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_angle_unit() -> Unit {
|
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…
x
Reference in New Issue
Block a user