跳到正文

个人记录

如何用c++写一个c++编译器

yujincheng约 5 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月24日
篇幅
约 5 分钟

版本

  1. v1当前

粉碎粉碎机的粉碎机。

首先我们设计一个编译器,必须要有输入的功能吧。

C++
#include <bits/stdc++.h>
using namespace std;
string str;
int main()
{
	while(true)
	{
		char ch=getchar();
		str+=ch;
	}
}

输入完一定也可以保存吧。

C++
#include <bits/stdc++.h>
#include <pthread.h>
#include <windows.h>
using namespace std;
string str,s;
void*function1(void* arg)
{
	while(true)
	{
		char ch=getchar();
		str+=ch;
	}
}
void*function2(void* arg)
{
	while(true)
	{
		if((GetAsyncKeyState(VK_CONTROL)&0x8000)&&(GetAsyncKeyState('S')&0x8000))
		{
			system("cls");
			cout<<str;
		}
	}
}
int main()
{
	cout<<"请问您想把代码存到哪里?";
	cin>>s;
	system("cls");
	if(s[s.size()-4]=='.')
		s.erase(s.end()-4,s.end());
	freopen(s.c_str(),"w",stdout);
	pthread_t thread1, thread2;
	pthread_create(&thread1,NULL,function1,NULL);
	pthread_create(&thread2,NULL,function2,NULL);
	pthread_join(thread1,NULL);
	pthread_join(thread2,NULL);
}

写完了也要可以关闭吧。

C++
#include <bits/stdc++.h>
#include <pthread.h>
#include <windows.h>
using namespace std;
string str,s;
void*function1(void* arg)
{
	while(true)
	{
		char ch=getchar();
		str+=ch;
	}
}
void*function2(void* arg)
{
	while(true)
	{
	    if((GetAsyncKeyState(VK_CONTROL)&0x8000)&&(GetAsyncKeyState('S')&0x8000))
	    {
	    	system("cls");
	    	cout<<str;
	    }
	    if((GetAsyncKeyState(VK_CONTROL)&0x8000)&&(GetAsyncKeyState('W')&0x8000))
	    {
	    	system("cls");
	    	cout<<str;
	    	exit(0);
	    }
	}
}
int main()
{
  	cout<<"请问您想把代码存到哪里?";
  	cin>>s;
  	system("cls");
  	if(s[s.size()-4]=='.')
    	s.erase(s.end()-4,s.end());
  	freopen(s.c_str(),"w",stdout);
  	pthread_t thread1, thread2;
	pthread_create(&thread1,NULL,function1,NULL);
	pthread_create(&thread2,NULL,function2,NULL);
	pthread_join(thread1,NULL);
	pthread_join(thread2,NULL);
}

写错了总要可以删除吧?

C++
#include <bits/stdc++.h>
#include <pthread.h>
#include <windows.h>
using namespace std;
string str,s;
void*function1(void* arg)
{
	while(true)
	{
		char ch=getchar();
		str+=ch;
	}
}
void*function2(void* arg)
{
  	while(true)
	{
	    if((GetAsyncKeyState(VK_CONTROL)&0x8000)&&(GetAsyncKeyState('S')&0x8000))
	    {
	    	system("cls");
	    	cout<<str;
	    }
	    if((GetAsyncKeyState(VK_CONTROL)&0x8000)&&(GetAsyncKeyState('W')&0x8000))
	    {
	      	system("cls");
	    	cout<<str;
			exit(0);
	    }
	    if(GetAsyncKeyState(VK_BACK)&0x8000)
	    	str.erase(str.end()-1);
	}
}
int main()
{
	cout<<"请问您想把代码存到哪里?";
	getline(cin,s);
	system("cls");
	if(s.size()<=4)
		s+=".cpp";
	else if(s[s.size()-4]!='.')
		s+=".cpp";
	freopen(s.c_str(),"w",stdout);
	pthread_t thread1, thread2;
	pthread_create(&thread1,NULL,function1,NULL);
	pthread_create(&thread2,NULL,function2,NULL);
	pthread_join(thread1,NULL);
	pthrthread_join(thread2,NULL);
}
原文