Skip to content

Instantly share code, notes, and snippets.

Created November 5, 2017 21:10
Show Gist options
  • Save anonymous/1ae4bb2b0d5b851d9ec1cae3daf4e3fd to your computer and use it in GitHub Desktop.
Save anonymous/1ae4bb2b0d5b851d9ec1cae3daf4e3fd to your computer and use it in GitHub Desktop.
Rust code shared from the playground
// Did you ever want to have a compile-time evaluated string and didn't
// want to use so called "procedural macros" because it's too easy to
// have that with those? Well, now you can.
//
// ... Oh, right, there is a small catch. The computation has to be in
// Brainfuck. But I mean, it's Turing complete, so you can do any
// computation you like, so it's not a big deal, is it?
//
// Scroll down to the end to see usage, but essentially this file
// defines `bf!` macro to which you can pass whatever Brainfuck code
// you like. Its output will be returned as a constant string you can
// use for whatever purpose you like, including using in `const` and as
// a constant for macros like `concat!` and `compile_error!`.
//
// If you actually want to use this for some crazy reason (...), then
// have a license.
//
// --------------------------------------------------------------------
//
// whyiamdoingthis - Brainfuck compile-time interpreter implemented
// using Rust macros
//
// Written in 2017 by Konrad Borowski <konrad@borowski.pw>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to the
// public domain worldwide. This software is distributed without any
// warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication
// along with this software. If not, see
// <http://creativecommons.org/publicdomain/zero/1.0/>.
#![recursion_limit = "2048"]
macro_rules! bf {
($($code:tt)*) => {
b!(
[[$($code)*]]
[] [] []
[]
)
}
}
macro_rules! b {
// End condition
(
[[]]
$left:tt $current:tt $right:tt
[$([$($out:tt)*])*]
) => {
concat!($(to_ascii!($($out)*)),*)
};
// End of current block,
(
[[] $($codes:tt)*]
$left:tt $current:tt $right:tt
$out:tt
) => {
b!(
[$($codes)*]
$left $current $right
$out
)
};
// Start of a block, skipped
(
[[[$($new_block:tt)*] $($rest:tt)*] $($codes:tt)*]
$left:tt [] $right:tt
$out:tt
) => {
b!(
[[$($rest)*] $($codes)*]
$left [] $right
$out
)
};
// Start of a block, entering
(
[[[$($new_block:tt)*] $($rest:tt)*] $($codes:tt)*]
$left:tt $current:tt $right:tt
$out:tt
) => {
b!(
[[$($new_block)*] [[$($new_block)*] $($rest)*] $($codes)*]
$left $current $right
$out
)
};
// Increment
(
[[+ $($rest:tt)*] $($codes:tt)*]
$left:tt [$($current:tt)*] $right:tt
$out:tt
) => {
b!(
[[$($rest)*] $($codes)*]
$left [+ $($current)*] $right
$out
)
};
// Decrement
(
[[- $($rest:tt)*] $($codes:tt)*]
$left:tt [+ $($current:tt)*] $right:tt
$out:tt
) => {
b!(
[[$($rest)*] $($codes)*]
$left [$($current)*] $right
$out
)
};
// Move tape left
(
[[< $($rest:tt)*] $($codes:tt)*]
[$next:tt $($left:tt)*] $current:tt [$($right:tt)*]
$out:tt
) => {
b!(
[[$($rest)*] $($codes)*]
[$($left)*] $next [$current $($right)*]
$out
)
};
// Move tape right, empty right tape
(
[[> $($rest:tt)*] $($codes:tt)*]
[$($left:tt)*] $current:tt []
$out:tt
) => {
b!(
[[$($rest)*] $($codes)*]
[$current $($left)*] [] []
$out
)
};
// Move tape right
(
[[> $($rest:tt)*] $($codes:tt)*]
[$($left:tt)*] $current:tt [$next:tt $($right:tt)*]
$out:tt
) => {
b!(
[[$($rest)*] $($codes)*]
[$current $($left)*] $next [$($right)*]
$out
)
};
// Output
(
[[. $($rest:tt)*] $($codes:tt)*]
$left:tt $current:tt $right:tt
[$($out:tt)*]
) => {
b!(
[[$($rest)*] $($codes)*]
$left $current $right
[$($out)* $current]
)
};
// Special handling for << token
(
[[<< $($rest:tt)*] $($codes:tt)*]
$left:tt $current:tt $right:tt
$out:tt
) => {
b!(
[[< < $($rest)*] $($codes)*]
$left $current $right
$out
)
};
// >> token
(
[[>> $($rest:tt)*] $($codes:tt)*]
$left:tt $current:tt $right:tt
$out:tt
) => {
b!(
[[> > $($rest)*] $($codes)*]
$left $current $right
$out
)
};
// -> token
(
[[-> $($rest:tt)*] $($codes:tt)*]
$left:tt $current:tt $right:tt
$out:tt
) => {
b!(
[[- > $($rest)*] $($codes)*]
$left $current $right
$out
)
};
// <- token
(
[[<- $($rest:tt)*] $($codes:tt)*]
$left:tt $current:tt $right:tt
$out:tt
) => {
b!(
[[< - $($rest)*] $($codes)*]
$left $current $right
$out
)
};
// .. token
(
[[.. $($rest:tt)*] $($codes:tt)*]
$left:tt $current:tt $right:tt
$out:tt
) => {
b!(
[[. . $($rest)*] $($codes)*]
$left $current $right
$out
)
};
}
macro_rules! to_ascii {
(++++++++++) => {"\n"};
(++++++++++++++++++++++++++++++++) => {" "};
(+++++++++++++++++++++++++++++++++) => {"!"};
(++++++++++++++++++++++++++++++++++) => {"\""};
(+++++++++++++++++++++++++++++++++++) => {"#"};
(++++++++++++++++++++++++++++++++++++) => {"$"};
(+++++++++++++++++++++++++++++++++++++) => {"%"};
(++++++++++++++++++++++++++++++++++++++) => {"&"};
(+++++++++++++++++++++++++++++++++++++++) => {"'"};
(++++++++++++++++++++++++++++++++++++++++) => {"("};
(+++++++++++++++++++++++++++++++++++++++++) => {")"};
(++++++++++++++++++++++++++++++++++++++++++) => {"*"};
(+++++++++++++++++++++++++++++++++++++++++++) => {"+"};
(++++++++++++++++++++++++++++++++++++++++++++) => {","};
(+++++++++++++++++++++++++++++++++++++++++++++) => {"-"};
(++++++++++++++++++++++++++++++++++++++++++++++) => {"."};
(+++++++++++++++++++++++++++++++++++++++++++++++) => {"/"};
(++++++++++++++++++++++++++++++++++++++++++++++++) => {"0"};
(+++++++++++++++++++++++++++++++++++++++++++++++++) => {"1"};
(++++++++++++++++++++++++++++++++++++++++++++++++++) => {"2"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++) => {"3"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"4"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"5"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"6"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"7"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"8"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"9"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {":"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {";"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"<"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"="};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {">"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"?"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"@"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"A"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"B"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"C"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"D"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"E"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"F"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"G"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"H"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"I"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"J"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"K"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"L"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"M"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"N"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"O"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"P"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"Q"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"R"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"S"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"T"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"U"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"V"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"W"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"X"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"Y"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"Z"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"["};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"\\"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"]"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"^"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"_"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"`"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"a"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"b"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"c"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"d"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"e"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"f"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"g"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"h"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"i"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"j"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"k"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"l"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"m"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"n"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"o"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"p"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"q"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"r"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"s"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"t"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"u"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"v"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"w"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"x"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"y"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"z"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"{"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"|"};
(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"}"};
(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++) => {"~"};
}
const OUTPUT: &str = bf!(
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
);
fn main() {
print!("{}", OUTPUT);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment