Lusp语言

发布时间 2023-07-17 20:37:43作者: 夏一锐的blog

解释器

#include<bits/stdc++.h>
#define pt st.top();st.pop()
using namespace std;
map<string,int>mp;
struct node{
	string name,body[105];
	int cnt;
};
vector<node>e;
int num(string u){
	stack<int>st;
	string t;
	for(int i=0;i<u.size();i++){
		if(u[i]!=' ')t+=u[i];
		if(u[i]==' '||i==u.size()-1){
			if(t=="+"){
				int x=pt;int y=pt;
				st.push(x+y);
			}
			else if(t=="-"){
				int x=pt;int y=pt;
				st.push(y-x);
			}
			else if(t=="*"){
				int x=pt;int y=pt;
				st.push(x*y);
			}
			else if(t=="/"){
				int x=pt;int y=pt;
				st.push(y/x);
			}
			else if(t=="%"){
				int x=pt;int y=pt;
				st.push(y%x);
			}
			else if(t[0]>='0'&&t[0]<='9')st.push(atoi(t.c_str()));
			else{
				if(!mp.count(t)){
					cout<<"Error!\n";
					return-1;
				}
				else st.push(mp[t]);
			}
			t="";
		}
	}
	if(st.size()!=1){
		cout<<"Error!\n";
		return -1;
	}
	return st.top();
}
void go(string s){
	if(s.substr(0,3)=="def"){
		mp[s.substr(4)]=0;
		cout<<"define:"<<s.substr(4)<<endl;
	}
	else if(s.substr(0,2)=="eq"){
		string t;int i=3;
		for(;s[i]!=' ';i++)t+=s[i];
		if(!mp.count(t)){
			cout<<"Error:\""<<t<<"\" undefined.\n";
		}
		else cout<<"number "<<t<<":"<<(mp[t]=num(s.substr(i+1)))<<"\n";
	}
	else if(s.substr(0,5)=="print"){
		cout<<"print ";
		if(s[5]=='\"')cout<<s.substr(6)<<endl;
		else{
			string t=s.substr(6);
			cout<<t<<":";
			if(!mp.count(t)){
				cout<<"Error:\""<<t<<"\" undefined.\n";
			}
			else if(s[5]=='\'')cout<<(char)mp[t]<<endl;
			else cout<<mp[t]<<endl;
		}
	}
	else if(s.substr(0,4)=="scan"){
		string t=s.substr(5);
		if(!mp.count(t)){
			cout<<"Error:\""<<t<<"\" undefined.\n";
		}
		else{
			cout<<"Please scan a number:";
			int a;cin>>a;
			mp[t]=a;	
		}
	}
	else if(s.substr(0,3)=="end")exit(puts("Byebye!Hope you use Lusp again."));
	else if(s.substr(0,2)=="if"){
		if(num(s.substr(3))){
			puts("then do:");
			while(1){
				getline(cin,s);
				if(s=="endif")break;
				go(s); 
			}
		}
		else{
			puts("else do:");
			while(1){
				getline(cin,s);
				if(s=="endif")break;
				go(s); 
			}
		}
	}
	else if(s.substr(0,5)=="while"){
		puts("do:");int cnt=0;
		string u=s.substr(6),t[105];
		while(1){
			getline(cin,t[++cnt]);
			if(t[cnt]=="endw")break;
		}
		--cnt;
		while(num(u)){
			for(int i=1;i<=cnt;i++){
				go(t[i]);
			}
		}
	}
	else if(s.substr(0,4)=="deun"){
		string t=s.substr(5),b[105],q;
		puts("body:");int cnt=0;
		node a;
		while(1){
			getline(cin,q);
			if(q=="endf")break;
			b[++cnt]=q;
		}
		a.name=t;a.cnt=cnt;
		for(int i=1;i<=cnt;i++)a.body[i]=b[i];
		e.push_back(a);
		cout<<"define func:"<<t<<".\n";
	}
	else if(s.substr(0,4)=="func"){
		string t=s.substr(5);
		cout<<"do func:"<<t<<endl;
		for(int i=0;i<e.size();i++){
			if(e[i].name==t){
				for(int j=1;j<=e[i].cnt;j++){
					go(e[i].body[j]);
				}
			}
		}
	}
}
string s;
int main(){
	puts("--------<Lusp Programming Language>--------");
	puts("Thank you for using this Programming Language!");
	puts("It\'s a little Programming Language, It\'s name is Lusp.");
	puts("");while(1){
		printf("Lusp>>");
		getline(cin,s); 
		go(s);
	}
	return 0;
}

语法自己看。