Skip to content

Instantly share code, notes, and snippets.

@nanase
Created July 31, 2013 16:04
Show Gist options
  • Save nanase/6123386 to your computer and use it in GitHub Desktop.
Save nanase/6123386 to your computer and use it in GitHub Desktop.
Brainf*ckのインタプリタをD言語で作ってみた。習作。添字チェックはしてない。引数にコードを書いて実行してね。
import std.stdio;
int main(string[] args)
{
if (args.length < 2)
{
writeln("Brainf*ck のコードを記述してください.");
return 1;
}
int c = 0, p = 0;
ubyte[] m = new ubyte[32000];
string buf;
const int Max = 2048;
for (int i = 0; i < args[1].length; i++)
{
switch(args[1][i])
{
case '>': p++; break;
case '<': p--; break;
case '+': m[p]++; break;
case '-': m[p]--; break;
case '.': write(cast(char)m[p]); break;
case ',':
while ((buf = readln()) == null || buf.length == 0) { }
m[p] = cast(ubyte)buf[0];
break;
case '[': if (m[p] == 0) while (args[1][i] != ']') i++; break;
case ']': if (m[p] != 0) while (args[1][i] != '[') i--; break;
default: break;
}
if(++c > Max)
{
writeln("Clock overflow!");
break;
}
}
writeln();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment