digitalmars.D - Compile-time madness: Compile-time compiler!
- Gregor Richards <Richards codu.org> Feb 20 2007
- janderson <askme me.com> Feb 20 2007
- kris <foo bar.com> Feb 20 2007
- "John S. Skogtvedt" <jss2k2 chello.no> Feb 21 2007
- Pragma <ericanderton yahoo.removeme.com> Feb 21 2007
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
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
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
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
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









janderson <askme me.com> 