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

m
Fixed syntax highlighting.
(Move 2 alternate implementations from Execute Brain****. http://rosettacode.org/mw/index.php?title=Execute_Brain****&action=history)
m (Fixed syntax highlighting.)
 
(7 intermediate revisions by one other user not shown)
Line 1:
{{implementation|Brainf***}}{{collection|RCBF}}
===Version 1===
<syntaxhighlight lang="d">import core.stdc.stdio, core.stdc.stdlib, std.conv;
 
void brainfuckRun(in dstring code) nothrow {
== Implementation 1 ==
static int[int] matchBraces(in dstring code) pure nothrow
{{works with|D|2.007+}}
out(result) {
An implementation of Rosetta Code [[Brainfuck|BrainF*ck]] interpreter in [[D]].
foreach (immutable k, immutable v; result) {
assert(k >=0 && k < code.length);
Implement notes:
assert(v >=0 && v < code.length);
*Needs D version 2.007+, because [[closure]] support is required (it should compile in D1, but will run abnormally if brackets/loop-commands are in the BF source).
}
*Memory is represented by an [[associative array]], so that negative addresses are allowed, though it is not efficient.
} body {
*Input and output are in character mode, rather than in numerical.
int[int] loops;
*Nesting level is checked during parsing, and if loops/brackets are not matched, an error is thrown before executing the code.
int[] loopStack;
<lang d>module rcbf ;
import std.file, std.c.stdio ;
 
foreach (immutable i, immutable instruction; code) {
alias void delegate() Act ;
if (instruction == '[')
 
loopStack ~= i;
char[int] mem ; // memory
else if (instruction == ']') {
Act[char] cmd ; // bf command except loop-control
int ptr ; // mem pointer loops[loopStack[$ - 1]] = i;
loopStack.length -= 1;
 
static this() {
cmd['>'] = { if(!(++ptr in mem)) mem[ptr] = 0 ; } ;
cmd['<'] = { if(!(--ptr in mem)) mem[ptr] = 0 ; } ;
cmd['+'] = { mem[ptr] += 1 ; } ;
cmd['-'] = { mem[ptr] -= 1 ; } ;
cmd['.'] = { printf("%c", mem[ptr]) ; } ;
cmd[','] = { printf("%c", mem[ptr] = getch()) ; flushall ; } ;
}
 
void bf(string code) {
int cp = 0 ; // code pointer
int nested = 0 ; // nested loop level
 
Act bfAct() {
Act[] acts ; // store commands of current nesting level
char cc ;
while(cp < code.length)
switch(cc = code[cp++]) { // cc get next command and code pointer cp is advanced
case '[':
nested++ ;
acts ~= bfAct() ; // begin inner loop
break ;
case ']':
if(--nested < 0) throw new Exception("Unmatched Loops") ;
return { while(mem[ptr]) { foreach(x ; acts){x();} } } ;
default:
if(cc in cmd)
acts ~= cmd[cc] ;
//else ignore other non-command char
}
return { foreach(x ; acts){x();} } ;
}
mem = null ; mem[0] = 0 ; ptr = 0 ; // reset memory
Act run = bfAct() ;
if(nested != 0) throw new Exception("Unmatched Loops") ;
run() ; // execute the whole bf program
printf("\n") ;
}
 
void main(string[] args) { // if no argument, demo code will be run, else
if(args.length > 1) // the arguments are treated as filenames of bf source
foreach(f ; args[1..$]) // and executed one by one.
try bf(cast(string)read(f)) ;
catch (Exception e) printf("%*s",e.msg) ;
else
bf(">+++++++++[<+++++++++>-]<+.>+++[<----->-]"
"<.-.++++.>++++++[<------>-]<--.>++++++++["
"<+++++++++>-]<+.>++[<++>-]<+.>++++++++[<-"
"-------->-]<------.>++++++[<++++++>-]<.>+"
"++++[<------->-]<.") ;
}
</lang>
 
== Implementation 2 ==
Alternative version, simpler and faster:
<lang d>import core.stdc.stdio: getchar, putchar, EOF;
import core.stdc.stdlib: exit;
 
void brainfuckRun(const string code) {
static pure int[int] matchBraces(const string code)
out(result) {
foreach (k, v; result) {
assert(k >=0 && k < code.length);
assert(v >=0 && v < code.length);
assert(v in result);
}
} body {
int[int] loops;
int[] loopStack;
 
foreach assert(i, instruction; code!loopStack.length) {;
return loops;
if (instruction == '[')
}
loopStack ~= i;
else if (instruction == ']') {
assert(loopStack.length);
loops[i] = loopStack[$ - 1];
loopStack.length -= 1;
loops[loops[i]] = i;
}
}
 
static void runCode(in dstring code, in int[int] loops) nothrow {
assert(!loopStack.length);
return loops;
}
 
static void runCode(const string code, const int[int] loops) {
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 < cast(int)code.length.signed) {
immutable int instruction = code[index];
 
Line 120 ⟶ 44:
case '.': putchar(tape[cell]); break;
case ',':
immutable int c = getchar();
if (c == EOF)
exit(1);
Line 128 ⟶ 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 = loops[index]matching - 1;
break;
default:
break;
default: break;
}
 
Line 144 ⟶ 75:
}
 
void main(in string[] args) {
import std.file;
brainfuckRun("++++++++++[>+++++++>++++++++++>+++>+<<<<-]
>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.");
}</lang>
 
// if no argument, demo code will be run, else the first
== Implementation 3 ==
// argument is treated as filename of bf source and executed.
Faster partially compile-time version (code generated at compile-time, run at run time):
if (args.length > 1) {
<lang d>import core.stdc.stdio, core.stdc.stdlib;
args[1].readText.dtext.brainfuckRun;
} else {
brainfuckRun("++++++++++[>+++++++>++++++++++>+++>+<<<<-]
>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.
+++.------.--------.>+.>.");
}
}</syntaxhighlight>
 
===Version 2===
pure string ctbf(in string code) {
Much faster version, code generated at compile-time, run at run-time:
string r;
<syntaxhighlight lang="d">string ctbf(in string code) pure nothrow {
foreach (c; code)
switchstring (c) {r;
foreach (immutable c; code)
case '>': r ~= "i++; assert(i < m.length);"; break;
switch (c) {
case '<': r ~= "i--; assert(i >= 0);"; break;
case '+>': r ~= "m[i]++;"; assert(i < m.length);"; break;
case '-<': r ~= "m[i]--;"; assert(i >= 0);"; break;
case '[+': r ~= "while (m[i]) {++;"; break;
case ']-': r ~= "}m[i]--;"; break;
case '.[': r ~= "putcharwhile (m[i]); {"; break;
case ',]': r ~= "int}"; d = getchar()break;
case '.': r ~= "m[i].putchar;"; if (d == EOF) exit(1)break;
case ',': r m[i] ~= cast(char)d;"; { int d = breakgetchar;
default: if (d == EOF) breakexit(1);
m[i] = cast(char)d; }"; break;
}
default: break;
return r;
}
return r;
}
 
void main() {
import core.stdc.stdio, core.stdc.stdlib;
char[30_000] m = '\0';
 
size_t i;
char[30_000] m = '\0';
mixin(ctbf("++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++
size_t i;
++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>."));
mixin(ctbf(
}</lang>
" A mandelbrot set fractal viewer in brainf*** written by Erik Bosman
+++++++++++++[->++>>>+++++>++>+<<<<<<]>>>>>++++++>--->>>>>>>>>>+++++++++++++++[[
>>>>>>>>>]+[<<<<<<<<<]>>>>>>>>>-]+[>>>>>>>>[-]>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>[-]+
<<<<<<<+++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>>>+>>>>>>>>>>>>>>>>>>>>>>>>>>
>+<<<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[-]+[>>>>>>[>>>>>>>[-]>>]<<<<<<<<<[<<<<<<<<<]>>
>>>>>[-]+<<<<<<++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>>+<<<<<<+++++++[-[->>>
>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>>+<<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[[-]>>>>>>[>>>>>
>>[-<<<<<<+>>>>>>]<<<<<<[->>>>>>+<<+<<<+<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>
[>>>>>>>>[-<<<<<<<+>>>>>>>]<<<<<<<[->>>>>>>+<<+<<<+<<]>>>>>>>>]<<<<<<<<<[<<<<<<<
<<]>>>>>>>[-<<<<<<<+>>>>>>>]<<<<<<<[->>>>>>>+<<+<<<<<]>>>>>>>>>+++++++++++++++[[
>>>>>>>>>]+>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>-]+[
>+>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>[>->>>>[-<<<<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