mirror of
https://github.com/PaddiM8/kalker.git
synced 2025-06-23 19:21:26 +02:00
Made it possible to load an input file (with variables and functions).
This commit is contained in:
parent
f8ef60b336
commit
0ab31c18cb
@ -1,21 +1,52 @@
|
|||||||
mod output;
|
mod output;
|
||||||
mod repl;
|
mod repl;
|
||||||
|
|
||||||
|
use kalk::parser;
|
||||||
use kalk::parser::Unit;
|
use kalk::parser::Unit;
|
||||||
use kalk::parser::{self};
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::Read;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut parser = parser::Context::new().set_angle_unit(get_angle_unit());
|
let mut parser_context = 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() {
|
let mut args = env::args().skip(1);
|
||||||
output::eval(&mut parser, &expr);
|
let mut expr_input: Option<String> = None;
|
||||||
return;
|
loop {
|
||||||
|
// Get the next argument if possible, otherwise break the loop.
|
||||||
|
let arg = if let Some(arg) = args.next() {
|
||||||
|
arg
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
|
||||||
|
if arg == "-i" {
|
||||||
|
let file_name = &args.next().expect("Expected input file."); // The next argument will be the file name.
|
||||||
|
let mut file_content = String::new();
|
||||||
|
File::open(&file_name)
|
||||||
|
.expect("Couldn't find file.")
|
||||||
|
.read_to_string(&mut file_content)
|
||||||
|
.expect("Failed to read input file.");
|
||||||
|
|
||||||
|
// Parse the input file content, resulting in the symbol table being filled out.
|
||||||
|
// Output is not needed here.
|
||||||
|
parser::parse(&mut parser_context, &file_content, 53)
|
||||||
|
.expect("Failed to parse input file.");
|
||||||
|
} else {
|
||||||
|
// Main argument. This is expected to be a maths expression.
|
||||||
|
// After the loop is finished, this will be parsed and outputted.
|
||||||
|
expr_input = Some(arg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(input) = expr_input {
|
||||||
|
// Direct output
|
||||||
|
output::eval(&mut parser_context, &input);
|
||||||
|
} else {
|
||||||
// REPL
|
// REPL
|
||||||
repl::start(&mut parser);
|
repl::start(&mut parser_context);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_angle_unit() -> Unit {
|
fn get_angle_unit() -> Unit {
|
||||||
|
@ -39,6 +39,10 @@ pub fn eval(parser: &mut parser::Context, input: &str) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn print_err(msg: &str) {
|
||||||
|
println!("{}", Red.paint(msg));
|
||||||
|
}
|
||||||
|
|
||||||
fn print_calc_err(err: CalcError) {
|
fn print_calc_err(err: CalcError) {
|
||||||
print_err(&match err {
|
print_err(&match err {
|
||||||
IncorrectAmountOfArguments(expected, func, got) => format!(
|
IncorrectAmountOfArguments(expected, func, got) => format!(
|
||||||
@ -54,7 +58,3 @@ fn print_calc_err(err: CalcError) {
|
|||||||
Unknown => format!("Unknown error."),
|
Unknown => format!("Unknown error."),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_err(msg: &str) {
|
|
||||||
println!("{}", Red.paint(msg));
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user