编译原理课设(NEU)代码exe2.cpp
代码有几处bug,另外当时开发的时候心太急,理论基础没有做好,导致代码有不可逆性的缺陷:符号表,大家可以将符号表结构修改一下
代码是分为多个文章中存放的
VS 2017 VC++环境
代码是分为6个部分,在个人主页中都可以找到
#include "pch.h"
#include "exe2.h"
#include"Symbol_table.h"
#include <string>
#include<sstream>
#include <fstream>
using namespace std;
//公共函数区-------------------------------
string vert_int_to_string(int number)
{
string res;
stringstream ss;
ss << number;
ss >> res;
return res;
}
int vert_string_to_int(string word)
{
int res;
stringstream ss;
ss << word;
ss >> res;
return res;
}
//公共函数区结束-------------------------------
void exe2::show_quattro()
{
vector<struct_quattro>::iterator i_quattro = quattro.begin();
for (; i_quattro != quattro.end(); i_quattro++)
{
cout << (*i_quattro).a1 << "," << (*i_quattro).a2 << "," << (*i_quattro).a3 << "," << (*i_quattro).a4 << " \t\t^^^^^over" << endl;
}
}
void exe2::show_VALL()//显示函数的VALL
{
cout << endl << "show_VALL() 函数的VALL表-----------------------------------------------------------" << endl;
for (list<Symbol_table>::iterator l_i = list_symbol.begin(); l_i != list_symbol.end(); l_i++)
{
//cout << (*l_i).CAT << endl;
if ((*l_i).CAT == "f")
{
//cout << "找到了函数 显示 " << endl;
for (list<struct_VALL>::iterator x = ((PFINL *)(*l_i).ADDR)->VALL_TABLE->member.begin(); x != ((PFINL *)(*l_i).ADDR)->VALL_TABLE->member.end(); x++)
{
cout << (*x).var_name << " " << (*x).offset << endl;
}
}
}
//cout << "-----------------------------------------------------------------------------------" << endl;
cout << endl << endl;
}
int exe2::get_data_len(string name)//获取数据长度
{
if (name.substr(0, 6) == "struct")
name.erase(0, 7);
if (name == "int" || name == "void")
return 2;
list<Symbol_table>::iterator l_i;
//如果是个数字 则返回两个长度
list<string>::iterator l_num_i;
l_num_i = find(list_number.begin(), list_number.end(), name);
if (l_num_i != list_number.end())
{
//cout << 2 << endl;
return 2;
}
for (l_i = list_symbol.begin(); l_i != list_symbol.end(); l_i++)
{
if ((*l_i).NAME == name)
{
if ((*l_i).ADDR == NULL)
{
//cout << 2 << endl;
return 2;
}
else
{
//cout << ((LEN *)(*l_i).ADDR)->len << endl;
return ((LEN *)(*l_i).ADDR)->len;
}
}
}
string error = "error can't find the len of " + name;
throw error;
}
string exe2::get_function_return_tmp()//得到一个函数的临时返回变量
{
return "function_re_tmp" + vert_int_to_string(glen_et_function_return_tmp++);
}
void exe2::delete_new_object()//注销所有开辟的内存
{
for (list< LEN *>::iterator l_d_i = delete_LEN.begin(); l_d_i != delete_LEN.end(); l_d_i++)
{
delete (*l_d_i);
}
for (list<VALL *>::iterator l_d_i = delete_VALL.begin(); l_d_i != delete_VALL.end(); l_d_i++)
{
delete (*l_d_i);
}
for (list<Symbol_table*>::iterator l_d_i = delete_Symbol_table.begin(); l_d_i != delete_Symbol_table.end(); l_d_i++)
{
delete (*l_d_i);
}
for (list< PFINL*>::iterator l_d_i = delete_PFINL.begin(); l_d_i != delete_PFINL.end(); l_d_i++)
{
delete (*l_d_i);
}
for (list< _RINFL*> ::iterator l_d_i = delete__RINFL.begin(); l_d_i != delete__RINFL.end(); l_d_i++)
{
delete (*l_d_i);
}
for (list< RINFL*> ::iterator l_d_i = delete_RINFL.begin(); l_d_i != delete_RINFL.end(); l_d_i++)
{
delete (*l_d_i);
}
for (list< typel*> ::iterator l_d_i = delete_typel.begin(); l_d_i != delete_typel.end(); l_d_i++)
{
delete (*l_d_i);
}
for (list< Symbol_table*> ::iterator l_d_i = delete_Symbol_table.begin(); l_d_i != delete_Symbol_table.end(); l_d_i++)
{
delete (*l_d_i);
}
}
void exe2::show_Symbol(list<Symbol_table> x)//显示符号表
{
list<Symbol_table>::iterator iter_symbol;//遍历符号总表
/*
for (iter_symbol = x.begin(); iter_symbol != x.end(); iter_symbol++)
{
cout << (*iter_symbol).NAME <<" ";
if ((*iter_symbol).CAT != "f")
cout << get_data_len((*iter_symbol).NAME) << endl;
}
*/
//cout << "&&&&&&&&&&&&&&&&" << endl;
/*
cout << list_symbol.back().NAME << " ";
cout << list_symbol.back().CAT << " ";
cout << ((PFINL *)(list_symbol.back().ADDR))->PARAM->front().NAME << endl;
cout << ((PFINL *)(list_symbol.back().ADDR))->PARAM->front().CAT << endl;
cout << ((PFINL *)(list_symbol.back().ADDR))->VALL_TABLE->list_user_var.back() << endl;
*/
//cout << "&&&&&&&&&&&&&&&&" << endl;
}
int exe2::get_struct_len(string name)//获取name struct 的长度
{
list<Symbol_table>::iterator l_i;
for (l_i = list_symbol.begin(); l_i != list_symbol.end(); l_i++)
{
if ((*l_i).NAME == name)
{
return ((LEN *)((*l_i).ADDR))->len;
}
}
}
void exe2::change_VALL()//修改形式VALL 变为 真正带有偏移量的VALL
{
list<Symbol_table>::iterator l_i;
for (l_i = list_symbol.begin(); l_i != list_symbol.end(); l_i++)
{
if ((*l_i).CAT == "f")
{//这是一个函数定义 开始调整VALL CAT的"f"表明这是一个函数定义 TYPE指向函数返回类型 返回int void 为空 struct时 指向struct的定义 ADDR指向PNIFL表
VALL * tmp_vall = ((PFINL *)(*l_i).ADDR)->VALL_TABLE;//名字太长了 所以定义一个新的变量 方便
int mem_off = 2;//内存偏移量 开始加4 VALL的管理区(即SP恢复)占两个字节 返回值占两个字节
//c处理返回值 int是两个字节 规定void返回值为0 也是两个字节
if ((*l_i).TYPE == NULL)
{
mem_off += 2;
tmp_vall->this_function_return_len = 2;
}
else
{
mem_off += ((*l_i).TYPE->TPIONT->list_rinfl.back()).OFF;
mem_off += get_data_len((*l_i).TYPE->TPIONT->list_rinfl.back().MEMBER_NAME);
tmp_vall->this_function_return_len = mem_off;//保存函数的返回值长度
}
for (list<string>::iterator l_para_i = tmp_vall->list_parameter.begin(); l_para_i != tmp_vall->list_parameter.end(); l_para_i++)
{//处理形参表
//cout << "处理形参表" << endl;
struct_VALL tmp_struct_VALL;
tmp_struct_VALL.offset = mem_off;
tmp_struct_VALL.var_name = (*l_para_i);
//查找符号表 获取偏移量 并累加
mem_off += get_data_len((*l_para_i));
//将结果存入 VALL表中
tmp_vall->member.push_back(tmp_struct_VALL);
}
//cout << "****************************" << mem_off << endl;
for (list<string>::iterator l_para_i = tmp_vall->list_user_var.begin(); l_para_i != tmp_vall->list_user_var.end(); l_para_i++)
{//处理用户变量表
//cout << "处理用户变量表" << endl;
struct_VALL tmp_struct_VALL;
tmp_struct_VALL.offset = mem_off;
tmp_struct_VALL.var_name = (*l_para_i);
//查找符号表 获取偏移量 并累加
mem_off += get_data_len((*l_para_i));
//将结果存入 VALL表的member 链表中
tmp_vall->member.push_back(tmp_struct_VALL);
}
//cout << "****************************" << mem_off << endl;
for (list<string>::iterator l_para_i = tmp_vall->list_tmp_var.begin(); l_para_i != tmp_vall->list_tmp_var.end(); l_para_i++)
{//处理临时变量表
//cout << "处理临时变量表" << endl;
struct_VALL tmp_struct_VALL;
tmp_struct_VALL.offset = mem_off;
tmp_struct_VALL.var_name = (*l_para_i);
//查找符号表 获取偏移量 并累加
mem_off += get_data_len((*l_para_i));
//将结果存入 VALL表的member 链表中
tmp_vall->member.push_back(tmp_struct_VALL);
}
//cout << "************************************" << mem_off<<endl;
tmp_vall->this_VALL_len = mem_off;
}
}
}
string exe2::show_kongge()
{
string tmp = "";
for (int i = 0; i < len_show_kongge; i++)
tmp += " ";
return tmp;
}
exe2::exe2()
{
}
exe2::exe2(vector<string> x, list<string> number)//x 输入的代码串 number 常量数字串
{
struct_off = 0; //在struct符号表中的偏移量
len_show_kongge = 0;
flag_struct_define = false;
list_number = number;
cur_index = 0;
this->x = x;
string_tmp_var = "tmp_var";
int_tmp_var = 0;
Key_word_table.insert(pair<string, string>("int", "kw"));//关键字表
Key_word_table.insert(pair<string, string>("void", "kw"));//关键字表
Key_word_table.insert(pair<string, string>("return", "kw"));//关键字表
Key_word_table.insert(pair<string, string>("struct", "kw"));//关键字表
Key_word_table.insert(pair<string, string>("Function", "kw"));//关键字表
Key_word_table.insert(pair<string, string>("Type", "kw"));//关键字表
Key_word_table.insert(pair<string, string>("if", "kw"));//关键字表
Key_word_table.insert(pair<string, string>("else", "kw"));//关键字表
Key_word_table.insert(pair<string, string>("while", "kw"));//关键字表
//Key_word_table.insert(pair<string, string>("main", "kw"));//关键字表
Bound_table.insert(pair<string, string>(",", "bt"));;//界符表
Bound_table.insert(pair<string, string>(".", "bt"));;//界符表
Bound_table.insert(pair<string, string>(";", "bt"));;//界符表
Bound_table.insert(pair<string, string>("{", "bt"));;//界符表
Bound_table.insert(pair<string, string>("}", "bt"));;//界符表
Bound_table.insert(pair<string, string>("+", "bt"));;//界符表
Bound_table.insert(pair<string, string>("-", "bt"));;//界符表
Bound_table.insert(pair<string, string>("/", "bt"));;//界符表
Bound_table.insert(pair<string, string>("*", "bt"));;//界符表
Bound_table.insert(pair<string, string>("(", "bt"));;//界符表
Bound_table.insert(pair<string, string>(")", "bt"));;//界符表
Bound_table.insert(pair<string, string>(">", "bt"));;//界符表
Bound_table.insert(pair<string, string>("<", "bt"));;//界符表
Bound_table.insert(pair<string, string>("&", "bt"));;//界符表
Bound_table.insert(pair<string, string>("|", "bt"));;//界符表
Bound_table.insert(pair<string, string>("=", "bt"));;//界符表
Bound_table.insert(pair<string, string>("==", "bt"));;//界符表
Bound_table.insert(pair<string, string>(">=", "bt"));;//界符表
Bound_table.insert(pair<string, string>("<=", "bt"));;//界符表
Programmer();
change_VALL();
last_index = cur_index;
if (!has_main)
{
string error = "You don't have mian function";
throw error;
}
cout << show_kongge() << "END ALL RIGHT" << endl;
cout << "^^^^^^^^^^^^^^^^^^^^^^^文法扫描结束^^^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl;
cout << endl << endl;
ofstream out_quatro("D:\\VS_code\\编译课程设计\\exe1_quattro.txt",ios::trunc);//四元式 写入txt
for (vector<struct_quattro>::iterator v_i = quattro.begin(); v_i != quattro.end(); v_i++)
{//变换四元式格式
list_quattro.push_back(*v_i);
out_quatro << "(" << (*v_i).a1 << " # " << (*v_i).a2 << " # "<<(*v_i).a3 << " # " << (*v_i).a4 << ")" << endl;
}
out_quatro.close();
//show_quattro();
cout << endl << endl;
last_index = cur_index;
for (list<struct_name_type>::iterator l_i = list_struct_name_type.begin(); l_i != list_struct_name_type.end(); l_i++)
cout << (*l_i).name << " " << (*l_i).type << endl;
list_DSEG.push_back("DSEG SEGMENT");
list_CSEG.push_back("CSEG SEGMENT");
}
void exe2::get_const_number(list<string> x)//获取常数表
{
list_number = x;
}
int exe2::find_list_symbol_name(string name)//查找变量名 用于查重
{//有重复的返回1 无重复的返回0
list<Symbol_table>::iterator l_i = list_symbol.begin();
for (; l_i != list_symbol.end(); l_i++)
{
if ((*l_i).NAME == name)
{//找到了重复的
return 1;
}
}
return 0;
}
string exe2::get_tmp_var()//获取一个不重复的临时变量
{
//cout << "生成一个临时变量 tmp_var" << endl;
string_tmp_var = "tmp_var" + vert_int_to_string(int_tmp_var++);
//生成临时变量 编入Synbol总表 和函数VALL
common_VALL->list_tmp_var.push_back(string_tmp_var);
Symbol_table tmp_symbol;
tmp_symbol.CAT = "tmp_var";
tmp_symbol.NAME = string_tmp_var;
tmp_symbol.TYPE = NULL;//所有的临时变量在本编译器中都是int型 所以就是NULL
tmp_symbol.ADDR = NULL;//所有的临时变量在本编译器中都是int型 所以就是NULL
list_symbol.push_back(tmp_symbol);
return string_tmp_var;
}
exe2::~exe2()
{
}
void exe2::increase_cur_index()
{
if (cur_index < x.size() - 1)//最后加了一个 #
{
//cout << "///the now is ^^^^^^" << x[cur_index];
cur_index++;
//cout << "^^^^^ and the after is ^^^^^" << x[cur_index] << endl;
}
}
bool exe2::is_if_i(string name) //判断是否是一个标识符
{
//cout << "is_if_i()" << endl;
if (Key_word_table.find(name) != Key_word_table.end())//检查是否是一个关键字
{
//cout<< "is_if_i() has return 关键字" << endl;
return false;
}
if (Bound_table.find(name) != Bound_table.end()) //检查是否是一个界符
{
//cout << "is_if_i() has return 界符" << endl;
return false;
}
list<string>::iterator l_i;
l_i = find(list_number.begin(), list_number.end(), name);
if (l_i != list_number.end())
return false;
//cout<< "is_if_i() return is false" << endl;
return true;
}
bool exe2::is_if_num(string name)//判断是否是一个数字
{
//cout << "is_if_num()" << endl;
list<string>::iterator l_i;
l_i = find(list_number.begin(), list_number.end(), name);
if (l_i != list_number.end())
{
return true;
}
else
{
return false;
}
}
bool exe2::is_if_function(string name)//判断是否是一个函数
{
//cout << "is_if_function()" << endl;
if (find(vector_Function.begin(), vector_Function.end(), name) == vector_Function.end())
{
//cout << name << " is a not FUNCtion" << endl;
return false;
}
else
{
//cout << name << " is a FUNCtion" <<endl;
return true;
}
}
int exe2::Actual_parameter()//<函数实参>
{
cout << show_kongge() << "Actual_parameter()" << endl;
len_show_kongge++;
if (is_if_i(x[cur_index]))
{
Identifier();
//cout << "###########################" << vector_identify.back()<< endl;
//生成四元式 (actual_parameter,var,,)
struct_quattro struct_q;
struct_q.a1 = "actual_parameter";
struct_q.a2 = vector_identify.back();
vector_identify.pop_back();
struct_q.a3 = "";
struct_q.a4 = "";
quattro.push_back(struct_q);
Actual_parameter_suffix();
len_show_kongge--;
return 1;
}
len_show_kongge--;
return 0;
}
int exe2::Actual_parameter_suffix()//<函数实参后缀>
{
cout << show_kongge() << "Actual_parameter_suffix()" << endl;
len_show_kongge++;
if (x[cur_index] == ",")
{
increase_cur_index();
Identifier();
//cout << "###########################" << vector_identify.back() << endl;
//生成四元式 (actual_parameter,var,,)
struct_quattro struct_q;
struct_q.a1 = "actual_parameter";
struct_q.a2 = vector_identify.back();
vector_identify.pop_back();
struct_q.a3 = "";
struct_q.a4 = "";
quattro.push_back(struct_q);
Actual_parameter_suffix();
len_show_kongge--;
return 1;
}
len_show_kongge--;
return 0;
}
bool flag_struct;
int exe2::Identifier() //用于规范一个变量名
{
cout << show_kongge() << "Identifier()" << endl;
len_show_kongge++;
if (is_if_i(x[cur_index]))
{
//推出终结符 也是标识符 入栈
vector_identify.push_back(x[cur_index]);
increase_cur_index();
Identifier();//主要用于循环后缀 可以识别struct的成员标识符
len_show_kongge--;
return 1;
}
else if (x[cur_index] == ".")//struct的成员
{
//生成struct对象 的成员数据 及四元式
flag_struct = true;
struct_quattro struct_q;
struct_q.a1 = "struct_member";
struct_q.a2 = vector_identify.back();
vector_identify.pop_back();
struct_q.a3 = "";
struct_q.a4 = "";
quattro.push_back(struct_q);
increase_cur_index();
Identifier();
len_show_kongge--;
return 1;
}
else if (flag_struct == true)
{
//结构体的成员访问到此结束 连我都忘是怎么实现的 VS是个成熟的编译环境了 它会自己码代码了 它也有自己的想法了
flag_struct = false;
struct_quattro struct_q;
struct_q.a1 = "struct_member_end";
struct_q.a2 = vector_identify.back();
struct_q.a3 = "";
struct_q.a4 = "";
quattro.push_back(struct_q);
len_show_kongge--;
return 1;
}
len_show_kongge--;
return 0;
}
int exe2::Struct_member_suffix() //struct成员后继
{
return 0;
}
int exe2::Programmer()
{
cout << show_kongge() << "Programmer()" << endl;
len_show_kongge++;
if (x[cur_index] == "#")
{
len_show_kongge--;
return 0;
}
else if (Function() == 1)//函数声明
{
Programmer();
}
else if (Define_var() == 1)//定义变量
{
// << "adsf" << endl;
Programmer();
}
else if (Type_struct() == 1)//自定义数据结构
{
// << "adsf" << endl;
Programmer();
}
len_show_kongge--;
return 1;
}
int exe2::Function() //<函数> 定义函数
{
cout << show_kongge() << "Function()" << endl;
len_show_kongge++;
if (x[cur_index] == "Function")
{
Symbol_table tmp_token;
increase_cur_index();//<<"wkn"<<endl;
if (Kind_var() == 1)//<数据类型>
{
if (list_opt.back() == "void")//标记函数返回值为void
flag_function_return_void = true;
else
flag_function_return_void = false;
if (Identifier() == 1)
{
if (vector_identify.back() == "main")
has_main = true;
for (list<Symbol_table>::iterator l_i = list_symbol.begin(); l_i != list_symbol.end(); l_i++)
{//检查该函数名是否被定义过
if (vector_identify.back() == (*l_i).NAME)
{
string error = "you have define the name '" + vector_identify.back() + "' when you want to define a function";
throw error;
}
}
flag_function_define = true;//打开定义函数标志
function_para_number = 0;//初始化形参个数
//生成符号表
tmp_token.NAME = vector_identify.back();//函数名
tmp_token.CAT = "f"; //SYMBL表中类型 这是一个函数
if (list_opt.back() == "int" || list_opt.back() == "void")
{
tmp_token.TYPE = NULL; //类型 为NULL 在PFINL中给出
}
else if (list_opt.back().substr(0, 6) == "struct")
{//返回值是个struct 查表 是否有该类型
string tmp_string = list_opt.back();
tmp_string.erase(0, 7);//将struct_ 删除 留下struct类型 这个操作与自己的数据结构有关 不是必要的行为
list<Symbol_table>::iterator l_i;
for (l_i = list_symbol.begin(); l_i != list_symbol.end(); l_i++)
{
if ((*l_i).NAME == tmp_string)//获取函数的返回值 类型
break;
}
if (l_i == list_symbol.end())
{//没有这个struct类型
string error = "error donit has this struct type when want to define a struct " + vector_identify.back();
throw error;
}
tmp_token.TYPE = (*l_i).TYPE;//将返回值的struct 的相关信息传给 符号表中 函数的TYPE
}
common_PFINL = new PFINL(); //指向函数的ADDR其他
//.push_back(common_PFINL);
common_PFINL->type_function = list_opt.back();//将函数的返回类型保存
delete_PFINL.push_back(common_PFINL);
common_VALL = new VALL(); //指向VALL表
//.push_back(common_VALL);
delete_VALL.push_back(common_VALL);
common_PFINL->VALL_TABLE = common_VALL;//将VALL表放入其中
tmp_token.ADDR = common_PFINL; //SYMBL表指向 函数表
list_symbol.push_back(tmp_token);
common_list_param = new list<Symbol_table>();//形参链表
//.push_back(common_list_param);
delete_list_Symbol_table.push_back(common_list_param);
flag_param_unqiue = false;
if (x[cur_index] == "(")
{
local_var = true;
sum_function_para = 0;
//将函数名存起来
vector_Function.push_back(vector_identify.back());
//生成一个四元式 函数定义的头部 (DEF,Function,返回值,名称)
struct_quattro struct_q;
struct_q.a1 = "DEF";
struct_q.a2 = "Function";
struct_q.a3 = list_opt.back();
struct_q.a4 = vector_identify.back();
quattro.push_back(struct_q);
list_opt.pop_back();
vector_identify.pop_back();
increase_cur_index();//<<"wkn"<<endl;
Formal_parameter();//<形参表>
//已经生成了数个形参表
common_PFINL->FN = sum_function_para;
sum_function_para = 0;
if (x[cur_index] == ")")
{
increase_cur_index();//<<"wkn"<<endl;
if (x[cur_index] == "{")
{
//生成一个四元式 表示 函数定义代码段 (DEF,F_S,,)
struct_quattro struct_q;
struct_q.a1 = "DEF";
struct_q.a2 = "F_S";
struct_q.a3 = "";
struct_q.a4 = "";
quattro.push_back(struct_q);
increase_cur_index();
if (Complex_expression() == 1)//<复杂表达式>
{
if (x[cur_index] == "}")
{
local_var = false;
flag_function_define = false;
//生成一个四元式 表示 函数定义代码段 (DEF,F_E,,)
struct_quattro struct_q;
struct_q.a1 = "DEF_END";
struct_q.a2 = "F_E";
struct_q.a3 = "";
struct_q.a4 = "";
quattro.push_back(struct_q);
increase_cur_index();
//函数定义完成
len_show_kongge--;
return 1;
}
else
{
string error = "lost '}' at Function Define" + cur_index;
throw error;
}
}
else
{
//函数空警告
}
}
else
{
string error = "lost '{' at Function Define" + cur_index;
throw error;
}
}
else
{
string error = "lost ')' at Function Define" + cur_index;
throw error;
}
}
else
{
string error = "lost a '(' at Function Define" + cur_index;
throw error;
}
}
else
{
}
}
else
{
string error = "the value of Function is error" + cur_index;//函数返回值类型错误
throw error;
}
}
else
{
len_show_kongge--;
return 0; //表示未执行
}
len_show_kongge--;
}
int exe2::real_type_suffix()//定义struct的另一行数据
{
cout << show_kongge() << "real_type_suffix()" << endl;
len_show_kongge++;
if (Define_var() == 1)
{
real_type_suffix();
}
len_show_kongge--;
return 0;
}
int exe2::Type_struct()//定义struct结构体
{
cout << show_kongge() << "Type_struct()" << endl;
len_show_kongge++;
Symbol_table tmp_token;
if (x[cur_index] == "Type")
{
increase_cur_index();//<<"wkn"<<endl;
if (x[cur_index] == "struct")
{
increase_cur_index();
Identifier();
if (find_list_symbol_name(vector_identify.back()) == 1)
{
string error = "error has define '" + vector_identify.back() + "' when define Type struct";
throw error;
}
//生成符号表的struct表 RINFL
tmp_token.NAME = vector_identify.back();//struct 类型名
tmp_token.ADDR = NULL;
tmp_token.CAT = "type_struct"; //表明 定义一个struct结构
//cout << tmp_token.CAT << endl;
typel * tmp_typel = new typel();
delete_typel.push_back(tmp_typel);
//.push_back(tmp_typel);
tmp_typel->TVAL = "struct";//表明这个类型是struct
common_RINFL = new RINFL();// 生成一个后续完善的struct结构表的成员
//.push_back(common_RINFL);
delete_RINFL.push_back(common_RINFL);
tmp_typel->TPIONT = common_RINFL;//TYPEL表和RINFL表连接起来
tmp_token.TYPE = tmp_typel;
flag_struct_define = true;//开启定义struct类型的标志
//生成一个四元式 struct定义 (DEF,struct,名称,)
struct_quattro struct_q;
struct_q.a1 = "DEF";
struct_q.a2 = "struct";
struct_q.a3 = vector_identify.back();
struct_q.a4 = "";
quattro.push_back(struct_q);
vector_identify.pop_back();
//同时给类型定义栈加入新生成的类型
struct_off = 0;
vector_type_struct.push_back(struct_q.a3);
if (x[cur_index] == "{")
{
increase_cur_index();
//Struct_data_member();//<struct 数据成员>
if (Define_var() == 1)
{
//Define_var();//define_var 中生成的四元式通用 不再需要多余定义动作
real_type_suffix();
}
if (x[cur_index] == "}")
{
LEN * tmp_LEN = new LEN();
delete_LEN.push_back(tmp_LEN);
//cout << "++++++++++++++++++++1908-283457543269812-3084239048652-03489510-2394 " << struct_off<< endl;
tmp_LEN->len = struct_off;
tmp_token.ADDR = tmp_LEN;//将struct 的长度保存
list_symbol.push_back(tmp_token);
//cout << ((LEN *)list_symbol.back().ADDR)->len << endl;
//cout << list_symbol.back().NAME << endl;
//cout << list_symbol.back().CAT << endl;
flag_struct_define = false; //结构体定义结束 恢复标志
struct_off = 0;//结构体定义结束 内存偏移值归0
//生成四元式 完成struct定义 (DEF,,,)
struct_quattro struct_q;
struct_q.a1 = "DEF_END";
struct_q.a2 = "";
struct_q.a3 = "";
struct_q.a4 = "";
quattro.push_back(struct_q);
increase_cur_index();
//struct完成 动作
len_show_kongge--;
return 1;
}
else
{
string error = "Lost '}' at Type Struct" + cur_index;
throw error;
}
}
else
{
string error = "lost '{' at Type struct " + cur_index;
throw error;
}
}
else
{
string error = "lost 'struct'after Type" + cur_index;
throw error;
}
}
else
{
len_show_kongge--;
return 0;
}
len_show_kongge--;
}
bool flag_Define_var;
struct_name_type span_name_type;
int exe2::Define_var()//<定义变量>
{//加入了 判断定义struct 和函数内定义的局部变量表示
cout << show_kongge() << "Define_var()" << endl;
len_show_kongge++;
flag_Define_var = false;
if (Kind_var() == 1)//先判断类型
{
Identifier(); //获取变量名 需要查重
if (find_list_symbol_name(vector_identify.back()) == 1)
{//变量名 查重定义
string error = "error this name '" + vector_identify.back() + "' has defined please renew";
throw error;
}
//生成符号表 判断
Symbol_table tmp_token;
tmp_token.CAT = "user_var"; //符号表种类编码 v 变量
if (list_opt.back() == "int" || list_opt.back() == "void")
{
tmp_token.TYPE = NULL;
tmp_token.ADDR = NULL;
if (flag_struct_define == true)
{//定义struct的类型成员 中 需要将结果写入 RINFL表中
_RINFL * tmp_RINFL = new _RINFL();//生成了一条成员
//.push_back(tmp_RINFL);
delete__RINFL.push_back(tmp_RINFL);
tmp_RINFL->OFF = struct_off;//结构体的内存4中的偏移量
common_struct_off = 2;
struct_off += 2; //int 的长度是2
tmp_RINFL->MEMBER_NAME = vector_identify.back();
tmp_RINFL->TP = NULL; //这里时int和void 所以为空即可
common_RINFL->list_rinfl.push_back((*tmp_RINFL));//将这条结构成员 加入到 RINFL总表中
}
if (flag_function_define == true)
{//正在定义 函数中的局部用户变量
common_VALL->list_user_var.push_back(vector_identify.back());
}
}
else if (list_opt.back().substr(0, 6) == "struct")//是个struct的结构
{
//声明的是一个变量 无论是声明struct成员 都是要加入SYNBOL总表
typel * tmp_typel = new typel();
delete_typel.push_back(tmp_typel);
tmp_typel->TVAL = list_opt.back();//表明这是一个声明struct 类型的变量
//tmp_typel->TPIONT = RINFL ;//struct的表 待定 符号表别处赋值完成 这里是声明 不是定义一个结构体类型
string tmp_string = list_opt.back();
tmp_string.erase(0, 7);//将struct_ 删除 这个操作与自己的数据结构有关 不是必要的行为
//用这个名字去查找相关的信息 长度和相应的TYPEL的TPIONT
flag_Define_var = true;
struct_name_type tmp_name_type;
tmp_name_type.type = tmp_string;
tmp_name_type.name = vector_identify.back();
span_name_type = tmp_name_type;
list_struct_name_type.push_back(tmp_name_type);
list<Symbol_table>::iterator l_i;
for (l_i = list_symbol.begin(); l_i != list_symbol.end(); l_i++)
{
if ((*l_i).NAME == tmp_string)
break;
}
if (l_i == list_symbol.end())
{//没有这个struct类型
string error = "error don't has this struct type when want to define a struct " + vector_identify.back();
throw error;
}
tmp_typel = (*l_i).TYPE;//将相应的结构赋值给这个新声明的struct类型变量
tmp_token.TYPE = tmp_typel;
tmp_token.ADDR = (*l_i).ADDR;//j将相应的struct的长度赋值
if (flag_struct_define == true)
{//定义struct 中 需要将结果写入 RINFL表中
_RINFL * tmp_RINFL = new _RINFL();
delete__RINFL.push_back(tmp_RINFL);
//.push_back(tmp_RINFL);
tmp_RINFL->OFF = struct_off;//结构体的内存中的偏移量
//这里需要查表获取 该struct长度 进而改变 struct_off 的值 struct_off+=该struct长度;
common_struct_off = ((LEN *)(*l_i).ADDR)->len;
struct_off += ((LEN *)(*l_i).ADDR)->len;
tmp_RINFL->MEMBER_NAME = vector_identify.back();//struct类型 的 成员名
//tmp_RINFL->TP=查询表返回相应TYPEL中某一行 并指向该行
tmp_RINFL->TP = (*l_i).TYPE;
common_RINFL->list_rinfl.push_back((*tmp_RINFL));//将这条结构成员 加入到 RINFL总表中
}
if (flag_function_define == true)
{//正在定义 函数中的局部用户变量
common_VALL->list_user_var.push_back(vector_identify.back());
}
}
//生成四元式 (var,类型,,) 同时将类型栈弹出
struct_quattro struct_q;
struct_q.a1 = "var";
struct_q.a2 = list_opt.back();
struct_q.a3 = "";
struct_q.a4 = "";
if (!local_var)
quattro.push_back(struct_q);
list_opt.pop_back();
vector_var.push_back(vector_identify.back());
tmp_token.NAME = vector_identify.back();//符号表的名称
span_between_define_var_and_suffix_tmp_token = tmp_token;
list_symbol.push_back(tmp_token);
//生成四元式 (名称,,,)
struct_q.a1 = vector_identify.back();
struct_q.a2 = "";
struct_q.a3 = "";
struct_q.a4 = "";
if (!local_var)
quattro.push_back(struct_q);
vector_identify.pop_back();
Define_var_Suffix();
len_show_kongge--;
return 1;
}
else
{
len_show_kongge--;
return 0;
}
len_show_kongge--;
}
int exe2::Define_var_Suffix()//<定义变量后缀>
{
cout << show_kongge() << "Define_var_Suffix()" << x[cur_index] << endl;
len_show_kongge++;
if (x[cur_index] == ",")
{
increase_cur_index();//<<"wkn"<<endl;
Identifier();
if (flag_struct_define == true)
{//定义struct的类型成员 中 需要将结果写入 RINFL表中
_RINFL * tmp_RINFL = new _RINFL();//生成了一条成员
//.push_back(tmp_RINFL);
delete__RINFL.push_back(tmp_RINFL);
tmp_RINFL->OFF = struct_off;//结构体的内存中的偏移量
struct_off += common_struct_off; //int 的长度是2
tmp_RINFL->MEMBER_NAME = vector_identify.back();
tmp_RINFL->TP = NULL; //这里时int和void 所以为空即可
common_RINFL->list_rinfl.push_back((*tmp_RINFL));//将这条结构成员 加入到 RINFL总表中
}
if (flag_function_define == true)
{//正在定义 函数中的局部用户变量
//cout << "Define_var_Suffix()******测试函数内定义" << vector_identify.back() << endl;
common_VALL->list_user_var.push_back(vector_identify.back());
}
//需要在定义后缀的地方也复制一份和之前一样的
if (find_list_symbol_name(vector_identify.back()) == 1)
{//变量名 查重定义
string error = "error this name '" + vector_identify.back() + "' has defined please renew";
throw error;
}
vector_var.push_back(vector_identify.back());
Symbol_table tmp_copy_SYMBOL = span_between_define_var_and_suffix_tmp_token;//因为是 逗号分隔 所以和前面的变量都是一个类型 符号表中的值必然都一样 只需要复制将 NAME修改一下即可
tmp_copy_SYMBOL.NAME = vector_identify.back();
list_symbol.push_back(tmp_copy_SYMBOL);
//生成四元式 (名称,,,)
struct_quattro struct_q;
struct_q.a1 = vector_identify.back();
struct_q.a2 = "";
struct_q.a3 = "";
struct_q.a4 = "";
if (flag_Define_var)
{
struct_name_type tmp;
tmp.name = vector_identify.back();
tmp.type = (span_name_type).type;
list_struct_name_type.push_back(tmp);
}
if (!local_var)
quattro.push_back(struct_q);
vector_identify.pop_back();
Define_var_Suffix();//<定义变量后缀>
len_show_kongge--;
return 1;
}
else if (x[cur_index] == ";")
{
increase_cur_index();//<<"wkn"<<endl;
//弹出类型栈栈顶
len_show_kongge--;
return 1;
}
else
{
string error = "error at Define Var " + cur_index;
throw error;
}
len_show_kongge--;
}
int exe2::Kind_var()//<数据类型>
{
cout << show_kongge() << "Kind_var()" << endl;
len_show_kongge++;
//cout << "+++++now the kind is " << x[cur_index] << endl;
if (x[cur_index] == "int")
{
increase_cur_index();//<<"wkn"<<endl;
//将 所得数据类型压入栈 类型栈
list_opt.push_back("int");
len_show_kongge--;
return 1;
}
else if (x[cur_index] == "void")
{
//将 所得数据类型压入栈 类型栈
list_opt.push_back("void");
increase_cur_index();//<<"wkn"<<endl;
len_show_kongge--;
return 1;
}
else if (x[cur_index] == "struct")
{
//将 所得数据类型压入栈 类型栈
increase_cur_index();//<<"wkn"<<endl;
vector_identify.push_back(x[cur_index]);
list_opt.push_back("struct_" + x[cur_index]);//加入类型
//在这个要检查 该 变量类型是否是struct的正确类型
if (vector_type_struct.end() == find(vector_type_struct.begin(), vector_type_struct.end(), vector_identify.back()))//检查是否有该类型的struct结构
{
string error = "error don't has this type struct" + cur_index;
throw error;
}
increase_cur_index();
len_show_kongge--;
return 1;
}
len_show_kongge--;
return 0;
}
int exe2::Formal_parameter()//<形参表>
{
cout << show_kongge() << "Formal_parameter()" << endl;
len_show_kongge++;
delete_list_Symbol_table.push_back(common_list_param);
common_PFINL->PARAM = common_list_param;//形参链表初始化
if (Kind_var() == 1)//非空参数表
{
Identifier();
for (list<Symbol_table>::iterator l_i = list_symbol.begin(); l_i != list_symbol.end(); l_i++)
{//检查该形参名是否被定义过
if (vector_identify.back() == (*l_i).NAME)
{
string error = "you have define the name '" + vector_identify.back() + "' when you want to define a function";
throw error;
}
}
//生成的形参也要加入到SYMBOL总表中
Symbol_table tmp_SYMBOL;
tmp_SYMBOL.CAT = "para_var";//形参表中的变量是个变量
tmp_SYMBOL.NAME = vector_identify.back();
if (list_opt.back().substr(0, 6) == "struct")//是否为struct定义类型
{
string t = list_opt.back();
t.erase(0, 7);
//cout << "----------------------------" << vector_identify.back() << " " << list_opt.back() << endl;
struct_name_type tmp_struct_name_type;
tmp_struct_name_type.name = vector_identify.back();
tmp_struct_name_type.type = t;
list_struct_name_type.push_back(tmp_struct_name_type);
//通过查符号表 的 类型 来指向相应的地方
list<Symbol_table>::iterator l_i = list_symbol.begin();
for (; l_i != list_symbol.end(); l_i++)
{
string tmp_struct_type = list_opt.back();
tmp_struct_type.erase(0, 7);
if ((*l_i).NAME == tmp_struct_type)
{
break;
}
}
if (l_i == list_symbol.end())
{
string error = "error when you define paramater at Function ";
throw error;
}
//cout << "**************** NAME is" << (*l_i).NAME << endl;
//当前cur_index指向的就是struct的类型 Identify_vector 的back 也是 记得弹栈
tmp_SYMBOL.TYPE = (*l_i).TYPE;
tmp_SYMBOL.ADDR = ((*l_i).ADDR);
}
else if (list_opt.back() == "int" || list_opt.back() == "void")
{//是常规类型 如 int void
tmp_SYMBOL.TYPE = new typel();
delete_typel.push_back(tmp_SYMBOL.TYPE);
//.push_back(tmp_SYMBOL.TYPE);
tmp_SYMBOL.TYPE->TVAL = list_opt.back();
tmp_SYMBOL.TYPE->TPIONT = NULL;
}
common_list_param->push_back(tmp_SYMBOL);
list_symbol.push_back(tmp_SYMBOL);
//生成一个四元式 (parameter,数据类型,名称,)
struct_quattro struct_q;
struct_q.a1 = "parameter";
struct_q.a2 = list_opt.back();
struct_q.a3 = vector_identify.back();
struct_q.a4 = "";
//quattro.push_back(struct_q);
vector_identify.pop_back();
list_opt.pop_back();
//同时将 类型栈中的栈顶弹出
Formal_parameter_suffix();//形参表的后继符
}
else
{// 空参数表 或者是 最后一个形参定义完毕
common_PFINL->PARAM = common_list_param;//将形参结果复制给 函数表的 形参 项
for (list<Symbol_table>::iterator l_i = common_list_param->begin(); l_i != common_list_param->end(); l_i++)
{
common_VALL->list_parameter.push_back((*l_i).NAME);//将形参转换到VALL表中
}
}
common_PFINL->PARAM = common_list_param;//将形参结果复制给 函数表的 形参 项
if (!flag_param_unqiue)
{
for (list<Symbol_table>::iterator l_i = common_list_param->begin(); l_i != common_list_param->end(); l_i++)
{
common_VALL->list_parameter.push_back((*l_i).NAME);//将形参转换到VALL表中
}
flag_param_unqiue = true;
}
len_show_kongge--;
return 0;
}
int exe2::Formal_parameter_suffix()//<形参表后缀>
{//所有的四元式都在形参表中完成 该处只作为中继使用
cout << show_kongge() << "Formal_parameter_suffix()" << endl;
len_show_kongge++;
if (x[cur_index] == ",")
{
increase_cur_index();
Formal_parameter();
len_show_kongge--;
return 1;
}
else
{
len_show_kongge--;
return 0;
}
len_show_kongge--;
}
int exe2::Complex_expression()//<复杂表达式>
{
cout << show_kongge() << "Complex_expression()" << endl;
len_show_kongge++;
if (if_expression() == 1)
{
Complex_expression();
len_show_kongge--;
return 1;
}
else if (while_expression() == 1)
{
Complex_expression();
len_show_kongge--;
return 1;
}
else if (return_express() == 1)
{
Complex_expression();
len_show_kongge--;
return 1;
}
else if (Define_var() == 1)
{
Complex_expression();
len_show_kongge--;
return 1;
}
else if (Operational_expression() == 1)
{
Complex_expression();
len_show_kongge--;
return 1;
}
else
{
len_show_kongge--;
return 0;//空函数
}
}
int exe2::while_expression()//<while表达式>
{
cout << show_kongge() << "while_expression()" << endl;
len_show_kongge++;
if (x[cur_index] == "while")
{
//生成一个四元式 标志while语句开始(while,label_while_start,,)
struct_quattro struct_q;
struct_q.a1 = "while";
struct_q.a2 = "label_while_start";
struct_q.a3 = "";
struct_q.a4 = "";
quattro.push_back(struct_q);
increase_cur_index();
if (x[cur_index] == "(")
{
increase_cur_index();
Logical_judgement();//由逻辑判断语句完成四元式生成
//生成四元式 while条件判断后跳转 到while_end (jz,label,,)
string a;
struct_quattro struct_q;
struct_q.a1 = "JZ";
struct_q.a2 = vector_identify.back();
vector_identify.pop_back();
struct_q.a3 = "label_while_end";
struct_q.a4 = "";
quattro.push_back(struct_q);
if (x[cur_index] == ")")
{
increase_cur_index();
if (x[cur_index] == "{")
{
increase_cur_index();
//while 执行体
//四元式由while_execute函数来生成
while_execute();
//while循环体执行结束
//生成四元式 用于while循环返回 (jmp,label while start,,)
struct_quattro struct_q;
struct_q.a1 = "JMP";
struct_q.a2 = "label_while_start";
struct_q.a3 = "";
struct_q.a4 = "";
quattro.push_back(struct_q);
//
if (x[cur_index] == "}")
{
//生成四元式 标志while定义结束(while_end,,,)
struct_quattro struct_q;
struct_q.a1 = "while_end";
struct_q.a2 = "";
struct_q.a3 = "";
struct_q.a4 = "";
quattro.push_back(struct_q);
increase_cur_index();
return 1;
}
else
{
string error = "error while lost '}' at" + cur_index;
throw error;
}
}
else
{
string error = "error while lost '{' at" + cur_index;
throw error;
}
}
else
{
string error = "error while lost ')' at" + cur_index;
throw error;
}
}
else
{
string error = "error while lost '(' at" + cur_index;
throw error;
}
}
len_show_kongge--;
return 0;
}
int exe2::while_execute() //while 执行体
{
cout << show_kongge() << "while_execute()" << endl;
len_show_kongge++;
Complex_expression();//由Complex_expression生成四元式
len_show_kongge--;
return 0;
}
int exe2::if_expression()//<if表达式>
{
cout << show_kongge() << "if_expression()" << endl;
len_show_kongge++;
if (x[cur_index] == "if")
{
//生成四元式 标志if开始 (if,,,)
struct_quattro struct_q;
struct_q.a1 = "if";
struct_q.a2 = "";
struct_q.a3 = "";
struct_q.a4 = "";
quattro.push_back(struct_q);
increase_cur_index();//<<"wkn"<<endl;
if (x[cur_index] == "(")
{
increase_cur_index();//<<"wkn"<<endl;
Logical_judgement(); //<逻辑判断> 四元式由Logical_judgement()生成
//生成四元式 用于逻辑判断 跳转(jz,label,,)
struct_quattro struct_q;
struct_q.a1 = "jz";
struct_q.a2 = vector_identify.back();
vector_identify.pop_back();
struct_q.a3 = "if_label_judge";
struct_q.a4 = "";
quattro.push_back(struct_q);
if (x[cur_index] == ")")
{
increase_cur_index();
if (x[cur_index] == "{")
{
increase_cur_index();
if_execute();//<if 执行体>
//生成四元式 if的直辖表达式已经执行完成 绕过else表达式 (jmp,label,,)
struct_quattro struct_q;
struct_q.a1 = "JMP";
struct_q.a2 = "label_if_end";
struct_q.a3 = "";
struct_q.a4 = "";
quattro.push_back(struct_q);
if (x[cur_index] == "}") //
{ //
increase_cur_index();//<<"wkn"<<endl; //
if_execute_suffix(); //
//if的 else 或 无 部分执行完成 生成四元式 用于 接受 绕过else执行体表达式(endif,label,,)
struct_quattro struct_q;
struct_q.a1 = "if_end";
struct_q.a2 = "label_if_end";
struct_q.a3 = "";
struct_q.a4 = "";
quattro.push_back(struct_q);
}
else
{
string error = "error at if lost '}'" + cur_index;
throw error;
}
}
else
{
string error = "error at if lost '{'" + cur_index;
throw error;
}
}
else
{
string error = "error at if expression lost ')' " + cur_index;
throw error;
}
}
else
{
string error = "error at if expression lost '(' " + cur_index;
throw error;
}
len_show_kongge--;
return 1;
}
len_show_kongge--;
return 0;
}
int exe2::if_execute() //<if执行体>
{
cout << show_kongge() << "if_execute()" << endl;
len_show_kongge++;
Complex_expression(); //四元式由Complex_expression();生成
//if_execute_suffix();
len_show_kongge--;
return 1;
}
int exe2::if_execute_suffix() //<if执行体后缀>
{
cout << show_kongge() << "if_execute_suffix()" << endl;
len_show_kongge--;
//生成四元式 if逻辑判断为fasle转到这里 (else,label,,)
struct_quattro struct_q;
struct_q.a1 = "else";
struct_q.a2 = "label_else";
struct_q.a3 = "";
struct_q.a4 = "";
quattro.push_back(struct_q);
if (x[cur_index] == "else")
{
increase_cur_index();
if (x[cur_index] == "{")
{
increase_cur_index();
Complex_expression();
if (x[cur_index] == "}")
{
increase_cur_index();//<<"wkn"<<endl;
}
else
{
string error = "error lost }" + cur_index;
throw error;
}
}
else
{
string error = "lost { " + cur_index;
throw error;
}
}
len_show_kongge--;
return 0;
}
int exe2::return_express()//<return表达式> 函数的 return表达式
{
cout << show_kongge() << "return_express()" << endl;
len_show_kongge++;
if (x[cur_index] == "return")
{
increase_cur_index();
if (flag_function_return_void)//查符号表 如果函数返回值是void 则return后跟;即可
{
struct_quattro struct_q;
struct_q.a1 = "RET";
struct_q.a2 = "void";
struct_q.a3 = "";
struct_q.a4 = "";
quattro.push_back(struct_q);
if (x[cur_index] == ";")
{
increase_cur_index();
len_show_kongge--;
return 1;
}
else
{
string error = "error at the function after 'return' lost ';'";
throw error;
}
len_show_kongge--;
return 1;
}
if (Identifier() == 1) //返回值
{
//生成四元式 函数返回 (RET,var,,) 将值送入VILL的返回值区
struct_quattro struct_q;
struct_q.a1 = "RET";
struct_q.a2 = vector_identify.back();
struct_q.a3 = "";
struct_q.a4 = "";
quattro.push_back(struct_q);
vector_identify.pop_back();
if (x[cur_index] == ";")
{
increase_cur_index();
len_show_kongge--;
return 1;
}
else
{
string error = "error at the function after 'return' lost ';'";
throw error;
}
len_show_kongge--;
return 1;
}
len_show_kongge--;
return 0;
}
len_show_kongge--;
return 0;
}
int exe2::Operational_expression()//<常规运算表达式>
{
cout << show_kongge() << "Operational_expression() " << endl;
len_show_kongge++;
Identifier();
if (is_if_function(vector_identify.back()))
{//判断是否是一个函数 如果是 则只调用
//生成四元式 调用函数(call,function_name,,)
struct_quattro struct_q;
struct_q.a1 = "call";
struct_q.a2 = vector_identify.back();
use_function_name = struct_q.a2;
actual_param_now = 0;
vector_identify.pop_back();
struct_q.a3 = "";
struct_q.a4 = "";
quattro.push_back(struct_q);
if (x[cur_index] == "(")
{
increase_cur_index();
//函数传入实参
Actual_parameter();
if (x[cur_index] == ")")
{
increase_cur_index();
//根据函数类型 生成相应的 临时变量类型
string tmp_function_return;//函数返回值的临时名字
tmp_function_return = get_function_return_tmp();
list<Symbol_table>::iterator l_s_i_t;
for (l_s_i_t = list_symbol.begin(); l_s_i_t != list_symbol.end(); l_s_i_t++)
{
if (use_function_name == (*l_s_i_t).NAME)
break;
}
string function_type=((PFINL *)(*l_s_i_t).ADDR)->type_function;//函数的返回类型
//cout << "函数返回类型为" << function_type<<endl;
Symbol_table tmp_sym_bol;
list<Symbol_table>::iterator l_i;
for (l_i = list_symbol.begin(); l_i != list_symbol.end(); l_i++)
{
//cout << (*l_i).NAME<<" the function is "<< function_type<< endl;
if (("struct_"+(*l_i).NAME) == function_type)
break;
}
if (l_i == list_symbol.end())
{//没有这个struct类型
string error = "error don't has this struct type when want to define a struct " + vector_identify.back();
throw error;
}
tmp_sym_bol.NAME = tmp_function_return;
tmp_sym_bol.CAT = "v";
LEN * tmp_len = new LEN();
tmp_sym_bol.ADDR = (*l_i).ADDR;
tmp_sym_bol.TYPE = (*l_i).TYPE;
tmp_sym_bol.CAT = (*l_i).CAT;
list_symbol.push_back(tmp_sym_bol);
//这个函数返回的临时变量 需要加入到当前函数的临时变量中
((PFINL*)(*l_s_i_t).ADDR)->VALL_TABLE->list_tmp_var.push_back(tmp_sym_bol.NAME);
//cout << "把函数的临时会返回值 保存" << endl;
vector_identify.push_back(tmp_sym_bol.NAME);
//将其加入到struct list链表中
struct_name_type tmp_s_n_t;
tmp_s_n_t.name = tmp_sym_bol.NAME;
tmp_s_n_t.type = function_type;
//cout << tmp_s_n_t.name << " !!!!!!!!!! " << tmp_s_n_t.type << endl;
list_struct_name_type.push_back(tmp_s_n_t);
struct_quattro struct_q;
struct_q.a1 = "function_return";
struct_q.a2 = tmp_function_return;
vector_identify.push_back(tmp_function_return);//将这个临时变量保存起来
struct_q.a3 = "";
struct_q.a4 = "";
quattro.push_back(struct_q);
if (x[cur_index] == ";")
increase_cur_index();
else
{
string error = "error lost ';' after use function";
throw;
}
}
else
{
string error = "use Function lost ')'" + cur_index;
throw error;
}
}
else
{
string error = "use Function lost '('" + cur_index;
throw error;
}
len_show_kongge--;
return 1;
}
if (x[cur_index] == "=")
{
increase_cur_index();//<<"wkn"<<endl;
flag_log_opt = false;
E();//<算术运算>
//检查两个运算类型是否一样
struct_quattro struct_q;
struct_q.a1 = "=";
struct_q.a2 = vector_identify.back();
int len_a1 = get_data_len(vector_identify.back());
vector_identify.pop_back();
struct_q.a3 = "";
struct_q.a4 = vector_identify.back();
int len_a2 = get_data_len(vector_identify.back());
//cout << len_a1 << " " << len_a2 << endl;
if (len_a2 != len_a1)
{
string error = "can't convert type ";
throw error;
}
quattro.push_back(struct_q);
vector_identify.pop_back();
if (x[cur_index] == ";")
{
increase_cur_index();//<<"wkn"<<endl;
len_show_kongge--;
return 1;
}
}
len_show_kongge--;
}
int exe2::Logical_judgement()//<逻辑判断表达式>
{
cout << show_kongge() << "Logical_judgement()" << endl;
len_show_kongge++;
flag_log_opt = true;
A();
len_show_kongge--;
return 1;
}
int exe2::A()
{
cout << show_kongge() << "A()" << endl;
len_show_kongge++;
B();
A1();
len_show_kongge--;
return 1;
}
int exe2::A1()
{
cout << show_kongge() << "A1()" << endl;
len_show_kongge++;
if (x[cur_index] == "|")
{
increase_cur_index();//<<"wkn"<<endl;
B();
struct_quattro struct_q;
struct_q.a1 = "|";
struct_q.a3 = vector_identify.back();
vector_identify.pop_back();
struct_q.a2 = vector_identify.back();;
vector_identify.pop_back();
string tmp_var = get_tmp_var();
struct_q.a4 = tmp_var;
int len_1 = get_data_len(struct_q.a3);
int len_2 = get_data_len(struct_q.a2);
if (len_1 != len_2)
{
string error = "can't convert type";
throw error;
}
vector_identify.push_back(tmp_var);
quattro.push_back(struct_q);
A1();
len_show_kongge--;
return 1;
}
len_show_kongge--;
return 0;
}
int exe2::B()
{
cout << show_kongge() << "B()" << endl;
len_show_kongge++;
C();
B1();
len_show_kongge--;
return 1;
}
int exe2::B1()
{
cout << show_kongge() << "B1()" << endl;
len_show_kongge++;
if (x[cur_index] == "&")
{
increase_cur_index();//<<"wkn"<<endl;
C();
//生成四元式 (操作符,对象1,对象2,结果)
struct_quattro struct_q;
struct_q.a1 = "&";
struct_q.a3 = vector_identify.back();
vector_identify.pop_back();
struct_q.a2 = vector_identify.back();;
vector_identify.pop_back();
string tmp_var = get_tmp_var();
struct_q.a4 = tmp_var;
int len_1 = get_data_len(struct_q.a3);
int len_2 = get_data_len(struct_q.a2);
if (len_1 != len_2)
{
string error = "can't convert type";
throw error;
}
vector_identify.push_back(tmp_var);
quattro.push_back(struct_q);
B1();
len_show_kongge--;
return 1;
}
len_show_kongge--;
return 0;
}
int exe2::C()
{
cout << show_kongge() << "C()" << endl;
len_show_kongge++;
D();
C1();
len_show_kongge--;
return 1;
}
int exe2::C1()
{
cout << show_kongge() << "C1()" << endl;
len_show_kongge++;
if (x[cur_index] == "==")
{
increase_cur_index();//<<"wkn"<<endl;
D();
//生成四元式 (操作符,对象1,对象2,结果)
struct_quattro struct_q;
struct_q.a1 = "==";
struct_q.a3 = vector_identify.back();
vector_identify.pop_back();
struct_q.a2 = vector_identify.back();;
vector_identify.pop_back();
string tmp_var = get_tmp_var();
struct_q.a4 = tmp_var;
int len_1 = get_data_len(struct_q.a3);
int len_2 = get_data_len(struct_q.a2);
if (len_1 != len_2)
{
string error = "can't convert type";
throw error;
}
vector_identify.push_back(tmp_var);
quattro.push_back(struct_q);
C1();
len_show_kongge--;
return 1;
}
len_show_kongge--;
return 0;
}
int exe2::D()
{
cout << show_kongge() << "D()" << endl;
len_show_kongge++;
E();
D1();
len_show_kongge--;
return 1;
}
int exe2::D1()
{
cout << show_kongge() << "D1()" << endl;
len_show_kongge++;
if (x[cur_index] == "<" | x[cur_index] == "<=" | x[cur_index] == ">" | x[cur_index] == ">=")
{
string tmp_o = x[cur_index];
increase_cur_index();//<<"wkn"<<endl;
E();
//生成运算四元式 (操作符,对象1,对象2,结果)
struct_quattro struct_q;
struct_q.a1 = tmp_o;
struct_q.a3 = vector_identify.back();
vector_identify.pop_back();
struct_q.a2 = vector_identify.back();;
vector_identify.pop_back();
string tmp_var = get_tmp_var();
struct_q.a4 = tmp_var;
int len_1 = get_data_len(struct_q.a3);
int len_2 = get_data_len(struct_q.a2);
if (len_1 != len_2)
{
string error = "can't convert type";
throw error;
}
vector_identify.push_back(tmp_var);
quattro.push_back(struct_q);
D1();
len_show_kongge--;
return 1;
}
len_show_kongge--;
return 0;
}
int exe2::E()
{
cout << show_kongge() << "E()" << endl;
len_show_kongge++;
F();
//生成运算四元式 (操作符,对象1,对象2,结果)
E1();
len_show_kongge--;
return 1;
}
int exe2::E1()
{
cout << show_kongge() << "E1()" << endl;
len_show_kongge++;
if (x[cur_index] == "-" | x[cur_index] == "+")
{
string tmp_o = x[cur_index];
increase_cur_index();//<<"wkn"<<endl;
F();
struct_quattro struct_q;
struct_q.a1 = tmp_o;
struct_q.a3 = vector_identify.back();
vector_identify.pop_back();
struct_q.a2 = vector_identify.back();;
vector_identify.pop_back();
string tmp_var = get_tmp_var();
struct_q.a4 = tmp_var;
int len_1 = get_data_len(struct_q.a3);
int len_2 = get_data_len(struct_q.a2);
if (len_1 != len_2)
{
string error = "can't convert type";
throw error;
}
vector_identify.push_back(tmp_var);
quattro.push_back(struct_q);
E1();
len_show_kongge--;
return 1;
}
len_show_kongge--;
return 0;
}
int exe2::F()
{
cout << show_kongge() << "F()" << endl;
len_show_kongge++;
G();
//生成运算四元式 (操作符,对象1,对象2,结果)
F1();
len_show_kongge--;
return 1;
}
int exe2::F1()
{
cout << show_kongge() << "F1()" << endl;
len_show_kongge++;
if (x[cur_index] == "*" | x[cur_index] == "/")
{
string tmp_o = x[cur_index];
increase_cur_index();//<<"wkn"<<endl;
G();
//生成运算四元式 (操作符,对象1,对象2,结果)
struct_quattro struct_q;
struct_q.a1 = tmp_o;
struct_q.a3 = vector_identify.back();
vector_identify.pop_back();
struct_q.a2 = vector_identify.back();
vector_identify.pop_back();
string tmp_var = get_tmp_var();
struct_q.a4 = tmp_var;
vector_identify.push_back(tmp_var);
int len_1 = get_data_len(struct_q.a3);
int len_2 = get_data_len(struct_q.a2);
if (len_1 != len_2)
{
string error = "can't convert type";
throw error;
}
quattro.push_back(struct_q);
F1();
len_show_kongge--;
return 1;
}
len_show_kongge--;
return 0;
}
int exe2::G()
{
cout << show_kongge() << "G()" << endl;
len_show_kongge++;
if (x[cur_index] == "(")
{
increase_cur_index();
if (flag_log_opt == true)
A();
else
E();
if (x[cur_index] == ")")
{
increase_cur_index();
}
else
{
string error = "lost ')' at " + cur_index;
throw error;
}
}
else if (is_if_num(x[cur_index]))//是否是一个常量
{
vector_identify.push_back(x[cur_index]);
increase_cur_index();//<<"wkn"<<endl;
}
else if (is_if_function(x[cur_index]) == 1)//是否是一个函数
{
//cout << x[cur_index] << "是个函数" << endl;
vector_identify.push_back(x[cur_index]); //是个函数 但是没有加入 vector_identify
cur_index++; //并指向下一个
actual_param_now = 0;
//生成四元式 调用函数(call,function_name,,)
struct_quattro struct_q;
struct_q.a1 = "call";
struct_q.a2 = vector_identify.back();
use_function_name = struct_q.a2;
vector_identify.pop_back();
struct_q.a3 = "";
struct_q.a4 = "";
quattro.push_back(struct_q);
//cout << x[cur_index] << endl;
if (x[cur_index] == "(")
{
increase_cur_index();
//函数传入实参
Actual_parameter();
if (x[cur_index] == ")")
{
/*/
increase_cur_index();
string tmp_function_return = get_tmp_var();
struct_quattro struct_q;
struct_q.a1 = "function_return";
struct_q.a2 = tmp_function_return;
vector_identify.push_back(tmp_function_return);
struct_q.a3 = "";
struct_q.a4 = "";
quattro.push_back(struct_q);
*/
increase_cur_index();
//根据函数类型 生成相应的 临时变量类型
string tmp_function_return;//函数返回值的临时名字
tmp_function_return = get_function_return_tmp();
list<Symbol_table>::iterator l_s_i_t;
for (l_s_i_t = list_symbol.begin(); l_s_i_t != list_symbol.end(); l_s_i_t++)
{
//cout << use_function_name << " " << (*l_s_i_t).NAME << endl;
if (use_function_name == (*l_s_i_t).NAME)
break;
}
string function_type = ((PFINL *)(*l_s_i_t).ADDR)->type_function;//函数的返回类型
//cout << "函数返回类型为" << function_type<<endl;
Symbol_table tmp_sym_bol;
list<Symbol_table>::iterator l_i;
for (l_i = list_symbol.begin(); l_i != list_symbol.end(); l_i++)
{
//cout << (*l_i).NAME<<" the function is "<< function_type<< endl;
if (("struct_" + (*l_i).NAME) == function_type)
break;
}
if (l_i == list_symbol.end())
{//没有这个struct类型
string error = "error don't has this struct type when want to define a struct " + vector_identify.back();
throw error;
}
tmp_sym_bol.NAME = tmp_function_return;
tmp_sym_bol.CAT = "v";
LEN * tmp_len = new LEN();
tmp_sym_bol.ADDR = (*l_i).ADDR;
tmp_sym_bol.TYPE = (*l_i).TYPE;
tmp_sym_bol.CAT = (*l_i).CAT;
list_symbol.push_back(tmp_sym_bol);
//这个函数返回的临时变量 需要加入到当前函数的临时变量中
((PFINL*)(*l_s_i_t).ADDR)->VALL_TABLE->list_tmp_var.push_back(tmp_sym_bol.NAME);
//cout << "把函数的临时会返回值 保存" << endl;
//vector_identify.push_back(tmp_sym_bol.NAME);
//将其加入到struct list链表中
struct_name_type tmp_s_n_t;
tmp_s_n_t.name = tmp_sym_bol.NAME;
tmp_s_n_t.type = function_type;
//cout << tmp_s_n_t.name << " !!!!!!!!!! " << tmp_s_n_t.type << endl;
list_struct_name_type.push_back(tmp_s_n_t);
struct_quattro struct_q;
struct_q.a1 = "function_return";
struct_q.a2 = tmp_function_return;
vector_identify.push_back(tmp_function_return);//将这个临时变量保存起来
struct_q.a3 = "";
struct_q.a4 = "";
quattro.push_back(struct_q);
if (x[cur_index] == ";")
increase_cur_index();
else
{
string error = "error lost ';' after use function";
throw;
}
}
else
{
string error = "use Function lost ')'" + cur_index;
throw error;
}
}
else
{
string error = "use Function lost '('" + cur_index;
throw error;
}
}
else if (is_if_i(x[cur_index]))//是否是一个用户定义的变量
{
//increase_cur_index();
Identifier();
//压入栈
//动作
}
else
{
string error = "error at optional " + cur_index;
throw error;
}
len_show_kongge--;
}
void exe2::show_list_DSEG()
{
cout << "the DSEG is ---------------------------" << endl;
for (list<string>::iterator l_i = list_DSEG.begin(); l_i != list_DSEG.end(); l_i++)
cout << (*l_i) << endl;
}
void exe2::show_list_CSEG()
{
cout << "the CSEG is ---------------------------" << endl;
for (list<string>::iterator l_i = list_CSEG.begin(); l_i != list_CSEG.end(); l_i++)
cout << (*l_i) << endl;
}
void exe2::start()
{
cout << "start()" << endl;
//开始应该开辟基本所需
list<struct_quattro>::iterator list_iterator_struct_quattro;//遍历所有四元式
for (list_iterator_struct_quattro = list_quattro.begin(); list_iterator_struct_quattro != list_quattro.end(); list_iterator_struct_quattro++)
{
if ((*list_iterator_struct_quattro).a1 == "DEF" && (*list_iterator_struct_quattro).a2 == "Function")
{//遇到定义struct 循环 直到 struct结束
string Function_name = (*list_iterator_struct_quattro).a4;//获取函数名
string Function_return_type = (*list_iterator_struct_quattro).a3;//获取函数返回值 是带 struct_ 前缀的
//cout << "函数定义中" << endl;
for (; (*list_iterator_struct_quattro).a1 != "DEF_END"; list_iterator_struct_quattro++)
{
string tmp_last_struct_type;
if ((*list_iterator_struct_quattro).a1 == "struct_member")
{
//cout << "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"<<(*list_iterator_struct_quattro).a2 << endl;
int sum_offset = 0;
//第一个struct_member是函数的局部成员 偏移量要从函数的VALL表中获取
sum_offset += get_VALL_off(Function_name, (*list_iterator_struct_quattro).a2);
tmp_last_struct_type = get_type_by_name((*list_iterator_struct_quattro).a2);//通过当前变量获取相应的struct类型
//cout << "START offset is " << sum_offset << endl;
list_iterator_struct_quattro++;
while ((*list_iterator_struct_quattro).a1 == "struct_member")
{
//获得struct 类型后 查找偏移量
sum_offset += get_member_offset_by_name(tmp_last_struct_type, (*list_iterator_struct_quattro).a2);
tmp_last_struct_type = get_type_by_name((*list_iterator_struct_quattro).a2);
list_iterator_struct_quattro++;
}
//接下来的是 最后一个 是struct——member的最后一个成员
//cout<< " var_name is " << (*list_iterator_struct_quattro).a2 << " 获取的结构体类型 " << get_type_by_name((*list_iterator_struct_quattro).a2) << endl;
list<Symbol_table>::iterator l_s_i = list_symbol.begin();
sum_offset += get_member_offset_by_name(tmp_last_struct_type, (*list_iterator_struct_quattro).a2);
struct_tmp_offset_var tmp_s_o_v;
tmp_s_o_v.var_name = (*list_iterator_struct_quattro).a2;
tmp_s_o_v.offset = vert_int_to_string(sum_offset);
list_tmp_var_offset.push_back(tmp_s_o_v);//先保存起来 不知道后面 这个变量位于a2 还是a3 或者 a4
}
}
list_iterator_struct_quattro--;
}
}
/*
cout << "记录到的 !@#$%^&*()_" << endl;
for (list<struct_tmp_offset_var>::iterator r = list_tmp_var_offset.begin(); r != list_tmp_var_offset.end(); r++)
{
cout << (*r).var_name << endl;
}
cout << "记录到的 !@#$%^&*()_" << endl;
*/
for (list_iterator_struct_quattro = list_quattro.begin(); list_iterator_struct_quattro != list_quattro.end(); list_iterator_struct_quattro++)
{
if ((*list_iterator_struct_quattro).a1 == "DEF" && (*list_iterator_struct_quattro).a2 == "struct")
{//遇到定义struct 跳过 循环 直到 struct结束
//cout << "结构体定义中" << endl;
for (; (*list_iterator_struct_quattro).a1 != "DEF_END"; list_iterator_struct_quattro++);
}
if ((*list_iterator_struct_quattro).a1 == "DEF" && (*list_iterator_struct_quattro).a2 == "Function")
{//遇到定义struct 循环 直到 struct结束
string Function_name = (*list_iterator_struct_quattro).a4;//获取函数名
string Function_return_type = (*list_iterator_struct_quattro).a3;//获取函数返回值 是带 struct_ 前缀的
list_CSEG.push_back(Function_name + " PROC START");
//cout << "函数定义中" << endl;
for (; (*list_iterator_struct_quattro).a1 != "DEF_END"; list_iterator_struct_quattro++)
{
ASEEM_execute(&list_iterator_struct_quattro, Function_name);
//if
if ((*list_iterator_struct_quattro).a1 == "if")
{
list_iterator_struct_quattro++;
//if条件判断
while ((*list_iterator_struct_quattro).a1 != "jz" && (*list_iterator_struct_quattro).a3 != "if_label_judge")
{
ASEEM_execute(&list_iterator_struct_quattro, Function_name);
list_iterator_struct_quattro++;
}
if (is_if_num((*list_iterator_struct_quattro).a2))
{//是否是常数
list_CSEG.push_back("MOV " + (*list_iterator_struct_quattro).a2 + " ,R");
}
else if (if_is_Gobal_var((*list_iterator_struct_quattro).a2, Function_name))//判断变量是全局还是局部
{//是个全局变量 将变量装入寄存器
list_CSEG.push_back("ST " + (*list_iterator_struct_quattro).a2 + " ,R");
}
else
{//需要查找其VALL的偏移量来完成间接寻址
string tmp_offset;
if ((tmp_offset = if_is_cahe_struct_var((*list_iterator_struct_quattro).a2)) != "")
{//从list_tmp_var_offset找到 是个struct成员
list_CSEG.push_back("MOV [SP+" + tmp_offset + "] ,R");
}
else
{//是个常规局部变量
int tmp_len = get_VALL_off(Function_name, (*list_iterator_struct_quattro).a2);
//获取偏移量后 需要 将其放入内存中
list_CSEG.push_back("ST [SP+" + vert_int_to_string(tmp_len) + "] ,R");
}
}
list_CSEG.push_back("XOR R");
list_CSEG.push_back("JZ ");//需要保留反填 判断if条件
list<string>::iterator tmp_if_jz = --list_CSEG.end();
list_iterator_struct_quattro++;
//执行if体
while ((*list_iterator_struct_quattro).a1 != "JMP" && (*list_iterator_struct_quattro).a2 != "label_if_end")
{
ASEEM_execute(&list_iterator_struct_quattro, Function_name);
list_iterator_struct_quattro++;
}
//if结构体执行结束 开始强制跳转 到 if结束处
list_CSEG.push_back("JMP ");
list<string>::iterator tmp_if_jmp = --list_CSEG.end();
list_iterator_struct_quattro++;
//if的else结构体 先生成else标签
string if_else_label = produce_a_label();//生成label
(*tmp_if_jz).append(if_else_label);//反填 if的条件判断
list_CSEG.push_back(if_else_label + ":");
while ((*list_iterator_struct_quattro).a1 != "if_end" && (*list_iterator_struct_quattro).a2 != "label_if_end")
{
ASEEM_execute(&list_iterator_struct_quattro, Function_name);
list_iterator_struct_quattro++;
}
//生成if结束标签 反填if执行体结束处JMP标签
string tmp_if_end_label = produce_a_label();
(*tmp_if_jmp).append(tmp_if_end_label);
list_CSEG.push_back(tmp_if_end_label + ":");
}
//while
if ((*list_iterator_struct_quattro).a1 == "while")
{
//生成一个while开始标签
string tmp_while_label = produce_a_label();
list_CSEG.push_back(tmp_while_label + ":");
list_iterator_struct_quattro++;
//开始判断 while条件
while ((*list_iterator_struct_quattro).a1 != "JZ" && (*list_iterator_struct_quattro).a3 != "label_while_end")
{
ASEEM_execute(&list_iterator_struct_quattro, Function_name);
list_iterator_struct_quattro++;
}
//while判断结束 条件不成立跳转while结束 不需要标签
//判断 while条件 结果的值 送入R寄存器
if (is_if_num((*list_iterator_struct_quattro).a2))
{//是否是常数
list_CSEG.push_back("MOV " + (*list_iterator_struct_quattro).a2 + " ,R");
}
else if (if_is_Gobal_var((*list_iterator_struct_quattro).a2, Function_name))//判断变量是全局还是局部
{//是个全局变量 将变量装入寄存器
list_CSEG.push_back("ST " + (*list_iterator_struct_quattro).a2 + " ,R");
}
else
{//需要查找其VALL的偏移量来完成间接寻址
string tmp_offset;
if ((tmp_offset = if_is_cahe_struct_var((*list_iterator_struct_quattro).a2)) != "")
{//从list_tmp_var_offset找到 是个struct成员
list_CSEG.push_back("MOV [SP+" + tmp_offset + "] ,R");
}
else
{//是个常规局部变量
int tmp_len = get_VALL_off(Function_name, (*list_iterator_struct_quattro).a2);
//获取偏移量后 需要 将其放入内存中
list_CSEG.push_back("ST [SP+" + vert_int_to_string(tmp_len) + "] ,R");
}
}
list_CSEG.push_back("XOR R");//为了让标志位变化 从而JZ
list_CSEG.push_back("JZ ");//后面加while执行体结束处的标签
list<string>::iterator while_jz_label = --list_CSEG.end();
list_iterator_struct_quattro++;
//while执行体
while ((*list_iterator_struct_quattro).a1 != "JMP" && (*list_iterator_struct_quattro).a1 != "label_while_start")
{
ASEEM_execute(&list_iterator_struct_quattro, Function_name);
list_iterator_struct_quattro++;
}
//while结构结束 无条件跳转到while判断处
list_CSEG.push_back("JMP " + tmp_while_label);
tmp_while_label = produce_a_label();
list_CSEG.push_back(tmp_while_label + ":");
(*while_jz_label).append(tmp_while_label);
}
}
list_CSEG.push_back(Function_name + " PROC ENDS");
list_iterator_struct_quattro--;
}
//定义全局变量
if ((*list_iterator_struct_quattro).a1 == "var")
{//全局变量 生成相应空间
string type_name;
if ((*list_iterator_struct_quattro).a2 == "int" || (*list_iterator_struct_quattro).a2 == "void")
type_name = (*list_iterator_struct_quattro).a2;
else
{
type_name = (*list_iterator_struct_quattro).a2.erase(0, 7);
}
//cout << "全局变量的类型是 " << type_name << endl;
int data_len = get_data_len(type_name);//获取相应类型的长度
//cout << "全局变量的 数据长度是 " << data_len << endl;
list_iterator_struct_quattro++;//指向数据定义
while ((*list_iterator_struct_quattro).a1 != "var" && (*list_iterator_struct_quattro).a1 != "DEF")
{//符号表查找不到的标识符 就不再进行定义全局变量 变为下一个类型
list_DSEG.push_back((*list_iterator_struct_quattro).a1 + " DB " + vert_int_to_string(data_len) + " DUP(?)");//DSEG中共初始化
//cout << (*list_iterator_struct_quattro).a1 << endl;
list_iterator_struct_quattro++;
if (list_iterator_struct_quattro == list_quattro.end())
{
//cout << "四元式序列已经结束" << endl;
break;
}
}
list_iterator_struct_quattro--;
}
}
list_DSEG.push_back("DSEG SEGMENT ENDS");
list_DSEG.push_back("STACK SEGMENT");
list_DSEG.push_back("DB 20000 DUP(0)");
list_DSEG.push_back("STACK SEGMENT ENDS");
list_CSEG.push_back("CSEG SEGMENT ENDS");
}
list<Symbol_table>::iterator exe2::find_name(string name)
{
cout << "find_name(string name)" << endl;
for (list<Symbol_table>::iterator tmp_l_i_sym_tab = (*list_Symbol_table).begin(); tmp_l_i_sym_tab != (*list_Symbol_table).end(); tmp_l_i_sym_tab++)
{
if (name == (*tmp_l_i_sym_tab).NAME)
return tmp_l_i_sym_tab;
}
}
bool exe2::if_exist_name(string name)
{
cout << "if_exist_name(string name)" << endl;
if (find_name(name) == (*list_Symbol_table).end())
return false;
else
return true;
}
bool exe2::if_is_Gobal_var(string var_name, string Function_name)//判断是全局变量还是局部变量
{
list<Symbol_table>::iterator l_i;
for (l_i = list_symbol.begin(); l_i != list_symbol.end(); l_i++)
{
if ((*l_i).CAT == "f")
{
//cout <<"if_is_Gobal_var() the function name is "<< (*l_i).NAME <<" the var is "<< var_name << " the aim function name is "<< Function_name<<endl;
for (list<struct_VALL>::iterator l_sv_i = ((PFINL *)(*l_i).ADDR)->VALL_TABLE->member.begin(); l_sv_i != ((PFINL *)(*l_i).ADDR)->VALL_TABLE->member.end(); l_sv_i++)
{
//cout << "!!!!!!!!!!!!!!!" << (*l_sv_i).var_name << "!!!!!!!!!!!!!!!" << var_name << endl;
if ((*l_sv_i).var_name == var_name)
{
//cout << "" << (*l_i).NAME << "!!!!!!!!!!!!!!!" << Function_name << endl;
if ((*l_i).NAME == Function_name)
{
return false;//是个局部变量
}
else
{
string error = "error this var don't define " + var_name + " at the function " + Function_name;
cout << error << endl;
throw error;
}
}
}
}
}
for (l_i = list_symbol.begin(); l_i != list_symbol.end(); l_i++)
{
if ((*l_i).NAME == var_name)
{
return true;//是个全局变量
}
}
return false;
}
int exe2::get_VALL_off(string Function_name, string var_name)//获取 某函数 其中的 某变量 的偏移量
{
//cout << "get_VALL_off() function is " << Function_name << " the var name is " << var_name << endl;
list<Symbol_table>::iterator l_i;
for (l_i = list_symbol.begin(); l_i != list_symbol.end(); l_i++)
{
if ((*l_i).NAME == Function_name)
for (list<struct_VALL>::iterator l_sv_i = ((PFINL *)(*l_i).ADDR)->VALL_TABLE->member.begin(); l_sv_i != ((PFINL *)(*l_i).ADDR)->VALL_TABLE->member.end(); l_sv_i++)
{
if ((*l_sv_i).var_name == var_name)
{
return (*l_sv_i).offset;
}
}
}
//cout << "get_VALL_off() don't find " << endl;
}
string exe2::get_type_by_name(string var_name)//通过变量名 获取 struct 类型 如果是int void 时 返回""字符串
{
for (list<struct_name_type>::iterator l_i_s_n_t = list_struct_name_type.begin(); l_i_s_n_t != list_struct_name_type.end(); l_i_s_n_t++)
{
//cout << "get_type_by_name(string var_name)" << (*l_i_s_n_t).type << " " << var_name << endl;
if ((*l_i_s_n_t).name == var_name)
return (*l_i_s_n_t).type;
}
return "";
}
int exe2::get_member_offset_by_name(string struct_type_name, string member_name)//通过名字在struct获取相对于 结构体的偏移量
{
int sum_off = 0;//用于返回 偏移量
//cout << "get_member_offset_by_name()" << struct_type_name<<" memeber is "<< member_name << endl;
for (list<Symbol_table>::iterator l_s_i = list_symbol.begin(); l_s_i != list_symbol.end(); l_s_i++)
{
if ((*l_s_i).NAME == struct_type_name)
{
for (list< _RINFL>::iterator l_R_i = (*(*(*l_s_i).TYPE).TPIONT).list_rinfl.begin(); l_R_i != (*(*(*l_s_i).TYPE).TPIONT).list_rinfl.end(); l_R_i++)
{
if ((*l_R_i).MEMBER_NAME == member_name)
{
return (*l_R_i).OFF;
}
}
}
}
string error = "error";
throw error;
return sum_off;
}
string exe2::if_is_cahe_struct_var(string var_name)//查表 是否是一个struct的list缓冲值
{
//cout << "if_is_cahe_struct_var(string var_name) " << var_name << endl;
for (list<struct_tmp_offset_var>::iterator l_i = list_tmp_var_offset.begin(); l_i != list_tmp_var_offset.end(); l_i++)
{
if (var_name == (*l_i).var_name)
return (*l_i).offset;
}
return "";
}
string exe2::produce_a_label()//生成一个汇编代码中的标签
{
int_produce_a_label++;
return "LABEL_" + vert_int_to_string(int_produce_a_label);
}
void exe2::ASEEM_execute(list<struct_quattro>::iterator *list_iterator_struct_quattro, string Function_name)//执行体四元式转化为汇编
{
//四则运算 逻辑双运算
if ((**list_iterator_struct_quattro).a1 == "+" || (**list_iterator_struct_quattro).a1 == "-" || (**list_iterator_struct_quattro).a1 == "*" || (**list_iterator_struct_quattro).a1 == "/" || (**list_iterator_struct_quattro).a1 == "&" || (**list_iterator_struct_quattro).a1 == "|" || (**list_iterator_struct_quattro).a1 == ">" || (**list_iterator_struct_quattro).a1 == "<")
{
//需要判断是全局变量还是局部变量
string tmp_opt = (**list_iterator_struct_quattro).a1;
string tmp_ORDER;
if (tmp_opt == "+")
tmp_ORDER = "ADD ";
if (tmp_opt == "-")
tmp_ORDER = "SUB ";
if (tmp_opt == "*")
tmp_ORDER = "MUL ";
if (tmp_opt == "/")
tmp_ORDER = "DIV ";
if (tmp_opt == "|")
tmp_ORDER = "OR ";
if (tmp_opt == "&")
tmp_ORDER = "AND ";
if (tmp_opt == ">")
tmp_ORDER = "GT ";
if (tmp_opt == "<")
tmp_ORDER = "LT ";
//操作数 1
if (is_if_num((**list_iterator_struct_quattro).a2))
{//是否是常数
list_CSEG.push_back("MOV " + (**list_iterator_struct_quattro).a2 + " ,R");
}
else if (if_is_Gobal_var((**list_iterator_struct_quattro).a2, Function_name))//判断变量是全局还是局部
{//是个全局变量 将变量装入寄存器
list_CSEG.push_back("ST " + (**list_iterator_struct_quattro).a2 + " ,R");
}
else
{//需要查找其VALL的偏移量来完成间接寻址
string tmp_offset;
if ((tmp_offset = if_is_cahe_struct_var((**list_iterator_struct_quattro).a2)) != "")
{//从list_tmp_var_offset找到 是个struct成员
list_CSEG.push_back("MOV [SP+" + tmp_offset + "] ,R");
}
else
{//是个常规局部变量
int tmp_len = get_VALL_off(Function_name, (**list_iterator_struct_quattro).a2);
//获取偏移量后 需要 将其放入内存中
list_CSEG.push_back("ST [SP+" + vert_int_to_string(tmp_len) + "] ,R");
}
}
//操作数2
string opt_2;
//cout << (**list_iterator_struct_quattro).a3 << endl;
if (is_if_num((**list_iterator_struct_quattro).a3))
{
//list_CSEG.push_back("MOV " + (**list_iterator_struct_quattro).a3 + " ,R");
opt_2 = (**list_iterator_struct_quattro).a3;
}
else if (if_is_Gobal_var((**list_iterator_struct_quattro).a3, Function_name))//判断变量是全局还是局部
{//是个全局变量 将变量装入寄存器
opt_2 = (**list_iterator_struct_quattro).a3;
}
else
{//需要查找其VALL的偏移量来完成间接寻址
string tmp_offset;
if ((tmp_offset = if_is_cahe_struct_var((**list_iterator_struct_quattro).a3)) != "")
{//从list_tmp_var_offset找到 是个struct成员
opt_2 = "[SP+" + tmp_offset + "]";
}
else
{//是个常规局部变量
int tmp_len = get_VALL_off(Function_name, (**list_iterator_struct_quattro).a3);
//获取偏移量后 需要 将其放入内存中
opt_2 = "[SP+" + vert_int_to_string(tmp_len) + "]";
}
}
list_CSEG.push_back(tmp_ORDER + " R ," + opt_2);
//需要将结果送回
if (is_if_num((**list_iterator_struct_quattro).a4))
{
//list_CSEG.push_back("MOV " + (**list_iterator_struct_quattro).a3 + " ,R");
opt_2 = (**list_iterator_struct_quattro).a4;
string error = "error the result can't divert to a number !";
throw error;
}
else if (if_is_Gobal_var((**list_iterator_struct_quattro).a4, Function_name))//判断变量是全局还是局部
{//是个全局变量 将变量装入寄存器
opt_2 = (**list_iterator_struct_quattro).a4;
}
else
{//需要查找其VALL的偏移量来完成间接寻址
string tmp_offset;
if ((tmp_offset = if_is_cahe_struct_var((**list_iterator_struct_quattro).a4)) != "")
{//从list_tmp_var_offset找到 是个struct成员
opt_2 = "[SP+" + tmp_offset + "]";
}
else
{//是个常规局部变量
int tmp_len = get_VALL_off(Function_name, (**list_iterator_struct_quattro).a4);
//获取偏移量后 需要 将其放入内存中
opt_2 = "[SP+" + vert_int_to_string(tmp_len) + "]";
}
}
list_CSEG.push_back("MOV R ," + opt_2);
}
//赋值操作
if ((**list_iterator_struct_quattro).a1 == "=")
{//赋值操作 如果是结构体 需要测量长度 然后逐个字节复制
//操作数 1
string opt_1;
if (is_if_num((**list_iterator_struct_quattro).a2))
{//是否是常数
opt_1 = (**list_iterator_struct_quattro).a2;
}
else if (if_is_Gobal_var((**list_iterator_struct_quattro).a2, Function_name))//判断变量是全局还是局部
{//是个全局变量 将变量装入寄存器
opt_1 = (**list_iterator_struct_quattro).a2;
}
else
{//需要查找其VALL的偏移量来完成间接寻址
string tmp_offset;
if ((tmp_offset = if_is_cahe_struct_var((**list_iterator_struct_quattro).a2)) != "")
{//从list_tmp_var_offset找到 是个struct成员
opt_1 = "[SP+" + tmp_offset;
}
else
{//是个常规局部变量
int tmp_len = get_VALL_off(Function_name, (**list_iterator_struct_quattro).a2);
//获取偏移量后 需要 将其放入内存中
opt_1 = "[SP+" + vert_int_to_string(tmp_len);
}
}
//操作数2
string opt_2;
if (is_if_num((**list_iterator_struct_quattro).a4))
{
//list_CSEG.push_back("MOV " + (**list_iterator_struct_quattro).a3 + " ,R");
opt_2 = (**list_iterator_struct_quattro).a4;
string error = "error you can't divert to a number";
throw error;
}
else if (if_is_Gobal_var((**list_iterator_struct_quattro).a4, Function_name))//判断变量是全局还是局部
{//是个全局变量 将变量装入寄存器
opt_2 = (**list_iterator_struct_quattro).a4;
}
else
{//需要查找其VALL的偏移量来完成间接寻址
string tmp_offset;
if ((tmp_offset = if_is_cahe_struct_var((**list_iterator_struct_quattro).a4)) != "")
{//从list_tmp_var_offset找到 是个struct成员
opt_2 = "[SP+" + tmp_offset;
}
else
{//是个常规局部变量
int tmp_len = get_VALL_off(Function_name, (**list_iterator_struct_quattro).a4);
//获取偏移量后 需要 将其放入内存中
opt_2 = "[SP+" + vert_int_to_string(tmp_len);
}
}
//判断是否是struct
string tmp_type = get_type_by_name((**list_iterator_struct_quattro).a4);
if (tmp_type != "")
{//结构体 赋值 多次循坏
//获取结构体的长度
int tmp_len = get_data_len(tmp_type);
int xiangdui_offset = 0;
//cout << tmp_len << " " << xiangdui_offset << endl;
while (xiangdui_offset < tmp_len)
{
list_CSEG.push_back("ST " + opt_1 + "+" + vert_int_to_string(xiangdui_offset) + "],R");
list_CSEG.push_back("ST R," + opt_2 + "+" + vert_int_to_string(xiangdui_offset) + "]");
xiangdui_offset += 2;
}
}
else
{//int void赋值
list_CSEG.push_back("ST " + opt_1 + "],R");
list_CSEG.push_back("ST R," + opt_2 + "]");
}
}
//调用函数
if ((**list_iterator_struct_quattro).a1 == "call")
{//保护现场
//需要检查是否为main函数 如果是 就要生成相应
int len_VALL;//获取当前函数的VALL长度 不是即将调用的函数VALL长度
for (list<Symbol_table>::iterator l_find = list_symbol.begin(); l_find != list_symbol.end(); l_find++)
{
if ((*l_find).NAME == Function_name)
{
len_VALL = ((PFINL*)(*l_find).ADDR)->VALL_TABLE->this_VALL_len;
}
}
list_VALL_len.push_back(vert_int_to_string(len_VALL));
list_CSEG.push_back("MOV SP,[SP+8+" + list_VALL_len.back() + "]");//保存上一次的SP值 +8是因为保护现场消耗8个长度 然后是下一个函数的VALL区间
list_CSEG.push_back("ADD SP," + list_VALL_len.back() + "+8");//增加SP偏移 +8跳过现场保护的数值
list_CSEG.push_back("PUSH R");
list_CSEG.push_back("PUSH CS");
list_CSEG.push_back("PUSH DS");
list_CSEG.push_back("PUSH SS");
list_CSEG.push_back("CALL " + (**list_iterator_struct_quattro).a2);
//传入形参
(*list_iterator_struct_quattro)++;
int param_num = 0;
while ((**list_iterator_struct_quattro).a1 == "actual_parameter")
{
//通过函数名 查看形参赋值是否合法
for (list<Symbol_table>::iterator l_i_s_t = list_symbol.begin(); l_i_s_t != list_symbol.end(); l_i_s_t++)
{
if ((*l_i_s_t).NAME == Function_name)
{
list<string>::iterator tmp_l_i = ((PFINL*)(*l_i_s_t).ADDR)->VALL_TABLE->list_parameter.begin();
for (int i = 0; i < param_num; tmp_l_i++, i++);
//判断形参和实参的类型是否匹配
int len_a = get_data_len((**list_iterator_struct_quattro).a2);//实参
int len_p = get_data_len((*tmp_l_i));//形参
if (len_a != len_p)
{
//cout << len_a << " " << len_p << endl;
string error = "error at Actual_paramater : you can't convert type from " + (**list_iterator_struct_quattro).a2 + " to " + (*tmp_l_i);
throw error;
}
{
//实参传入
string opt_1 = (**list_iterator_struct_quattro).a2;//实参 实参需要从上一个函数区间或者全局变量获取 需要逆向计算
if (is_if_num(opt_1))
{//是否是常数
}
else if (if_is_Gobal_var(opt_1, Function_name))//判断变量是全局还是局部
{//是个全局变量 将变量装入寄存器
}
else
{//需要查找其VALL的偏移量来完成间接寻址
string tmp_offset;
if ((tmp_offset = if_is_cahe_struct_var(opt_1)) != "")
{//从list_tmp_var_offset找到 是个struct成员
opt_1 = "[SP-8-" + list_VALL_len.back() + "+" + tmp_offset;
}
else
{//是个常规局部变量
int tmp_len = get_VALL_off(Function_name, opt_1);
//获取偏移量后 需要 将其放入内存中
opt_1 = "[SP-8-" + list_VALL_len.back() + "+" + vert_int_to_string(tmp_len);
}
}
//形参
string opt_2 = (*tmp_l_i);//形参 形参不需要 计算 都在调用函数的VALL的相应区间
if (is_if_num(opt_2))
{
//list_CSEG.push_back("MOV " + (**list_iterator_struct_quattro).a3 + " ,R");
//opt_2 = (**list_iterator_struct_quattro).a4;
string error = "error you can't divert to a number";
throw error;
}
else if (if_is_Gobal_var(opt_2, Function_name))//判断变量是全局还是局部
{//是个全局变量 将变量装入寄存器
//opt_2 = (**list_iterator_struct_quattro).a4;
}
else
{//需要查找其VALL的偏移量来完成间接寻址
string tmp_offset;
if ((tmp_offset = if_is_cahe_struct_var(opt_2)) != "")
{//从list_tmp_var_offset找到 是个struct成员
opt_2 = "[SP+" + tmp_offset;
}
else
{//是个常规局部变量
int tmp_len = get_VALL_off(Function_name, opt_2);
//获取偏移量后 需要 将其放入内存中
opt_2 = "[SP+" + vert_int_to_string(tmp_len);
}
}
//判断是否是struct
string tmp_type = get_type_by_name((**list_iterator_struct_quattro).a2);
//cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"<<tmp_type <<" "<< (**list_iterator_struct_quattro).a2 << endl;
if (tmp_type != "")
{//结构体 赋值 多次循坏
//获取结构体的长度
int tmp_len = get_data_len(tmp_type);
//cout <<"/"<< tmp_len << endl;
int xiangdui_offset = 0;
//cout << tmp_len << " " << xiangdui_offset << endl;
while (xiangdui_offset < tmp_len)
{
list_CSEG.push_back("ST " + opt_1 + "+" + vert_int_to_string(xiangdui_offset) + "],R");
list_CSEG.push_back("ST R," + opt_2 + "+" + vert_int_to_string(xiangdui_offset) + "]");
xiangdui_offset += 2;
}
}
else
{//int void赋值
list_CSEG.push_back("ST " + opt_1 + "],R");
list_CSEG.push_back("ST R," + opt_2 + "]");
}
}
}
}
param_num++;
(*list_iterator_struct_quattro)++;
}
}
//函数返回 对应return
if ((**list_iterator_struct_quattro).a1 == "RET")//一个是自然返回 一个是return 返回
{
//清理栈 恢复栈
//保存函数返回值
list_CSEG.push_back("RET");
//将返回值填入VALL返回值处
{
{//赋值操作 如果是结构体 需要测量长度 然后逐个字节复制
//操作数 1
string opt_1 = (**list_iterator_struct_quattro).a2;
if (is_if_num(opt_1))
{//是否是常数
//opt_1 = (**list_iterator_struct_quattro).a2;
}
else if (if_is_Gobal_var((**list_iterator_struct_quattro).a2, Function_name))//判断变量是全局还是局部
{//是个全局变量 将变量装入寄存器
//opt_1 = (**list_iterator_struct_quattro).a2;
}
else
{//需要查找其VALL的偏移量来完成间接寻址
string tmp_offset;
if ((tmp_offset = if_is_cahe_struct_var((**list_iterator_struct_quattro).a2)) != "")
{//从list_tmp_var_offset找到 是个struct成员
opt_1 = "[SP+" + tmp_offset;
}
else
{//是个常规局部变量
int tmp_len = get_VALL_off(Function_name, (**list_iterator_struct_quattro).a2);
//获取偏移量后 需要 将其放入内存中
opt_1 = "[SP+" + vert_int_to_string(tmp_len);
}
}
//操作数2
string opt_2 = "[SP+2";//VALL的返回值处
//判断是否是struct
string tmp_type = get_type_by_name((**list_iterator_struct_quattro).a2);
if (tmp_type != "")
{//结构体 赋值 多次循坏
//获取结构体的长度
int tmp_len = get_data_len(tmp_type);
int xiangdui_offset = 0;
//cout << tmp_len << " " << xiangdui_offset << endl;
while (xiangdui_offset < tmp_len)
{
list_CSEG.push_back("ST " + opt_1 + "+" + vert_int_to_string(xiangdui_offset) + "],R");
list_CSEG.push_back("ST R," + opt_2 + "+" + vert_int_to_string(xiangdui_offset) + "]");
xiangdui_offset += 2;
}
}
else
{//int void赋值
list_CSEG.push_back("ST " + opt_1 + "],R");
list_CSEG.push_back("ST R," + opt_2 + "]");
}
}
}
}
//将函数的返回值赋值给其后的目标变量
if ((**list_iterator_struct_quattro).a1 == "function_return")
{
//cout << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
list_CSEG.push_back("POP R");
list_CSEG.push_back("POP CS");
list_CSEG.push_back("POP DS");
list_CSEG.push_back("POP SS");
list_CSEG.push_back("SP MOV [SP+8],SP");//将保存的SP值送入SP 恢复上一个函数区间
//将函数的返回值 赋给相应的变量
string aim_var = (**list_iterator_struct_quattro).a2;//函数返回值的 赋值目标 变量
//何查函数返回值长度 做出判断
int function_return_len = 0;
for (list<Symbol_table>::iterator l_s_i = list_symbol.begin(); l_s_i != list_symbol.end(); l_s_i++)
{
if ((*l_s_i).NAME == Function_name)
{
if ((*l_s_i).TYPE == NULL)
function_return_len = 2;
else
{
function_return_len = ((PFINL *)(*l_s_i).ADDR)->VALL_TABLE->this_function_return_len;//得到函数返回值的长度
function_return_len -= 2;
}
}
}
//cout << function_return_len << " " << get_data_len((**list_iterator_struct_quattro).a2) <<" "<< (**list_iterator_struct_quattro).a2 << endl;
string tmpd = get_type_by_name((**list_iterator_struct_quattro).a2);
if (function_return_len != get_data_len(tmpd.erase(0,7)))
{
string error = "error the Function 's return can't divert at " + Function_name;
throw error;
}
if ((**list_iterator_struct_quattro).a2 == "")
{//函数为void
function_return_len = 0;
}
else
{
//处理函数返回值
string opt_2;
if (is_if_num((**list_iterator_struct_quattro).a2))
{
opt_2 = (**list_iterator_struct_quattro).a2;
}
else if (if_is_Gobal_var((**list_iterator_struct_quattro).a2, Function_name))//判断变量是全局还是局部
{//是个全局变量 将变量装入寄存器
opt_2 = (**list_iterator_struct_quattro).a2;
}
else
{//由VALL的偏移量得到
opt_2 = "[SP+" + list_VALL_len.back() + "+10"; //算出上一个函数的VALL返回值的位置 +10是保护现场和返回值偏移量
}
//处理赋值 变量 (**list_iterator_struct_quattro).a2
string opt_1;
if (is_if_num((**list_iterator_struct_quattro).a2))
{
opt_1 = (**list_iterator_struct_quattro).a2;
}
else if (if_is_Gobal_var((**list_iterator_struct_quattro).a2, Function_name))//判断变量是全局还是局部
{//是个全局变量 将变量装入寄存器
opt_1 = (**list_iterator_struct_quattro).a2;
}
else
{//需要查找其VALL的偏移量来完成间接寻址
string tmp_offset;
if ((tmp_offset = if_is_cahe_struct_var((**list_iterator_struct_quattro).a2)) != "")
{//从list_tmp_var_offset找到 是个struct成员
opt_1 = "[SP+" + tmp_offset + "]";
}
else
{//是个常规局部变量
int tmp_len = get_VALL_off(Function_name, (**list_iterator_struct_quattro).a2);
//获取偏移量后 需要 将其放入内存中
opt_1 = "[SP+" + vert_int_to_string(tmp_len) + "]";
}
}
//开始赋值
string tmp_type = get_type_by_name((**list_iterator_struct_quattro).a2);
//cout << tmp_type << endl;
if (tmp_type != "")
{//结构体 赋值 多次循坏
//获取结构体的长度
int tmp_len = get_data_len(tmp_type);
int xiangdui_offset = 0;
//cout << tmp_len << " " << xiangdui_offset << endl;
while (xiangdui_offset < tmp_len)
{
list_CSEG.push_back("ST " + opt_2 + "+" + vert_int_to_string(xiangdui_offset) + "],R");
list_CSEG.push_back("ST R," + opt_1 + "+" + vert_int_to_string(xiangdui_offset) + "]");
xiangdui_offset += 2;
}
}
else
{//int void赋值
list_CSEG.push_back("ST " + opt_2 + "],R");
list_CSEG.push_back("ST R," + opt_1 + "]");
}
}
}
}