跳到正文

个人记录

编译器2

summer0703约 9 分钟
原文

这是 2025年9月20日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2025年9月20日
收录
2026年5月24日
更新
2026年11月11日
篇幅
约 9 分钟
获赞
1

版本

  1. v9当前
  2. v8
  3. v7
  4. v6
更早的 5 个版本
  1. v5
  2. v4
  3. v3
  4. v2
  5. v1
C++
#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;
}