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

📄 code.h

📁 <compiler construction principles and practice>书中定义的tiny语言编译器。
💻 H
字号:
#define  pc 7
#define  mp 6
#define gp 5
#define  ac 0
#define  ac1 1

class Code
{

private:
	char *codefilename;
	static int emitLoc;
	static int highEmitLoc;
	ofstream fcout;


public:

	Code(char *);
	void emitComment( char * c );
	void emitRO( char *op, int r, int s, int t, char *c);
	void emitRM( char * op, int r, int d, int s, char *c);
	int emitSkip( int howMany);
	void emitBackup( int loc);
	void emitRestore(void);
	void emitRM_Abs( char *op, int r, int a, char * c);

};

int Code::emitLoc = 0;

int Code::highEmitLoc = 0;

Code::Code(char* codefile)
{
	codefilename = new char [strlen(codefile)+1];
	strcpy(codefilename,codefile);
	
	fcout.open(codefile);

}


void Code::emitComment( char * c )
{ 

	cout<<"*\t"<<c<<endl;
}

void Code::emitRO( char *op, int r, int s, int t, char *c)
{ 
	fcout<<setw(3)<<emitLoc<<':'<<setw(7)<<op<<setw(3)<<r<<','<<s<<','<<t<<endl;
	cout<<setw(3)<<emitLoc++<<setw(5)<<op<<r<<','<<s<<t<<'\t'<<c<<endl;
	
    if (highEmitLoc < emitLoc) highEmitLoc = emitLoc ;
}

void Code::emitRM( char * op, int r, int d, int s, char *c)
{ 
	fcout<<setw(3)<<emitLoc<<':'<<setw(7)<<op<<setw(3)<<r<<','<<d<<'('<<s<<')'<<endl;
	cout<<setw(3)<<emitLoc++<<setw(5)<<op<<r<<','<<d<<'('<<s<<')'<<'\t'<<c<<endl;

    if (highEmitLoc < emitLoc)  highEmitLoc = emitLoc ;
}


int Code::emitSkip( int howMany)
{  
	int i = emitLoc;
    emitLoc += howMany ;
    if (highEmitLoc < emitLoc)  highEmitLoc = emitLoc ;
    return i;
} 


void Code::emitBackup( int loc)
{ 
	if (loc > highEmitLoc) emitComment("BUG in emitBackup");
    emitLoc = loc ;
} 


void Code::emitRestore(void)
{ 
	emitLoc = highEmitLoc;
}


void Code::emitRM_Abs( char *op, int r, int a, char * c)
{ 
	fcout<<setw(3)<<emitLoc<<':'<<setw(7)<<op<<setw(3)<<r<<','<<a-(emitLoc+1)<<'('<<pc<<')'<<endl;
	cout<<setw(3)<<emitLoc<<setw(5)<<op<<r<<','<<a-(emitLoc+1)<<'('<<pc<<')'<<'\t'<<c<<endl;
	emitLoc++;
	
    if (highEmitLoc < emitLoc) highEmitLoc = emitLoc ;
} 

⌨️ 快捷键说明

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