Skip to content

Instantly share code, notes, and snippets.

@SpamixOfficial
Last active May 2, 2024 14:17
Show Gist options
  • Save SpamixOfficial/f9fa35606da4e6769ede26d4d4800a19 to your computer and use it in GitHub Desktop.
Save SpamixOfficial/f9fa35606da4e6769ede26d4d4800a19 to your computer and use it in GitHub Desktop.
Brainfuck interpreter as rust macro
/* µ-macro Brainfuck, by _madfrog */
macro_rules!f{($d:tt$f:tt$($p:tt$b:tt)*)=>
{macro_rules!b{(f$i:tt$o:tt$d($d$f:tt)*)=>
{use std::io::*;let mut x=($o,$i,[0u8;8<<9
],0);$d(b!{x$d$f};)*};$(($d$f:tt$p)=>$b;)*
($a:tt$c:tt)=>{}}}}f!($f->(b!($f-);b!($f>)
)<-(b!($f<);b!($f-))<<($f.3-=2)>>($f.3+=2)
..(b!($f.);b!($f.))>($f.3+=1)<($f.3-=1)+($
f.2[$f.3]+=1)-($f.2[$f.3]-=1).($f.0.write(
&[$f.2[$f.3]]).and($f.0.flush()).ok())[$($
c:tt)*](while$f.2[$f.3]>0{$(b!{$f$c};)*}),
($f.1.read(&mut $f.2[$f.3..=$f.3]).ok()));
let o = stdout();
let i = stdin();
b!(f i o
++++++++[>++++[>++>+++>+++>+<<<<-]>+>-
>+>>+[<]<-]>>.>>---.+++++++..+++.>.<<-
.>.+++.------.--------.>+.>++.
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment