/// <summary> /// 更新某个节点的子树的信息 /// </summary> /// <param name="subTreeRoot">子树根节点</param> /// <param name="disposeFlag">操作标记:true - 删除操作; false - 更新操作</param> private void update(KagaNode subTreeRoot, bool disposeFlag) { this.DFS( match: (x) => { // 删除子树时 if (disposeFlag == true) { return(x.isNewBlock == true); } // 更新信息时 else { return(x != null); } }, func: (x) => { // 消除符号表 if (disposeFlag == true) { return(this.disposeChildrenSymbolTable(x)); } // 更新信息 else { return(this.updateChildrenInfo(x)); } }, unique: false, startNode: subTreeRoot ); }
/// <summary> /// 在指定的深度和广度处插入一个节点,并生成一个语句块 /// </summary> /// <param name="dep">插入深度</param> /// <param name="bre">插入广度</param> /// <param name="obj">待插入节点</param> /// <returns>插入是否成功</returns> public bool insertBlockNode(int dep, int bre, KagaNode obj) { // 生成它的孩子节点,追加代码块光标节点、代码块右边界 obj.children.Add(new KagaNode(obj.anodeName + "___PADDING_CURSOR", NodeType.PILE__PADDING_CURSOR, dep + 1, 0, obj)); obj.children.Add(new KagaNode(obj.anodeName + "___BRIGHT_BRUCKET", NodeType.PILE__BRIGHT_BRUCKET, dep + 1, 1, obj)); // 插入到代码树 return(this.insertNode(dep, bre, obj)); }
/// <summary> /// 层次符号表的构造器 /// </summary> /// <param name="tableDepth">符号表深度</param> /// <param name="belonging">符号表的属节点</param> public KagaTable(int tableDepth, KagaNode belonging) { this.depth = tableDepth; this.belong = belonging; this.symbols = new List<KagaVar>(); this.prefix = String.Format("{0}_{1}_{2}", Consta.prefix_var, this.depth, this.belong.index); // 把自己追加到符号表里 this.symbolMana = SymbolManager.getInstance(); this.symbolMana.addSymbolTable(this); }
/// <summary> /// 层次符号表的构造器 /// </summary> /// <param name="tableDepth">符号表深度</param> /// <param name="belonging">符号表的属节点</param> public KagaTable(int tableDepth, KagaNode belonging) { this.depth = tableDepth; this.belong = belonging; this.symbols = new List <KagaVar>(); this.prefix = String.Format("{0}_{1}_{2}", Consta.prefix_var, this.depth, this.belong.index); // 把自己追加到符号表里 this.symbolMana = SymbolManager.getInstance(); this.symbolMana.addSymbolTable(this); }
/// <summary> /// 删除一张符号表 /// </summary> /// <param name="belonging">待删除符号表的属节点</param> /// <returns>操作成功与否</returns> public bool deleteSymbolTable(KagaNode belonging) { KagaTable kt; if ((kt = this.tableContainer.Find((x) => x.belong == belonging)) != null) { this.tableContainer.Remove(kt); return(true); } return(false); }
/// <summary> /// 更新某节点所有直接孩子的符号表 /// </summary> /// <param name="father">待处理节点</param> /// <returns>返回参数自身</returns> private KagaNode disposeChildrenSymbolTable(KagaNode father) { for (int i = 0; i < father.children.Count; i++) { if (father.children[i].symbolTable != null) { symbolMana.deleteSymbolTable(father.children[i]); father.children[i].symbolTable = null; } } return(father); }
/// <summary> /// 更新某节点所有直接孩子的深度和广度信息 /// </summary> /// <param name="father">待处理节点</param> /// <returns>返回参数自身</returns> private KagaNode updateChildrenInfo(KagaNode father) { for (int i = 0; i < father.children.Count; i++) { father.children[i].index = i; father.children[i].depth = father.depth + 1; if (father.children[i].isNewBlock == true) { father.children[i].symbolTable.depth = father.depth + 1; } } return(father); }
/// <summary> /// 获得指定函数的子树根节点 /// </summary> /// <param name="callname">函数名称</param> /// <returns>函数子树的根节点</returns> public KagaNode getFunRoot(string callname) { KagaNode rootNode = this.parseTree; foreach (KagaNode kn in rootNode.children) { if (kn.anodeName == callname) { return(kn); } } return(null); }
/// <summary> /// 深度优先遍历语法树,对满足条件的节点执行委托func,并返回他们 /// </summary> /// <param name="match">节点匹配条件</param> /// <param name="startNode">搜索开始的节点</param> /// <param name="func">满足条件的节点处理函数</param> /// <param name="unique">是否命中一个节点就结束</param> /// <returns>符合要求的节点向量,若没有符合的节点就返回null</returns> public List <KagaNode> DFS( Predicate <KagaNode> match, KagaNode startNode, Func <KagaNode, KagaNode> func = null, bool unique = true) { // 如果节点为空就直接退出 if (startNode == null) { return(null); } List <KagaNode> resultContainer = new List <KagaNode>(); Stack <KagaNode> iStack = new Stack <KagaNode>(); iStack.Push(startNode); KagaNode currentNode; while (iStack.Count > 0) { currentNode = iStack.Pop(); // 测试当前节点 if (match(currentNode) == true) { // 如果有func,就进行 if (func != null) { currentNode = func(currentNode); } // 添加到输出缓冲 resultContainer.Add(currentNode); // 如果保证唯一就退出 if (unique == true) { break; } } // 反向追加孩子节点到栈里 if (currentNode.children != null) { for (int i = currentNode.children.Count - 1; i >= 0; i--) { iStack.Push(currentNode.children[i]); } } } // 没有匹配项就返回null return(resultContainer.Count > 0 ? resultContainer : null); }
/// <summary> /// 把指定的深度和广度处的节点移除掉,并销毁掉它的符号表 /// </summary> /// <param name="dnode">待删除节点</param> /// <returns>删除是否成功</returns> public bool deleteNode(KagaNode dnode) { // 找到双亲 KagaNode pNode = dnode.parent; if (pNode == null) { return(false); } // 移除自己和子代的符号表 this.update(dnode, true); pNode.children.Remove(dnode); // 更新姐妹和姐妹后代的信息 this.updateChildrenInfo(pNode); return(true); }
/// <summary> /// 初始化代码管理器,获得一棵带有root和main函数块的树 /// </summary> public void initCodeTree() { // 建立一个根节点 this.parseTree = new KagaNode("TreeRoot", NodeType.PILE__BLOCK__ROOT, 0, 0, null); // 为根节点追加一个main函数节点 KagaNode mainFunNode = new KagaNode("main", NodeType.PILE__BLOCK__FUNCTION, 1, 0, this.parseTree); FunctionCell mainFunCell = new FunctionCell("main", null, VarType.VOID); mainFunNode.funBinding = mainFunCell; this.parseTree.children.Add(mainFunNode); // 为main函数节点追加代码块光标节点、代码块右边界 mainFunNode.children.Add(new KagaNode("main___PADDING_CURSOR", NodeType.PILE__PADDING_CURSOR, 2, 0, mainFunNode)); mainFunNode.children.Add(new KagaNode("main___BRIGHT_BRUCKET", NodeType.PILE__BRIGHT_BRUCKET, 2, 1, mainFunNode)); // 追加main函数到符号管理器 symbolMana.addFunction(mainFunCell); }
/// <summary> /// 广度优先遍历语法树,对满足条件的节点执行委托func,并返回他们 /// </summary> /// <param name="match">节点匹配条件</param> /// <param name="startNode">搜索开始的节点</param> /// <param name="func">满足条件的节点处理函数</param> /// <param name="unique">是否命中一个节点就结束</param> /// <returns>符合要求的节点向量,若没有符合的节点就返回null</returns> public List <KagaNode> BFS( Predicate <KagaNode> match, KagaNode startNode, Func <KagaNode, KagaNode> func = null, bool unique = true) { // 如果节点为空就直接退出 if (startNode == null) { return(null); } List <KagaNode> resultContainer = new List <KagaNode>(); Queue <KagaNode> pendingList = new Queue <KagaNode>(); pendingList.Enqueue(startNode); while (pendingList.Count > 0) { KagaNode currentNode = pendingList.Dequeue(); // 判断当前节点的条件 if (match(currentNode) == true) { // 如果有func,就进行 if (func != null) { currentNode = func(currentNode); } // 添加到输出缓冲 resultContainer.Add(currentNode); // 如果条件保证唯一就返回 if (unique == true) { break; } } // 节点没有后继结点就迭代 if (currentNode.children != null) { // 把后继节点放到队列里 foreach (KagaNode kn in currentNode.children) { pendingList.Enqueue(kn); } } } // 如果没有满足条件的节点就返回null return(resultContainer.Count > 0 ? resultContainer : null); }
/// <summary> /// 广度优先遍历语法树,对满足条件的节点执行委托func,并返回他们 /// </summary> /// <param name="match">节点匹配条件</param> /// <param name="startNode">搜索开始的节点</param> /// <param name="func">满足条件的节点处理函数</param> /// <param name="unique">是否命中一个节点就结束</param> /// <returns>符合要求的节点向量,若没有符合的节点就返回null</returns> public List<KagaNode> BFS( Predicate<KagaNode> match, KagaNode startNode, Func<KagaNode, KagaNode> func = null, bool unique = true) { // 如果节点为空就直接退出 if (startNode == null) { return null; } List<KagaNode> resultContainer = new List<KagaNode>(); Queue<KagaNode> pendingList = new Queue<KagaNode>(); pendingList.Enqueue(startNode); while (pendingList.Count > 0) { KagaNode currentNode = pendingList.Dequeue(); // 判断当前节点的条件 if (match(currentNode) == true) { // 如果有func,就进行 if (func != null) { currentNode = func(currentNode); } // 添加到输出缓冲 resultContainer.Add(currentNode); // 如果条件保证唯一就返回 if (unique == true) { break; } } // 节点没有后继结点就迭代 if (currentNode.children != null) { // 把后继节点放到队列里 foreach (KagaNode kn in currentNode.children) { pendingList.Enqueue(kn); } } } // 如果没有满足条件的节点就返回null return resultContainer.Count > 0 ? resultContainer : null; }
/// <summary> /// 在指定的深度和广度处插入一个节点 /// </summary> /// <param name="dep">插入深度</param> /// <param name="bre">插入广度</param> /// <param name="obj">待插入节点</param> /// <returns>插入是否成功</returns> public bool insertNode(int dep, int bre, KagaNode obj) { // 插入深度必须大于0 if (dep < 1) { return(false); } if (obj.parent == null || obj.parent.children.Count < bre) { return(false); } // 更新自己的信息 obj.depth = dep; obj.index = bre; // 接下来找姐妹中自己的排位并插入 obj.parent.children.Insert(bre, obj); // 更新子树信息 this.update(obj.parent, false); return(true); }
/// <summary> /// 删除一张符号表 /// </summary> /// <param name="belonging">待删除符号表的属节点</param> /// <returns>操作成功与否</returns> public bool deleteSymbolTable(KagaNode belonging) { KagaTable kt; if ((kt = this.tableContainer.Find((x) => x.belong == belonging)) != null) { this.tableContainer.Remove(kt); return true; } return false; }
/// <summary> /// 获得满足指定条件的广度优先遍历得到的第一个根节点的子树 /// </summary> /// <param name="match">匹配条件</param> /// <param name="startNode">开始搜索的节点</param> /// <returns>满足条件的子树根节点</returns> public KagaNode getSubTree(Predicate <KagaNode> match, KagaNode startNode) { List <KagaNode> res = this.BFS(match, startNode, null, true); return(res != null ? res[0] : null); }
/// <summary> /// 获得指定节点下满足指定条件的第一个直接子节点 /// </summary> /// <param name="match">匹配条件</param> /// <param name="startNode">开始搜索的节点</param> /// <returns>满足条件的第一个节点</returns> public KagaNode getChild(Predicate <KagaNode> match, KagaNode startNode) { return(startNode.children.Find(match)); }
/// <summary> /// 获得指定节点下满足指定条件的第一个直接子节点 /// </summary> /// <param name="match">匹配条件</param> /// <param name="startNode">开始搜索的节点</param> /// <returns>满足条件的第一个节点</returns> public KagaNode getChild(Predicate<KagaNode> match, KagaNode startNode) { return startNode.children.Find(match); }
/// <summary> /// 深度优先遍历语法树,对满足条件的节点执行委托func,并返回他们 /// </summary> /// <param name="match">节点匹配条件</param> /// <param name="startNode">搜索开始的节点</param> /// <param name="func">满足条件的节点处理函数</param> /// <param name="unique">是否命中一个节点就结束</param> /// <returns>符合要求的节点向量,若没有符合的节点就返回null</returns> public List<KagaNode> DFS( Predicate<KagaNode> match, KagaNode startNode, Func<KagaNode, KagaNode> func = null, bool unique = true) { // 如果节点为空就直接退出 if (startNode == null) { return null; } List<KagaNode> resultContainer = new List<KagaNode>(); Stack<KagaNode> iStack = new Stack<KagaNode>(); iStack.Push(startNode); KagaNode currentNode; while (iStack.Count > 0) { currentNode = iStack.Pop(); // 测试当前节点 if (match(currentNode) == true) { // 如果有func,就进行 if (func != null) { currentNode = func(currentNode); } // 添加到输出缓冲 resultContainer.Add(currentNode); // 如果保证唯一就退出 if (unique == true) { break; } } // 反向追加孩子节点到栈里 if (currentNode.children != null) { for (int i = currentNode.children.Count - 1; i >= 0; i--) { iStack.Push(currentNode.children[i]); } } } // 没有匹配项就返回null return resultContainer.Count > 0 ? resultContainer : null; }
/// <summary> /// 获得满足指定条件的广度优先遍历得到的第一个根节点的子树 /// </summary> /// <param name="match">匹配条件</param> /// <param name="startNode">开始搜索的节点</param> /// <returns>满足条件的子树根节点</returns> public KagaNode getSubTree(Predicate<KagaNode> match, KagaNode startNode) { List<KagaNode> res = this.BFS(match, startNode, null, true); return res != null ? res[0] : null; }
/// <summary> /// 在指定的深度和广度处插入一个节点 /// </summary> /// <param name="dep">插入深度</param> /// <param name="bre">插入广度</param> /// <param name="obj">待插入节点</param> /// <returns>插入是否成功</returns> public bool insertNode(int dep, int bre, KagaNode obj) { // 插入深度必须大于0 if (dep < 1) { return false; } if (obj.parent == null || obj.parent.children.Count < bre) { return false; } // 更新自己的信息 obj.depth = dep; obj.index = bre; // 接下来找姐妹中自己的排位并插入 obj.parent.children.Insert(bre, obj); // 更新子树信息 this.update(obj.parent, false); return true; }
/// <summary> /// 在指定的深度和广度处插入一个节点,并生成一个语句块 /// </summary> /// <param name="dep">插入深度</param> /// <param name="bre">插入广度</param> /// <param name="obj">待插入节点</param> /// <returns>插入是否成功</returns> public bool insertBlockNode(int dep, int bre, KagaNode obj) { // 生成它的孩子节点,追加代码块光标节点、代码块右边界 obj.children.Add(new KagaNode(obj.anodeName + "___PADDING_CURSOR", NodeType.PILE__PADDING_CURSOR, dep + 1, 0, obj)); obj.children.Add(new KagaNode(obj.anodeName + "___BRIGHT_BRUCKET", NodeType.PILE__BRIGHT_BRUCKET, dep + 1, 1, obj)); // 插入到代码树 return this.insertNode(dep, bre, obj); }
/// <summary> /// 更新某节点所有直接孩子的符号表 /// </summary> /// <param name="father">待处理节点</param> /// <returns>返回参数自身</returns> private KagaNode disposeChildrenSymbolTable(KagaNode father) { for (int i = 0; i < father.children.Count; i++) { if (father.children[i].symbolTable != null) { symbolMana.deleteSymbolTable(father.children[i]); father.children[i].symbolTable = null; } } return father; }
/// <summary> /// 更新某个节点的子树的信息 /// </summary> /// <param name="subTreeRoot">子树根节点</param> /// <param name="disposeFlag">操作标记:true - 删除操作; false - 更新操作</param> private void update(KagaNode subTreeRoot, bool disposeFlag) { this.DFS( match: (x) => { // 删除子树时 if (disposeFlag == true) { return x.isNewBlock == true; } // 更新信息时 else { return x != null; } }, func: (x) => { // 消除符号表 if (disposeFlag == true) { return this.disposeChildrenSymbolTable(x); } // 更新信息 else { return this.updateChildrenInfo(x); } }, unique: false, startNode: subTreeRoot ); }
/// <summary> /// 更新某节点所有直接孩子的深度和广度信息 /// </summary> /// <param name="father">待处理节点</param> /// <returns>返回参数自身</returns> private KagaNode updateChildrenInfo(KagaNode father) { for (int i = 0; i < father.children.Count; i++) { father.children[i].index = i; father.children[i].depth = father.depth + 1; if (father.children[i].isNewBlock == true) { father.children[i].symbolTable.depth = father.depth + 1; } } return father; }
/// <summary> /// 控制代码演绎的方法 /// </summary> /// <param name="dashNode">待处理节点</param> /// <returns>返回参数自身</returns> private KagaNode pile(KagaNode dashNode) { // 跳跃掉不需要处理的节点 if (dashNode.atype == NodeType.PILE__BLOCK__ROOT || dashNode.atype == NodeType.PILE__IF || dashNode.atype == NodeType.PILE__ENDIF || dashNode.atype == NodeType.NOP || dashNode.atype == NodeType.PILE__PADDING_CURSOR) { return(dashNode); } // 处理缩进 if (dashNode.atype != NodeType.PILE__BRIGHT_BRUCKET) { for (int i = 0; i < this.indentCount; i++) { pileBuilder.Append(" "); } } else { for (int i = 0; i < this.indentCount - 2; i++) { pileBuilder.Append(" "); } } // 翻译 switch (dashNode.atype) { // 语句块:函数 case NodeType.PILE__BLOCK__FUNCTION: // 头部 pileBuilder.Append(dashNode.funBinding.getSign(containLeftBrucket: true)); pileBuilder.Append(Environment.NewLine); // 进 indWait(); break; // 语句块:条件真分支 case NodeType.BLOCK__IF_TRUE: pileBuilder.Append("if ("); if (dashNode.isConditionEx == true) { pileBuilder.Append(dashNode.conditionEx); } else { switch (dashNode.LopType) { case OperandType.VO_Switch: pileBuilder.Append(Consta.switch_name + "[" + dashNode.operand1 + "]"); break; default: pileBuilder.Append(dashNode.operand1); break; } switch (dashNode.condOperateType) { case CondOperatorType.等于: pileBuilder.Append(" == "); break; case CondOperatorType.等于: pileBuilder.Append(" != "); break; case CondOperatorType.大于: pileBuilder.Append(" > "); break; case CondOperatorType.大于等于: pileBuilder.Append(" >= "); break; case CondOperatorType.小于: pileBuilder.Append(" < "); break; case CondOperatorType.小于等于: pileBuilder.Append(" <= "); break; default: pileBuilder.Append(" == "); break; } switch (dashNode.RopType) { case OperandType.VO_Switch: pileBuilder.Append(dashNode.operand2 == "关闭" ? "0" : "1"); break; default: pileBuilder.Append(dashNode.operand2); break; } } pileBuilder.Append(") {" + Environment.NewLine); // 进 indWait(); break; // 语句块:条件假分支 case NodeType.BLOCK__IF_FALSE: pileBuilder.Append("else {" + Environment.NewLine); // 进 indWait(); break; // 语句块:条件循环 case NodeType.BLOCK__WHILE: if (dashNode.isCondPostCheck == true) { pileBuilder.Append("do {" + Environment.NewLine); // 这里 while-end 有问题 } else { pileBuilder.Append("while ("); switch (dashNode.condLoopType) { case CondLoopType.CLT_EXPRESSION: pileBuilder.Append(dashNode.conditionEx); break; case CondLoopType.CLT_SWITCH: pileBuilder.Append(Consta.switch_name + "["); pileBuilder.Append(dashNode.conditionEx + "]"); break; case CondLoopType.CLT_FOREVER: default: pileBuilder.Append("true"); break; } pileBuilder.Append(") {" + Environment.NewLine); } // 进 indWait(); break; // 语句块:次数循环 case NodeType.BLOCK__FOR: pileBuilder.Append("for (int __KAGA_FOR_ITER = "); pileBuilder.Append(dashNode.forBeginIter); pileBuilder.Append("; __KAGA_FOR_ITER < "); pileBuilder.Append(dashNode.forEndIter); pileBuilder.Append("; __KAGA_FOR_ITER += "); pileBuilder.Append(dashNode.forStep); pileBuilder.Append(") {" + Environment.NewLine); // 进 indWait(); break; // 指令:注释 case NodeType.NOTE: pileBuilder.Append("/*" + Environment.NewLine); string[] notationCollection = dashNode.notation.Split('\n'); foreach (string ncs in notationCollection) { for (int i = 0; i < this.indentCount; i++) { pileBuilder.Append(" "); } pileBuilder.Append(ncs); } pileBuilder.Append(Environment.NewLine); for (int i = 0; i < this.indentCount; i++) { pileBuilder.Append(" "); } pileBuilder.Append("*/" + Environment.NewLine); break; // 指令:代码片段 case NodeType.CODEBLOCK: pileBuilder.Append("// __CODEBLOCK_BEGIN_" + Environment.NewLine); string[] myCodeCollection = dashNode.myCode.Split('\n'); foreach (string mcs in myCodeCollection) { for (int i = 0; i < this.indentCount; i++) { pileBuilder.Append(" "); } pileBuilder.Append(mcs); } pileBuilder.Append(Environment.NewLine); for (int i = 0; i < this.indentCount; i++) { pileBuilder.Append(" "); } pileBuilder.Append("// __CODEBLOCK_END_" + Environment.NewLine); break; // 指令:中断循环 case NodeType.BREAK: pileBuilder.Append("break;" + Environment.NewLine); break; // 指令:函数退出 case NodeType.RETURN: switch (dashNode.funretType) { case OperandType.VO_Constant: case OperandType.VO_DefVar: case OperandType.VO_GlobalVar: pileBuilder.Append("return " + dashNode.funretOperand + Consta.pile_statend + Environment.NewLine); break; default: pileBuilder.Append("return;" + Environment.NewLine); break; } break; // 指令:变量定义 case NodeType.DEFINE_VARIABLE: pileBuilder.Append(Consta.parseVarTypeToCType(dashNode.defineVarType)); pileBuilder.Append(" " + dashNode.defineVarName + Consta.pile_statend + Environment.NewLine); break; // 指令:变量操作 case NodeType.EXPRESSION: pileBuilder.Append(dashNode.operand1); switch (dashNode.varOperateType) { case VarOperateType.赋值为: pileBuilder.Append(" = "); break; case VarOperateType.加上: pileBuilder.Append(" += "); break; case VarOperateType.减去: pileBuilder.Append(" -= "); break; case VarOperateType.乘以: pileBuilder.Append(" *= "); break; case VarOperateType.除以: pileBuilder.Append(" /= "); break; case VarOperateType.取余: pileBuilder.Append(" %= "); break; default: pileBuilder.Append(" = "); break; } switch (dashNode.RopType) { case OperandType.VO_Random: break; default: pileBuilder.Append(dashNode.operand2); break; } pileBuilder.Append(Consta.pile_statend + Environment.NewLine); break; // 指令:开关操作 case NodeType.USING_SWITCHES: pileBuilder.Append(Consta.switch_name + "[" + dashNode.switchId + "]"); pileBuilder.Append(" = "); pileBuilder.Append(dashNode.switchFlag == true ? "1" : "0"); pileBuilder.Append(";" + Environment.NewLine); break; // 编译控制:右括弧 case NodeType.PILE__BRIGHT_BRUCKET: // 缩 indSignal(); // 右括 pileBuilder.Append("}" + Environment.NewLine); break; // 编译控制:插入节点 case NodeType.PILE__PADDING_CURSOR: break; // 编译控制:条件语句体 case NodeType.PILE__IF: case NodeType.PILE__ENDIF: break; default: break; } return(dashNode); }
/// <summary> /// 把指定的深度和广度处的节点移除掉,并销毁掉它的符号表 /// </summary> /// <param name="dnode">待删除节点</param> /// <returns>删除是否成功</returns> public bool deleteNode(KagaNode dnode) { // 找到双亲 KagaNode pNode = dnode.parent; if (pNode == null) { return false; } // 移除自己和子代的符号表 this.update(dnode, true); pNode.children.Remove(dnode); // 更新姐妹和姐妹后代的信息 this.updateChildrenInfo(pNode); return true; }