/// <summary> /// Reads the next for-statement from the file and returns it. /// </summary> /// <param name="parentReference"> /// The parent code unit. /// </param> /// <param name="unsafeCode"> /// Indicates whether the code being parsed resides in an unsafe code block. /// </param> /// <returns> /// Returns the statement. /// </returns> private ForStatement ParseForStatement(Reference<ICodePart> parentReference, bool unsafeCode) { Param.AssertNotNull(parentReference, "parentReference"); Param.Ignore(unsafeCode); Reference<ICodePart> statementReference = new Reference<ICodePart>(); // Add the for keyword. CsToken firstToken = this.GetToken(CsTokenType.For, SymbolType.For, parentReference, statementReference); Node<CsToken> firstTokenNode = this.tokens.InsertLast(firstToken); // Get the opening parenthesis. Bracket openParenthesis = this.GetBracketToken(CsTokenType.OpenParenthesis, SymbolType.OpenParenthesis, statementReference); Node<CsToken> openParenthesisNode = this.tokens.InsertLast(openParenthesis); // Get each of the initializers. List<Expression> initializers = this.ParseForStatementInitializers(statementReference, unsafeCode); // Get the condition expression. Expression condition = this.ParseForStatementCondition(statementReference, unsafeCode); // Get the iterators. List<Expression> iterators = this.ParseForStatementIterators(statementReference, unsafeCode, openParenthesis, openParenthesisNode); // Get the embedded statement. Statement childStatement = this.GetNextStatement(statementReference, unsafeCode); if (childStatement == null || childStatement.Tokens.First == null) { throw new SyntaxException(this.document.SourceCode, firstToken.LineNumber); } // Create the token list for the statement. CsTokenList partialTokens = new CsTokenList(this.tokens, firstTokenNode, this.tokens.Last); // Create and return the for-statement. ForStatement statement = new ForStatement(partialTokens, initializers.ToArray(), condition, iterators.ToArray()); statement.EmbeddedStatement = childStatement; statementReference.Target = statement; // Add the variables declared in the statement. foreach (Expression initializer in initializers) { VariableDeclarationExpression variableDeclaration = initializer as VariableDeclarationExpression; if (variableDeclaration != null) { Reference<ICodePart> initializerReference = new Reference<ICodePart>(initializer); foreach (VariableDeclaratorExpression declarator in variableDeclaration.Declarators) { Variable variable = new Variable( variableDeclaration.Type, declarator.Identifier.Token.Text, VariableModifiers.None, CodeLocation.Join(variableDeclaration.Type.Location, declarator.Identifier.Token.Location), initializerReference, variableDeclaration.Type.Generated || declarator.Identifier.Token.Generated); // If there is already a variable in this scope with the same name, ignore this one. if (!statement.Variables.Contains(declarator.Identifier.Token.Text)) { statement.Variables.Add(variable); } } } } return statement; }
/// <summary> /// The save. /// </summary> /// <param name="forStatement"> /// The for statement. /// </param> private void Save(ForStatement forStatement) { this.cppWriter.Write("for ("); @switch(forStatement.Initializers); this.cppWriter.Write("; "); @switch(forStatement.Condition); this.cppWriter.Write("; "); @switch(forStatement.Iterators); this.cppWriter.Write(")"); this.Save(forStatement.EmbeddedStatement); }