Lit语言

语法

  • def定义整型变量,如def a
  • eq表示赋值,如eq a 5 3 +。注意,变量名后的表达式是后缀表达式而非常用的中缀表达式。
  • scan表示输入,如scan a
  • print表示输出,如print aprint"Hello, Wordprint'a。注意,print"表示输出字符串,print 表示输出变量值,print'表示输出变量对应的字符;
  • lab表示定义标签,跳转用goto,如lab labelgoto label
  • if表示判断,如if a b - 。注意条件变量为后缀表达式,且要用endf结束判断;
  • end表示结束程序。

示例程序

def a
def b
scan a
lab l
eq b a
eq a a 1 -
lab t
print"*
eq b b 1 -
if b
goto t
endf
print'10
if a
goto l
endf
end

这个程序会读入一个\(a\),然后输出。输入输出示例:
输入:5
输出:

*****
****
***
**
*

解释器

#include<bits/stdc++.h>
#define pt st.top();st.pop()
using namespace std;
map<string,int>mp,lab;
struct arr{
	string name;
	int len,a[1005];
}ar[1005],ppq;
int mad;
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=="&"){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=="^"){int x=pt;int y=pt;st.push(y^x);}
			else if(t=="!"){int x=pt;st.push(!x);}
			else if(t=="~"){int x=pt;st.push(~x);}
			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==">="){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=="=="){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=="&&"){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(t.find('[')!=string::npos){
				int pos=t.find('['),jj;
				string q=t.substr(0,pos);
				jj=atoi(t.substr(pos+1).c_str());
				for(int i=1;i<=mad;i++){
					if(ar[i].name==q){
						st.push(ar[i].a[jj]);
						break;
					}
				}
			}
			else st.push(mp[t]);
			t="";
		}
	}
	return st.top();
}
int cnt,nu[10005],pos,p;
string s[1005];
void run(int x){
	for(int i=x;i<=cnt;i++){
		if(s[i].substr(0,3)=="def"){
			string t="";
			for(int j=4;j<s[i].size();j++){
				if(s[i][j]!=' ')t+=s[i][j];
				if(s[i][j]==' '||j==s[i].size()-1)mp[t]=0,t="";
			}
		}
		else if(s[i].substr(0,3)=="daf"){
			string t="";int j=4;
			for(;s[i][j]!=' ';j++)t+=s[i][j];
			int lp=num(s[i].substr(j+1));
			ppq.name=t,ppq.len=lp;ar[++mad]=ppq;
		}
		else if(s[i].substr(0,5)=="print"){
			if(s[i][5]==' ')cout<<num(s[i].substr(6));
			else if(s[i][5]=='\"')cout<<s[i].substr(6);
			else cout<<(char)num(s[i].substr(6));
		}
		else if(s[i].substr(0,4)=="scan"){
			if(s[i].find('[')==string::npos)
			mp[s[i].substr(5)]=nu[++p];
			else{
				string t=s[i].substr(5);
				int pos=t.find('['),jj;
				string q=t.substr(0,pos);
				jj=atoi(t.substr(pos+1).c_str());
				for(int k=1;k<=mad;k++){
					if(ar[k].name==q){
						ar[k].a[jj]=nu[++p];
						break;
					}
				}
			}
		}
		else if(s[i].substr(0,2)=="eq"){
			string t="";int j=3;
			for(;s[i][j]!=' ';j++)t+=s[i][j];
			if(t.find('[')!=string::npos){
				int pos=t.find('['),jj;
				string q=t.substr(0,pos);
				jj=atoi(t.substr(pos+1).c_str());
				for(int k=1;k<=mad;k++){
					if(ar[k].name==q){
						ar[k].a[jj]=num(s[i].substr(j+1));
						break;
					}
				}
			}
			else mp[t]=num(s[i].substr(j+1));
		}
		else if(s[i].substr(0,4)=="goto"){
			run(lab[s[i].substr(5)]+1);
			return;
		}
		else if(s[i].substr(0,2)=="if"){
			string t=s[i].substr(3);
			int ppp=num(t),j=i+1;
			for(;s[j]!="endf";j++);
			if(ppp)run(i+1);
			else run(j+1);
			return;
		}
	}
}
int main(){
	puts("--------<Lit Programming Language>--------");
	puts("Thank you for using this Programming Language!");
	puts("It\'s a little Programming Language, It\'s name is Lit.");
	puts("--------------<CODE>-------------");
	for(int i=1;;i++){
		printf("%3d. | ",i);
		getline(cin,s[++cnt]);
		if(s[cnt]=="end"){
			cnt--;
			break;
		}
	}
	puts("--------------<INPUT>-------------");
	for(int i=1;i<=cnt;i++){
		if(s[i].substr(0,3)=="lab")lab[s[i].substr(4)]=i;
		if(s[i].substr(0,4)=="scan")cin>>nu[++pos];
	}
	puts("--------------<OUTPUT>--------------");
	run(1);
	puts("\n--------------<END>--------------");
	return system("pause");
}