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
[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"

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 🦀
## 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)

View file

@ -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<String> {
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)
}