Beispiel #1
0
        public static bool TryParse(string content, int start, int length, out KirinNode result)
        {
            result = null;
            if (!content.StartsWith("For every") || !content.Contains(" in "))
            {
                return(false);
            }
            var match = Regex.Match(content, @"^For every (.+) in (.+)");

            if (!match.Success)
            {
                return(false);
            }

            var node = new KirinForInLoop(start, length)
            {
                VariableName = match.Groups[1].Value,
                RawValue     = match.Groups[2].Value
            };

            var eType = FiMHelper.DeclarationType.Determine(" " + node.VariableName, out string eKeyword);

            node.VariableName = node.VariableName.Substring(eKeyword.Length);
            node.ExpectedType = eType;

            result = node;
            return(true);
        }
Beispiel #2
0
        public static KirinForInLoop ParseNodes(
            KirinForInLoop startNode,
            KirinNode[] nodes,
            KirinLoopEnd endNode,
            string content)
        {
            var statement = startNode;

            if (endNode != null)
            {
                statement.Length    = (endNode.Start + endNode.Length) - startNode.Start;
                statement.Statement = FiMLexer.ParseStatement(nodes, content);
            }

            return(statement);
        }
Beispiel #3
0
        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());
        }