|
Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript electronics |
digitalmars.D - Compile-time madness: Compile-time compiler!
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: ------------------------------------------------------------+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.>>>++++++++[<++++>-] 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; } ------------------------------------------------------------ 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 Feb 21 2007
|