import std.c.stdio; template bracket(char[] str, int index=0, int nest=-1, int end=index) { static if (str[index] == '[') { const int bracket = bracket!(str, index+1, nest+1, end+1); } else static if (str[index] == ']') { static if (nest == 0) const int bracket = end; else const int bracket = bracket!(str, index+1, nest-1, end+1); } else { const int bracket = bracket!(str, index+1, nest, end+1); } } ubyte[30_000] cells; ubyte *cell = &cells[0]; debug template str(char[] s) { const char[] str = s; } void BF(char[] code, int index=0, int endindex=code.length)() { static if (index != endindex) { debug pragma(msg, str!((code)[index..endindex])); static if (code[index] == '>') { ++cell; } else static if (code[index] == '<') { --cell; } else static if (code[index] == '+') { ++(*cell); } else static if (code[index] == '-') { --(*cell); } else static if (code[index] == '.') { putchar(*cell); } else static if (code[index] == ',') { *cell = getchar(); } static if (code[index] == '[') { debug pragma(msg, "bracket"); const int b = bracket!(code, index); while (*cell) { BF!(code, index+1, b)(); } BF!(code, b, endindex)(); } else { BF!(code, index+1, endindex)(); } } } int main(char[][] args) { //const char[] code = "+[,.]"; const char[] code = ",>,>++++++++[<------<------>>-]" "<<[>[>+>+<<-]>>[<<+>>-]<<<-]" ">>>++++++[<++++++++>-],<.>."; BF!(code)(); return 0; }