代码拉取完成,页面将自动刷新
#include "irtree.h"
#include <iostream>
#include <typeinfo>
#include "ast.hpp"
using namespace std;
extern Program* program;
queue <TreeNode*> *tempNode = new queue<TreeNode*>();
queue <TreeExp*> *tempExp = new queue<TreeExp*>();
vector<TreeExp*> *tempExpList = new vector<TreeExp*>();
TreeNode* root;
void translate();
void _translate(Node * n);
void translate(){
_translate(program);
queue <TreeNode*> *q = new queue<TreeNode*>();
// level order travelsal to print the AST tree
cout << "|" << root->toString() <<"|"<< endl;
root->pushto(q);
while(!q->empty()){
int size = q->size();
cout << "|";
for(int i = 0; i < size; i++){
TreeNode* temp = q->front();
cout << temp->toString()<<"|";
temp->pushto(q);
q->pop();
}
cout<<endl;
}
}
void _translate(Node * n){
queue <Node*> *q = new queue<Node*>();
n->pushto(q);
int size = q->size();
for(int i = 0; i < size; i++){
Node* temp = q->front();
q->pop();
_translate(temp);
}
string nodeId = typeid(*n).name();
for(auto it = nodeId.begin(); it != nodeId.end();){
if(*it >= '0' && *it <= '9'){
nodeId.erase(it);
}
else{
++it;
}
}
if(nodeId == "IntValue"){
tempExp->push(new ConstExp(n->value));
}
else if (nodeId == "Identifier"){
tempExp->push(new NameExp(n->toString()));
}
else if (nodeId == "Variable"){
tempExp->push(new MemExp(tempExp->front()));
tempExp->pop();
}
else if (nodeId == "Argument"){
int size = tempExp->size();
for(int i = 0; i < size; i++){
tempExpList->push_back(tempExp->front());
tempExp->pop();
}
}
else if (nodeId == "FunctionCallExpression"){
tempExp->push(new CallExp(n->identifier->toString(), tempExpList));
tempExpList->clear();
}
else if (nodeId == "NotExpression"){
tempExp->push(new SinopExp(NOT, tempExp->front()));
tempExp->pop();
}
else if (nodeId == "UnaryExpression"){
tempExp->push(new SinopExp(UMINUS, tempExp->front()));
tempExp->pop();
}
else if (nodeId == "DivExpression"){
TreeExp* left = tempExp->front();
tempExp->pop();
TreeExp* right = tempExp->front();
tempExp->pop();
tempExp->push(new BinopExp(DIV,left,right));
}
else if (nodeId == "MulExpression"){
TreeExp* left = tempExp->front();
tempExp->pop();
TreeExp* right = tempExp->front();
tempExp->pop();
tempExp->push(new BinopExp(MUL,left,right));
}
else if (nodeId == "SubExpression"){
TreeExp* left = tempExp->front();
tempExp->pop();
TreeExp* right = tempExp->front();
tempExp->pop();
tempExp->push(new BinopExp(SUB,left,right));
}
else if (nodeId == "AddExpression"){
TreeExp* left = tempExp->front();
tempExp->pop();
TreeExp* right = tempExp->front();
tempExp->pop();
tempExp->push(new BinopExp(ADD,left,right));
}
else if (nodeId == "AndExpression"){
TreeExp* left = tempExp->front();
tempExp->pop();
TreeExp* right = tempExp->front();
tempExp->pop();
tempExp->push(new BinopExp(AND,left,right));
}
else if (nodeId == "OrExpression"){
TreeExp* left = tempExp->front();
tempExp->pop();
TreeExp* right = tempExp->front();
tempExp->pop();
tempExp->push(new BinopExp(OR,left,right));
}
else if (nodeId == "AssignExpression"||nodeId == "VariableDeclarationStatement"){
TreeExp* left = tempExp->front();
tempExp->pop();
TreeExp* right = tempExp->front();
tempExp->pop();
tempNode->push(new MoveNode(left,right));
}
else if (nodeId == "ExpressionStatement"){
tempNode->push(new ExpNode(tempExp->front()));
tempExp->pop();
}
else if (nodeId == "Block"){
TreeNode* block;
int size = tempNode->size();
if(size > 2){
TreeNode* left = tempNode->front();
tempNode->pop();
TreeNode* right = tempNode->front();
tempNode->pop();
block = new SeqNode(left,right);
for(int i = 0; i < size-2; i++){
block = new SeqNode(block,tempNode->front());
tempNode->pop();
}
tempNode->push(block);
}
else if (size == 2){
TreeNode* left = tempNode->front();
tempNode->pop();
TreeNode* right = tempNode->front();
tempNode->pop();
block = new SeqNode(left,right);
tempNode->push(block);
}
}
else if (nodeId == "Program"){
int size = tempNode->size();
if(size > 2){
TreeNode* left = tempNode->front();
tempNode->pop();
TreeNode* right = tempNode->front();
tempNode->pop();
root = new SeqNode(left,right);
for(int i = 0; i < size-2; i++){
root = new SeqNode(root,tempNode->front());
tempNode->pop();
}
}
else if (size == 2){
TreeNode* left = tempNode->front();
tempNode->pop();
TreeNode* right = tempNode->front();
tempNode->pop();
root = new SeqNode(left,right);
}
else{
root = tempNode->front();
tempNode->pop();
}
}
else if (nodeId == "IntValue"){
tempExp->push(new ConstExp(n->value));
}
else if (nodeId == "IntValue"){
tempExp->push(new ConstExp(n->value));
}
else if (nodeId == "IntValue"){
tempExp->push(new ConstExp(n->value));
}
else if (nodeId == "IntValue"){
tempExp->push(new ConstExp(n->value));
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。