Format the code 🦀
This commit is contained in:
parent
0d66ddde35
commit
6c3f121111
5 changed files with 45 additions and 45 deletions
|
@ -21,4 +21,4 @@ pub enum Feature {
|
|||
/// If the pointer at the end of the array, set the pointer to 0, otherwise increment the pointer.
|
||||
/// If the pointer at the beginning of the array, set the pointer to the end of the array, otherwise decrement the pointer.
|
||||
ReversePointer,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use std::io::{Read, Write};
|
||||
use std::usize;
|
||||
use crate::arguments;
|
||||
use crate::interpreter::error::InterpreterError;
|
||||
use std::io::{Read, Write};
|
||||
use std::usize;
|
||||
|
||||
pub struct Interpreter {
|
||||
pub cells: Vec<u8>,
|
||||
|
@ -13,9 +13,11 @@ pub struct Interpreter {
|
|||
}
|
||||
|
||||
impl Interpreter {
|
||||
pub fn new(array_size: usize,
|
||||
bf_code: Option<String>,
|
||||
features: Vec<arguments::Feature>) -> Self {
|
||||
pub fn new(
|
||||
array_size: usize,
|
||||
bf_code: Option<String>,
|
||||
features: Vec<arguments::Feature>,
|
||||
) -> Self {
|
||||
Self {
|
||||
cells: vec![0; array_size],
|
||||
pointer: 0,
|
||||
|
@ -32,12 +34,12 @@ impl Interpreter {
|
|||
self.bf_code.push_str(&*bf_code);
|
||||
bf_code
|
||||
}
|
||||
None => self.bf_code.clone()
|
||||
None => self.bf_code.clone(),
|
||||
};
|
||||
|
||||
match self.run_brainfuck_code(&bf_code) {
|
||||
Ok(_) => Ok(0),
|
||||
Err(e) => Err(e)
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -49,7 +51,6 @@ impl Interpreter {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
fn run_brainfuck_code(&mut self, bf_code: &str) -> Result<(), error::InterpreterError> {
|
||||
for (i, ch) in bf_code.chars().enumerate() {
|
||||
match BfCommand::from_char(ch, i) {
|
||||
|
@ -93,17 +94,17 @@ impl Interpreter {
|
|||
} else {
|
||||
self.pointer -= 1;
|
||||
}
|
||||
},
|
||||
}
|
||||
BfCommand::IncVal => {
|
||||
self.cells[self.pointer] = self.cells[self.pointer].wrapping_add(1);
|
||||
},
|
||||
}
|
||||
BfCommand::DecVal => {
|
||||
self.cells[self.pointer] = self.cells[self.pointer].wrapping_sub(1);
|
||||
},
|
||||
}
|
||||
BfCommand::Print => {
|
||||
print!("{}", self.cells[self.pointer] as char);
|
||||
std::io::stdout().flush().unwrap();
|
||||
},
|
||||
}
|
||||
BfCommand::Read => {
|
||||
self.cells[self.pointer] = match std::io::stdin().bytes().next() {
|
||||
Some(Ok(byte)) => byte,
|
||||
|
@ -120,10 +121,10 @@ impl Interpreter {
|
|||
));
|
||||
}
|
||||
};
|
||||
},
|
||||
}
|
||||
BfCommand::LoopStart(i) => {
|
||||
self.brackets.push(BfCommand::LoopStart(i));
|
||||
},
|
||||
}
|
||||
BfCommand::LoopEnd(i) => {
|
||||
let open_bracket = self.brackets.pop();
|
||||
match open_bracket {
|
||||
|
@ -132,7 +133,7 @@ impl Interpreter {
|
|||
let code = self.bf_code[j..i].to_string();
|
||||
self.iterate(code)?;
|
||||
}
|
||||
},
|
||||
}
|
||||
_ => {
|
||||
return Err(error::InterpreterError::new(
|
||||
format!("Unmatched closing bracket at position {}", i),
|
||||
|
@ -152,7 +153,6 @@ impl Interpreter {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
enum BfCommand {
|
||||
IncPtr,
|
||||
|
|
14
src/main.rs
14
src/main.rs
|
@ -1,7 +1,7 @@
|
|||
mod arguments;
|
||||
mod interpreter;
|
||||
mod utils;
|
||||
mod repl;
|
||||
mod utils;
|
||||
|
||||
use clap::Parser;
|
||||
|
||||
|
@ -22,7 +22,7 @@ fn main() {
|
|||
let mut interpreter = interpreter::Interpreter::new(
|
||||
args.array_size,
|
||||
utils::read_brainfuck_code_if_any(&args.source),
|
||||
args.features.unwrap_or_else(|| vec![])
|
||||
args.features.unwrap_or_else(|| vec![]),
|
||||
);
|
||||
|
||||
match args.source {
|
||||
|
@ -30,7 +30,10 @@ fn main() {
|
|||
info!("Running brainfuck source code from file: {}", source);
|
||||
match interpreter.run(None) {
|
||||
Ok(exit_code) => {
|
||||
println!("Successfully ran brainfuck source code from file: {}", source);
|
||||
println!(
|
||||
"Successfully ran brainfuck source code from file: {}",
|
||||
source
|
||||
);
|
||||
println!("Exiting with code: {exit_code}");
|
||||
std::process::exit(0);
|
||||
}
|
||||
|
@ -40,9 +43,6 @@ fn main() {
|
|||
}
|
||||
}
|
||||
}
|
||||
None => {
|
||||
repl::start(interpreter)
|
||||
}
|
||||
None => repl::start(interpreter),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
36
src/repl.rs
36
src/repl.rs
|
@ -1,5 +1,5 @@
|
|||
use std::io::Write;
|
||||
use crate::interpreter::Interpreter;
|
||||
use std::io::Write;
|
||||
|
||||
struct Repl {
|
||||
interpreter: Interpreter,
|
||||
|
@ -71,8 +71,7 @@ impl Repl {
|
|||
}
|
||||
}
|
||||
"save" | "s" => {
|
||||
let file_name = cmd.next()
|
||||
.unwrap_or("brainfuck_repl_history.bfr");
|
||||
let file_name = cmd.next().unwrap_or("brainfuck_repl_history.bfr");
|
||||
|
||||
println!("Saving history to file: {file_name}");
|
||||
match std::fs::write(file_name, self.history.join("\n")) {
|
||||
|
@ -85,25 +84,27 @@ impl Repl {
|
|||
}
|
||||
}
|
||||
"load" | "l" => {
|
||||
let file_name = cmd.next()
|
||||
.unwrap_or("brainfuck_repl_history.bfr");
|
||||
let file_name = cmd.next().unwrap_or("brainfuck_repl_history.bfr");
|
||||
|
||||
println!("Loading history from file: {file_name}");
|
||||
match std::fs::read_to_string(file_name) {
|
||||
Ok(history) => {
|
||||
info!("Successfully loaded history from file: {file_name}");
|
||||
self.history = history.split("\n")
|
||||
.map(|s| s.to_string())
|
||||
.collect();
|
||||
self.history = history.split("\n").map(|s| s.to_string()).collect();
|
||||
|
||||
// Run all commands in history
|
||||
for cmd in self.history.iter() {
|
||||
match self.interpreter.run(Some(cmd.clone())) {
|
||||
Ok(_) => {
|
||||
info!("Successfully ran brainfuck source code from REPL");
|
||||
info!(
|
||||
"Successfully ran brainfuck source code from REPL"
|
||||
);
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to run brainfuck source code from REPL: {}", e);
|
||||
error!(
|
||||
"Failed to run brainfuck source code from REPL: {}",
|
||||
e
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -128,10 +129,9 @@ impl Repl {
|
|||
println!("!help: show this fu*king help message");
|
||||
println!("!fuck: exit the REPL mode");
|
||||
}
|
||||
_ => println!("Unknown command: {}, type !help to show the help",
|
||||
input)
|
||||
_ => println!("Unknown command: {}, type !help to show the help", input),
|
||||
}
|
||||
},
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
|
@ -140,12 +140,14 @@ impl Repl {
|
|||
pub fn start(interpreter: Interpreter) {
|
||||
info!("Entering REPL mode");
|
||||
println!("Welcome to the brainfuck REPL mode! :)");
|
||||
println!("Brainfuck interpreter v {}\nBy {}",
|
||||
clap::crate_version!(), clap::crate_authors!());
|
||||
println!(
|
||||
"Brainfuck 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 fuck to get more help");
|
||||
|
||||
Repl::new(interpreter)
|
||||
.run();
|
||||
Repl::new(interpreter).run();
|
||||
}
|
||||
|
|
|
@ -10,9 +10,7 @@ pub(crate) fn read_brainfuck_code_if_any(source: &Option<String>) -> Option<Stri
|
|||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
},
|
||||
None => {
|
||||
None
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue