Last active
February 26, 2016 11:38
-
-
Save alphaKAI/946a0b3564d48916ce94 to your computer and use it in GitHub Desktop.
An one-line Brainfuck interpreter in D.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import std.algorithm, | |
| std.string, | |
| std.array, | |
| std.stdio, | |
| std.conv; | |
| import core.memory; | |
| /* | |
| Useing raw pointer and manually memory manegement for perfourmance. | |
| D's dynamic array is very useful but a litle slower but safe. | |
| On the other hand handling raw pointer is not safe but more faster. | |
| */ | |
| // Z Combinator : Anonymous recursion | |
| R delegate(Args) Z(R, Args...)(R delegate(R delegate(Args), Args) f) { | |
| return (Args args) => f(Z(f), args); | |
| } | |
| void main() { | |
| ((string input) => | |
| (operators => | |
| ((char* code) => | |
| ((ubyte* memory) => | |
| ((ulong memoryIndex) => | |
| (removeTrash => | |
| (process => | |
| process(input) | |
| )((string input) => | |
| ((string[] _code) => | |
| ( | |
| ( | |
| GC.realloc(code, _code.length * char.sizeof, GC.BlkAttr.NO_SCAN | GC.BlkAttr.APPENDABLE), | |
| GC.realloc(memory, _code.length * ubyte.sizeof, GC.BlkAttr.NO_SCAN | GC.BlkAttr.APPENDABLE) | |
| ), | |
| Z((int delegate(int) copy, int i) => i < _code.length ? (code[i] = _code[i].to!char, copy(i + 1)) : 0)(0), | |
| ((int pc) => | |
| (optimizer => | |
| ((int delegate() destructor) => | |
| ((int[int] brackets) => | |
| (process => | |
| ( | |
| process(int.init), | |
| destructor() | |
| ) | |
| )(Z((int delegate(int) process, int index) => | |
| index < _code.length | |
| ? ((char current) => | |
| ( | |
| (current == '>' | |
| ? memoryIndex++ | |
| : current == '<' | |
| ? memoryIndex-- | |
| : current == '+' | |
| ? memory[memoryIndex]++ | |
| : current == '-' | |
| ? memory[memoryIndex]-- | |
| : current == '.' | |
| ? ( | |
| write(memory[memoryIndex].to!char), | |
| stdout.flush() | |
| ) | |
| : current == ',' | |
| ? ((string buf) => | |
| ( | |
| Z((int delegate() getLine) => ((buf = readln()) == null || !buf.length) ? getLine() : 0)(), | |
| memory[memoryIndex] = cast(ubyte)buf[0] | |
| ) | |
| )(string.init) | |
| : current == '[' | |
| ? (memory[memoryIndex] == 0 ? (index = brackets[index], 0) : 0) | |
| : current == ']' | |
| ? (memory[memoryIndex] != 0 ? (index = brackets[index], 0) : 0) | |
| : 0 | |
| ), | |
| process(index + 1) | |
| ) | |
| )(code[index]) | |
| : 0 | |
| )) | |
| )(optimizer(int.init, (int[]).init, (int[int]).init)) | |
| )(() => (GC.free(code), GC.free(memory), memoryIndex = 0, 0)) | |
| )(Z((int[int] delegate(size_t, int[], int[int]) optimizer, size_t i, int[] leftstack, int[int] brackets) => | |
| i < _code.length ? | |
| ((char c) => | |
| !canFind(operators, _code[i]) | |
| ? optimizer(i + 1, leftstack, brackets) | |
| : ( | |
| c == '[' | |
| ? (optimizer(i + 1, leftstack ~ pc++, brackets)) | |
| : (c == ']' && leftstack.length != 0) | |
| ? (left => | |
| (leftstack.popBack(), | |
| (right => | |
| ( | |
| (brackets[left] = right, brackets[right] = left), | |
| pc++, | |
| optimizer(i + 1, leftstack, brackets) | |
| ) | |
| )(pc)) | |
| )(leftstack[$ - 1]) | |
| : (pc++, optimizer(i + 1, leftstack, brackets)) | |
| ) | |
| )(code[i]) : brackets | |
| ) | |
| ) | |
| )(int.init) | |
| ) | |
| )(removeTrash(input)) | |
| ) | |
| )((string key) => key.split("").filter!(x => operators.canFind(x)).array) | |
| )(ulong.init) | |
| )(cast(ubyte*)GC.malloc(300000 * ubyte.sizeof, GC.BlkAttr.NO_SCAN | GC.BlkAttr.APPENDABLE)) | |
| )(cast(char*) GC.malloc(300000 * char.sizeof, GC.BlkAttr.NO_SCAN | GC.BlkAttr.APPENDABLE)) | |
| )([">", "<", "+", "-", ".", ",", "[", "]"]) | |
| )(">+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-]<.>+++++++++++[<+++++>-]<.>++++++++[<+++>-]<.+++.------.--------.[-]>++++++++[<++++>-]<+.[-]++++++++++."); | |
| // => Hello World! | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment