Exemplo n.º 1
0
        public AstBlock(AstBlockType type, IAstNode condition = null)
        {
            Type      = type;
            Condition = condition;

            _nodes = new LinkedList <IAstNode>();
        }
Exemplo n.º 2
0
        private static bool BlockMatches(IAstNode node, AstBlockType type, IAstNode cond)
        {
            if (!(node is AstBlock block))
            {
                return(false);
            }

            return(block.Type == type && IsSameCond(block.Condition, cond));
        }
Exemplo n.º 3
0
        private static AstBlock Enclose(
            AstBlock block,
            AstBlockType type,
            IAstNode cond,
            IAstNode first,
            IAstNode last = null)
        {
            if (first == last)
            {
                return(null);
            }

            if (type == AstBlockType.If)
            {
                cond = InverseCond(cond);
            }

            // Do a quick check, if we are enclosing a single block,
            // and the block type/condition matches the one we're going
            // to create, then we don't need a new block, we can just
            // return the old one.
            bool hasSingleNode = Next(first) == last;

            if (hasSingleNode && BlockMatches(first, type, cond))
            {
                return(first as AstBlock);
            }

            AstBlock newBlock = new AstBlock(type, cond);

            block.AddBefore(first, newBlock);

            while (first != last)
            {
                IAstNode next = Next(first);

                block.Remove(first);

                newBlock.Add(first);

                first = next;
            }

            return(newBlock);
        }