Created
July 31, 2013 16:04
-
-
Save nanase/6123386 to your computer and use it in GitHub Desktop.
Brainf*ckのインタプリタをD言語で作ってみた。習作。添字チェックはしてない。引数にコードを書いて実行してね。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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