本文整理汇总了C#中ForStatement类的典型用法代码示例。如果您正苦于以下问题:C# ForStatement类的具体用法?C# ForStatement怎么用?C# ForStatement使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ForStatement类属于命名空间,在下文中一共展示了ForStatement类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: VisitForStatement
public override void VisitForStatement(ForStatement forStatement)
{
if (findReturn)
{
base.VisitForStatement(forStatement);
}
}
示例2: Run
public void Run (RefactoringContext context)
{
var foreachStatement = GetForeachStatement (context);
var result = context.ResolveType (foreachStatement.InExpression);
var countProperty = GetCountProperty (result);
var initializer = new VariableDeclarationStatement (new PrimitiveType ("int"), "i", new PrimitiveExpression (0));
var id1 = new IdentifierExpression ("i");
var id2 = id1.Clone ();
var id3 = id1.Clone ();
var forStatement = new ForStatement () {
Initializers = { initializer },
Condition = new BinaryOperatorExpression (id1, BinaryOperatorType.LessThan, new MemberReferenceExpression (foreachStatement.InExpression.Clone (), countProperty)),
Iterators = { new ExpressionStatement (new UnaryOperatorExpression (UnaryOperatorType.PostIncrement, id2)) },
EmbeddedStatement = new BlockStatement {
new VariableDeclarationStatement (foreachStatement.VariableType.Clone (), foreachStatement.VariableName, new IndexerExpression (foreachStatement.InExpression.Clone (), id3))
}
};
if (foreachStatement.EmbeddedStatement is BlockStatement) {
foreach (var child in ((BlockStatement)foreachStatement.EmbeddedStatement).Statements) {
forStatement.EmbeddedStatement.AddChild (child.Clone (), BlockStatement.StatementRole);
}
} else {
forStatement.EmbeddedStatement.AddChild (foreachStatement.EmbeddedStatement.Clone (), BlockStatement.StatementRole);
}
using (var script = context.StartScript ()) {
script.Replace (foreachStatement, forStatement);
script.Link (initializer.Variables.First ().NameToken, id1, id2, id3);
}
}
示例3: VisitForStatement
public override void VisitForStatement(ForStatement node)
{
VisitChildren(node);
// Special-case constant conditions
var constant = BoolConstant(node.Condition);
if (constant == true) {
node.Condition.Remove();
}
ReplaceBlockWithSingleStatement(node.EmbeddedStatement);
}
示例4: VisitForStatement
public override void VisitForStatement (ForStatement forStatement)
{
base.VisitForStatement (forStatement);
if (forStatement.Initializers.Count != 1)
return;
var variableDecl = forStatement.Initializers.First () as VariableDeclarationStatement;
if (variableDecl == null)
return;
VariableInitializer controlVariable = null;
if (forStatement.Condition is BinaryOperatorExpression) {
controlVariable = GetControlVariable (variableDecl, (BinaryOperatorExpression)forStatement.Condition);
} else if (forStatement.Condition is UnaryOperatorExpression) {
controlVariable = GetControlVariable (variableDecl, (UnaryOperatorExpression)forStatement.Condition);
} else if (forStatement.Condition is IdentifierExpression) {
controlVariable = variableDecl.Variables.FirstOrDefault (
v => v.Name == ((IdentifierExpression)forStatement.Condition).Identifier);
}
if (controlVariable == null)
return;
var localResolveResult = ctx.Resolve (controlVariable) as LocalResolveResult;
if (localResolveResult == null)
return;
var results = ctx.FindReferences (forStatement, localResolveResult.Variable);
var modified = false;
foreach (var result in results) {
if (modified)
break;
var node = result.Node;
var unary = node.Parent as UnaryOperatorExpression;
if (unary != null && unary.Expression == node) {
modified = unary.Operator == UnaryOperatorType.Decrement ||
unary.Operator == UnaryOperatorType.PostDecrement ||
unary.Operator == UnaryOperatorType.Increment ||
unary.Operator == UnaryOperatorType.PostIncrement;
continue;
}
var assignment = node.Parent as AssignmentExpression;
modified = assignment != null && assignment.Left == node;
}
if (!modified)
AddIssue (controlVariable.NameToken,
ctx.TranslateString ("'for' loop control variable is never modified"));
}
示例5: GetActions
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var foreachStatement = GetForeachStatement(context);
if (foreachStatement == null) {
yield break;
}
yield return new CodeAction(context.TranslateString("Convert 'foreach' loop to 'for'"), script => {
var result = context.Resolve(foreachStatement.InExpression);
var countProperty = GetCountProperty(result.Type);
// TODO: use another variable name if 'i' is already in use
var initializer = new VariableDeclarationStatement(new PrimitiveType("int"), "i", new PrimitiveExpression(0));
var id1 = new IdentifierExpression("i");
var id2 = id1.Clone();
var id3 = id1.Clone();
var variableDeclarationStatement = new VariableDeclarationStatement(
foreachStatement.VariableType.Clone(),
foreachStatement.VariableName,
new IndexerExpression(foreachStatement.InExpression.Clone(), id3)
);
var forStatement = new ForStatement() {
Initializers = { initializer },
Condition = new BinaryOperatorExpression (id1, BinaryOperatorType.LessThan, new MemberReferenceExpression (foreachStatement.InExpression.Clone (), countProperty)),
Iterators = { new ExpressionStatement (new UnaryOperatorExpression (UnaryOperatorType.PostIncrement, id2)) },
EmbeddedStatement = new BlockStatement {
variableDeclarationStatement
}
};
if (foreachStatement.EmbeddedStatement is BlockStatement) {
variableDeclarationStatement.Remove();
var oldBlock = (BlockStatement)foreachStatement.EmbeddedStatement.Clone();
if (oldBlock.Statements.Any()) {
oldBlock.Statements.InsertBefore(oldBlock.Statements.First(), variableDeclarationStatement);
} else {
oldBlock.Statements.Add(variableDeclarationStatement);
}
forStatement.EmbeddedStatement = oldBlock;
} else {
forStatement.EmbeddedStatement.AddChild (foreachStatement.EmbeddedStatement.Clone (), BlockStatement.StatementRole);
}
script.Replace (foreachStatement, forStatement);
script.Link (initializer.Variables.First ().NameToken, id1, id2, id3);
});
}
示例6: Visit
public override object Visit (For forStatement)
{
var result = new ForStatement ();
var location = LocationsBag.GetLocations (forStatement);
result.AddChild (new CSharpTokenNode (Convert (forStatement.loc), "for".Length), ForStatement.Roles.Keyword);
if (location != null)
result.AddChild (new CSharpTokenNode (Convert (location[0]), 1), ForStatement.Roles.LPar);
if (forStatement.InitStatement != null)
result.AddChild ((INode)forStatement.InitStatement.Accept (this), ForStatement.Roles.Initializer);
if (location != null)
result.AddChild (new CSharpTokenNode (Convert (location[1]), 1), ForStatement.Roles.Semicolon);
if (forStatement.Test != null)
result.AddChild ((INode)forStatement.Test.Accept (this), ForStatement.Roles.Condition);
if (location != null)
result.AddChild (new CSharpTokenNode (Convert (location[2]), 1), ForStatement.Roles.Semicolon);
if (forStatement.Increment != null)
result.AddChild ((INode)forStatement.Increment.Accept (this), ForStatement.Roles.Iterator);
if (location != null)
result.AddChild (new CSharpTokenNode (Convert (location[3]), 1), ForStatement.Roles.RPar);
result.AddChild ((INode)forStatement.Statement.Accept (this), ForStatement.Roles.EmbeddedStatement);
return result;
}
示例7: VisitForStatement
public virtual void VisitForStatement (ForStatement forStatement)
{
VisitChildren (forStatement);
}
示例8: AddStatementOrList
void AddStatementOrList(ForStatement forStatement, Mono.CSharp.Statement init, Role<Statement> role)
{
if (init == null)
return;
var stmtList = init as StatementList;
if (stmtList != null) {
foreach (var stmt in stmtList.Statements) {
forStatement.AddChild((Statement)stmt.Accept(this), role);
}
} else if (init is Mono.CSharp.EmptyStatement) {
} else {
forStatement.AddChild((Statement)init.Accept(this), role);
}
}
示例9: Walk
// ForStatement
public override bool Walk(ForStatement node) { return false; }
示例10: VisitForStatement
public override void VisitForStatement(ForStatement forStatement) {
// Initializer. In case we need more than one statement, put all other statements just before this loop.
JsStatement initializer;
if (forStatement.Initializers.Count == 1 && forStatement.Initializers.First() is VariableDeclarationStatement) {
forStatement.Initializers.First().AcceptVisitor(this);
initializer = _result[_result.Count - 1];
_result.RemoveAt(_result.Count - 1);
}
else {
JsExpression initExpr = null;
foreach (var init in forStatement.Initializers) {
var compiledInit = CompileExpression(((ExpressionStatement)init).Expression, CompileExpressionFlags.None);
if (compiledInit.AdditionalStatements.Count == 0) {
initExpr = (initExpr != null ? JsExpression.Comma(initExpr, compiledInit.Expression) : compiledInit.Expression);
}
else {
if (initExpr != null)
_result.Add(initExpr);
_result.AddRange(compiledInit.AdditionalStatements);
initExpr = compiledInit.Expression;
}
}
initializer = (initExpr != null ? (JsStatement)initExpr : JsStatement.Empty);
}
// Condition
JsExpression condition;
List<JsStatement> preBody = null;
if (!forStatement.Condition.IsNull) {
var compiledCondition = CompileExpression(forStatement.Condition, CompileExpressionFlags.ReturnValueIsImportant);
if (compiledCondition.AdditionalStatements.Count == 0) {
condition = compiledCondition.Expression;
}
else {
// The condition requires additional statements. Transform "for (int i = 0; i < (SomeProperty = 1); i++) { ... }" to "for (var i = 0;; i++) { this.set_SomeProperty(1); if (!(i < 1)) { break; } ... }
preBody = new List<JsStatement>();
preBody.AddRange(compiledCondition.AdditionalStatements);
preBody.Add(JsStatement.If(JsExpression.LogicalNot(compiledCondition.Expression), JsStatement.Break(), null));
condition = null;
}
}
else {
condition = null;
}
// Iterators
JsExpression iterator = null;
List<JsStatement> postBody = null;
if (forStatement.Iterators.Count > 0) {
var compiledIterators = forStatement.Iterators.Select(i => CompileExpression(((ExpressionStatement)i).Expression, CompileExpressionFlags.None)).ToList();
if (compiledIterators.All(i => i.AdditionalStatements.Count == 0)) {
// No additional statements are required, add them as a single comma-separated expression to the JS iterator.
iterator = compiledIterators.Aggregate(iterator, (current, i) => (current != null ? JsExpression.Comma(current, i.Expression) : i.Expression));
}
else {
// At least one of the compiled iterators need additional statements. We could add the last expressions that don't need extra statements to the iterators section of the for loop, but for simplicity we'll just add everything to the end of the loop.
postBody = new List<JsStatement>();
foreach (var i in compiledIterators) {
postBody.AddRange(i.AdditionalStatements);
postBody.Add(i.Expression);
}
}
}
// Body
var body = CreateInnerCompiler().Compile(forStatement.EmbeddedStatement);
if (preBody != null || postBody != null) {
body = JsStatement.Block(((IEnumerable<JsStatement>)preBody ?? new JsStatement[0]).Concat(body.Statements).Concat((IEnumerable<JsStatement>)postBody ?? new JsStatement[0]));
}
_result.Add(JsStatement.For(initializer, condition, iterator, body));
}
示例11: performAction
/**
* Call back method that must be called when the given <code>ForStatement
* </code> will become the next <i>traverse candidate</i>.
*
* @param pForStatement The <code>ForStatement</code> object that will
* become the next <i>traverse candidate</i>.
*/
public void performAction(
ForStatement pForStatement)
{
// Nothing to do.
}
示例12: VisitForStatement
public override void VisitForStatement(ForStatement forStatement)
{
foreach (AstNode node in forStatement.Children) {
if (node.Role == Roles.Semicolon) {
if (node.NextSibling is CSharpTokenNode || node.NextSibling is EmptyStatement) {
continue;
}
ForceSpacesBefore(node, policy.SpaceBeforeForSemicolon);
ForceSpacesAfter(node, policy.SpaceAfterForSemicolon);
}
}
ForceSpacesBefore(forStatement.LParToken, policy.SpaceBeforeForParentheses);
ForceSpacesAfter(forStatement.LParToken, policy.SpacesWithinForParentheses);
ForceSpacesBefore(forStatement.RParToken, policy.SpacesWithinForParentheses);
FixEmbeddedStatment(policy.StatementBraceStyle, policy.ForBraceForcement, forStatement.EmbeddedStatement);
}
示例13: VisitFor
public void VisitFor(ForStatement f)
{
w.Write("for");
w.Write(" ");
f.exprs.Write(writer);
w.Write(" ");
w.Write("in");
w.Write(" ");
f.tests.Write(writer);
w.WriteLine(":");
++w.IndentLevel;
f.Body.Accept(this);
--w.IndentLevel;
}
示例14: GetActions
public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
bool hasIndexAccess;
var foreachStatement = GetForeachStatement(context, out hasIndexAccess);
if (foreachStatement == null || foreachStatement.EmbeddedStatement == null)
yield break;
var state = context.GetResolverStateBefore (foreachStatement.EmbeddedStatement);
string name = GetName(state, VariableNames);
if (name == null) // very unlikely, but just in case ...
yield break;
yield return new CodeAction(context.TranslateString("Convert 'foreach' loop to 'for'"), script => {
var result = context.Resolve(foreachStatement.InExpression);
var countProperty = GetCountProperty(result.Type);
var inExpression = foreachStatement.InExpression;
var initializer = hasIndexAccess ? new VariableDeclarationStatement(new PrimitiveType("int"), name, new PrimitiveExpression(0)) :
new VariableDeclarationStatement(new SimpleType("var"), name, new InvocationExpression(new MemberReferenceExpression (inExpression.Clone (), "GetEnumerator")));
var id1 = new IdentifierExpression(name);
var id2 = id1.Clone();
var id3 = id1.Clone();
Statement declarationStatement = null;
if (inExpression is ObjectCreateExpression || inExpression is ArrayCreateExpression) {
string listName = GetName(state, CollectionNames) ?? "col";
declarationStatement = new VariableDeclarationStatement (
new PrimitiveType ("var"),
listName,
inExpression.Clone ()
);
inExpression = new IdentifierExpression (listName);
}
var variableDeclarationStatement = new VariableDeclarationStatement(
foreachStatement.VariableType.Clone(),
foreachStatement.VariableName,
hasIndexAccess ? (Expression)new IndexerExpression(inExpression.Clone(), id3) : new MemberReferenceExpression(id1, "Current")
);
var forStatement = new ForStatement {
Initializers = { initializer },
Condition = hasIndexAccess ? (Expression)new BinaryOperatorExpression (id1, BinaryOperatorType.LessThan, new MemberReferenceExpression (inExpression.Clone (), countProperty)) :
new InvocationExpression(new MemberReferenceExpression (id2, "MoveNext")),
EmbeddedStatement = new BlockStatement {
variableDeclarationStatement
}
};
if (hasIndexAccess)
forStatement.Iterators.Add(new UnaryOperatorExpression (UnaryOperatorType.PostIncrement, id2));
if (foreachStatement.EmbeddedStatement is BlockStatement) {
variableDeclarationStatement.Remove();
var oldBlock = (BlockStatement)foreachStatement.EmbeddedStatement.Clone();
if (oldBlock.Statements.Any()) {
oldBlock.Statements.InsertBefore(oldBlock.Statements.First(), variableDeclarationStatement);
} else {
oldBlock.Statements.Add(variableDeclarationStatement);
}
forStatement.EmbeddedStatement = oldBlock;
} else {
forStatement.EmbeddedStatement.AddChild (foreachStatement.EmbeddedStatement.Clone (), BlockStatement.StatementRole);
}
if (declarationStatement != null)
script.InsertBefore (foreachStatement, declarationStatement);
script.Replace (foreachStatement, forStatement);
if (hasIndexAccess) {
script.Link (initializer.Variables.First ().NameToken, id1, id2, id3);
} else {
script.Link (initializer.Variables.First ().NameToken, id1, id2);
}
}, foreachStatement);
if (!hasIndexAccess)
yield break;
yield return new CodeAction(context.TranslateString("Convert 'foreach' loop to optimized 'for'"), script => {
var result = context.Resolve(foreachStatement.InExpression);
var countProperty = GetCountProperty(result.Type);
var initializer = new VariableDeclarationStatement(new PrimitiveType("int"), name, new PrimitiveExpression(0));
var id1 = new IdentifierExpression(name);
var id2 = id1.Clone();
var id3 = id1.Clone();
var inExpression = foreachStatement.InExpression;
Statement declarationStatement = null;
if (inExpression is ObjectCreateExpression || inExpression is ArrayCreateExpression) {
string listName = GetName(state, CollectionNames) ?? "col";
declarationStatement = new VariableDeclarationStatement (
new PrimitiveType ("var"),
listName,
inExpression.Clone ()
);
inExpression = new IdentifierExpression (listName);
}
var variableDeclarationStatement = new VariableDeclarationStatement(
foreachStatement.VariableType.Clone(),
foreachStatement.VariableName,
new IndexerExpression(inExpression.Clone(), id3)
);
//.........这里部分代码省略.........
示例15: GenerateForTuple
private void GenerateForTuple(ForStatement f, List<Exp> ids)
{
var localVar = GenSymLocalTuple();
var v = f.tests.Accept(xlat);
gen.Foreach(localVar, v, () =>
{
EmitTupleFieldAssignments(ids, localVar);
f.Body.Accept(this);
});
}