www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Compile-time madness: Compile-time compiler!

reply Gregor Richards <Richards codu.org> writes:
This is a compile-time compiler for Brainfuck, the infinitely perfect 
esoteric programming language ( http://www.esolangs.org/wiki/Brainfuck )

------------------------------------------------------------
ctbf.d:
------------------------------------------------------------
module ctbf;

import std.cstream;
import std.stdio;

static char[] ctbf(char[] bf)
{
     char[] code = `
     byte[] mem;
     uint memptr = 0;
     mem.length = 1;
     void expand() {
         if (mem.length <= memptr) {
             mem.length = memptr + 1;
         }
     }
     `;

     foreach (c; bf) {
         switch (c) {
             case '>':
                 code ~= "memptr++; expand();\n";
                 break;

             case '<':
                 code ~= "memptr--;\n";
                 break;

             case '+':
                 code ~= "mem[memptr]++;\n";
                 break;

             case '-':
                 code ~= "mem[memptr]--;\n";
                 break;

             case '[':
                 code ~= "while (mem[memptr]) {\n";
                 break;

             case ']':
                 code ~= "}\n";
                 break;

             case '.':
                 code ~= "dout.write(cast(char) mem[memptr]);\n";
                 break;

             case ',':
                 code ~= "din.read(mem[memptr]);\n";
                 break;

             default:
         }
     }

     return code;
}

int main()
{
     mixin(ctbf(import("helloworld.bf")));
     return 0;
}
------------------------------------------------------------





------------------------------------------------------------
helloworld.bf:
------------------------------------------------------------
+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.>>>++++++++[<++++>-]
<.>>>++++++++++[<+++++++++>-]<---.<<<<.+++.------.--------.>>+. ------------------------------------------------------------ - Gregor Richards
Feb 20 2007
next sibling parent janderson <askme me.com> writes:
Awesome, I'm going to code all my brainfunk code in D-compiler-brainfuck 
from now on :)

-Joel

Gregor Richards wrote:
 This is a compile-time compiler for Brainfuck, the infinitely perfect 
 esoteric programming language ( http://www.esolangs.org/wiki/Brainfuck )
 
 ------------------------------------------------------------
 ctbf.d:
 ------------------------------------------------------------
 module ctbf;
 
 import std.cstream;
 import std.stdio;
 
 static char[] ctbf(char[] bf)
 {
     char[] code = `
     byte[] mem;
     uint memptr = 0;
     mem.length = 1;
     void expand() {
         if (mem.length <= memptr) {
             mem.length = memptr + 1;
         }
     }
     `;
 
     foreach (c; bf) {
         switch (c) {
             case '>':
                 code ~= "memptr++; expand();\n";
                 break;
 
             case '<':
                 code ~= "memptr--;\n";
                 break;
 
             case '+':
                 code ~= "mem[memptr]++;\n";
                 break;
 
             case '-':
                 code ~= "mem[memptr]--;\n";
                 break;
 
             case '[':
                 code ~= "while (mem[memptr]) {\n";
                 break;
 
             case ']':
                 code ~= "}\n";
                 break;
 
             case '.':
                 code ~= "dout.write(cast(char) mem[memptr]);\n";
                 break;
 
             case ',':
                 code ~= "din.read(mem[memptr]);\n";
                 break;
 
             default:
         }
     }
 
     return code;
 }
 
 int main()
 {
     mixin(ctbf(import("helloworld.bf")));
     return 0;
 }
 ------------------------------------------------------------
 
 
 
 
 
 ------------------------------------------------------------
 helloworld.bf:
 ------------------------------------------------------------
  >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.
>>++++++++[<++++>-] 
 
 <.>>>++++++++++[<+++++++++>-]<---.<<<<.+++.------.--------.>>+.
 ------------------------------------------------------------
 
 
 
 
 
  - Gregor Richards
Feb 20 2007
prev sibling next sibling parent kris <foo bar.com> writes:
Gregor Richards wrote:
 This is a compile-time compiler for Brainfuck, the infinitely perfect 
 esoteric programming language ( http://www.esolangs.org/wiki/Brainfuck )
 
 ------------------------------------------------------------
 ctbf.d:
 ------------------------------------------------------------
 module ctbf;
 
 import std.cstream;
 import std.stdio;
 
 static char[] ctbf(char[] bf)
 {
     char[] code = `
     byte[] mem;
     uint memptr = 0;
     mem.length = 1;
     void expand() {
         if (mem.length <= memptr) {
             mem.length = memptr + 1;
         }
     }
     `;
 
     foreach (c; bf) {
         switch (c) {
             case '>':
                 code ~= "memptr++; expand();\n";
                 break;
 
             case '<':
                 code ~= "memptr--;\n";
                 break;
 
             case '+':
                 code ~= "mem[memptr]++;\n";
                 break;
 
             case '-':
                 code ~= "mem[memptr]--;\n";
                 break;
 
             case '[':
                 code ~= "while (mem[memptr]) {\n";
                 break;
 
             case ']':
                 code ~= "}\n";
                 break;
 
             case '.':
                 code ~= "dout.write(cast(char) mem[memptr]);\n";
                 break;
 
             case ',':
                 code ~= "din.read(mem[memptr]);\n";
                 break;
 
             default:
         }
     }
 
     return code;
 }
 
 int main()
 {
     mixin(ctbf(import("helloworld.bf")));
     return 0;
 }
 ------------------------------------------------------------
hehe ... nice one
Feb 20 2007
prev sibling next sibling parent "John S. Skogtvedt" <jss2k2 chello.no> writes:
If anyone's interested, here's how a compile-time BF compiler looked
like in November.
Basically each BF command compiled into a function, and it only worked
for smaller BF programs.

http://www.digitalmars.com/d/archives/digitalmars/D/learn/A_brainf..._implementation_using_D_templates_5158.html
http://home.chello.no/~jskogtv/bf.d

At least when it comes to being a BF compiler, D has improved a lot :)
Feb 21 2007
prev sibling parent Pragma <ericanderton yahoo.removeme.com> writes:
Gregor Richards wrote:
 This is a compile-time compiler for Brainfuck, the infinitely perfect 
 esoteric programming language ( http://www.esolangs.org/wiki/Brainfuck )
 
 ------------------------------------------------------------
 ctbf.d:
 ------------------------------------------------------------
 module ctbf;
 
 import std.cstream;
 import std.stdio;
 
 static char[] ctbf(char[] bf)
 {
     char[] code = `
     byte[] mem;
     uint memptr = 0;
     mem.length = 1;
     void expand() {
         if (mem.length <= memptr) {
             mem.length = memptr + 1;
         }
     }
     `;
 
     foreach (c; bf) {
         switch (c) {
             case '>':
                 code ~= "memptr++; expand();\n";
                 break;
 
             case '<':
                 code ~= "memptr--;\n";
                 break;
 
             case '+':
                 code ~= "mem[memptr]++;\n";
                 break;
 
             case '-':
                 code ~= "mem[memptr]--;\n";
                 break;
 
             case '[':
                 code ~= "while (mem[memptr]) {\n";
                 break;
 
             case ']':
                 code ~= "}\n";
                 break;
 
             case '.':
                 code ~= "dout.write(cast(char) mem[memptr]);\n";
                 break;
 
             case ',':
                 code ~= "din.read(mem[memptr]);\n";
                 break;
 
             default:
         }
     }
 
     return code;
 }
 
 int main()
 {
     mixin(ctbf(import("helloworld.bf")));
     return 0;
 }
 ------------------------------------------------------------
 
 
 
 
 
 ------------------------------------------------------------
 helloworld.bf:
 ------------------------------------------------------------
  >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.
>>++++++++[<++++>-] 
 
 <.>>>++++++++++[<+++++++++>-]<---.<<<<.+++.------.--------.>>+.
 ------------------------------------------------------------
 
 
 
 
 
  - Gregor Richards
Awesome. Nice work. -- - EricAnderton at yahoo
Feb 21 2007