From 7a143614eba5b9d1b0521db5f64e07167b91901e Mon Sep 17 00:00:00 2001 From: Agatha Rose Date: Thu, 4 Jun 2020 17:46:47 +0300 Subject: [PATCH] Add brainfuck interpreter --- Cargo.lock | 27 ++++++++++++++++++++++++++- Cargo.toml | 3 ++- src/main.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 79 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9b5bfec..cc8a358 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -84,6 +84,14 @@ dependencies = [ "byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "brainfrick" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "itertools 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "bumpalo" version = "3.2.1" @@ -222,6 +230,11 @@ name = "dtoa" version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "either" +version = "1.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "encoding_rs" version = "0.8.22" @@ -528,6 +541,14 @@ dependencies = [ "libc 0.2.68 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "itertools" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "itoa" version = "0.3.4" @@ -1050,8 +1071,9 @@ dependencies = [ [[package]] name = "rustcord" -version = "1.0.1" +version = "1.0.2" dependencies = [ + "brainfrick 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "colored 1.9.3 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "owoify 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1776,6 +1798,7 @@ dependencies = [ "checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" "checksum block-buffer 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b" "checksum block-padding 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fa79dedbb091f449f1f39e53edf88d5dbe95f895dae6135a8d7b881fb5af73f5" +"checksum brainfrick 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a5cb51d3ef4c36997a3d9d0cf219c3f9404be7dbbe32d88906f7691d9c293b94" "checksum bumpalo 3.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "12ae9db68ad7fac5fe51304d20f016c911539251075a214f8e663babefa35187" "checksum byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" "checksum byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" @@ -1795,6 +1818,7 @@ dependencies = [ "checksum ct-logs 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4d3686f5fa27dbc1d76c751300376e167c5a43387f44bb451fd1c24776e49113" "checksum digest 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5" "checksum dtoa 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "4358a9e11b9a09cf52383b451b49a169e8d797b68aa02301ff586d70d9661ea3" +"checksum either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3" "checksum encoding_rs 0.8.22 (registry+https://github.com/rust-lang/crates.io-index)" = "cd8d03faa7fe0c1431609dfad7bbe827af30f82e1e2ae6f7ee4fca6bd764bc28" "checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" "checksum flate2 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)" = "2cfff41391129e0a856d6d822600b8d71179d46879e310417eb9c762eb178b42" @@ -1828,6 +1852,7 @@ dependencies = [ "checksum indexmap 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "076f042c5b7b98f31d205f1249267e12a6518c1481e9dae9764af19b707d2292" "checksum input_buffer 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8e1b822cc844905551931d6f81608ed5f50a79c1078a4e2b4d42dbc7c1eedfbf" "checksum iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e" +"checksum itertools 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b" "checksum itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8324a32baf01e2ae060e9de58ed0bc2320c9a2833491ee36cd3b4c414de4db8c" "checksum itoa 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "b8b7a7c0c47db5545ed3fef7468ee7bb5b74691498139e4b3f6a20685dc6dd8e" "checksum js-sys 0.3.37 (registry+https://github.com/rust-lang/crates.io-index)" = "6a27d435371a2fa5b6d2b028a74bbdb1234f308da363226a2854ca3ff8ba7055" diff --git a/Cargo.toml b/Cargo.toml index f956b9e..4a1a68b 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rustcord" -version = "1.0.1" +version = "1.0.2" authors = ["Agatha Rose "] edition = "2018" @@ -15,6 +15,7 @@ urbandict = "0.2.0" owoify = "0.1.5" lazy_static = "1.4.0" colored = "1.9.3" +brainfrick = "1.1.0" [patch.crates-io] openssl = { git = "https://github.com/ishitatsuyuki/rust-openssl", branch = "0.9.x" } diff --git a/src/main.rs b/src/main.rs index 2cb1f40..5dfc206 100755 --- a/src/main.rs +++ b/src/main.rs @@ -50,7 +50,7 @@ impl EventHandler for Handler { #[group] #[commands( init, ping, halt, list_srv, host, ship, bottom_rng, headpat, uwu, gayculator, sausage, help, - embed, define, owo, info, echo, desc, pinned + embed, define, owo, info, echo, desc, pinned, brainfuck )] struct General; @@ -675,6 +675,7 @@ fn pinned(ctx: &mut Context, message: &Message, mut args: Args) -> CommandResult a.name(&pinned[idx].author.name) .icon_url(&pinned[idx].author.avatar_url().unwrap()) }); + e.colour(0xffd1dc); if !&pinned[idx].attachments.is_empty() { e.image(&pinned[idx].attachments[0].url); } @@ -685,3 +686,52 @@ fn pinned(ctx: &mut Context, message: &Message, mut args: Args) -> CommandResult Ok(()) } + +#[command] +#[aliases("bf", "brainfrick")] +fn brainfuck(ctx: &mut Context, message: &Message, args: Args) -> CommandResult { + use brainfrick::Brainfuck; + + let input = match args.rest().trim() { + "" => { + return Err(CommandError("Empty input!".to_string())); + }, + v @ _ => v + }; + let output = Brainfuck::execute(input); + + match output { + Ok(v) => { + let _ = message.channel_id.send_message(&ctx.http, |m| { + m.embed(|e| { + e.title("Brainfuck interpreter") + .description( + format!("Input\n```brainfuck\n{}\n```\nOutput:\n```{}\n```", input, v) + ) + .author(|a| { + a.name(&message.author.name) + .icon_url(message.author.avatar_url().unwrap()) + }) + .colour(0xffd1dc) + }) + }); + }, + Err(err) => { + let _ = message.channel_id.send_message(&ctx.http, |m| { + m.embed(|e| { + e.title("Brainfuck interpreter") + .description( + format!("Error at:\n```\n{}:{}\n```", err.line(), err.col()) + ) + .author(|a| { + a.name(&message.author.name) + .icon_url(message.author.avatar_url().unwrap()) + }) + .colour(0xff6961) + }) + }); + } + } + + Ok(()) +}