Skip to content

Instantly share code, notes, and snippets.

@alphaKAI
Last active February 27, 2016 07:14
Show Gist options
  • Save alphaKAI/268227099872cdcebc4d to your computer and use it in GitHub Desktop.
Save alphaKAI/268227099872cdcebc4d to your computer and use it in GitHub Desktop.
An One-liner Brainfuck interpreter - Non typed version -
import std.algorithm,
std.array,
std.stdio,
std.string,
std.conv;
import core.memory;
R delegate(Args) Z(R, Args...)(R delegate(R delegate(Args), Args) f){
return (Args args) => f(Z(f), args);
}
void main() {
(input =>
(operators =>
(code =>
(memory =>
(memoryIndex =>
(removeTrash =>
(process =>
process(input)
)(
(string input) =>
(_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),
(pc =>
(optimizer =>
(destructor =>
(brackets =>
(process =>
(
process(int.init),
destructor
)
)(Z((int delegate(int) process, int index) =>
index < _code.length
? (current =>
(
(current == '>'
? memoryIndex++
: current == '<'
? memoryIndex--
: current == '+'
? memory[memoryIndex]++
: current == '-'
? memory[memoryIndex]--
: current == '.'
? (
write(memory[memoryIndex].to!char),
stdout.flush
)
: current == ','
? (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), code = null, memory = null, memoryIndex = 0, 0))
)(Z((int[int] delegate(size_t, int[], int[int]) optimizer, size_t i, int[] leftstack, int[int] brackets) =>
i < _code.length ?
(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(string.init).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