Split main() into subfunctions

This commit is contained in:
greg 2016-03-03 22:18:16 -08:00
parent 3fe9ec95d5
commit cd69ebaa9d

View File

@ -21,6 +21,13 @@ mod eval;
fn main() { fn main() {
let args: Vec<String> = std::env::args().collect(); let args: Vec<String> = std::env::args().collect();
if let Some(filename) = args.get(1) { if let Some(filename) = args.get(1) {
run_noninteractive(filename);
} else {
run_repl();
}
}
fn run_noninteractive(filename: &String) {
let mut source_file = File::open(&Path::new(filename)).unwrap(); let mut source_file = File::open(&Path::new(filename)).unwrap();
let mut buffer = String::new(); let mut buffer = String::new();
source_file.read_to_string(&mut buffer).unwrap(); source_file.read_to_string(&mut buffer).unwrap();
@ -40,8 +47,9 @@ fn main() {
for result in results.iter() { for result in results.iter() {
println!("{}", result); println!("{}", result);
} }
}
} else { fn run_repl() {
println!("Schala v 0.02"); println!("Schala v 0.02");
let initial_state = InterpreterState { let initial_state = InterpreterState {
show_tokens: false, show_tokens: false,
@ -50,7 +58,6 @@ fn main() {
}; };
REPL::with_prompt_and_state(Box::new(repl_handler), ">> ", initial_state) REPL::with_prompt_and_state(Box::new(repl_handler), ">> ", initial_state)
.run(); .run();
}
} }
struct InterpreterState { struct InterpreterState {