use std::{
io::{
self,
Write,
},
env,
fs,
process::exit,
collections::HashMap,
};
use colored::*;
use elemental::{
interpret,
error::*,
Expression
};
const VERSION: &str = "0.7.0";
fn main() {
if env::args().len() < 2 {
interpreter();
}
let input_file: String = match env::args().nth(1) {
Some(f) => f.to_owned(),
None => unreachable!(), };
let code: Vec<String> = match fs::read_to_string(input_file.to_owned()) {
Ok(c) => c.split("\n").map(|x| x.to_string()).collect::<Vec<String>>(),
Err(_) => {
throw(CouldNotReadFile (input_file));
exit(0);
},
};
let mut variables = HashMap::from([
("pi".to_string(), Expression::Float (3.141592653)),
("deg".to_string(), Expression::Float (3.141592653/180.0)),
]);
for mut command in code {
command.push('\n');
let (expression, is_silent) = interpret(&mut variables, command.to_owned());
if !is_silent {
let output = format!(
"{}",
expression,
);
println!("\n{}\n=\n\n{}\n", command, output);
}
}
}
fn interpreter() -> ! {
println!("{}\nVersion {}", "The Elemental Interpreter".truecolor(255, 140, 0).bold(), VERSION);
let mut input = String::new();
let stdin = io::stdin();
let mut stdout = io::stdout();
let mut variables = HashMap::from([
("pi".to_string(), Expression::Float (3.141592653)),
("deg".to_string(), Expression::Float (3.141592653/180.0)),
]);
loop {
print!(">>> ");
match stdout.flush() {
Ok(_) => (),
Err(_) => throw(CouldNotFlushOutput),
};
match stdin.read_line(&mut input) {
Ok(_) => (),
Err(_) => throw(CouldNotReadStdin),
};
let (expression, is_silent) = interpret(&mut variables, input.to_owned());
if !is_silent {
let output = format!(
"{}",
expression,
);
println!("\n{}\n", output);
}
input.clear();
}
}