#include<bits/stdc++.h>
#define pre(i, l, r) for(int i = (l); i <= (r); ++i)
#define fpre(i, r, l) for(int i = (r); i >= (l); --i)
#define fr(i, a) for(int _ = (a).size(), i = 0; i < _; ++i)
#define ll long long
using namespace std;
const int maxn = 3e5 + 5;
const int inf = 1e9;
char gc(void){ static char *p1, *p2, buf[1000000];return (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++);}
template<typename T> inline void S(T &x){x = 0;int f = 1;char ch = gc();while(!isdigit(ch)){if(ch == '-'){
f = -1;}ch = gc();}; while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = gc();x *= f;}
template<typename type, typename... T>
inline void S(type &x, T&...y) {S(x); S(y...);}
template<typename T = int>T S(void) {T x;S(x);return x;}
map<string,int> f;
int cnt = 0;
ll value[maxn];
string st[maxn] =
{
};
string str(string s, int x)
{
string ans = "";
int sz = s.size();
while(x < sz && s[x] == ' ') x++;
while(x < sz && s[x] != ' ') ans += s[x++];
return ans;
}
ll strdigit(string s, int x)
{
ll ans = 0;
int sz = s.size();
int f = 1;
while(x < sz && !isdigit(s[x]))
{
if(s[x] == '-') f = 0;
x++;
}
while(x < sz && isdigit(s[x])) ans = ans * 10 + (s[x++] ^ 48);
if(!f) ans = -ans;
return ans;
}
int main()
{
int n = 0;
while(st[n] != "") ++n;
if(!n)
{
while(getline(cin, st[n++]));
n--;
}
pre(i, 1, n)
{
string s = st[i - 1];
string op = str(s, 0);
if(op == "int")
{
string t = str(s, 3);
f[t] = ++cnt;
string val = str(s, 4 + t.size());
if(val == "") val = "0";
bool ok = true;
for(auto y : val)
if(!isdigit(y)) ok = false;
if(ok) f[val] = ++cnt, value[cnt] = stoll(val);
value[f[t]] = value[f[val]];
}
else
if(op == "write")
{
string a = str(s, 5);
bool ok = true;
for(auto y : a)
if(!isdigit(y)) ok = false;
if(ok) f[a] = ++cnt, value[cnt] = stoll(a);
printf("%lld\n", value[f[str(s, 5)]]);
}
else
if(op == "read")
scanf("%lld", &value[f[str(s, 4)]]);
else
if(op == "add" || op == "sub" || op == "mul" || op == "div" || op == "mod")
{
/*
0 1 2 3 4 5 6 7 8 9
a d d t t 5 2 3
*/
string r = str(s, 3);
string t = str(s, 4 + r.size());
bool ok = true;
for(auto y : t)
if(!isdigit(y)) ok = false;
if(ok) f[t] = ++cnt, value[cnt] = stoll(t);
if(op == "add")
value[f[r]] += value[f[t]];
if(op == "sub")
value[f[r]] -= value[f[t]];
if(op == "mul")
value[f[r]] *= value[f[t]];
if(op == "div")
value[f[r]] /= value[f[t]];
if(op == "mod")
value[f[r]] %= value[f[t]];
}
else
if(op == "copy")
{
string a = str(s, 4);
string b = str(s, 5 + a.size());
bool ok = true;
for(auto y : b)
if(!isdigit(y)) ok = false;
if(ok) f[b] = ++cnt, value[cnt] = stoll(b);
value[f[a]] = value[f[b]];
}
else
if(op == "goto")
{
i = strdigit(s, 4) - 1;
}
else
/*
0 1 2 3 4 5 6 7 8 9
i f a < b 6
*/
if(op == "if")
{
string a = str(s, 2);
string c = str(s, 3 + a.size());
string b = str(s, 4 + c.size() + a.size());
bool ok = true;
for(auto y : a)
if(!isdigit(y)) ok = false;
if(ok) f[a] = ++cnt, value[cnt] = stoll(a);
ok = true;
for(auto y : b)
if(!isdigit(y)) ok = false;
if(ok) f[b] = ++cnt, value[cnt] = stoll(b);
int t = strdigit(s, 5 + a.size() + b.size() + c.size());
if(c == "=")
{
if(value[f[a]] == value[f[b]]) i = t - 1;
}
if(c == "!=")
{
if(value[f[a]] != value[f[b]]) i = t - 1;
}
if(c == "<=")
{
if(value[f[a]] <= value[f[b]]) i = t - 1;
}
if(c == ">=")
{
if(value[f[a]] >= value[f[b]]) i = t - 1;
}
if(c == "<")
{
if(value[f[a]] < value[f[b]]) i = t - 1;
}
if(c == ">")
{
if(value[f[a]] > value[f[b]]) i = t - 1;
}
}
else
if(op == "delete")
f[str(s, 6)] = 0;
else
if(op == "swap")
{
string a = str(s, 4);
string b = str(s, 5 + a.size());
swap(f[a], f[b]);
}
else
if(op == "putchar")
printf("%s", s.substr(8, s.size()).c_str());
}
return 0;
}编译器2
这是 2025年9月20日 存档的第 1 版,不是当前版本。查看当前版本