From 3aaf2e9e6d42f998e91a47e19198917744ccc256 Mon Sep 17 00:00:00 2001 From: Anas Elgarhy Date: Wed, 12 Oct 2022 19:29:53 +0200 Subject: [PATCH] =?UTF-8?q?Improve=20=F0=9F=92=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 4 ++-- README.md | 27 ++++++++++++++++----------- src/utils.rs | 18 ++++++++++++++---- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fed4edd..39a5697 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,11 +34,11 @@ exclude = [ # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -clap = { version = "4.0.10", features = ["derive", "color", "cargo"] } +clap = { version = "4.0.14", features = ["derive", "color", "cargo"] } log = "0.4.17" pretty_env_logger = "0.4.0" colored = "2.0.0" -no-panic = "0.1.16" +# no-panic = "0.1.16" [dev-dependencies] pretty_assertions = "1.3.0" \ No newline at end of file diff --git a/README.md b/README.md index 9c331c7..c0589dd 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,26 @@ -![brainfuc*k interpreter](./assets/cover.png) +

+ +brainfuc*k interpreter + +

+ # brainfuc*k interpreter: a simple brainfuc*k interpreter and REPL writen in rust 🦀 ## Install - from crates.io - ```bash - crago install bf-bf_interpreter + ```shell + cargo install bf-interpreter ``` - From aur: - ```shell + ```shell yay -S bf-interpreter ``` ## Options and arguments -```bash -bf-bf_interpreter --help +```shell +bf-interpreter --help ``` ```text Brainfu*k interpreter and REPL written in Rust @@ -52,7 +57,7 @@ Options: ### Examples ```bash -bf-bf_interpreter test_code/hello_world.bf +bf-interpreter test_code/hello_world.bf ``` ```text Hello world! @@ -61,14 +66,14 @@ Exiting with code: 0 ``` ```bash -bf-bf_interpreter -w test_code/hello_world.bf +bf-interpreter -w test_code/hello_world.bf ``` ```text Hello world! ``` ```bash -bf-bf_interpreter test_code/print_hi_yooo.bf +bf-interpreter test_code/print_hi_yooo.bf ``` ```text Hi yoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo!Successfully ran brainfuck source code from file: test_code/print_hi_yooo.bf @@ -76,13 +81,13 @@ Exiting with code: 0 ``` ```bash -bf-bf_interpreter -w test_code/print_hi_yooo.bf +bf-interpreter -w test_code/print_hi_yooo.bf ``` ```text Hi yoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo! ``` ```bash -bf-bf_interpreter test_code/like_cat.bf +bf-interpreter test_code/like_cat.bf ``` ![output](./screenshots/like_cat_output.png) diff --git a/src/utils.rs b/src/utils.rs index 1b73ede..d1b521a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,7 +1,10 @@ pub fn read_brainfuck_code(source: &String) -> String { info!("Reading brainfuck source code from file: {}", source); match std::fs::read_to_string(source) { - Ok(source) => clean(source), + Ok(source) => clean(source).unwrap_or_else(|| { + error!("The source code is empty"); + std::process::exit(2); + }), Err(e) => { error!("Failed to read source code file: {}", e); eprintln!("Failed to read source code file: {}", e); @@ -10,12 +13,19 @@ pub fn read_brainfuck_code(source: &String) -> String { } } -fn clean(source: String) -> String { - source +fn clean(source: String) -> Option { + if source.is_empty() { + return None; + } + let code: String = source .chars() .filter(|c| match c { '+' | '-' | '<' | '>' | '[' | ']' | '.' | ',' => true, _ => false, }) - .collect() + .collect(); + if code.is_empty() { + return None; + } + Some(code) }