Improve 💙

This commit is contained in:
Anas Elgarhy 2022-10-12 19:29:53 +02:00
parent c4f35dd63c
commit 3aaf2e9e6d
3 changed files with 32 additions and 17 deletions

View file

@ -34,11 +34,11 @@ exclude = [
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
clap = { version = "4.0.10", features = ["derive", "color", "cargo"] } clap = { version = "4.0.14", features = ["derive", "color", "cargo"] }
log = "0.4.17" log = "0.4.17"
pretty_env_logger = "0.4.0" pretty_env_logger = "0.4.0"
colored = "2.0.0" colored = "2.0.0"
no-panic = "0.1.16" # no-panic = "0.1.16"
[dev-dependencies] [dev-dependencies]
pretty_assertions = "1.3.0" pretty_assertions = "1.3.0"

View file

@ -1,21 +1,26 @@
![brainfuc*k interpreter](./assets/cover.png) <p align="center">
<img alt="brainfuc*k interpreter" src="./assets/cover.png" width="100%" />
</p>
# brainfuc*k interpreter: a simple brainfuc*k interpreter and REPL writen in rust 🦀 # brainfuc*k interpreter: a simple brainfuc*k interpreter and REPL writen in rust 🦀
## Install ## Install
- from crates.io - from crates.io
```bash ```shell
crago install bf-bf_interpreter cargo install bf-interpreter
``` ```
- From aur: - From aur:
```shell ```shell
yay -S bf-interpreter yay -S bf-interpreter
``` ```
## Options and arguments ## Options and arguments
```bash ```shell
bf-bf_interpreter --help bf-interpreter --help
``` ```
```text ```text
Brainfu*k interpreter and REPL written in Rust Brainfu*k interpreter and REPL written in Rust
@ -52,7 +57,7 @@ Options:
### Examples ### Examples
```bash ```bash
bf-bf_interpreter test_code/hello_world.bf bf-interpreter test_code/hello_world.bf
``` ```
```text ```text
Hello world! Hello world!
@ -61,14 +66,14 @@ Exiting with code: 0
``` ```
```bash ```bash
bf-bf_interpreter -w test_code/hello_world.bf bf-interpreter -w test_code/hello_world.bf
``` ```
```text ```text
Hello world! Hello world!
``` ```
```bash ```bash
bf-bf_interpreter test_code/print_hi_yooo.bf bf-interpreter test_code/print_hi_yooo.bf
``` ```
```text ```text
Hi yoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo!Successfully ran brainfuck source code from file: test_code/print_hi_yooo.bf Hi yoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo!Successfully ran brainfuck source code from file: test_code/print_hi_yooo.bf
@ -76,13 +81,13 @@ Exiting with code: 0
``` ```
```bash ```bash
bf-bf_interpreter -w test_code/print_hi_yooo.bf bf-interpreter -w test_code/print_hi_yooo.bf
``` ```
```text ```text
Hi yoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo! Hi yoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo!
``` ```
```bash ```bash
bf-bf_interpreter test_code/like_cat.bf bf-interpreter test_code/like_cat.bf
``` ```
![output](./screenshots/like_cat_output.png) ![output](./screenshots/like_cat_output.png)

View file

@ -1,7 +1,10 @@
pub fn read_brainfuck_code(source: &String) -> String { pub fn read_brainfuck_code(source: &String) -> String {
info!("Reading brainfuck source code from file: {}", source); info!("Reading brainfuck source code from file: {}", source);
match std::fs::read_to_string(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) => { Err(e) => {
error!("Failed to read source code file: {}", e); error!("Failed to read source code file: {}", e);
eprintln!("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 { fn clean(source: String) -> Option<String> {
source if source.is_empty() {
return None;
}
let code: String = source
.chars() .chars()
.filter(|c| match c { .filter(|c| match c {
'+' | '-' | '<' | '>' | '[' | ']' | '.' | ',' => true, '+' | '-' | '<' | '>' | '[' | ']' | '.' | ',' => true,
_ => false, _ => false,
}) })
.collect() .collect();
if code.is_empty() {
return None;
}
Some(code)
} }