ForEachStatement ParseForEachStatement() { // foreach-statement: // foreach ({variable} in {end-value}) { {statements} } // foreach (var {variable} in {end-value}) { {statements} } // foreach (int {variable} in {end-value}) { {statements} } ReadNextToken(); // skip 'foreach' CheckForUnexpectedEndOfSource(); ReadNextToken(); // skip '(' CheckForUnexpectedEndOfSource(); Token lToken = fCurrentToken; ReadNextToken(); Variable lVariable; if (fCurrentToken.Value == "in") lVariable = new Variable(lToken.Value, "var"); else { lVariable = new Variable(fCurrentToken.Value, lToken.Value); ReadNextToken(); } SkipExpected(TokenType.Word, "in"); // skip 'in' Expression lCollection = ParseExpression(); CheckForUnexpectedEndOfSource(); SkipExpected(TokenType.Symbol, ")"); // skip ')' List<Statement> lStatements = new List<Statement>(); bool hasBrace = false; Statement lStatement; if (fCurrentToken.Equals(TokenType.Symbol, "{")) { hasBrace = true; SkipExpected(TokenType.Symbol, "{"); // skip '{' CheckForUnexpectedEndOfSource(); while (!fCurrentToken.Equals(TokenType.Symbol, "}")) { if ((lStatement = ReadNextStatement()) != null) lStatements.Add(lStatement); else throw new ParserException("Unexpected end of source."); } ReadNextToken(); // skip '}' } else { if ((lStatement = ReadNextStatement()) != null) lStatements.Add(lStatement); else throw new ParserException("Unexpected end of source."); } return new ForEachStatement(lVariable, lCollection, new StatementCollection(lStatements, hasBrace)); }
VariableDefinition ParseVarStatement(string varType) { // var-statement: // var {assignment} | {variable} // DateTime {assignment} | {variable} if(string.IsNullOrEmpty(varType)) ReadNextToken(); // skip 'var' Variable v = new Variable(fCurrentToken.Value, varType); ReadNextToken(); // skip variable name if (fCurrentToken.Equals(TokenType.Symbol, "=")) { ReadNextToken(); // skip '=' Expression value = ParseExpression(); return new VariableDefinition(v, value); } else return new VariableDefinition(v, null); }
Assignment ParseAssignment(Variable variable, string op) { // assignment: // {variable} += {value} ReadNextToken(); // skip '+=' Assignment a = null; if (op == "++" || op == "--") a = new Assignment(variable, op, null); else a = new Assignment(variable, op, ParseExpression()); return a; }
public VariableIncrement(Variable var, int amount) { fVar = var; fAmount = amount; }