2022-10-10 20:55:50 +00:00
|
|
|
pub fn read_brainfuck_code(source: &String) -> String {
|
|
|
|
info!("Reading brainfuck source code from file: {}", source);
|
|
|
|
match std::fs::read_to_string(source) {
|
2022-10-12 17:29:53 +00:00
|
|
|
Ok(source) => clean(source).unwrap_or_else(|| {
|
|
|
|
error!("The source code is empty");
|
|
|
|
std::process::exit(2);
|
|
|
|
}),
|
2022-10-10 20:55:50 +00:00
|
|
|
Err(e) => {
|
|
|
|
error!("Failed to read source code file: {}", e);
|
|
|
|
eprintln!("Failed to read source code file: {}", e);
|
|
|
|
std::process::exit(1);
|
2022-10-07 19:54:49 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-07 23:06:35 +00:00
|
|
|
}
|
2022-10-08 20:11:13 +00:00
|
|
|
|
2022-10-12 17:29:53 +00:00
|
|
|
fn clean(source: String) -> Option<String> {
|
|
|
|
if source.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let code: String = source
|
2022-10-08 20:11:13 +00:00
|
|
|
.chars()
|
|
|
|
.filter(|c| match c {
|
|
|
|
'+' | '-' | '<' | '>' | '[' | ']' | '.' | ',' => true,
|
|
|
|
_ => false,
|
|
|
|
})
|
2022-10-12 17:29:53 +00:00
|
|
|
.collect();
|
|
|
|
if code.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(code)
|
2022-10-08 20:11:13 +00:00
|
|
|
}
|