Exemple #1
0
        public static bool TryParse(string content, int start, int length, out KirinNode result)
        {
            result = null;
            var match = PreKeywords.FirstOrDefault(k => content.StartsWith(k));

            if (match == null)
            {
                return(false);
            }

            string condition = content.Substring(match.Length);

            if (!KirinConditional.IsConditional(condition, out var cResult))
            {
                throw new FiMException("Expression is not a conditional");
            }

            var node = new KirinWhileLoop(start, length)
            {
                Condition = cResult
            };

            result = node;
            return(true);
        }
Exemple #2
0
        public static KirinWhileLoop ParseNodes(
            KirinWhileLoop 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);
        }
Exemple #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());
        }