前言
这一次做的是编程语言,这是一个庞大的工程,我最近在了解关于BrainFuck编程语言的东西,所以这次想要做一个类似于BF的编程语言.
BF是一个是由Urban Müller在1993年创建的编程语言,有以下几个简单的指令:
> | 指针加一 |
< | 指针减一 |
+ | 指针指向的字节的值加一 |
- | 指针指向的字节的值减一 |
. | 输出指针指向的单元内容(ASCII码) |
, | 输入内容到指针指向的单元(ASCII码) |
[ | 如果指针指向的单元值为零,向后跳转到对应的]指令的次一指令处 |
] | 如果指针指向的单元值不为零,向前跳转到对应的[指令的次一指令处 |
做一个编程语言,编译器是很重要的.所以我做了一个编译器,并把它封装成了一个函数,叫做run.
注意:此为作者随便写的,勿喷谢谢.
主体
先给大家看一下Alpha0.1的代码:
#include<bits/stdc++.h>
using namespace std;
//>< >< >< ><
bool finish_run = 0;
void help(){
cout << "========KSS HELP========"<<endl;
cout << "== Help"<<endl;
cout << "=///.= Cls"<<endl;
cout << "=//..= Mod"<<endl;
cout << "=/...= Quit"<<endl;
cout << ">Morse< Print"<<endl;
}
void run(string s){
if (s == "=="){
cout << endl;
help();
}
else if (s == "=///.="){
system("cls");
}
else if (s == "=//..="){
cout << endl;
cout << ">....<< >.>< >.-..>< >.-..>< >--->< >.-.-.-< >.--<< >--->< >.-.>< >.-..>< >-..>< >-.-.--<@"<<endl;
cout << "Hello,World!"<<endl;
}
else if (s == "=/...="){
finish_run = 1;
}
else if (s == ">/../<"){
cout << " ";
}
else if (s == ">//./<"){
cout << endl;
}
else if (s == ">/.//<"){
cout << " ";
}
else if (s == "><"){
cout << "\0";
}
else if (s == ">.-<<"){
cout << "A";
}
else if (s == ">.-><"){
cout << "a";
}
else if (s == ">-...<<"){
cout << "B";
}
else if (s == ">-...><"){
cout << "b";
}
else if (s == ">-.-.<<"){
cout << "C";
}
else if (s == ">-.-.><"){
cout << "c";
}
else if (s == ">-..<<"){
cout << "D";
}
else if (s == ">-..><"){
cout << "d";
}
else if (s == ">.<<"){
cout << "E";
}
else if (s == ">.><"){
cout << "e";
}
else if (s == ">..-.<<"){
cout << "F";
}
else if (s == ">..-.><"){
cout << "f";
}
else if (s == ">--.<<"){
cout << "G";
}
else if (s == ">--.><"){
cout << "g";
}
else if (s == ">....<<"){
cout << "H";
}
else if (s == ">....><"){
cout << "h";
}
else if (s == ">..<<"){
cout << "I";
}
else if (s == ">..><"){
cout << "i";
}
else if (s == ">.---<<"){
cout << "J";
}
else if (s == ">.---><"){
cout << "j";
}
else if (s == ">-.-<<"){
cout << "K";
}
else if (s == ">-.-><"){
cout << "k";
}
else if (s == ">.-..<<"){
cout << "L";
}
else if (s == ">.-..><"){
cout << "l";
}
else if (s == ">--<<"){
cout << "M";
}
else if (s == ">--><"){
cout << "m";
}
else if (s == ">-.<<"){
cout << "N";
}
else if (s == ">-.><"){
cout << "n";
}
else if (s == ">---<<"){
cout << "O";
}
else if (s == ">---><"){
cout << "o";
}
else if (s == ">.--.<<"){
cout << "P";
}
else if (s == ">.--.><"){
cout << "p";
}
else if (s == ">--.-<<"){
cout << "Q";
}
else if (s == ">--.-><"){
cout << "q";
}
else if (s == ">.-.<<"){
cout << "R";
}
else if (s == ">.-.><"){
cout << "r";
}
else if (s == ">...<<"){
cout << "S";
}
else if (s == ">...><"){
cout << "s";
}
else if (s == ">-<<"){
cout << "T";
}
else if (s == ">-><"){
cout << "t";
}
else if (s == ">..-<<"){
cout << "U";
}
else if (s == ">..-><"){
cout << "u";
}
else if (s == ">...-<<"){
cout << "V";
}
else if (s == ">...-><"){
cout << "v";
}
else if (s == ">.--<<"){
cout << "W";
}
else if (s == ">.--><"){
cout << "w";
}
else if (s == ">-..-<<"){
cout << "X";
}
else if (s == ">-..-><"){
cout << "x";
}
else if (s == ">-.--<<"){
cout << "Y";
}
else if (s == ">-.--><"){
cout << "y";
}
else if (s == ">--..<<"){
cout << "Z";
}
else if (s == ">--..><"){
cout << "z";
}
else if (s == ">.----<"){
cout << "1";
}
else if (s == ">..---<"){
cout << "2";
}
else if (s == ">...--<"){
cout << "3";
}
else if (s == ">....-<"){
cout << "4";
}
else if (s == ">.....<"){
cout << "5";
}
else if (s == ">-....<"){
cout << "6";
}
else if (s == ">--...<"){
cout << "7";
}
else if (s == ">---..<"){
cout << "8";
}
else if (s == ">----.<"){
cout << "9";
}
else if (s == ">-----<"){
cout << "0";
}
else if (s == ">.-.-.-<"){
cout << ",";
}
else if (s == ">---...<"){
cout << ":";
}
else if (s == ">--..--<"){
cout << ",";
}
else if (s == ">-.-.-.<"){
cout << ";";
}
else if (s == ">..--..<"){
cout << "?";
}
else if (s == ">-...-<"){
cout << "=";
}
else if (s == ">.---.<"){
cout << "'";
}
else if (s == ">-..-.<"){
cout << "/";
}
else if (s == ">-.-.--<"){
cout << "!";
}
else if (s == ">-....-<"){
cout << "-";
}
else if (s == ">..--.-<"){
cout << "_";
}
else if (s == ">.-..-.<"){
cout << '"';
}
else if (s == ">-.--.<"){
cout << "(";
}
else if (s == ">-.--.-<"){
cout << ")";
}
else if (s == ">...-..-<"){
cout << "$";
}
else if (s == ">.--.-.<"){
cout << "@";
}
//>....<< >.>< >.-..>< >.-..>< >--->< >.-.-.-< >.--<< >--->< >.-.>< >.-..>< >-..>< >-.-.--<@
//Hello,World!
}
int main(){
help();
string s;
while(1){
cout << "KSS |";
getline(cin,s);
int len = s.size();
string ready = "";
if (s.find('@') == -1 && s.size() != 0){
cout << "Syntax Error"<<endl;
}
else{
cout << "Output:"<<endl;
for (int i = 0;i < len;i++){
if (s[i] == ' '){
run(ready);
ready = "";
}
else if (s[i] == '@'){
run(ready);
break;
}
else{
ready += s[i];
}
}
}
cout << endl;
if (finish_run == 1){
system("cls");
return 0;
}
}
return 0;
}
语法结构:
== | 帮助 |
=///.= | 清屏 |
=//..= | 模版 |
=/...= | 退出 |
>摩斯电码< | 输出(><内写摩斯电码如要打字母在后面加"<"(大写)">"(小写 |