Lut语言与Lit语言差不多,将Lit语言的后缀表达式换成普通的中缀表达式,但删去了有关数组的内容。
解释器:
#include<bits/stdc++.h>
#define pt st.top();st.pop()
using namespace std;
map<string,int>mp,lab;
namespace expr{
struct Opt{
char name;
int stackInJb,stackOutJb;
Opt(char name,int in,int out){
this->name=name;
this->stackInJb=in;
this->stackOutJb=out;
}
bool cmp(Opt*opt){
return this->stackOutJb>opt->stackInJb;
}
};
map<char,Opt*>maps;
void mapOpt(){
maps['%']=new Opt('%',2,2);
maps['/']=new Opt('/',2,2);
maps['*']=new Opt('*',2,2);
maps['+']=new Opt('+',1,1);
maps['-']=new Opt('-',1,1);
maps['(']=new Opt('(',0,4);
maps[')']=new Opt(')',-1,-1);
}
int calc(string s){
mapOpt();
stack<int>numStack;
stack<char>optStack;
char exps[20]="(";
strcat(exps,s.c_str());
exps[strlen(exps)]=')';
optStack.push(exps[0]);
Opt*opt,*opt_;
for(int i=1;exps[i];){
if(isalpha(exps[i])){
string names="";
while(isalpha(exps[i]))names+=exps[i],i++;
numStack.push(mp[names]);
}else if(!isdigit(exps[i])){
opt=maps[optStack.top()];
opt_=maps[exps[i]];
if(opt_->name==')'&&opt->name=='('){
optStack.pop();
i++;
continue;
}
bool com=opt_->cmp(opt);
if(com){
optStack.push(opt_->name);
i++;
}else{
char n=opt->name;
optStack.pop();
int res,optNum1=numStack.top();
numStack.pop();
int optNum2=numStack.top();
numStack.pop();
if(n=='*')
res=optNum2*optNum1;
else if(n=='+')
res=optNum2+optNum1;
else if(n=='-')
res=optNum2-optNum1;
else if(n=='/')
res=optNum2*optNum1;
else if(n=='%')
res=optNum2%optNum1;
numStack.push(res);
}
}else{
int num=0;
while(isdigit(exps[i]))num=num*10+exps[i]-48,i++;
numStack.push(num);
}
}
return numStack.top();
}
}
using namespace expr;
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,5)=="print"){
if(s[i][5]==' ')cout<<calc(s[i].substr(6));
else if(s[i][5]=='\"')cout<<s[i].substr(6);
else cout<<(char)calc(s[i].substr(6));
}
else if(s[i].substr(0,4)=="scan"){
mp[s[i].substr(5)]=nu[++p];
}
else if(s[i].substr(0,2)=="eq"){
string t="";int j=3;
for(;s[i][j]!=' ';j++)t+=s[i][j];
mp[t]=calc(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=calc(t),j=i+1;
for(;s[j]!="endf";j++);
if(ppp)run(i+1);
else run(j+1);
return;
}
}
}
int main(){
puts("--------<Lut Programming Language>--------");
puts("Thank you for using this Programming Language!");
puts("It\'s a little Programming Language, It\'s name is Lut.");
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");
}
本文来自博客园,作者:夏一锐,转载请注明原文链接:https://www.cnblogs.com/2021changqing52/p/17573579.html
(不过似乎不会有人转载)