2022-10-07 18:17:21 +00:00
|
|
|
mod arguments;
|
|
|
|
mod interpreter;
|
2022-10-07 19:54:49 +00:00
|
|
|
mod utils;
|
2022-10-07 18:17:21 +00:00
|
|
|
|
2022-10-07 21:40:56 +00:00
|
|
|
use std::io::Write;
|
2022-10-07 18:17:21 +00:00
|
|
|
use clap::Parser;
|
|
|
|
extern crate pretty_env_logger;
|
|
|
|
#[macro_use] extern crate log;
|
|
|
|
|
|
|
|
use arguments::Args;
|
2022-10-07 21:40:56 +00:00
|
|
|
use crate::utils::read_brainfuck_code_if_any;
|
|
|
|
|
2022-10-07 18:17:21 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
pretty_env_logger::init();
|
|
|
|
info!("Initialized logger");
|
|
|
|
info!("Parsing command line arguments");
|
|
|
|
let args = Args::parse();
|
|
|
|
info!("Parsed command line arguments: {:?}", args);
|
|
|
|
|
|
|
|
if args.verbose {
|
|
|
|
info!("Verbose mode enabled");
|
|
|
|
}
|
|
|
|
|
2022-10-07 19:54:49 +00:00
|
|
|
info!("Initializing interpreter");
|
|
|
|
let mut interpreter = interpreter::Interpreter::new(
|
|
|
|
args.array_size,
|
2022-10-07 21:40:56 +00:00
|
|
|
read_brainfuck_code_if_any(&args.source),
|
2022-10-07 19:54:49 +00:00
|
|
|
args.features.unwrap_or_else(|| vec![]));
|
|
|
|
|
2022-10-07 18:17:21 +00:00
|
|
|
match args.source {
|
|
|
|
Some(source) => {
|
|
|
|
info!("Running brainfuck source code from file: {}", source);
|
2022-10-07 21:40:56 +00:00
|
|
|
interpreter.run(None).unwrap();
|
2022-10-07 18:17:21 +00:00
|
|
|
},
|
|
|
|
None => {
|
|
|
|
info!("Entering REPL mode");
|
2022-10-07 19:54:49 +00:00
|
|
|
println!("Welcome to the brainfuck REPL mode! :)");
|
2022-10-07 21:40:56 +00:00
|
|
|
println!("Brainfuck interpreter v {}\nBy {}",
|
|
|
|
clap::crate_version!(), clap::crate_authors!());
|
2022-10-07 19:54:49 +00:00
|
|
|
println!("Enter your brainfuck code and press enter to run it.");
|
|
|
|
println!("Enter !fuck to exit :D");
|
|
|
|
println!("Enter !help fuck to get more help");
|
|
|
|
|
|
|
|
loop {
|
2022-10-07 21:40:56 +00:00
|
|
|
print!("> ");
|
|
|
|
std::io::stdout().flush().unwrap();
|
2022-10-07 19:54:49 +00:00
|
|
|
let mut input = String::new();
|
|
|
|
std::io::stdin().read_line(&mut input).unwrap();
|
|
|
|
if input.starts_with("!") {
|
2022-10-07 21:40:56 +00:00
|
|
|
match input.trim().get(1..).unwrap() {
|
|
|
|
"fuck" => {
|
2022-10-07 19:54:49 +00:00
|
|
|
println!("Bye bye :D");
|
|
|
|
break;
|
|
|
|
},
|
2022-10-07 21:40:56 +00:00
|
|
|
"array" | "a" => {
|
|
|
|
println!("Current array: {:#?}", interpreter.cells);
|
|
|
|
},
|
|
|
|
"array_size" | "as" => {
|
|
|
|
println!("Current array size: {}", interpreter.array_size);
|
|
|
|
},
|
|
|
|
"pointer" | "p" => {
|
|
|
|
println!("Current pointer: {}", interpreter.pointer);
|
|
|
|
},
|
|
|
|
|
|
|
|
"help" => {
|
2022-10-07 19:54:49 +00:00
|
|
|
println!("!fuck: exit the REPL mode");
|
|
|
|
println!("!array, !a: print the current array");
|
|
|
|
println!("!array_size, !as: print the current array size");
|
|
|
|
println!("!pointer, !p: print the current pointer value");
|
|
|
|
println!("!history: print the history of the commands");
|
|
|
|
println!("!help: show this fu*king help message");
|
|
|
|
},
|
|
|
|
_ => println!("Unknown command: {}, type !help to show the help", input.trim())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
interpreter.run(Some(input));
|
|
|
|
}
|
|
|
|
}
|
2022-10-07 18:17:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|