Skip to content

Instantly share code, notes, and snippets.

@AlephAlpha
Last active December 25, 2015 14:39
Show Gist options
  • Save AlephAlpha/6993043 to your computer and use it in GitHub Desktop.
Save AlephAlpha/6993043 to your computer and use it in GitHub Desktop.
Brainfuck interpreter in Mathematica.
ToExpression[
"input=StringToStream[\"" <> ##2 <>
"\"];Clear[memory];memory[_]=pointer=0;" <>
StringReplace[#,
{">" -> "pointer++;",
"<" -> "pointer--;",
"+" -> "memory[pointer]++;",
"-" -> "memory[pointer]--;",
"." -> "BinaryWrite[OutputStream[\"stdout\",1],memory[pointer]];",
"," -> "memory[pointer]=BinaryRead[input];",
"[" -> "While[memory[pointer]!=0,",
"]" -> "];",
_ -> ""}] <>
"Close[input];"] &
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment