public static KirinSwitch ParseNodes( KirinSwitchStart startNode, KirinNode[] nodes, KirinLoopEnd endNode, string content) { var statement = new KirinSwitch(-1, -1); statement.RawVariable = startNode.RawVariable; statement.Start = startNode.Start; statement.Length = (endNode.Start + endNode.Length) - startNode.Start; string currentCase = string.Empty; bool isDefault = false; List <KirinNode> subStatement = new List <KirinNode>(); foreach (var subnode in nodes) { if (subnode.NodeType == "KirinSwitchCase") { if (subStatement.Count > 0) { var s = FiMLexer.ParseStatement(subStatement.ToArray(), content); if (currentCase != string.Empty) { statement.AddCase(s, currentCase); } else if (isDefault) { statement.AddCase(s); } subStatement.Clear(); } var cNode = subnode as KirinSwitchCase; currentCase = cNode.RawCase; isDefault = false; } else if (subnode.NodeType == "KirinSwitchCaseDefault") { if (subStatement.Count > 0) { var s = FiMLexer.ParseStatement(subStatement.ToArray(), content); if (currentCase != string.Empty) { statement.AddCase(s, currentCase); } else if (isDefault) { statement.AddCase(s); } subStatement.Clear(); } currentCase = string.Empty; isDefault = true; } else { if (currentCase == string.Empty && !isDefault) { throw new FiMException("Switch case not found"); } subStatement.Add(subnode); } } if (subStatement.Count > 0) { var s = FiMLexer.ParseStatement(subStatement.ToArray(), content); if (currentCase != string.Empty) { statement.AddCase(s, currentCase); } else if (isDefault) { statement.AddCase(s); } subStatement.Clear(); } return(statement); }
public static KirinNode[] GetStatementNodes( KirinNode startingNode, KirinNode[] nodes, int startIndex, string content, out int endIndex ) { endIndex = startIndex; string endNodeType = startingNode.NodeType == "KirinIfStatementStart" ? "KirinIfStatementEnd" : "KirinLoopEnd"; List <KirinNode> statementNodes = new List <KirinNode>(); for (int si = startIndex + 1; si < nodes.Length; si++) { var subnode = nodes[si]; if (subnode.NodeType == "KirinIfStatementStart" || subnode.NodeType == "KirinForInLoop" || subnode.NodeType == "KirinForToLoop" || subnode.NodeType == "KirinWhileLoop" || subnode.NodeType == "KirinSwitchStart") { var subnodes = GetStatementNodes(subnode, nodes, si, content, out si); var eNode = new KirinExecutableNode(-1, -1); if (subnode.NodeType == "KirinIfStatementStart") { var sn = subnode as KirinIfStatementStart; var en = nodes[si] as KirinIfStatementEnd; eNode = KirinIfStatement.ParseNodes(sn, subnodes, en, content); } else if (subnode.NodeType == "KirinForInLoop") { var sn = subnode as KirinForInLoop; var en = nodes[si] as KirinLoopEnd; eNode = KirinForInLoop.ParseNodes(sn, subnodes, en, content); } else if (subnode.NodeType == "KirinForToLoop") { var sn = subnode as KirinForToLoop; var en = nodes[si] as KirinLoopEnd; eNode = KirinForToLoop.ParseNodes(sn, subnodes, en, content); } else if (subnode.NodeType == "KirinWhileLoop") { var sn = subnode as KirinWhileLoop; var en = nodes[si] as KirinLoopEnd; eNode = KirinWhileLoop.ParseNodes(sn, subnodes, en, content); } else if (subnode.NodeType == "KirinSwitchStart") { var sn = subnode as KirinSwitchStart; var en = nodes[si] as KirinLoopEnd; eNode = KirinSwitch.ParseNodes(sn, subnodes, en, content); } statementNodes.Add(eNode); continue; } if (subnode.NodeType == endNodeType) { endIndex = si; break; } if (si == nodes.Length - 1) { throw new FiMException($"Failed to find end of statement"); } statementNodes.Add(subnode); } return(statementNodes.ToArray()); }