Created
July 19, 2016 09:34
-
-
Save Mego/5401f902f2df07d59beac9c275aece01 to your computer and use it in GitHub Desktop.
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
#include <iostream> | |
#include <string> | |
#include <sstream> | |
#include <fstream> | |
std::stringstream prog; | |
constexpr unsigned c_strlen( char const* str, unsigned count = 0 ) | |
{ | |
return ('\0' == str[0]) ? count : c_strlen(str+1, count+1); | |
} | |
template < char t_c, char... tt_c > | |
struct rec_eval | |
{ | |
static void eval() | |
{ | |
rec_eval<t_c>::eval(); | |
rec_eval < tt_c... > :: eval (); | |
} | |
}; | |
template < char t_c > | |
struct rec_eval < t_c > | |
{ | |
static void eval() { | |
switch(t_c) { | |
case '+': | |
prog<<"++t[i];"; | |
break; | |
case '-': | |
prog<<"--t[i];"; | |
break; | |
case '>': | |
prog<<"++i;"; | |
break; | |
case '<': | |
prog<<"--i;"; | |
break; | |
case '[': | |
prog<<"while(t[i]){"; | |
break; | |
case ']': | |
prog<<"}"; | |
break; | |
case '.': | |
prog<<"putc(t[i],stdout);"; | |
break; | |
case ',': | |
prog<<"t[i]=getchar();"; | |
break; | |
} | |
} | |
}; | |
template < char... tt_c > | |
struct exploded_string | |
{ | |
static void eval() | |
{ | |
rec_eval < tt_c... > :: eval(); | |
} | |
}; | |
template < typename T_StrProvider, unsigned t_len, char... tt_c > | |
struct explode_impl | |
{ | |
using result = | |
typename explode_impl < T_StrProvider, t_len-1, | |
T_StrProvider::str()[t_len-1], | |
tt_c... > :: result; | |
}; | |
template < typename T_StrProvider, char... tt_c > | |
struct explode_impl < T_StrProvider, 0, tt_c... > | |
{ | |
using result = exploded_string < tt_c... >; | |
}; | |
template < typename T_StrProvider > | |
using explode = | |
typename explode_impl < T_StrProvider, | |
c_strlen(T_StrProvider::str()) > :: result; | |
int main(int argc, char** argv) | |
{ | |
if(argc < 2) return 1; | |
prog << "#include <stdio.h>\n#include <stdlib.h>\nint main(){unsigned char* t=calloc("; | |
prog << (1 << sizeof(unsigned short)); | |
prog << ",sizeof(unsigned short));unsigned short i=0;"; | |
struct my_str_provider | |
{ | |
constexpr static char const* str() { return "++++[>+++++<-]>+++[[>+>+<<-]>++++++[<+>-]+++++++++[<++++++++++>-]>[<+>-]<-]+++>+++++++++[<+++++++++>-]>++++++[<++++++++>-]<--[>+>+<<-]>>[<<+>>-]<-->>++++[<++++++++>-]++++++++++>+++++++++[>+++++++++++<-]>[[>+>+>+<<<-]>[<+>-]>>[[>+>+<<-]>>[<<+>>-]<[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<<<+>---------->->[-]]]]]]]]]]]<]<<[>>++++++++++++[<++++<++++>>-]<<[.[-]>]<<]>[<++++++[>++++++++<-]>.[-]]<<<<<.<<<<<.[<]>>>>>>>>>.<<<<<..>>>>>>>>.>>>>>>>.[>]>[>+>+<<-]>[<+>-]>[-[[-]+++++++++[<+++++++++++++>-]<--.[-]>]]<<<<<.[<]>>>>>>>>>.>>>>>>>>>.[>]<<.<<<<<.<<<..[<]>>>>>>.[>]<<.[<]>>>>>>>>>.>.[>]<<.[<]>>>>.>>>>>>>>>>>>.>>>.[>]<<.[<]>.[>]<<<<<<.<<<<<<<<<<<..[>]<<<.>.[>]>[>+>+>+<<<-]>[<+>-]>>[[>+>+<<-]>>[<<+>>-]<[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<<<+>---------->->[-]]]]]]]]]]]<]<<[>>++++++++++++[<++++<++++>>-]<<[.[-]>]<<]>[<++++++[>++++++++<-]>.[-]]<<<<<.<<<<<.[<]>>>>>>>>>.<<<<<..>>>>>>>>.>>>>>>>.[>]>[>+>+<<-]>[<+>-]>[-[[-]+++++++++[<+++++++++++++>-]<--.[-]>]]<<<<<.[<]>>>>>>>>>.>>>>>>>>>.[>]<<.<<<<<.<<<..[<]>>>>>>.[>]<<<<.>>>.<<<<.<.<<<<<<<<<<.>>>>>>.[>]<<.[<]>>>>>>>>>.>.>>>>>>>>>.[>]<<.<<<<<<<.[<]>>>>>>>>>.[<]>.>>>>>>>>>.[>]<<.<<<<.<<<<<<<<<<<<<.[>]<<<<<<<<<.[>]<<.[<]>>>>>>>>.[>]<<<<<<.[<]>>>>>..[>]<<.<<<<<<<<<<<<.[<]>>>>.[>]<<.<<<<.[<]>>>>>>.>>>.<<<<<<.>>>>>>>.>>>>>>>>>>.[>]<<<.>.>>>-[>+>+>+<<<-]>[<+>-]>>[[>+>+<<-]>>[<<+>>-]<[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<->-[<<<+>---------->->[-]]]]]]]]]]]<]<<[>>++++++++++++[<++++<++++>>-]<<[.[-]>]<<]>[<++++++[>++++++++<-]>.[-]]<<[>+>+<<-]>[<+>-]+>[<->[-]]<[-<<<[<]>>>>>>>>>>.<.[>]<<.[<]>>>>>>>>>>>.<<.<<<.[>]<<<<<<<<<<.[>]>>]<<<<.<<<<<.[<]>>>>>>>>>.<<<<<..>>>>>>>>.>>>>>>>.[>]>[>+>+<<-]>[<+>-]+>[<->-[<+>[-]]]<[++++++++[>+++++++++++++<-]>--.[-]<]<<<<.[<]>>>>>>>>>.>>>>>>>>>.[>]<<.<<<<<.<<<..[<]>>>>>>.[>]<<.[<]>>>>>>>>>.>.[>]<<.[<]>>>>.>>>>>>>>>>>>.>>>.[>]<<.[<]>.[>]<<<<<<.<<<<<<<<<<<..[>]<<<<.>>>..>>]"; } | |
}; | |
auto my_str = explode < my_str_provider >{}; | |
my_str.eval(); | |
prog << "}"; | |
std::ofstream ofs(argv[1]); | |
if(!ofs) return 2; | |
ofs << prog.str() << std::endl; | |
ofs.close(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment