规则:
-
不能出现关键字(以 https://zh.cppreference.com/cpp/keyword 为准)。
-
不能使用除引入同文件外的其它预编译命令(防止使用
#define或其它方式替换关键字)。 -
代码必须可单独运行(即不能将代码藏于自定义头文件然后引入)。
在规则约束下,我实现了一份 brainfuck 解释器以证明其图灵完备:
#include<bits/stdc++.h>
int32_t B;
int8_t code[1000000],
a[1000000]={0},*ptr,*pc;
main(int32_t x){
x==1?(
ptr=a,pc=code,scanf("%s",code),
std::for_each(code,code+1000000,
[](int32_t x){B+=code[x]!='\0';}),main(2)
):x==2?(
*pc?(
*pc=='>'?(ptr++,0):
*pc=='<'?(ptr--,0):
*pc=='+'?(++*ptr,0):
*pc=='-'?(--*ptr,0):
*pc=='.'?putchar(*ptr):
*pc==','?(*ptr=getchar()):
*pc=='['?main(3):main(4),pc++,main(2)
):0
):x==3?(*ptr?0:main(B-1)):x==4?(main(B+1),pc--,0):
x<B?(pc++,*pc=='['?main(x-1):*pc==']'?main(x+1):main(x)):
x>B?(pc--,*pc=='['?main(x-1):*pc==']'?main(x+1):main(x)):0;
}