帮帮我
  • 板块灌水区
  • 楼主Magic_ant
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/7/2 04:06
  • 上次更新2024/7/2 11:49:41
查看原帖
帮帮我
1366352
Magic_ant楼主2024/7/2 04:06

我正在写esolang语言 starry 的解释器,还剩下两个指令 ` 和 ' 不会写,能帮帮我吗?
目前代码:

//能完美运行测试starry代码
#include<bits/stdc++.h>
using namespace std;
string code="";
int space=0;
char ch='\0';
vector<int>label(0);
stack<int>sta;
int main(){
    while(ch!='@'){ch=getchar();code+=ch;}
	for(int i=0;i<(int)code.size();i++){
		switch(code[i]){
			case ' ':{
				space++;
				break;
			}
			case '+':{
				switch(space){
					case 0:break;
					case 1:{
						if(sta.empty()){cout<<"Error:too few elements in the stack."<<endl;return 0;}
						sta.push(sta.top());
						break;
					}
					case 2:{
						if((int)sta.size()<2){cout<<"Error:too few elements in the stack."<<endl;return 0;}
						int a=sta.top();sta.pop();
						int b=sta.top();sta.pop();
						sta.push(a);sta.push(b);
						break;
					}
					case 3:{
						if((int)sta.size()<3){cout<<"Error:too few elements in the stack."<<endl;return 0;}
						int a=sta.top();sta.pop();
						int b=sta.top();sta.pop();
						int c=sta.top();sta.pop();
						sta.push(a);sta.push(b);sta.push(c);
						break;
					}
					case 4:{
						if(sta.empty()){cout<<"Error:too few elements in the stack."<<endl;return 0;}
						sta.pop();
						break;
					}
					default:{
						sta.push(space-5);
						break;
					}
				}
				space=0;
				break;
			}
			case '*':{
				switch(space%5){
					case 0:{
						if((int)sta.size()<2){cout<<"Error:too few elements in the stack."<<endl;return 0;}
						int b=sta.top();sta.pop();
						int a=sta.top();sta.pop();
						sta.push(a+b);
						break;
					}
					case 1:{
						if((int)sta.size()<2){cout<<"Error:too few elements in the stack."<<endl;return 0;}
						int b=sta.top();sta.pop();
						int a=sta.top();sta.pop();
						sta.push(a-b);
						break;
					}
					case 2:{
						if((int)sta.size()<2){cout<<"Error:too few elements in the stack."<<endl;return 0;}
						int b=sta.top();sta.pop();
						int a=sta.top();sta.pop();
						sta.push(a*b);
						break;
					}
					case 3:{
						if((int)sta.size()<2){cout<<"Error:too few elements in the stack."<<endl;return 0;}
						int b=sta.top();sta.pop();
						int a=sta.top();sta.pop();
						if(!b){cout<<"Error:divide by zero."<<endl;return 0;}
						sta.push(a/b);
						break;
					}
					case 4:{
						if((int)sta.size()<2){cout<<"Error:too few elements in the stack."<<endl;return 0;}
						int b=sta.top();sta.pop();
						int a=sta.top();sta.pop();
						if(!b){cout<<"Error:divide by zero."<<endl;return 0;}
						sta.push(a%b);
						break;
					}
				}
				space=0;
				break;
			}
			case '.':{
				switch(space%2){
					case 0:{
						cout<<sta.top();
						sta.pop();
						break;
					}
					case 1:{
						cout<<(char)sta.top();
						sta.pop();
						break;
					}
				}
				space=0;
				break;
			}
			case ',':{
				switch(space%2){
					case 0:{
						int temp;
						cin>>temp;
						sta.push(temp);
						break;
					}
					case 1:{
						char temp;
						cin>>temp;
						sta.push((int)temp);
						break;
					}
				}
				space=0;
				break;
			}
		}
	}
    return 0;
}
2024/7/2 04:06
加载中...