Jump to content

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

m
Fixed syntax highlighting.
m (<code>)
m (Fixed syntax highlighting.)
 
(11 intermediate revisions by 5 users not shown)
Line 1:
{{implementation|Brainf***}}{{collection|RCBF}}[[Category:D]]
===Version 1===
{{works with|D|2.007+}}
<syntaxhighlight lang="d">import core.stdc.stdio, core.stdc.stdlib, std.conv;
An implementation of Rosetta Code [[Brainfuck|BrainF*ck]] interpreter in [[D]].
Implement notes:
*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.
*Input and output are in character mode, rather than in numerical.
*Nesting level is checked during parsing, and if loops/brackets are not matched, an error is thrown before executing the code.
<code d>module rcbf ;
import std.file, std.c.stdio ;
 
void brainfuckRun(in dstring code) nothrow {
alias void delegate() Act ;
static int[int] matchBraces(in dstring code) pure nothrow
out(result) {
foreach (immutable k, immutable v; result) {
assert(k >=0 && k < code.length);
assert(v >=0 && v < code.length);
}
} body {
int[int] loops;
int[] loopStack;
 
foreach (immutable i, immutable instruction; code) {
char[int] mem ; // memory
if (instruction == '[')
Act[char] cmd ; // bf command except loop-control
int ptr ; // mem pointer loopStack ~= i;
else if (instruction == ']') {
loops[loopStack[$ - 1]] = i;
loopStack.length -= 1;
}
}
 
assert(!loopStack.length);
static this() {
return loops;
cmd['>'] = { if(!(++ptr in mem)) mem[ptr] = 0 ; } ;
}
cmd['<'] = { if(!(--ptr in mem)) mem[ptr] = 0 ; } ;
 
cmd['+'] = { mem[ptr] += 1 ; } ;
static void runCode(in dstring code, in int[int] loops) nothrow {
cmd['-'] = { mem[ptr] -= 1 ; } ;
enum char empty = '\0';
cmd['.'] = { printf("%c", mem[ptr]) ; } ;
char[30_000] tape = empty;
cmd[','] = { printf("%c", mem[ptr] = getch()) ; flushall ; } ;
int cell, index;
int[10_000] stack; // Bracket stack
size_t stack_pos = 0; // Bracket stack position
 
while (index < code.length.signed) {
immutable int instruction = code[index];
 
switch (instruction) {
case '>': cell++; assert(cell < tape.length); break;
case '<': cell--; assert(cell >= 0); break;
case '+': tape[cell]++; break;
case '-': tape[cell]--; break;
case '.': putchar(tape[cell]); break;
case ',':
immutable int c = getchar;
if (c == EOF)
exit(1);
tape[cell] = cast(char)c;
break;
case '[':
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 = matching - 1;
break;
default:
break;
}
 
index++;
}
}
 
int[int] loops = matchBraces(code);
runCode(code, loops);
}
 
void bfmain(in string[] codeargs) {
import std.file;
int cp = 0 ; // code pointer
int nested = 0 ; // nested loop level
 
// if no argument, demo code will be run, else the first
Act bfAct() {
// argument is treated as filename of bf source and executed.
Act[] acts ; // store commands of current nesting level
char cc ; if (args.length > 1) {
args[1].readText.dtext.brainfuckRun;
while(cp < code.length)
} else {
switch(cc = code[cp++]) { // cc get next command and code pointer cp is advanced
brainfuckRun("++++++++++[>+++++++>++++++++++>+++>+<<<<-]
case '[':
>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.
nested++ ;
acts ~= bfAct() ; // begin inner loop +++.------.--------.>+.>.");
break ;}
}</syntaxhighlight>
case ']':
 
if(--nested < 0) throw new Exception("Unmatched Loops") ;
===Version 2===
return { while(mem[ptr]) { foreach(x ; acts){x();} } } ;
Much faster version, code generated at compile-time, run at run-time:
default:
<syntaxhighlight lang="d">string ctbf(in string code) pure nothrow {
if(cc in cmd)
string r;
acts ~= cmd[cc] ;
foreach (immutable c; code)
//else ignore other non-command char
} switch (c) {
case '>': r ~= "i++; assert(i < m.length);"; break;
return { foreach(x ; acts){x();} } ;
case '<': r ~= "i--; assert(i >= 0);"; break;
}
case '+': r ~= "m[i]++;"; break;
mem = null ; mem case '-': r ~= "m[0i]--;"; = 0 ; ptr = 0 ; // reset memory break;
case '[': r ~= "while (m[i]) {"; break;
Act run = bfAct() ;
case ']': r ~= "}"; break;
if(nested != 0) throw new Exception("Unmatched Loops") ;
case '.': r ~= "m[i].putchar;"; break;
run() ; // execute the whole bf program
case ',': r ~= "{ int d = getchar;
printf("\n") ;
if (d == EOF) exit(1);
m[i] = cast(char)d; }"; break;
default: break;
}
return r;
}
 
void main() {
void main(string[] args) { // if no argument, demo code will be run, else
import core.stdc.stdio, core.stdc.stdlib;
if(args.length > 1) // the arguments are treated as filenames of bf source
 
foreach(f ; args[1..$]) // and executed one by one.
char[30_000] m = '\0';
try bf(cast(string)read(f)) ;
size_t i;
catch (Exception e) printf("%*s",e.msg) ;
mixin(ctbf(
else
" A mandelbrot set fractal viewer in brainf*** written by Erik Bosman
bf(">+++++++++[<+++++++++>-]<+.>+++[<----->-]"
"<.-.++++.>+++++++++[<------>-]++>>>+++++>++>+<<<<<<]>>>>>++++++>--.->>>>>>>>>>+++++++++++++++[["
>>>>>>>>>]+[<<<<<<<<<]>>>>>>>>>-]+[>>>>>>>>[-]>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>[-]+
"<+++++++++>-]<+.>++[<++>-]<+.>++++++++[<-"
<<<<<<<+++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>>>+>>>>>>>>>>>>>>>>>>>>>>>>>>
"-------->-]<------.>++++++[<++++++>-]<.>+"
>+<<<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[-]+[>>>>>>[>>>>>>>[-]>>]<<<<<<<<<[<<<<<<<<<]>>
"++++[<------->-]<.") ;
>>>>>[-]+<<<<<<++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>>+<<<<<<+++++++[-[->>>
}</code>
>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>>+<<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[[-]>>>>>>[>>>>>
>>[-<<<<<<+>>>>>>]<<<<<<[->>>>>>+<<+<<<+<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>
[>>>>>>>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>>>[>>>>>>[-<<<<<<->>>>>>]+<
<<<<<[->>>>>>->>[-<<<<<<<<+>>>>>>>>]<<<<<<<<[->>>>>>>>+<<<<<<<<<<<<<<<<<[<<<<<<<
<<]>>>>[-]+>>>>>[>>>>>>>>>]>+<]]+>>>>>>>>[-<<<<<<<<->>>>>>>>]+<<<<<<<<[->>>>>>>>
-<<[-<<<<<<+>>>>>>]<<<<<<[->>>>>>+<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[-]+>>>>>>[>>>>>>
>>>]>[-]+<]]+>[-<[>>>>>>>>>]<<<<<<<<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>[-]<<<++++
+[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>->>>>>>>>>>>>>>>>>>>>>>>>>>>-<<<<<<[<<<<
<<<<<]]>>>]"));
}</syntaxhighlight>
LDC2 compiles this with full optimizations in 2.3 seconds, and the BF runs in less than 1.1 seconds.
9,488

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.