static BlockStatement ReplaceJump(JumpStatement jump, BlockStatement block) { if (jump.StartOffset < block.StartOffset) { throw new ArgumentOutOfRangeException("jump", "jump should be inside the given block"); } if (jump.JumpOffset > block.EndOffset) { throw new ArgumentOutOfRangeException("jump", "jump should be inside the given block"); } var newBlock = new BlockStatement(); var ifStatement = new IfStatement(Not(jump.Condition), new BlockStatement()) { StartOffset = jump.StartOffset, EndOffset = jump.JumpOffset }; var inside = false; foreach (var statement in block) { if (statement is IfStatement && statement.Contains(jump.StartOffset.Value) && statement.EndOffset >= jump.JumpOffset) { var ifStatement2 = (IfStatement)statement; var b2 = ReplaceJump(jump, (BlockStatement)ifStatement2.TrueStatement); var newIfStatement2 = new IfStatement(ifStatement2.Condition, new BlockStatement()) { StartOffset = ifStatement2.StartOffset, EndOffset = ifStatement2.EndOffset }; ((BlockStatement)newIfStatement2.TrueStatement).AddStatements((IEnumerable <Statement>)b2); newBlock.AddStatement(newIfStatement2); } else if (statement.StartOffset == jump.StartOffset) { inside = true; } else if (statement.StartOffset == jump.JumpOffset) { if (ifStatement == null) { throw new InvalidOperationException("ifStatement can't be null"); } newBlock.AddStatement(ifStatement); newBlock.AddStatement(statement); ifStatement = null; inside = false; } else if (inside) { ((BlockStatement)ifStatement.TrueStatement).AddStatement(statement); } else { var lastStatement = newBlock.LastOrDefault(); if (lastStatement != null && lastStatement.EndOffset > statement.StartOffset) { throw new NotSupportedException("invalid Statement"); } newBlock.AddStatement(statement); } } return(newBlock); }
public override IAstNode Visit(IfStatement node) { return(new IfStatement((Expression)node.Condition.Accept(this), (Statement)node.TrueStatement.Accept(this), (Statement)node.FalseStatement.Accept(this), node.StartOffset, node.EndOffset)); }