自己写的REPL
查看原帖
自己写的REPL
629944
wangzhaohan2910楼主2024/5/3 09:13

我今天写了一个针对这门语言的REPL,可能有bug,望大神指教! 以下是代码:

#include<bits/stdc++.h>
using namespace std;
int p[1001];
string r[1001];
int main()
{
	int n,t;
	cin>>t;
	cin.get();
	for(int i{1};i<=t;i++)
		getline(cin,r[i]);
	cin>>n;
	p[1]=n;
	for(int i{1};i<=t;i++)
	{
		int x,y,z;
		string t,u;
		stringstream ss;
		ss<<r[i];
		ss>>t>>x;
		//cout<<i<<endl;
		if(t=="new")
			n++,p[x]=n;
		else if(t=="dec")
			p[x]--;
		else if(t=="assign")
			ss>>y,p[x]=p[y];
		else if(t=="iftry")
		{
			ss>>u>>y;
			if(p[x]>=0)
				i=y-1;
		}
		else if(t=="ifeq")
		{
			ss>>y>>u>>z;
			if(p[x]==p[y])
				i=z-1;
		}
		else if(t=="ifneq")
		{
			ss>>y>>u>>z;
			if(p[x]!=p[y])
				i=z-1;
		}
	}
	cout<<n;
	return 0;
}
2024/5/3 09:13
加载中...