RT
code:
#include<iostream>//不会用map
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
#define cmd command[coding_line]
#define ing coding_line
#define CORRECT str[j]!=' '&&str[j]!=';'&&str[j]!='['&&str[j]!=']'&&str[j]!='\n'&&str[j]!='\0'
using namespace std;
struct QWQ{
int t[4];
int types[5];
/*
r1~r4 1~4
e1~e4 5~8
flag 9
val 10
ret 11
line 12
*/
/*
type 0:寄存器
type 1:普通数组use[]
type 2:常量
type 3: use[used[]]
type 4: 当前行数
type 5:callfunc特殊
*/
};
struct FUNCTION{
char name[101];
int st,len;
}funcs[50001];
struct STACK{
int line,num;
};
int LEN,ftot=0;
string str,temp;
char naming[50001][101],cad_[13][10]={"QWQ","r1","r2","r3","r4","e1","e2","e3","e4","flag","val","ret","line"},cad[32][10]={"udef","hlt","nop","set","jmp","jif","call","ret","inv","add","sub","mult","idiv","mod","lsft","rsft","band","bor","bxor","lgr","lls","lge","lle","leql","land","lor","rint","rch","wint","wch","function","callfunc"};
int command[50001],LTH[50001];//指令,指令长度
int vals[32]={0,0,0,2,1,2,0,1,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,1,1,1,1},fill1[32]={0,0,0,0,0,9,0,0,10,10,10,10,10,10,10,10,10,10,10,9,9,9,9,9,9,9,10,10,10,10,0,0},fill2[32];//指令参数填充
stack<STACK>stk;
QWQ used[50001];//指令使用的变量
int use[10000100],r[5]={0,0,0,0,0},e[5]={0,0,0,0,0},flag=0,val=0,ret=0,line=0,coding_line=1;//变量
inline bool check(int kind,int lenth)
{
if(lenth!=strlen(cad[kind]))
return false;
for(int i=0;i<lenth;i++)
if(temp[i]!=cad[kind][i])
return false;
return true;
}
inline bool check_(int kind,int lenth)
{
if(lenth!=strlen(cad_[kind]))
return false;
for(int i=0;i<lenth;i++)
if(temp[i]!=cad_[kind][i])
return false;
return true;
}
inline bool check__(int kind,int Now)
{
if(LTH[Now]!=funcs[kind].len)
return false;
for(int i=0;i<LTH[Now];i++)
if(naming[Now][i]!=funcs[kind].name[i])
return false;
return true;
}
inline void clear(){temp="\0";}
inline int get_V(int t,int type)
{
if(type==0)
{
if(t<=4){return r[t];}
if(t>4&&t<=8){return e[t-4];}
if(t==9){return flag;}
if(t==10){return val;}
if(t==11){return ret;}
if(t==12){return line;}
return 0;
}
if(type==1){return use[t];}
if(type==2){return t;}
if(type==3){return use[get_V(t,0)];}
if(type==4){return coding_line;}
if(type==5){return funcs[t].st;}
return 0;
}
inline void change_V(int t,int type,int valing)
{
if(type==0)
{
if(t<=4){r[t]=valing;return;}
if(t>4&&t<=8){e[t-4]=valing;return;}
if(t==9){flag=valing;return;}
if(t==10){val=valing;return;}
if(t==11){ret=valing;return;}
if(t==12){line=valing;return;}
return;
}
if(type==1){use[t]=valing;return;}
if(type==2){return;}
if(type==3){use[get_V(t,0)]=valing;return;}
if(type==4){coding_line=valing;return;}
if(type==5){funcs[t].st=valing;return;}
return;
}
inline void run()
{
if(cmd==0)
return;
if(cmd==1)
{
ing=LEN;
return;
}
if(cmd==2)
return;
if(cmd==3)
{
change_V(used[ing].t[2],used[ing].types[2],get_V(used[ing].t[1],used[ing].types[1]));
return;
}
if(cmd==4)
{
ing=get_V(used[ing].t[1],used[ing].types[1])+line-1;
return;
}
if(cmd==5)
{
if(get_V(used[ing].t[2],used[ing].t[2]))
ing=get_V(used[ing].t[1],used[ing].types[1])+line-1;
return;
}
if(cmd==6)
{
stk.push((STACK){line,ing+1});
ing=get_V(used[ing].t[1],used[ing].types[1])-1;
return;
}
if(cmd==7)
{
STACK out=stk.top();
stk.pop();
if(used[ing].types[1]!=-1)
ret=get_V(used[ing].t[1],used[ing].types[1]);
line=out.line;
ing=out.num-1;
return;
}
if(cmd==8)
{
int cac=-(get_V(used[ing].t[1],used[ing].types[1]));
change_V(used[ing].t[2],used[ing].types[2],cac);
return;
}
if(cmd==9)
{
int cac=(get_V(used[ing].t[1],used[ing].types[1]))+(get_V(used[ing].t[2],used[ing].types[2]));
change_V(used[ing].t[3],used[ing].types[3],cac);
return;
}
if(cmd==10)
{
int cac=(get_V(used[ing].t[1],used[ing].types[1]))-(get_V(used[ing].t[2],used[ing].types[2]));
change_V(used[ing].t[3],used[ing].types[3],cac);
return;
}
if(cmd==11)
{
int cac=(get_V(used[ing].t[1],used[ing].types[1]))*(get_V(used[ing].t[2],used[ing].types[2]));
change_V(used[ing].t[3],used[ing].types[3],cac);
return;
}
if(cmd==12)
{
int cac=(get_V(used[ing].t[1],used[ing].types[1]))/(get_V(used[ing].t[2],used[ing].types[2]));
change_V(used[ing].t[3],used[ing].types[3],cac);
return;
}
if(cmd==13)
{
int cac=(get_V(used[ing].t[1],used[ing].types[1]))%(get_V(used[ing].t[2],used[ing].types[2]));
change_V(used[ing].t[3],used[ing].types[3],cac);
return;
}
if(cmd==14)
{
int cac=(get_V(used[ing].t[1],used[ing].types[1]))<<(get_V(used[ing].t[2],used[ing].types[2]));
change_V(used[ing].t[3],used[ing].types[3],cac);
return;
}
if(cmd==15)
{
int cac=(get_V(used[ing].t[1],used[ing].types[1]))>>(get_V(used[ing].t[2],used[ing].types[2]));
change_V(used[ing].t[3],used[ing].types[3],cac);
return;
}
if(cmd==16)
{
int cac=(get_V(used[ing].t[1],used[ing].types[1]))&(get_V(used[ing].t[2],used[ing].types[2]));
change_V(used[ing].t[3],used[ing].types[3],cac);
return;
}
if(cmd==17)
{
int cac=(get_V(used[ing].t[1],used[ing].types[1]))|(get_V(used[ing].t[2],used[ing].types[2]));
change_V(used[ing].t[3],used[ing].types[3],cac);
return;
}
if(cmd==18)
{
int cac=(get_V(used[ing].t[1],used[ing].types[1]))^(get_V(used[ing].t[2],used[ing].types[2]));
change_V(used[ing].t[3],used[ing].types[3],cac);
return;
}
if(cmd==19)
{
bool cac=(get_V(used[ing].t[1],used[ing].types[1]))>(get_V(used[ing].t[2],used[ing].types[2]));
change_V(used[ing].t[3],used[ing].types[3],cac);
return;
}
if(cmd==20)
{
bool cac=(get_V(used[ing].t[1],used[ing].types[1]))<(get_V(used[ing].t[2],used[ing].types[2]));
change_V(used[ing].t[3],used[ing].types[3],cac);
return;
}
if(cmd==21)
{
bool cac=(get_V(used[ing].t[1],used[ing].types[1]))>=(get_V(used[ing].t[2],used[ing].types[2]));
change_V(used[ing].t[3],used[ing].types[3],cac);
return;
}
if(cmd==22)
{
bool cac=(get_V(used[ing].t[1],used[ing].types[1]))<=(get_V(used[ing].t[2],used[ing].types[2]));
change_V(used[ing].t[3],used[ing].types[3],cac);
return;
}
if(cmd==23)
{
bool cac=(get_V(used[ing].t[1],used[ing].types[1]))==(get_V(used[ing].t[2],used[ing].types[2]));
change_V(used[ing].t[3],used[ing].types[3],cac);
return;
}
if(cmd==24)
{
bool cac=(get_V(used[ing].t[1],used[ing].types[1]))&&(get_V(used[ing].t[2],used[ing].types[2]));
change_V(used[ing].t[3],used[ing].types[3],cac);
return;
}
if(cmd==25)
{
bool cac=(get_V(used[ing].t[1],used[ing].types[1]))||(get_V(used[ing].t[2],used[ing].types[2]));
change_V(used[ing].t[3],used[ing].types[3],cac);
return;
}
if(cmd==26)
{
int cac;
scanf("%d",&cac);
change_V(used[ing].t[1],used[ing].types[1],cac);
return;
}
if(cmd==27)
{
char cac=getchar();
change_V(used[ing].t[1],used[ing].types[1],cac);
return;
}
if(cmd==28)
{
printf("%d",(int){get_V(used[ing].t[1],used[ing].types[1])});
return;
}
if(cmd==29)
{
putchar(get_V(used[ing].t[1],used[ing].types[1]));
return;
}
if(cmd==31)
{
ing=funcs[get_V(used[ing].t[1],used[ing].types[1])].st-1;
return;
}
}
/*
所有变量均为整形
function 定义函数起点 30
callfunc 调用函数 31
udef 未定义 0
hlt 停机 1
nop 空指令 2
set <0> <1> 将<0>赋值给<1> 3
jmp <0> 跳至第<0>条语句 4
jif <0> <1> 如果(<1>存在且为真||<1>不存在且%flag为真),跳转至第<0>条语句 5
call <0> 将下一条语句的序号压入栈,跳转至第<0>条语句 6
ret <0> 将栈顶元素<1>取出,跳转至第<1>条语句,将<0>放入%ret 7
-----
以下操作结果放入<END>,若<END>不存在则放入%val
inv <0> <END> 取<0>的相反数 8
add <0> <1> <END> 计算<0>+<1> 9
sub <0> <1> <END> 计算<0>-<1> 10
mult <0> <1> <END> 计算<0>*<1> 11
idiv <0> <1> <END> 计算<0>/<1> 12
mod <0> <1> <END> 计算<0>%<1> 13
lsft <0> <1> <END> 计算(<0> << <1>) 14
rsft <0> <1> <END> 计算(<0> >> <1>) 15
band <0> <1> <END> 计算(<0>&<1>) 16
bor <0> <1> <END> 计算(<0>|<1>) 17
bxor <0> <1> <END> 计算(<0>^<1>) 18
以下操作结果放入<END>,若<END>不存在则放入%flag
lgr <0> <1> <END> 计算(<0> > <1>) 19
lls <0> <1> <END> 计算(<0> < <1>) 20
lge <0> <1> <END> 计算(<0> >= <1>) 21
lle <0> <1> <END> 计算(<0> <= <1>) 22
leql <0> <1> <END> 计算(<0> == <1>) 23
land <0> <1> <END> 计算(<0> && <1>) 24
lor <0> <1> <END> 计算(<0> || <1>) 25
以下操作结果放入<0>,若<0>不存在则<0>为%val
rint <0> 读入一个整数 26
rch <0> 读入一个字符 27
wint <0> 将<0>的数值输出 28
wch <0> 将<0>这个字符输出 29
*/
int main()
{
memset(use,0,sizeof(use));
memset(fill2,0,sizeof(fill2));
while(!stk.empty())
stk.pop();
int left=0,nowtype=0,lent,sum=0;
bool funcing=false,back_STARTONLY=true;
queue<int>callfuncs;
while(!callfuncs.empty())
callfuncs.pop();
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>LEN;
for(int i=1;i<=LEN;i++)
{
getline(cin,str);
if(back_STARTONLY&&str=="")
{
back_STARTONLY=false;
i--;
continue;
}
nowtype=0;
sum=0;
for(int j=0;j<str.size();j++)
{
if(str[j]=='['){left++;continue;}
if(left){if(str[j]==']')left--;continue;}
if(str[j]==' '||str[j]==';')continue;
lent=0;clear();
if(nowtype==0)
{
while(j<str.size()&&CORRECT)
{
temp[lent]=str[j];
j++;
lent++;
}
for(int k=0;k<=31;k++)
if(check(k,lent))
{
if(k==30||k==31)
funcing=true;
command[i]=k;
nowtype=k;
if(nowtype==7)
used[i].types[1]=-1;
break;
}
continue;
}
else
{
if(funcing)
{
while(str[j]!='$')
j++;
j++;
sum++;
if(nowtype==30)
{
ftot++;
while(j<str.size()&&CORRECT)
{
funcs[ftot].name[lent]=str[j];
j++;
lent++;
}
funcs[ftot].st=i+1;
funcs[ftot].len=lent;
funcing=false;
continue;
}
else
{
while(j<str.size()&&CORRECT)
{
naming[i][lent]=str[j];
j++;
lent++;
}
LTH[i]=lent;
callfuncs.push(i);
funcing=false;
continue;
}
}
if(str[j]=='%')
{
sum++;
j++;
while(j<str.size()&&CORRECT)
{
temp[lent]=str[j];
j++;
lent++;
}
for(int k=1;k<=12;k++)
if(check_(k,lent))
{
used[i].t[sum]=k;
used[i].types[sum]=0;
break;
}
}
else
{
if(str[j]=='@')
{
if(str[j+1]=='%')
{
sum++;
j+=2;
while(j<str.size()&&CORRECT)
{
temp[lent]=str[j];
j++;
lent++;
}
for(int k=1;k<=12;k++)
if(check_(k,lent))
{
used[i].t[sum]=k;
used[i].types[sum]=3;
break;
}
}
else
{
j++;
sum++;
int cache=0;
while(j<str.size()&&CORRECT)
{
cache=cache*10+(str[j]-'0');
j++;
}
used[i].t[sum]=cache;
used[i].types[sum]=1;
}
}
else
{
if(str[j]=='#')
{
sum++;
j++;
while(j<str.size()&&CORRECT)
{
temp[lent]=str[j];
j++;
lent++;
}
used[i].t[sum]=0;
used[i].types[sum]=4;
}
else
{
if(str[j]>='0'&&str[j]<='9')
{
sum++;
int cache=0;
while(j<str.size()&&CORRECT)
{
cache=cache*10+(str[j]-'0');
j++;
}
used[i].t[sum]=cache;
used[i].types[sum]=2;
}
else
continue;
}
}
}
}
}
if(sum<vals[nowtype])
{
sum++;
used[i].t[sum]=fill1[nowtype];
used[i].types[sum]=fill2[nowtype];
}
nowtype=0;
sum=0;
}
while(!callfuncs.empty())
{
clear();
int now=callfuncs.front();
callfuncs.pop();
for(int i=1;i<=ftot;i++)
if(check__(i,now))
{
used[now].t[1]=i;
used[now].types[1]=5;
break;
}
}
ing=1;
while(ing<=LEN)
{
run();
ing++;
}
return 0;
}