2022-10-13 15:40:05 +00:00
|
|
|
use super::repl::Repl;
|
2022-10-08 10:01:28 +00:00
|
|
|
use crate::bf_interpreter::interpreter::Interpreter;
|
2022-10-13 15:40:05 +00:00
|
|
|
use crate::repl::repl::{COMMAND_PREFIX, HISTORY_FILE, PROMPT};
|
2022-10-12 11:50:22 +00:00
|
|
|
use colored::Colorize;
|
2022-10-13 15:40:05 +00:00
|
|
|
use console::Key;
|
2022-10-12 16:59:12 +00:00
|
|
|
use std::io::Write;
|
2022-10-08 20:11:13 +00:00
|
|
|
|
2022-10-12 16:31:07 +00:00
|
|
|
impl Repl {
|
2022-10-13 15:11:50 +00:00
|
|
|
pub fn new(interpreter: Interpreter) -> Repl {
|
2022-10-12 11:50:22 +00:00
|
|
|
Repl {
|
2022-10-13 15:11:50 +00:00
|
|
|
term: interpreter.term.clone(),
|
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 21:21:21 +00:00
|
|
|
self.print_prompt();
|
2022-10-12 16:31:07 +00:00
|
|
|
|
|
|
|
std::io::stdout().flush()?;
|
2022-10-10 20:55:50 +00:00
|
|
|
|
2022-10-12 21:21:21 +00:00
|
|
|
match self.read_input() {
|
|
|
|
Ok(input) => {
|
|
|
|
let user_input = input.trim().to_string(); // Remove trailing newline
|
2022-10-07 22:28:14 +00:00
|
|
|
|
2022-10-12 21:21:21 +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-07 22:28:14 +00:00
|
|
|
Err(e) => {
|
2022-10-12 21:21:21 +00:00
|
|
|
eprintln!("Error: {}", e);
|
2022-10-07 22:28:14 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-12 21:21:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_prompt(&self) {
|
|
|
|
print!(
|
|
|
|
"{}",
|
|
|
|
if self.loop_depth != 0 {
|
2022-10-13 16:56:15 +00:00
|
|
|
"............... ".yellow()
|
2022-10-12 21:21:21 +00:00
|
|
|
} else {
|
|
|
|
PROMPT.to_string().truecolor(54, 76, 76)
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_input(&mut self) -> Result<String, std::io::Error> {
|
|
|
|
let mut input = String::new();
|
|
|
|
let mut rev_index = 0;
|
2022-10-07 22:28:14 +00:00
|
|
|
|
2022-10-12 21:21:21 +00:00
|
|
|
loop {
|
|
|
|
let key = self.term.read_key()?; // Read key from terminal
|
|
|
|
|
|
|
|
match key {
|
|
|
|
Key::ArrowUp => {
|
|
|
|
if !self.history.is_empty() && rev_index < self.history.len() {
|
2022-10-13 15:40:05 +00:00
|
|
|
let last = self
|
|
|
|
.history
|
|
|
|
.get(self.history.len() - 1 - rev_index)
|
2022-10-12 21:21:21 +00:00
|
|
|
.unwrap();
|
|
|
|
rev_index += 1;
|
|
|
|
self.term.clear_line()?;
|
|
|
|
self.print_prompt();
|
|
|
|
self.term.write_str(last)?;
|
|
|
|
input = last.clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Key::ArrowDown => {
|
|
|
|
if !self.history.is_empty() && rev_index > 0 {
|
2022-10-13 15:40:05 +00:00
|
|
|
let first = self.history.get(self.history.len() - rev_index).unwrap();
|
2022-10-12 21:21:21 +00:00
|
|
|
rev_index -= 1;
|
|
|
|
self.term.clear_line()?;
|
|
|
|
self.print_prompt();
|
|
|
|
self.term.write_str(first)?;
|
|
|
|
input = first.clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Key::Char(c) => {
|
|
|
|
self.term.write_str(&c.to_string())?;
|
|
|
|
input.push(c);
|
|
|
|
}
|
2022-10-13 16:56:15 +00:00
|
|
|
Key::Backspace if !input.is_empty() => {
|
2022-10-12 21:21:21 +00:00
|
|
|
self.term.clear_line()?;
|
2022-10-13 16:56:15 +00:00
|
|
|
self.print_prompt();
|
2022-10-12 21:21:21 +00:00
|
|
|
self.term.write_str(&input[0..input.len() - 1])?;
|
|
|
|
input.pop();
|
|
|
|
}
|
|
|
|
Key::Enter => {
|
|
|
|
self.term.write_str("\n")?;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_ => {}
|
2022-10-10 20:55:50 +00:00
|
|
|
}
|
2022-10-12 16:31:07 +00:00
|
|
|
}
|
2022-10-12 21:21:21 +00:00
|
|
|
Ok(input)
|
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) {
|
2022-10-13 17:58:02 +00:00
|
|
|
let mut print = false;
|
2022-10-13 12:23:46 +00:00
|
|
|
user_input.chars().for_each(|ch| {
|
|
|
|
if ch == '[' {
|
|
|
|
self.loop_depth += 1;
|
|
|
|
} else if ch == ']' {
|
|
|
|
self.loop_depth -= 1;
|
2022-10-13 17:58:02 +00:00
|
|
|
} else if ch == '.' {
|
|
|
|
print = true;
|
2022-10-13 12:23:46 +00:00
|
|
|
}
|
|
|
|
});
|
2022-10-12 16:31:07 +00:00
|
|
|
match user_input.find('[') {
|
2022-10-13 12:23:46 +00:00
|
|
|
Some(index) if self.loop_depth != 0 && self.loop_body.is_empty() => {
|
2022-10-12 16:31:07 +00:00
|
|
|
self.loop_body.push_str(&user_input[index..]);
|
|
|
|
user_input = user_input[..index].to_string();
|
2022-10-12 16:53:53 +00:00
|
|
|
}
|
2022-10-13 12:23:46 +00:00
|
|
|
Some(_) if !self.loop_body.is_empty() => {
|
2022-10-12 16:31:07 +00:00
|
|
|
self.loop_body.push_str(&user_input);
|
|
|
|
return;
|
2022-10-12 16:53:53 +00:00
|
|
|
}
|
2022-10-12 16:31:07 +00:00
|
|
|
_ => {
|
|
|
|
if user_input.contains(']') {
|
|
|
|
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");
|
2022-10-13 17:58:02 +00:00
|
|
|
if print {
|
|
|
|
println!(); // Print newline after output, if user requested print any output
|
|
|
|
}
|
2022-10-12 16:31:07 +00:00
|
|
|
}
|
|
|
|
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:59:12 +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:59:12 +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],
|
2022-10-13 15:40:05 +00:00
|
|
|
self.interpreter.cells[self.interpreter.pointer]
|
|
|
|
.to_char()
|
|
|
|
.unwrap_or_else(|_| '?')
|
2022-10-08 10:01:28 +00:00
|
|
|
);
|
|
|
|
}
|
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:59:12 +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:59:12 +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:59:12 +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:59:12 +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:59:12 +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:59:12 +00:00
|
|
|
!fuck: exit the REPL"
|
|
|
|
);
|
2022-10-07 22:52:43 +00:00
|
|
|
}
|
2022-10-12 16:59:12 +00:00
|
|
|
_ => println!(
|
|
|
|
"{}",
|
|
|
|
format!(
|
|
|
|
"Unknown command: {}, type {} to show the help",
|
|
|
|
user_input,
|
|
|
|
(COMMAND_PREFIX.to_string() + "help").green()
|
|
|
|
)
|
2022-10-13 15:40:05 +00:00
|
|
|
.red()
|
2022-10-12 16:59:12 +00:00
|
|
|
),
|
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
|
|
|
}
|
2022-10-07 22:28:14 +00:00
|
|
|
}
|