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

📄 code.cpp

📁 (1) 给定一段符合Pascal子集语法的语言
💻 CPP
字号:
#include "StdAfx.h"
#include ".\code.h"

code::code(void)
{
}

code::~code(void)
{
}

bool CompareString(code_item & s1, code_item & s2) 
{
	return s1.code_num < s2.code_num;
}

bool CompareString2(jump_item & s1, jump_item & s2) 
{
	return s1.pos < s2.pos;
}

void code::output()
{
	sort(_code.begin(), _code.end(), CompareString);
	for( int i = 0 ; i < _code.size() ; i ++)
	{
		output_code<<_code[i].code_num<<"("<<_code[i].opt<<","<<_code[i].opt_num1<<","<<_code[i].opt_num2<<","<<_code[i].dst<<")"<<endl;
	}
	output_code<<endl;
}

void code::output_C()
{
	make_jump_list();
	output_code<<"void Main()"<<endl;
	output_code<<"{"<<endl;
	output_state();
	output_temp_v();
	output_body();
	output_code<<"}"<<endl;
}

void code::output_temp_v()
{
	output_code<<"\tint ";
	for(int i = 0 ; i < _global.temp_num ; i++)
	{
		output_code<<"temp"<<i<<" , ";
	}
	output_code<<"temp"<<i<<";"<<endl<<endl;
}

void code::make_jump_list()
{
	int count = 0;
	for( int i = 0 ; i < _code.size() ; i ++)
	{
		if( _code[i].opt.at(0) == 'j' )
		{
			jump_item * tempItem = new jump_item();
			tempItem->name = "Label" + action::itos(count);
			count++;
			tempItem->pos = atol(_code[i].dst.c_str());
			_jumpList.push_back(*tempItem);
		}
	}
	sort(_jumpList.begin(), _jumpList.end(), CompareString2);
}

void code::output_state()
{
	for(int i = 0 ; i < _global.v_table.size() ; i++)
	{
		if( _global.v_table[i].type == integer)
		{
			output_code<<"\tint "<<_global.v_table[i].name<<";"<<endl;
		}
		else if( _global.v_table[i].type == real)
		{
			output_code<<"\tdouble "<<_global.v_table[i].name<<";"<<endl;
		}
		else if( _global.v_table[i].type == boolean)
		{
			output_code<<"\tbool "<<_global.v_table[i].name<<";"<<endl;
		}
		else if(_global.v_table[i].type == vector_type)
		{
			switch(_global.v_table[i].vectorType)
			{
			case integer:{output_code<<"\tint "<<_global.v_table[i].name;break;}
			case real:{output_code<<"\tdouble "<<_global.v_table[i].name;break;}
			case boolean:{output_code<<"\tbool "<<_global.v_table[i].name;break;}
			default:{break;}
			}

			for(int j = 0 ; j < _global.v_table[i].vector_size ; j ++)
			{
				output_code<<"["<<_global.v_table[i]._size[j]<<"]";
			}
			output_code<<";"<<endl;
		}
	}
	output_code<<endl;
}

void code::output_body()
{
	int jump_count = 0;
	for( int i = 0 ; i < this->_code.size(); i ++)
	{
		if( _code[i].code_num == _jumpList[jump_count].pos )
		{
			output_code<<_jumpList[jump_count].name<<":";
			jump_count++;
		}
		if( _code[i].opt == ":=" )
		{
			output_code<<"\t"<<_code[i].dst<<" = "<<_code[i].opt_num1<<";"<<endl;
		}
		else if( _code[i].opt == "-" || _code[i].opt == "+" || _code[i].opt == "*" || _code[i].opt == "/" )
		{
			output_code<<"\t"<<_code[i].dst<<" = "<<_code[i].opt_num1<<" "<<_code[i].opt<<" "<<_code[i].opt_num2<<";"<<endl;
		}
		else
		{
			if( _code[i].opt == "j" )
			{
				output_code<<"\t"<<"goto"<<" ";
				int jump_dst = find_in_jump_list( atol(_code[i].dst.c_str()) );
				output_code<<_jumpList[jump_dst].name<<";"<<endl;
			}
			else if( _code[i].opt == "j<" )
			{
				output_code<<"\t"<<"if( "<<_code[i].opt_num1<<" < 0"<<" )"<<endl;
				int jump_dst = find_in_jump_list( atol(_code[i].dst.c_str()) );
				output_code<<"\t\t"<<"goto ";
				output_code<<_jumpList[jump_dst].name<<";"<<endl;
			}
			else if( _code[i].opt == "j>" )
			{
				output_code<<"\t"<<"if( "<<_code[i].opt_num1<<" > 0"<<" )"<<endl;
				int jump_dst = find_in_jump_list( atol(_code[i].dst.c_str()) );
				output_code<<"\t\t"<<"goto ";
				output_code<<_jumpList[jump_dst].name<<";"<<endl;
			}
		}
	}
}

int code::find_in_jump_list(int pos)
{
	for( int i = 0 ; i < _jumpList.size() ; i ++)
	{
		if( _jumpList[i].pos == pos )
			return i;
	}
}
void code::pushForward(int dst, int num)
{
	for( int i = 0 ; i < _code.size() ; i++)
	{
		if( _code[i].code_num > dst )
		{
			_code[i].code_num += num;
			//update(_code[i].code_num-num, _code[i].code_num);
		}
		if( atol(_code[i].dst.c_str()) > dst )
			_code[i].dst = action::itos( atol(_code[i].dst.c_str()) + num);
	}
	//update( atol(_code[_code.size()-1].dst.c_str()), atol(_code[_code.size()-1].dst.c_str())+num);
}

void code::addCode(int temp1,string temp2, string temp3, string temp4, string temp5)
{
	code_item *newItem = new code_item();
	newItem->code_num = temp1;
	newItem->dst = temp2;
	newItem->opt = temp3;
	newItem->opt_num1 = temp4;
	newItem->opt_num2 = temp5;
	this->_code.push_back(*newItem);
	_global.sentence_num++;
}

void code::update(int dst, int skip_line)
{
	for( int i = 0 ; i < _code.size() ; i ++)
	{
		if( _code[i].dst == "temp0")
		{		
			int j = 3;
			j++;
			j = atol(_code[i].dst.c_str());
		}
		if( atol(_code[i].dst.c_str()) == dst && dst != 0)
		{
			_code[i].dst = action::itos(skip_line);
		}
	}
}

⌨️ 快捷键说明

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