Anonymous user
Execute Brain****/D: Difference between revisions
Removed first D entry, faster second entry
(Updated D entry) |
(Removed first D entry, faster second entry) |
||
Line 1:
{{implementation|Brainf***}}{{collection|RCBF}}
===Version 1===
<lang d>import core.stdc.stdio, core.stdc.stdlib, std.conv;
void brainfuckRun(in
static int[int] matchBraces(in
out(result) {
foreach (immutable k, immutable v; result) {
assert(k >=0 && k < code.length);
assert(v >=0 && v < code.length);
}
} body {
Line 119 ⟶ 18:
loopStack ~= i;
else if (instruction == ']') {
loopStack.length -= 1;
}
}
Line 130 ⟶ 27:
}
static void runCode(in
enum char empty = '\0';
char[30_000] tape = empty;
int cell, index;
int[10_000] stack; // Bracket stack
size_t stack_pos = 0; // Bracket stack position
while (index < code.length.signed) {
Line 153 ⟶ 52:
if (tape[cell] == empty)
index = loops[index];
else {
stack[stack_pos] = index;
stack_pos++;
}
break;
case ']':
immutable matching = stack[stack_pos - 1];
stack_pos -= 1;
if (tape[cell] != empty)
index =
break;
default:
Line 170 ⟶ 75:
}
void main(in string[] args) {
import std.file;
// if no argument, demo code will be run, else the first
// argument is treated as filename of bf source and executed.
if (args.length > 1) {
args[1].readText.dtext.brainfuckRun;
} else {
brainfuckRun("++++++++++[>+++++++>++++++++++>+++>+<<<<-]
>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.
+++.------.--------.>+.>.");
}
}</lang>
===Version
<lang d>string ctbf(in string code) pure nothrow {
string r;
|