Use a moudler out and input 😆

This commit is contained in:
Anas Elgarhy 2022-10-12 13:50:22 +02:00
parent 6dd7888aaf
commit d58b3e07d6
4 changed files with 110 additions and 76 deletions

View File

@ -2,6 +2,6 @@
<project version="4">
<component name="DiscordProjectSettings">
<option name="show" value="PROJECT_FILES" />
<option name="description" value="Fixing loop bug 🥲" />
<option name="description" value="Writing more tests yooo :D" />
</component>
</project>

View File

@ -3,18 +3,22 @@ use crate::bf_interpreter::error::{InterpreterError, InterpreterErrorKind};
use std::io::{Read, Write};
use std::{char, usize, vec};
pub struct Interpreter {
pub struct Interpreter<'a> {
pub cells: Vec<u8>,
pub pointer: usize,
pub array_size: usize,
pub bf_commands: Vec<BfCommand>,
brackets: Vec<BfCommand>,
pub input: &'a Box<dyn Read>,
pub output: &'a Box<dyn Write>,
pub features: Vec<arguments::Feature>,
}
impl Interpreter {
impl<'a> Interpreter<'a> {
pub fn new(
array_size: usize,
input: &'a mut Box<dyn Read>,
output: &'a mut Box<dyn Write>,
features: Vec<arguments::Feature>,
) -> Self {
Self {
@ -23,6 +27,8 @@ impl Interpreter {
array_size,
bf_commands: vec![],
brackets: Vec::new(),
input,
output,
features,
}
}
@ -262,7 +268,6 @@ mod tests {
}
#[test]
fn execute_hello_world_from_file() {
let mut interpreter = Interpreter::new(

View File

@ -3,10 +3,12 @@ mod repl;
mod utils;
mod bf_interpreter;
use std::io::{Read, Write};
use clap::Parser;
extern crate pretty_env_logger;
#[macro_use]
extern crate log;
use colored::Colorize;
use arguments::Args;
use bf_interpreter::interpreter::Interpreter;
@ -18,9 +20,13 @@ fn main() {
let args = Args::parse();
info!("Parsed command line arguments: {:?}", args);
info!("Initializing bf_interpreter");
let mut stdin: Box<dyn Read> = Box::new(std::io::stdin());
let mut stdout: Box<dyn Write> = Box::new(std::io::stdout());
info!("Initializing interpreter");
let mut interpreter = Interpreter::new(
args.array_size,
&mut stdin,
&mut stdout,
args.features.unwrap_or_else(|| vec![]),
);
@ -31,10 +37,10 @@ fn main() {
Ok(exit_code) => {
info!("Finished running brainfuck source code from file: {}", source);
if !args.without_tiles {
println!(
println!("{}", format!(
"Successfully ran brainfuck source code from file: {}",
source
);
).bold().green());
println!("Exiting with code: {exit_code}");
std::process::exit(0);
}
@ -45,6 +51,6 @@ fn main() {
}
}
}
None => repl::start(interpreter),
None => repl::start(&mut interpreter, &mut std::io::stdin(), &mut std::io::stdout()),
}
}

View File

@ -1,8 +1,9 @@
use crate::bf_interpreter::interpreter::Interpreter;
use std::io::Write;
use std::io::{Write, Read};
use colored::Colorize;
struct Repl {
interpreter: Interpreter,
struct Repl<'a> {
interpreter: &'a mut Interpreter<'a>,
history: Vec<String>,
}
@ -10,53 +11,53 @@ const PROMPT: &str = "bf-interpreter> ";
const HISTORY_FILE: &str = "bf-interpreter-history.bfr";
const COMMAND_PREFIX: &str = "!";
impl Repl {
pub fn new(interpreter: Interpreter) -> Self {
Self {
impl Repl<'_> {
pub fn new<'a>(interpreter: &'a mut Interpreter<'a>) -> Repl<'a> {
Repl {
interpreter,
history: Vec::new(),
}
}
pub fn run(mut self) {
pub fn run(mut self,
input: &mut impl Read,
output: &mut impl Write) -> Result<(), std::io::Error> {
let mut code_bat = String::new();
let mut is_loop = false;
loop {
if is_loop {
print!("... ");
} else {
print!("{}", PROMPT);
}
output.write_all(
if is_loop {
format!("{}", "... ".yellow())
} else {
format!("{}", PROMPT)
}.as_bytes()
)?;
std::io::stdout().flush().unwrap_or_else(|_| {
error!("Failed to flush stdout");
std::process::exit(1);
});
let mut input = String::new();
let mut user_input = String::new();
match std::io::stdin().read_line(&mut input) {
match input.read_to_string(&mut user_input) {
Ok(_) => {}
Err(e) => {
error!("Failed to read input: {}", e);
std::process::exit(1);
}
}
input = input.trim().to_string(); // Remove trailing newline
user_input = user_input.trim().to_string(); // Remove trailing newline
self.history.push(input.clone()); // Save input to history
self.history.push(user_input.clone()); // Save input to history
if input.contains('[') && (!input.contains(']') && !is_loop) {
let loop_start_index = input.find('[').unwrap();
if user_input.contains('[') && (!user_input.contains(']') && !is_loop) {
let loop_start_index = user_input.find('[').unwrap();
code_bat.push_str(&input[loop_start_index..]);
code_bat.push_str(&user_input[loop_start_index..]);
is_loop = true;
input = input[..loop_start_index].to_string();
user_input = user_input[..loop_start_index].to_string();
}
if input.starts_with(COMMAND_PREFIX) {
self.run_repl_cmd(input);
if user_input.starts_with(COMMAND_PREFIX) {
self.run_repl_cmd(user_input, output)?;
} else {
match self.interpreter.run(input) {
match self.interpreter.run(user_input) {
Ok(_) => {
info!("Successfully ran brainfuck source code from REPL");
}
@ -68,44 +69,47 @@ impl Repl {
}
}
fn run_repl_cmd(&mut self, input: String) {
let mut cmd = input.split_whitespace();
fn run_repl_cmd(&mut self, user_input: String, output: &mut impl Write) -> Result<(), std::io::Error> {
let mut cmd = user_input.split_whitespace();
match cmd.next() {
Some(repl_cmd) => {
match repl_cmd.get(COMMAND_PREFIX.len()..).unwrap_or("") {
"fuck" => {
println!("Bye bye :D");
output.write_all("Bye bye :D\n".green().as_bytes())?;
std::process::exit(0);
}
"array" | "a" => {
println!("Current array: {:?}", self.interpreter.cells);
output.write_all(format!("Current array: {:?}\n", self.interpreter.cells).as_bytes())?;
}
"array_size" | "as" => {
println!("Current array size: {}", self.interpreter.array_size);
output.write_all(format!("Current array size: {}\n",
self.interpreter.array_size.to_string().bold().green()).as_bytes())?;
}
"pointer" | "p" => {
println!("Current pointer: {}", self.interpreter.pointer);
output.write_all(format!("Current pointer: {}\n",
self.interpreter.pointer.to_string().bold().green()).as_bytes())?;
}
"pointer_value" | "pv" => {
println!(
"Current pointer value: {} = \'{}\' (char)",
format!(
"Current pointer value: {} = \'{}\' (char)\n",
self.interpreter.cells[self.interpreter.pointer],
self.interpreter.cells[self.interpreter.pointer] as char
);
}
"history" | "h" => {
println!("History:");
output.write_all("History:\n".underline().green().as_bytes())?;
for (i, cmd) in self.history.iter().enumerate() {
println!("{}: {}", i, cmd);
output.write_all(format!("{}: {}", i, cmd).as_bytes())?;
}
}
"save" | "s" => {
let file_name = cmd.next().unwrap_or(HISTORY_FILE);
println!("Saving history to file: {file_name}");
output.write_all(format!("Saving history to file: {file_name}").yellow().as_bytes())?;
match std::fs::write(file_name, self.history.join("\n")) {
Ok(_) => {
info!("Successfully saved history to file: {file_name}");
output.write_all(format!("Successfully saved history to file: {file_name}")
.green().as_bytes())?;
}
Err(e) => {
error!("Failed to save history to file: {}", e);
@ -115,10 +119,11 @@ impl Repl {
"load" | "l" => {
let file_name = cmd.next().unwrap_or(HISTORY_FILE);
println!("Loading history from file: {file_name}");
output.write_all(format!("Loading history from file: {file_name}\n").yellow().as_bytes())?;
match std::fs::read_to_string(file_name) {
Ok(history) => {
info!("Successfully loaded history from file: {file_name}");
output.write_all(format!("Successfully loaded history from file: {file_name}")
.green().as_bytes())?;
self.history = history.split("\n").map(|s| s.to_string()).collect();
// Run all commands in history
@ -144,52 +149,72 @@ impl Repl {
}
}
"reset" | "r" => {
println!("Resetting REPL");
output.write_all("Resetting REPL\n".truecolor(56, 33, 102).as_bytes())?;
self.interpreter.reset();
self.history = Vec::new();
}
"help" => {
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!("!pointer_value, !pv: print the current pointer value");
println!("!history, !h: print the history of the commands");
println!("!save, !s: save the history to a file");
println!("!load, !l: load the history from a file");
println!("!reset, !r: reset the REPL");
println!("!help: show this fu*king help message");
println!("!fuck: exit the REPL mode");
output.write_all("!array, !a: print the current array\n\
!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\
!fuck: exit the REPL\n".as_bytes())?;
}
_ => println!("Unknown command: {}, type !help to show the help", input),
_ => output.write_all(format!("Unknown command: {}, type {} to show the help",
user_input, (COMMAND_PREFIX.to_string() + "help").green()
).red().as_bytes())?,
}
}
None => {}
}
Ok(())
}
}
/// Run the REPL
/// # Arguments
/// * `interpreter` - The interpreter to use
pub fn start(interpreter: Interpreter) {
pub fn start<'a>(interpreter: &'a mut Interpreter<'a>,
input: &mut impl Read,
output: &mut impl Write) {
info!("Entering REPL mode");
println!("Welcome to the brainfuck REPL mode! :)");
println!(
"Brainfuck bf_interpreter v {}\nBy {}",
clap::crate_version!(),
clap::crate_authors!()
);
println!("Enter your brainfuck code and press enter to run it.");
println!("Enter !fuck to exit :D");
println!("Enter !help to get more fu*king help");
output.write_all(
format!(
"{}\n\
Brainfuck interpreter v {}\nBy {}\n\
{}\n\
Type {} to exit :D\n\
type {} to get more fu*king help\n",
"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(),
).as_bytes()).unwrap_or_else(|e| error!("Failed to write to output: {}", e));
Repl::new(interpreter).run();
match Repl::new(interpreter).run(input, output) {
Ok(_) => {
info!("Successfully ran REPL");
}
Err(e) => {
error!("Failed to run REPL: {}", e);
std::process::exit(1);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
/*#[test]
fn nested_loop_level_1() {
let mut interpreter = Interpreter::new(
30000,
@ -202,7 +227,5 @@ mod tests {
assert_eq!(interpreter.run(String::from("[>+<-]")), Ok(0));
assert_eq!(interpreter.run(String::from("<-]")), Ok(0));
assert_eq!(interpreter.cells[2], 4);
println!();
}
}*/
}