1 Star 0 Fork 1

mengbai/compile2021

forked from Ray/compile2021 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Tree.h 6.94 KB
一键复制 编辑 原始数据 按行查看 历史
Ray 提交于 2021-11-19 12:03 . grammar
#ifndef TREE_H
#define TREE_H
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <unordered_map>
#include "Token.h"
enum NodeType {
NODE_UNKNOWN = -1,
NODE_COMPILATION_UNIT = 0,
NODE_EXPRESSION,
NODE_IDENTIFIER,
NODE_CONJUNCTION,
NODE_DISJUNCTION,
NODE_DECL,
NODE_NUMBER,
NODE_GLOBAL,
NODE_FUNCTION,
NODE_FUNCTIONHEADER,
NODE_VOID,
NODE_RETURNTYPE,
NODE_INT,
NODE_PARAMLIST,
NODE_PARAM,
NODE_BLOCK,
NODE_STATEMENT,
NODE_FACTOR,
NODE_DECLARATION,
NODE_ASSIGN,
NODE_FUNCTIONDECL,
NODE_UNARY_ADD,
NODE_UNARY_SUB,
NODE_BINARY_ADD,
NODE_BINARY_SUB,
NODE_FUNCCALL,
NODE_FUNCARGS,
NODE_MUTIPLICATION,
NODE_DIVISION,
NODE_RETURN,
NODE_ASSEMBLY,
NODE_ASSEMBLY_LINE,
NODE_STRING,
NODE_REFERENCE,
NODE_CHAR,
NODE_EQUALITY,
NODE_LAND,
NODE_LNOT,
NODE_LOR,
NODE_FOR,
NODE_WHILE,
NODE_INEQUALITY,
NODE_GREATER,
NODE_GERATER_EQUAL,
NODE_LESS,
NODE_LESS_EQUAL,
NODE_INDEXER,
NODE_INDEX_BLOCK,
NODE_INDEX,
NODE_INITIALIZER,
NODE_DIMENSIIONBLOCK,
NODE_DIMENSIION,
NODE_MODULO,
NODE_IF,
NODE_ELSE,
NODE_COMMENT
};//节点类型
static std::unordered_map<NodeType, std::string> NodeTypeNames = {
{NODE_UNKNOWN, "Unknown"},
{NODE_COMPILATION_UNIT, "CompilationUnit"},
{NODE_EXPRESSION, "Expression"},
{NODE_IDENTIFIER, "Identifier"},
{NODE_CONJUNCTION, "Conjunction"},
{NODE_DISJUNCTION, "Disjunction"},
{NODE_DECL, "Decl"},
{NODE_NUMBER, "Number"},
{NODE_GLOBAL, "Global"},
{NODE_FUNCTION, "Function"},
{NODE_FUNCTIONHEADER, "FunctionHeader"},
{NODE_VOID, "Void"},
{NODE_RETURNTYPE, "ReturnType"},
{NODE_INT, "Int"},
{NODE_PARAMLIST, "ParamList"},
{NODE_PARAM, "Param"},
{NODE_BLOCK, "Block"},
{NODE_STATEMENT, "Statement"},
{NODE_FACTOR, "Factor"},
{NODE_DECLARATION, "Declaration"},
{NODE_ASSIGN, "Assign"},
{NODE_FUNCTIONDECL, "FunctionDecl"},
{NODE_UNARY_ADD, "UnaryAdd"},
{NODE_UNARY_SUB, "UnarySub"},
{NODE_BINARY_ADD, "BinaryAdd"},
{NODE_BINARY_SUB, "BinarySub"},
{NODE_FUNCCALL, "FuncCall"},
{NODE_FUNCARGS, "FuncArgs"},
{NODE_MUTIPLICATION, "Mutiplication"},
{NODE_DIVISION, "Division"},
{NODE_RETURN, "Return"},
{NODE_ASSEMBLY, "Assembly"},
{NODE_ASSEMBLY_LINE, "AssemblyLine"},
{NODE_STRING, "String"},
{NODE_REFERENCE, "Reference"},
{NODE_CHAR, "Char"},
{NODE_EQUALITY, "Equality"},
{NODE_LAND, "Land"},
{NODE_LNOT, "Lnot"},
{NODE_LOR, "Lor"},
{NODE_FOR, "For"},
{NODE_WHILE, "While"},
{NODE_INEQUALITY, "Inequality"},
{NODE_GREATER, "Greater"},
{NODE_GERATER_EQUAL, "GreaterEqual"},
{NODE_LESS, "Less"},
{NODE_LESS_EQUAL, "LessEqual"},
{NODE_INDEXER, "Indexer"},
{NODE_INDEX_BLOCK, "IndexBlock"},
{NODE_INDEX, "Index"},
{NODE_INITIALIZER, "Initializer"},
{NODE_DIMENSIIONBLOCK, "DimenisionBlock"},
{NODE_DIMENSIION, "Dimenision"},
{NODE_MODULO, "Modulo"},
{NODE_IF, "If"},
{NODE_ELSE, "Else"},
{NODE_COMMENT, "Comment"}};
static NodeType getNodeFromToken(Token token) {
switch (token._type) {
case TOKEN_IDENTIFIER:
return NODE_IDENTIFIER;
case TOKEN_NUMBER:
return NODE_NUMBER;
case TOKEN_INT:
return NODE_INT;
case TOKEN_CHAR:
return NODE_CHAR;
case TOKEN_VOID:
return NODE_VOID;
case TOKEN_ASSIGN:
return NODE_ASSIGN;
case TOKEN_BINARY_ADD:
return NODE_BINARY_ADD;
case TOKEN_BINARY_SUB:
return NODE_BINARY_SUB;
case TOKEN_STAR:
return NODE_MUTIPLICATION;
case TOKEN_DIV:
return NODE_DIVISION;
case TOKEN_MOD:
return NODE_MODULO;
case TOKEN_EQUAL:
return NODE_EQUALITY;
case TOKEN_LAND:
return NODE_LAND;
case TOKEN_LNOT:
return NODE_LNOT;
case TOKEN_LOR:
return NODE_LOR;
case TOKEN_NOTEQUAL:
return NODE_INEQUALITY;
case TOKEN_GREATER:
return NODE_GREATER;
case TOKEN_GREATEREQUAL:
return NODE_GERATER_EQUAL;
case TOKEN_LESS:
return NODE_LESS;
case TOKEN_LESSEQUAL:
return NODE_LESS_EQUAL;
case TOKEN_FOR:
return NODE_FOR;
case TOKEN_WHILE:
return NODE_WHILE;
case TOKEN_COMMENT:
return NODE_COMMENT;
case TOKEN_IF:
return NODE_IF;
case TOKEN_ELSE:
return NODE_ELSE;
case TOKEN_CONJUNCTION:
return NODE_CONJUNCTION;
case TOKEN_DISJUNCTION:
return NODE_DISJUNCTION;
case TOKEN_STRING:
return NODE_STRING;
case TOKEN_UNKNOWN:
return NODE_UNKNOWN;
default:
break;
}
return NODE_UNKNOWN;
}//得到token对应的节点
//语法树
class TreeNode {
public:
TreeNode *m_parent;
std::vector<TreeNode *> m_children;
NodeType m_type;
Token m_token;
//构造函数
TreeNode() {}
TreeNode(NodeType type) {
m_type = type;
}
TreeNode(NodeType type, Token token) {
m_type = type;
m_token = token;
}
void addChild(TreeNode *child) {
if (child == nullptr) {
return;
}
child->m_parent = this;
m_children.push_back(child);
}
TreeNode *getChild(unsigned int index) {
if (m_children.size() > index) {
return m_children[index];
}
return nullptr;
}
std::string getChildIdentifier(unsigned int index) {
auto child = getChild(index);
if (child) {
return child->m_token._identifier;
}
return "";
}
//打印节点
void printNode(int level) {
std::cout << NodeTypeNames[m_type] << " " << m_token.GetIdentifier() << std::endl;
level++;
for (auto it = m_children.begin(); it != m_children.end(); ++it) {
for (int i = 0; i < level; ++i) {
std::cout << " |";
}
std::cout << "---";
(*it)->printNode(level);
}
}
};
#endif // TREE_H
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mengbai_love/compile2021.git
[email protected]:mengbai_love/compile2021.git
mengbai_love
compile2021
compile2021
master

搜索帮助