目录
一,字符集
假设有门语言叫Z01语言,代码中只有0和1这两种字符。
二,编译分词
预处理剔除所有非0和1的字符之后,采用固定分词方式,从左往右每三个字符构成1个token,所以一共有8种token。
PS:预处理不是重点,本文不涉及,假设输入的都是只有0和1的字符。
三,token含义
token | 含义 |
000 | 指针加一 |
001 | 指针减一 |
010 | 指针指向的字节的值加一 |
011 | 指针指向的字节的值减一 |
100 | 输出指针指向的单元内容(ASCⅡ码) |
101 | 输入内容到指针指向的单元(ASCⅡ码) |
110 | 如果指针指向的单元值为零,向后跳转到对应的]指令的次一指令处 |
111 | 如果指针指向的单元值不为零,向前跳转到对应的[指令的次一指令处 |
聪明的读者或许已经看出来了,我这个只是二次创作,毕竟有趣的灵魂互相抄袭。
四,Z01翻译成C++
string translate(char a, char b, char c)
{
if (a == '0') {
if (b == '0') {
if (c == '0') {
return "p++";
}
else {
return "p--";
}
}
else {
if (c == '0') {
return "*p = *p + 1";
}
else {
return "*p = *p - 1";
}
}
}
else {
if (b == '0') {
if (c == '0') {
return "cout<<char(*p)";
}
else {
return "*p=getchar()";
}
}
else {
if (c == '0') {
return "while(*p){";
}
else {
return "}";
}
}
}
}
int main()
{
char a,b,c;
string s;
while (cin >> a >> b >> c) {
s += translate(a,b,c);
if (a == '1'&&b=='1'&&c=='0')continue;
s += ";\n";
}
cout << s;
return 0;
}
例如,输入:
010010010010010010010010010010010010010110011000010010010010010001111000100
输出:
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
while(*p){*p = *p - 1;
p++;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
p--;
};
p++;
cout<<char(*p);
五,执行翻译后的代码
void run()
{
char arr[1000] = { 0 };
char* p = arr+500;
//input code
}
int main()
{
run();
return 0;
}
只要把翻译后的代码粘贴进去就行。
六,打印Hello World!
有了打印一个字符的经验和归0操作,接下来就可以输出任意字符了:
Ascii码分别是72,101,108,108,111,32,87,111,114,108,100,33
代码应该是:
010010010010010010010010010110011000010010010010010010010010001111000100110110011111001111010010010010010010010010010010110011000010010010010010010010010010010001111000010100110110011111001111010010010010010010010010010110011000010010010010010010010010010010010010001111000100110110011111001111010010010010010010010010010110011000010010010010010010010010010010010010001111000100110110011111001111010010010010010010010010010110011000010010010010010010010010010010010010001111000010010010100110110011111001111010010010010010010010010110011000010010010010001111000100110110011111001111010010010010010010010010110011000010010010010010010010010010010010001111000011100110110011111001111010010010010010010010010010110011000010010010010010010010010010010010010001111000010010010100110110011111001111010010010010010010010010010110011000010010010010010010010010010010010010001111000010010010010010010100110110011111001111010010010010010010010010010110011000010010010010010010010010010010010010001111000100110110011111001111010010010010010010010010010010110011000010010010010010010010010010010001111000100110110011111001111010010010010010010010010110011000010010010010001111000010100110110011111001111
翻译结果:
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
while(*p){*p = *p - 1;
p++;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
p--;
};
p++;
cout<<char(*p);
while(*p){while(*p){*p = *p - 1;
};
p--;
};
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
while(*p){*p = *p - 1;
p++;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
p--;
};
p++;
*p = *p + 1;
cout<<char(*p);
while(*p){while(*p){*p = *p - 1;
};
p--;
};
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
while(*p){*p = *p - 1;
p++;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
p--;
};
p++;
cout<<char(*p);
while(*p){while(*p){*p = *p - 1;
};
p--;
};
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
while(*p){*p = *p - 1;
p++;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
p--;
};
p++;
cout<<char(*p);
while(*p){while(*p){*p = *p - 1;
};
p--;
};
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
while(*p){*p = *p - 1;
p++;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
p--;
};
p++;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
cout<<char(*p);
while(*p){while(*p){*p = *p - 1;
};
p--;
};
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
while(*p){*p = *p - 1;
p++;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
p--;
};
p++;
cout<<char(*p);
while(*p){while(*p){*p = *p - 1;
};
p--;
};
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
while(*p){*p = *p - 1;
p++;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
p--;
};
p++;
*p = *p - 1;
cout<<char(*p);
while(*p){while(*p){*p = *p - 1;
};
p--;
};
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
while(*p){*p = *p - 1;
p++;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
p--;
};
p++;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
cout<<char(*p);
while(*p){while(*p){*p = *p - 1;
};
p--;
};
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
while(*p){*p = *p - 1;
p++;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
p--;
};
p++;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
cout<<char(*p);
while(*p){while(*p){*p = *p - 1;
};
p--;
};
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
while(*p){*p = *p - 1;
p++;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
p--;
};
p++;
cout<<char(*p);
while(*p){while(*p){*p = *p - 1;
};
p--;
};
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
while(*p){*p = *p - 1;
p++;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
p--;
};
p++;
cout<<char(*p);
while(*p){while(*p){*p = *p - 1;
};
p--;
};
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
while(*p){*p = *p - 1;
p++;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
*p = *p + 1;
p--;
};
p++;
*p = *p + 1;
cout<<char(*p);
while(*p){while(*p){*p = *p - 1;
};
p--;
};
运行效果: