⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 compile.cpp

📁 与vc++界面十分类似的词法分析器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Compile.cpp: implementation of the CCompile class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "cff.h"
#include "Compile.h"
#include "EditBar.h"
//#include "data.h"
#include <io.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

#define NUM       500
#define Loop(i)   for(;i<sizewidth;i++){if(isdigit(constant[i]))continue;else break;}
#define Loop1(i)  for(;i<sizewidth;i++){if((constant[i]>='0'&&constant[i]<='9')||(constant[i]>='A'&&constant[i]<='F')||(constant[i]>='a'&&constant[i]<='f'))continue;else break;}
#define Loop2(i)  for(;i<sizewidth;i++) {if(constant[i]>='0'&&constant[i]<='7')continue; else break;}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CCompile::CCompile()
{
	start=search=0;
	Buffer=new char[NUM+1];
	memset(Buffer,32,NUM);
	Buffer[NUM]='@';
	errorlist.RemoveAll();
	m_time=2;
}

CCompile::~CCompile()
{
	theData.RemoveAll();
	errorlist.RemoveAll();
	constantlist.RemoveAll();
	identifierlist.RemoveAll();
}

int CCompile::DoFirst()
{
	char ch;
	//search++;//好像有点问题?
	if(Buffer[search]=='@')
		search=0;
	if(search==start)
	{
		ch=Check();
		if(!ch)//文件多完了!
		{
			Buffer[search]=' ';//在结尾加一个空格来标志
			search++;
			return 0;
		}
		else{
			Buffer[search]=ch;
			search++;
		}
	}
	while(search!=start)
	{
		if(Buffer[search]=='@')
			search=0;
		else{
			ch=Check();
			if(!ch)
			{
				Buffer[search]=' ';//在结尾加一个空格来标志
				search++;
				break;
			}
			else{
				Buffer[search]=ch;
				search++;
			}
		}
	}
	if(!search)
		search=NUM-1;
	else search--;
	return 1;

}

char CCompile::Check()
{
	char ch=0;
	while(!ch&&!feof(fp))
	{
		char ch1;
		if(!fread(&ch,1,1,fp))
		{
			ch=0;break;
		}
		switch(ch)
		{
		case '\t':
		case '\f':
		case '\r':
			ch=0;
			break;
		case '\n':
			ch=' ';
			break;
		case ' '://空格
			ch1=' ';
			while(ch1==' '&&!feof(fp))		
				fread(&ch1,1,1,fp);
			if(!feof(fp))
			    fseek(fp,-1,SEEK_CUR);//文件指针后退一个字符
			break;
		case '*':
			ch1=0;
			if(!fread(&ch1,1,1,fp))
				break;
			if(ch1=='/')
				errorlist.Add(CString("注释/*与*/不匹配!多个*/。"));
			else fseek(fp,-1,SEEK_CUR);
			break;
		case '/':
			if(!fread(&ch1,1,1,fp))break;
			switch(ch1)
			{
			case '/':
				while(ch1!='\n')
					fread(&ch1,1,1,fp);
				ch=' ';
				break;
			case '*':
				char ch2;
				fread(&ch2,1,1,fp);fread(&ch1,1,1,fp);
				long state;
				state=ftell(fp);
				while((ch1!='/'||ch2!='*')&&!feof(fp))
				{
					ch2=ch1;
					fread(&ch1,1,1,fp);
				}
				if(feof(fp))
				{
					errorlist.Add(CString("注释/*与*/不匹配!"));
					long temp=ftell(fp);
					fseek(fp,state-temp,SEEK_CUR);
				}
				ch=0;
				break;
			default:
				fseek(fp,-1,SEEK_CUR);
			}
			break;
		}
	}
	if(feof(fp)&&m_time)
	{
		m_time--;
		if(m_time==0)return ch;
		else{ch=' ';return ch;}
	}
	else return ch;
}

BOOL CCompile::Compile(CString filepath,CString compilefile)
{
	fp=fopen(filepath,"rb");
	Analyse();
	fclose(fp);

	CFont m_font;
	if (!m_font.CreateStockObject(ANSI_FIXED_FONT))
       if (!m_font.CreatePointFont(20, "华文行楷"))
            return -1;
	CEditBar *m_EditBar;
	m_EditBar=theApp.m_pMainFrame->GetEditBar();
	m_EditBar->m_wndChild.SetFont(&m_font);

	CString temp="--------------------Configuration: ";
	temp+=theApp.m_WorkName;
	temp+=" - Win32 Debug--------------------\r\r\n";
	temp+="Compiling...\r\r\n";
	temp+="Compiling.cff";

	int sizewidth=errorlist.GetSize();
	if(!sizewidth)
	{
		WriteResult();
		CFile file;
		file.Open(compilefile,CFile::typeBinary|CFile::modeReadWrite|
			CFile::shareDenyWrite|CFile::modeCreate);
		CArchive ar(&file,CArchive::store);
		theData.Serialize(ar);
		ar.Close();
		file.Close();

		temp+="\r\r\n";
		temp+="Linking...";
		temp+="\r\r\n";
		temp+="\r\r\n";
		temp+=theApp.m_WorkName;
		temp+=".exe - 0 error(s), 0 waring(s)";
		m_EditBar->m_wndChild.SetWindowText(temp);

		return true;
	}
	else
	{
		char str[10];
		memset(str,0,10);
		itoa(sizewidth,str,10);
		for(int i=0;i<sizewidth;i++)
		{
			temp+="\r\r\n";
			temp+=compilefile;
			temp+=" : ";
			temp+=errorlist[i];
		}
		temp+="\r\r\n";
		temp+="Error executing cl.exe.";
		temp+="\r\r\n";
		temp+="Linking...";
		temp+="\r\r\n";
		temp+="\r\r\n";
		temp+=theApp.m_WorkName;
		temp+=".exe - ";
		temp+=str;
		temp+=" error(s), 0 waring(s)";
		m_EditBar->m_wndChild.SetWindowText(temp);
		return false;
	}
}

int CCompile::Analyse()
{
	BOOL mark0=false;//双引号检测
	BOOL mark1=false;//单引号检测
	CString temp=_T("");
	CString str=_T("");
	char ch;
	int id;
	int ret;
	Word word;
	while(DoFirst())
	{
		while(start!=search)
		{
			ch=Buffer[start];
			str=ch;
			if(mark0)
			{
				if(ch=='\"')
				{
					int sizewidth=temp.GetLength();
					if(sizewidth==1){temp+=ch;ch=0;mark0=!mark0;goto Next;}
					else{
						if(temp[sizewidth-1]=='\\')
							if(temp[sizewidth-2]=='\\'){temp+=ch;ch=0;mark0=!mark0;goto Next;}
							else{temp+=ch;goto Next;}
						else{temp+=ch;ch=0;mark0=!mark0;goto Next;}
					}
				}
				else {temp+=ch;goto Next;}
			}
			if(mark1)
			{
				if(ch=='\'')
				{
					int sizewidth=temp.GetLength();
					if(sizewidth==1){temp+=ch;ch=0;mark1=!mark1;goto Next;}
					else{
						if(temp[sizewidth-1]=='\\')
							if(temp[sizewidth-2]=='\\'){temp+=ch;ch=0;mark1=!mark1;goto Next;}
							else{temp+=ch;goto Next;}
						else{temp+=ch;ch=0;mark1=!mark1;goto Next;}
					}
				}
				else {temp+=ch;goto Next;}
			}
			if(CheckOperator(str,id)&&!(ch=='.'&&temp.GetLength()>0&&temp[0]>='0'&&temp[0]<='9')&&
				!(ch=='-'&&temp.GetLength()>1&&(temp[temp.GetLength()-1]=='e'||temp[temp.GetLength()-1]=='E')))
			{
				str.Empty();
				word.type=Operator;
				word.name=id;
				theData.Add(word);
				ret=CheckConstant(temp);
				switch(ret)
				{
				case 0:
					temp+="定义错误!";
					errorlist.Add(temp);
					temp.Empty();
					break;
				case 1:
					id=IsConstant(temp);
					if(id==-1)
					{
						word.name=constantlist.GetSize();
					    word.type=Constant;
					    theData.Add(word);
						constantlist.Add(temp);
					}
					else{
						word.name=id;
					    word.type=Constant;
					    theData.Add(word);
					}
					temp.Empty();
					break;
				}
				ret=CheckIdentifier(temp);
				switch(ret)
				{
				case 0:
					temp+="定义错误!";
					errorlist.Add(temp);
					temp.Empty();
					break;
				case 1:
					id=IsIdentifier(temp);
					if(id==-1)
					{
						word.name=identifierlist.GetSize();
					    word.type=Identifier;
					    theData.Add(word);
						identifierlist.Add(temp);
					}
					else{
						word.name=id;
					    word.type=Identifier;
					    theData.Add(word);
					}
					temp.Empty();
					break;
				}
				if((id=IsKeyWord(temp,temp.GetLength()))!=-1)
				{
					word.type=KeyWord;
					word.name=id;
					theData.Add(word);
					temp.Empty();
				}
				if(!strcmp(temp,"sizeof"))
				{
					word.type=Operator;
				    word.name=0;
				    theData.Add(word);
					temp.Empty();
				}
				goto Next;
			}
			ch=Buffer[start];
			if((id=IsBoundary(&ch))!=-1)
			{
				word.type=Boundary;
				word.name=id;
				theData.Add(word);
				if((id=IsKeyWord(temp,temp.GetLength()))!=-1)
				{
					word.type=KeyWord;
					word.name=id;
					theData.Add(word);
					temp.Empty();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -