2022-10-07 21:40:56 +00:00
|
|
|
pub(crate) fn read_brainfuck_code_if_any(source: &Option<String>) -> Option<String> {
|
2022-10-07 19:54:49 +00:00
|
|
|
match source {
|
|
|
|
Some(source) => {
|
|
|
|
info!("Reading brainfuck source code from file: {}", source);
|
|
|
|
match std::fs::read_to_string(source) {
|
2022-10-08 20:11:13 +00:00
|
|
|
Ok(source) => Some(clean(source)),
|
2022-10-07 19:54:49 +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 23:06:35 +00:00
|
|
|
None => None,
|
2022-10-07 19:54:49 +00:00
|
|
|
}
|
2022-10-07 23:06:35 +00:00
|
|
|
}
|
2022-10-08 20:11:13 +00:00
|
|
|
|
|
|
|
fn clean(source: String) -> String {
|
|
|
|
source
|
|
|
|
.chars()
|
|
|
|
.filter(|c| match c {
|
|
|
|
'+' | '-' | '<' | '>' | '[' | ']' | '.' | ',' => true,
|
|
|
|
_ => false,
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|