public AstBlock(AstBlockType type, IAstNode condition = null) { Type = type; Condition = condition; _nodes = new LinkedList <IAstNode>(); }
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)); }
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); }