Execute Brain****/D: Difference between revisions

Content added Content deleted
(Updated third D entry)
(Updated D entry)
Line 101: Line 101:
===Version 2===
===Version 2===
Simpler and faster:
Simpler and faster:
<lang d>import core.stdc.stdio, core.stdc.stdlib;
<lang d>import core.stdc.stdio, core.stdc.stdlib, std.conv;


void brainfuckRun(in string code) nothrow {
void brainfuckRun(in string code) nothrow {
static int[int] matchBraces(in string code) pure nothrow
static int[int] matchBraces(in string code) pure nothrow
out(result) {
out(result) {
foreach (k, v; result) {
foreach (immutable k, immutable v; result) {
assert(k >=0 && k < code.length);
assert(k >=0 && k < code.length);
assert(v >=0 && v < code.length);
assert(v >=0 && v < code.length);
Line 115: Line 115:
int[] loopStack;
int[] loopStack;


foreach (i, instruction; code) {
foreach (immutable i, immutable instruction; code) {
if (instruction == '[')
if (instruction == '[')
loopStack ~= i;
loopStack ~= i;
Line 135: Line 135:
int cell, index;
int cell, index;


while (index < cast(int)code.length) {
while (index < code.length.signed) {
immutable int instruction = code[index];
immutable int instruction = code[index];


Line 145: Line 145:
case '.': putchar(tape[cell]); break;
case '.': putchar(tape[cell]); break;
case ',':
case ',':
immutable int c = getchar();
immutable int c = getchar;
if (c == EOF)
if (c == EOF)
exit(1);
exit(1);