2022-10-08 10:01:28 +00:00
|
|
|
use crate::bf_interpreter::interpreter::Interpreter;
|
2022-10-12 16:31:07 +00:00
|
|
|
use std::io::{Write, Read, BufRead, BufReader, Stdout, Stdin};
|
2022-10-12 11:50:22 +00:00
|
|
|
use colored::Colorize;
|
2022-10-12 16:31:07 +00:00
|
|
|
use no_panic::no_panic;
|
2022-10-07 22:28:14 +00:00
|
|
|
|
2022-10-12 16:31:07 +00:00
|
|
|
struct Repl {
|
|
|
|
interpreter: Interpreter,
|
2022-10-07 22:28:14 +00:00
|
|
|
history: Vec<String>,
|
2022-10-12 16:31:07 +00:00
|
|
|
loop_body: String,
|
|
|
|
loop_depth: usize,
|
2022-10-07 22:28:14 +00:00
|
|
|
}
|
|
|
|
|
2022-10-08 20:11:13 +00:00
|
|
|
const PROMPT: &str = "bf-interpreter> ";
|
|
|
|
const HISTORY_FILE: &str = "bf-interpreter-history.bfr";
|
|
|
|
const COMMAND_PREFIX: &str = "!";
|
|
|
|
|
2022-10-12 16:31:07 +00:00
|
|
|
impl Repl {
|
|
|
|
pub fn new(interpreter: Interpreter) -> Repl {
|
2022-10-12 11:50:22 +00:00
|
|
|
Repl {
|
2022-10-07 22:28:14 +00:00
|
|
|
interpreter,
|
|
|
|
history: Vec::new(),
|
2022-10-12 16:31:07 +00:00
|
|
|
loop_body: String::new(),
|
|
|
|
loop_depth: 0,
|
2022-10-07 22:28:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-12 16:31:07 +00:00
|
|
|
// #[no_panic]
|
|
|
|
pub fn run(mut self) -> Result<(), std::io::Error> {
|
2022-10-07 22:28:14 +00:00
|
|
|
loop {
|
2022-10-12 16:31:07 +00:00
|
|
|
print!("{}",
|
|
|
|
if self.loop_depth != 0 {
|
|
|
|
"........ ".yellow()
|
|
|
|
} else {
|
|
|
|
PROMPT.to_string().truecolor(54, 76, 76)
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
std::io::stdout().flush()?;
|
2022-10-10 20:55:50 +00:00
|
|
|
|
2022-10-12 11:50:22 +00:00
|
|
|
let mut user_input = String::new();
|
2022-10-07 22:28:14 +00:00
|
|
|
|
2022-10-12 16:31:07 +00:00
|
|
|
match std::io::stdin().read_line(&mut user_input) {
|
2022-10-07 22:52:43 +00:00
|
|
|
Ok(_) => {}
|
2022-10-07 22:28:14 +00:00
|
|
|
Err(e) => {
|
|
|
|
error!("Failed to read input: {}", e);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
2022-10-12 11:50:22 +00:00
|
|
|
user_input = user_input.trim().to_string(); // Remove trailing newline
|
2022-10-07 22:28:14 +00:00
|
|
|
|
2022-10-12 16:31:07 +00:00
|
|
|
if !user_input.is_empty() && user_input.len() > 0 {
|
|
|
|
self.history.push(user_input.clone()); // Save input to history
|
|
|
|
self.process(user_input); // Process the input
|
2022-10-10 20:55:50 +00:00
|
|
|
}
|
2022-10-12 16:31:07 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-10 20:55:50 +00:00
|
|
|
|
2022-10-12 16:31:07 +00:00
|
|
|
pub fn process(&mut self, mut user_input: String) {
|
|
|
|
match user_input.find('[') {
|
|
|
|
Some(index) if self.loop_depth == 0 => {
|
|
|
|
self.loop_body.push_str(&user_input[index..]);
|
|
|
|
self.loop_depth = 1;
|
|
|
|
user_input = user_input[..index].to_string();
|
|
|
|
},
|
|
|
|
Some(_) => {
|
|
|
|
self.loop_body.push_str(&user_input);
|
|
|
|
user_input.matches('[').for_each(|_| self.loop_depth += 1);
|
|
|
|
user_input.matches(']').for_each(|_| self.loop_depth -= 1);
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
if user_input.contains(']') {
|
|
|
|
if self.loop_depth == 0 {
|
|
|
|
error!("Found ']' without matching '['");
|
|
|
|
return;
|
2022-10-07 22:28:14 +00:00
|
|
|
}
|
2022-10-12 16:31:07 +00:00
|
|
|
self.loop_depth -= 1;
|
|
|
|
if self.loop_depth == 0 {
|
|
|
|
self.loop_body.push_str(&user_input);
|
|
|
|
user_input = self.loop_body.clone();
|
|
|
|
self.loop_body = String::new();
|
2022-10-07 22:28:14 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-12 16:31:07 +00:00
|
|
|
if self.loop_depth != 0 {
|
|
|
|
self.loop_body.push_str(&user_input);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if user_input.is_empty() || user_input.len() == 0 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if user_input.starts_with(COMMAND_PREFIX) {
|
|
|
|
self.run_repl_cmd(user_input);
|
|
|
|
} else {
|
|
|
|
match self.interpreter.run(user_input) {
|
|
|
|
Ok(_) => {
|
|
|
|
info!("Successfully ran brainfuck source code from REPL");
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!("Failed to run brainfuck source code from REPL: {}", e);
|
|
|
|
}
|
2022-10-07 22:28:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-12 16:31:07 +00:00
|
|
|
fn run_repl_cmd(&mut self, user_input: String) {
|
2022-10-12 11:50:22 +00:00
|
|
|
let mut cmd = user_input.split_whitespace();
|
2022-10-07 22:52:43 +00:00
|
|
|
match cmd.next() {
|
|
|
|
Some(repl_cmd) => {
|
2022-10-08 20:11:13 +00:00
|
|
|
match repl_cmd.get(COMMAND_PREFIX.len()..).unwrap_or("") {
|
2022-10-07 22:52:43 +00:00
|
|
|
"fuck" => {
|
2022-10-12 16:31:07 +00:00
|
|
|
println!("{}", "Bye bye :D".green());
|
2022-10-07 22:52:43 +00:00
|
|
|
std::process::exit(0);
|
|
|
|
}
|
|
|
|
"array" | "a" => {
|
2022-10-12 16:31:07 +00:00
|
|
|
println!("{}", format!("Current array: {:?}", self.interpreter.cells));
|
2022-10-07 22:52:43 +00:00
|
|
|
}
|
|
|
|
"array_size" | "as" => {
|
2022-10-12 16:31:07 +00:00
|
|
|
println!("{}", format!("Current array size: {}",
|
|
|
|
self.interpreter.cells.len()
|
|
|
|
.to_string().bold().green()));
|
2022-10-07 22:52:43 +00:00
|
|
|
}
|
|
|
|
"pointer" | "p" => {
|
2022-10-12 16:31:07 +00:00
|
|
|
println!("{}", format!("Current pointer: {}",
|
|
|
|
self.interpreter.pointer.to_string().bold().green()));
|
2022-10-07 22:28:14 +00:00
|
|
|
}
|
2022-10-08 10:01:28 +00:00
|
|
|
"pointer_value" | "pv" => {
|
2022-10-12 16:31:07 +00:00
|
|
|
println!(
|
|
|
|
"Current pointer value: {} = \'{}\' (char)",
|
2022-10-08 10:01:28 +00:00
|
|
|
self.interpreter.cells[self.interpreter.pointer],
|
|
|
|
self.interpreter.cells[self.interpreter.pointer] as char
|
|
|
|
);
|
|
|
|
}
|
2022-10-07 22:52:43 +00:00
|
|
|
"history" | "h" => {
|
2022-10-12 16:31:07 +00:00
|
|
|
println!("{}", "History:".underline().green());
|
2022-10-07 22:52:43 +00:00
|
|
|
for (i, cmd) in self.history.iter().enumerate() {
|
2022-10-12 16:31:07 +00:00
|
|
|
println!("{}", format!("{}: {}", i, cmd));
|
2022-10-07 22:52:43 +00:00
|
|
|
}
|
2022-10-07 22:28:14 +00:00
|
|
|
}
|
2022-10-07 22:52:43 +00:00
|
|
|
"save" | "s" => {
|
2022-10-08 20:11:13 +00:00
|
|
|
let file_name = cmd.next().unwrap_or(HISTORY_FILE);
|
2022-10-07 22:52:43 +00:00
|
|
|
|
2022-10-12 16:31:07 +00:00
|
|
|
println!("{}", format!("Saving history to file: {file_name}").yellow());
|
2022-10-07 22:52:43 +00:00
|
|
|
match std::fs::write(file_name, self.history.join("\n")) {
|
|
|
|
Ok(_) => {
|
2022-10-12 16:31:07 +00:00
|
|
|
println!("{}", format!("Successfully saved history to file: {file_name}")
|
|
|
|
.green());
|
2022-10-07 22:52:43 +00:00
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!("Failed to save history to file: {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"load" | "l" => {
|
2022-10-08 20:11:13 +00:00
|
|
|
let file_name = cmd.next().unwrap_or(HISTORY_FILE);
|
2022-10-07 22:52:43 +00:00
|
|
|
|
2022-10-12 16:31:07 +00:00
|
|
|
println!("{}", format!("Loading history from file: {file_name}").yellow());
|
2022-10-07 22:52:43 +00:00
|
|
|
match std::fs::read_to_string(file_name) {
|
|
|
|
Ok(history) => {
|
2022-10-12 16:31:07 +00:00
|
|
|
println!("{}", format!("Successfully loaded history from file: {file_name}")
|
|
|
|
.green());
|
2022-10-07 23:06:35 +00:00
|
|
|
self.history = history.split("\n").map(|s| s.to_string()).collect();
|
2022-10-07 22:52:43 +00:00
|
|
|
|
|
|
|
// Run all commands in history
|
|
|
|
for cmd in self.history.iter() {
|
2022-10-10 20:55:50 +00:00
|
|
|
match self.interpreter.run(cmd.clone()) {
|
2022-10-07 22:52:43 +00:00
|
|
|
Ok(_) => {
|
2022-10-07 23:06:35 +00:00
|
|
|
info!(
|
|
|
|
"Successfully ran brainfuck source code from REPL"
|
|
|
|
);
|
2022-10-07 22:52:43 +00:00
|
|
|
}
|
2022-10-07 23:05:21 +00:00
|
|
|
Err(e) => {
|
2022-10-07 23:06:35 +00:00
|
|
|
error!(
|
|
|
|
"Failed to run brainfuck source code from REPL: {}",
|
|
|
|
e
|
|
|
|
);
|
2022-10-07 22:52:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!("Failed to load history from file: {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"reset" | "r" => {
|
2022-10-12 16:31:07 +00:00
|
|
|
println!("{}", "Resetting REPL".truecolor(56, 33, 102));
|
2022-10-07 22:52:43 +00:00
|
|
|
self.interpreter.reset();
|
2022-10-12 11:50:22 +00:00
|
|
|
self.history = Vec::new();
|
2022-10-07 22:52:43 +00:00
|
|
|
}
|
|
|
|
"help" => {
|
2022-10-12 16:31:07 +00:00
|
|
|
println!("!array, !a: print the current array\n\
|
2022-10-12 11:50:22 +00:00
|
|
|
!array_size, !as: print the current array size\n\
|
|
|
|
!pointer, !p: print the current pointer\n\
|
|
|
|
!pointer_value, !pv: print the current pointer value\n\
|
|
|
|
!history, !h: print the REPL history\n\
|
|
|
|
!save, !s: save the REPL history to a file\n\
|
|
|
|
!load, !l: load the REPL history from a file\n\
|
|
|
|
!reset, !r: reset the REPL\n\
|
|
|
|
!help: print this help message\n\
|
2022-10-12 16:31:07 +00:00
|
|
|
!fuck: exit the REPL");
|
2022-10-07 22:52:43 +00:00
|
|
|
}
|
2022-10-12 16:31:07 +00:00
|
|
|
_ => println!("{}", format!("Unknown command: {}, type {} to show the help",
|
|
|
|
user_input, (COMMAND_PREFIX.to_string() + "help").green()
|
|
|
|
).red()),
|
2022-10-07 22:28:14 +00:00
|
|
|
}
|
2022-10-07 23:06:35 +00:00
|
|
|
}
|
2022-10-07 22:52:43 +00:00
|
|
|
None => {}
|
2022-10-07 22:28:14 +00:00
|
|
|
}
|
2022-10-12 16:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the interpreter
|
|
|
|
/// for testing purposes only!
|
|
|
|
pub fn interpreter(&self) -> &Interpreter {
|
|
|
|
&self.interpreter
|
2022-10-07 22:28:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-08 20:11:13 +00:00
|
|
|
/// Run the REPL
|
|
|
|
/// # Arguments
|
|
|
|
/// * `interpreter` - The interpreter to use
|
2022-10-12 16:31:07 +00:00
|
|
|
pub fn start(interpreter: Interpreter) {
|
2022-10-07 22:28:14 +00:00
|
|
|
info!("Entering REPL mode");
|
2022-10-12 16:31:07 +00:00
|
|
|
println!("{}\n\
|
2022-10-12 11:50:22 +00:00
|
|
|
Brainfuck interpreter v {}\nBy {}\n\
|
|
|
|
{}\n\
|
|
|
|
Type {} to exit :D\n\
|
2022-10-12 16:31:07 +00:00
|
|
|
type {} to get more fu*king help",
|
|
|
|
"Welcome to the brainfuck REPL mode! :)".green(),
|
|
|
|
clap::crate_version!().to_string().yellow(),
|
|
|
|
clap::crate_authors!().to_string().green(),
|
|
|
|
"Enter your brainfuck code and press enter to run it.".italic().blue(),
|
|
|
|
(COMMAND_PREFIX.to_string() + "fuck").bold().red(),
|
|
|
|
(COMMAND_PREFIX.to_string() + "help").bold().green(),
|
|
|
|
);
|
2022-10-12 11:50:22 +00:00
|
|
|
|
2022-10-12 16:31:07 +00:00
|
|
|
match Repl::new(interpreter).run() {
|
2022-10-12 11:50:22 +00:00
|
|
|
Ok(_) => {
|
|
|
|
info!("Successfully ran REPL");
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!("Failed to run REPL: {}", e);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
2022-10-07 22:28:14 +00:00
|
|
|
}
|
2022-10-10 20:55:50 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2022-10-12 16:31:07 +00:00
|
|
|
use pretty_assertions::assert_eq;
|
2022-10-10 20:55:50 +00:00
|
|
|
|
2022-10-12 16:31:07 +00:00
|
|
|
#[test]
|
2022-10-10 20:55:50 +00:00
|
|
|
fn nested_loop_level_1() {
|
2022-10-12 16:31:07 +00:00
|
|
|
let interpreter = Interpreter::new(
|
|
|
|
4,
|
|
|
|
vec![],
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut repl = Repl::new(interpreter);
|
|
|
|
|
|
|
|
repl.process("++".to_string());
|
|
|
|
repl.process("[>++".to_string());
|
|
|
|
repl.process("[>+<-]".to_string());
|
|
|
|
repl.process("<-]".to_string());
|
|
|
|
|
|
|
|
let cells = &repl.interpreter().cells;
|
|
|
|
|
|
|
|
assert_eq!(cells[0], 0);
|
|
|
|
assert_eq!(cells[1], 0);
|
|
|
|
assert_eq!(cells[2], 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn nested_loop_level_2() {
|
|
|
|
let interpreter = Interpreter::new(
|
|
|
|
4,
|
2022-10-10 20:55:50 +00:00
|
|
|
vec![],
|
|
|
|
);
|
|
|
|
|
2022-10-12 16:31:07 +00:00
|
|
|
let mut repl = Repl::new(interpreter);
|
|
|
|
|
|
|
|
repl.process("++".to_string());
|
|
|
|
repl.process("[>++".to_string());
|
|
|
|
repl.process("[>+<-]".to_string());
|
|
|
|
repl.process("[>++".to_string());
|
|
|
|
repl.process("[>+<-]".to_string());
|
|
|
|
repl.process("<-]".to_string());
|
|
|
|
repl.process("<-]".to_string());
|
|
|
|
|
|
|
|
let cells = &repl.interpreter().cells;
|
|
|
|
|
|
|
|
assert_eq!(cells[0], 0);
|
|
|
|
assert_eq!(cells[1], 0);
|
|
|
|
assert_eq!(cells[2], 4);
|
|
|
|
}
|
2022-10-10 20:55:50 +00:00
|
|
|
|
2022-10-12 16:31:07 +00:00
|
|
|
#[test]
|
|
|
|
fn print_my_first_name() {
|
|
|
|
let interpreter = Interpreter::new(
|
|
|
|
10,
|
|
|
|
vec![],
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut repl = Repl::new(interpreter);
|
|
|
|
|
|
|
|
let code = "++++ ++++ 8
|
|
|
|
[
|
|
|
|
>++++
|
|
|
|
[
|
|
|
|
>++ A
|
|
|
|
>+++ a
|
|
|
|
>++++
|
|
|
|
>+ space
|
|
|
|
<<<<-
|
|
|
|
]
|
|
|
|
|
|
|
|
>>>>>>++
|
|
|
|
[
|
|
|
|
<<<-
|
|
|
|
>>>-
|
|
|
|
]
|
|
|
|
|
|
|
|
<<<<<<<-
|
|
|
|
]
|
|
|
|
>>+. Print cell 2: A
|
|
|
|
<<++++
|
|
|
|
[
|
|
|
|
>+++
|
|
|
|
[
|
|
|
|
>+++
|
|
|
|
<-
|
|
|
|
]
|
|
|
|
>++
|
|
|
|
<<-
|
|
|
|
]
|
|
|
|
>>+. Print n
|
|
|
|
<<+++
|
|
|
|
[
|
|
|
|
>+++
|
|
|
|
[
|
|
|
|
>-
|
|
|
|
<-
|
|
|
|
]
|
|
|
|
>-
|
|
|
|
<<-
|
|
|
|
]
|
|
|
|
>>-. Print n
|
|
|
|
<<++++++
|
|
|
|
[
|
|
|
|
>>+++
|
|
|
|
<<-
|
|
|
|
]
|
|
|
|
>>. Print s".to_string().split("\n").map(|s| s.to_string()).collect::<Vec<String>>();
|
|
|
|
|
|
|
|
for line in code {
|
|
|
|
repl.process(line);
|
|
|
|
}
|
|
|
|
}
|
2022-10-10 20:55:50 +00:00
|
|
|
}
|