萌新MLE求调,码风良好
查看原帖
萌新MLE求调,码风良好
646208
Hughpig楼主2025/1/16 16:11
#include<bits/stdc++.h>
using namespace std;

map<string,int> var,func;
stack<pair<int,int> > addr;
vector<string> command;

int n,r;
string line[50007];

vector<string> analyze(int l){
	char ch;
	string s;
	vector<string> ret;
	int cnt=0;
	for(char c:line[l]){
		if(c=='[')++cnt;
		if(c==']'){
			--cnt;
			continue;
		}
		if(cnt>0)continue;
		if(c==' '||c==','||c==';'){
			if(s.size())ret.push_back(s);
			s="";
		}
		else s+=c;
	}
	if(s.size())ret.push_back(s);
	return ret;
}

int getval(string s){
	int ret=0;
	for(auto c:s){
		if(!isdigit(c))return var[s];
		ret=ret*10+c-'0';
	}
	return ret;
}

void save(int pos,int val){
	if(command.size()>pos){
		var[command[pos]]=val;
	}
	else{
		var["%val"]=val;
	}
}

void savel(bool val){
	if(command.size()>3)var[command[3]]=val;
	else{
		var["%flag"]=val;
	}
}

void run(int l){
	if(l>=n)return;
	command=analyze(l);
	//cout<<command.size()<<'\n';
	//for(auto x:command)cout<<x<<' ';
	string c0=command[0];
	for(auto &c:c0)c=tolower(c);
	if(c0=="function"){
		var["%line"]=l;
		func[command[1]]=l;
		run(l+1);
	}
	else if(c0=="callfunc"){
		addr.push(make_pair(l+1,getval("%line")));
		run(func[command[1]]);
	}
	else if(c0=="udef"){
		run(l+1);
	}
	else if(c0=="hlt"){
		exit(0);
	}
	else if(c0=="nop"){
		run(l+1);
	}
	else if(c0=="set"){
		if(command.size()<3){
			cout<<"Runtime Error\n";
			exit(0);
		}
		string c1=command[1],c2=command[2];
		var[c2]=getval(c1);
		run(l+1);
	}
	else if(c0=="jmp"){
		string c1=command[1];
		int x=getval(c1);
		run(x);
	}
	else if(c0=="jif"){
		if((command.size()>2&&getval(command[2]))||(command.size()==2&&getval("%Ffag"))){
			string c1=command[1];
			int x=getval(c1);
			run(x);
		}
	}
	else if(c0=="call"){
		addr.push(make_pair(l+1,getval("%line")));
		run(getval(command[1]));
	}
	else if(c0=="ret"){
		auto p=addr.top();
		addr.pop();
		var["%line"]=p.second;
		if(command.size()>1)var["%ret"]=getval(command[1]);
		return;
	}
	else if(c0=="inv"){
		save(2,-getval(command[1]));
		run(l+1);
	}
	else if(c0=="add"){
		save(3,getval(command[1])+getval(command[2]));
		run(l+1);
	}
	else if(c0=="sub"){
		save(3,getval(command[1])-getval(command[2]));
		run(l+1);
	}
	else if(c0=="mult"){
		save(3,getval(command[1])*getval(command[2]));
		run(l+1);
	}
	else if(c0=="idiv"){
		save(3,getval(command[1])/getval(command[2]));
		run(l+1);
	}
	else if(c0=="mod"){
		save(3,getval(command[1])%getval(command[2]));
		run(l+1);
	}
	else if(c0=="lsft"){
		save(3,getval(command[1])<<getval(command[2]));
		run(l+1);
	}
	else if(c0=="rsft"){
		save(3,getval(command[1])>>getval(command[2]));
		run(l+1);
	}
	else if(c0=="band"){
		save(3,getval(command[1])&getval(command[2]));
		run(l+1);
	}
	else if(c0=="bor"){
		save(3,getval(command[1])|getval(command[2]));
		run(l+1);
	}
	else if(c0=="bxor"){
		save(3,getval(command[1])^getval(command[2]));
		run(l+1);
	}
	else if(c0=="lgr"){
		int c1=getval(command[1]),c2=getval(command[2]);
		savel(c1>c2);
		run(l+1);
	}
	else if(c0=="lls"){
		int c1=getval(command[1]),c2=getval(command[2]);
		savel(c1<c2);
		run(l+1);
	}
	else if(c0=="lge"){
		int c1=getval(command[1]),c2=getval(command[2]);
		savel(c1>=c2);
		run(l+1);
	}
	else if(c0=="lle"){
		int c1=getval(command[1]),c2=getval(command[2]);
		savel(c1<=c2);
		run(l+1);
	}
	else if(c0=="leql"){
		int c1=getval(command[1]),c2=getval(command[2]);
		savel(c1==c2);
		run(l+1);
	}
	else if(c0=="land"){
		int c1=getval(command[1]),c2=getval(command[2]);
		savel(c1&&c2);
		run(l+1);
	}
	else if(c0=="lor"){
		int c1=getval(command[1]),c2=getval(command[2]);
		savel(c1||c2);
		run(l+1);
	}
	else if(c0=="rint"){
		int x;cin>>x;
		save(1,x);
		run(l+1);
	}
	else if(c0=="rch"){
		char c;cin>>c;
		save(1,c);
		run(l+1);
	}
	else if(c0=="wint"){
		if(command.size()>1)cout<<getval(command[1]);
		else cout<<getval("%val");
		run(l+1);
	}
	else if(c0=="wch"){
		if(command.size()>1)putchar(getval(command[1]));
		else putchar(getval("%val"));
		run(l+1);
	}
}

int main(){
	cin>>n;
	getline(cin,line[0]);
	for(int i=0;i<n;++i)getline(cin,line[i]);
	run(0);
}
2025/1/16 16:11
加载中...