private RValue parseStatements(boolean singleStatement) throws ParserException {
List<RValue> statements = new ArrayList<RValue>();
loop: while (position < tokens.size()) {
boolean expectSemicolon = false;
final Token current = peek();
switch (current.id()) {
case '{':
consumeCharacter('{');
statements.add(parseStatements(false));
consumeCharacter('}');
break;
case '}':
break loop;
case 'k':
final String keyword = ((KeywordToken) current).value;
switch (keyword.charAt(0)) {
case 'i': { // if
++position;
final RValue condition = parseBracket();
final RValue truePart = parseStatements(true);
final RValue falsePart;
if (hasKeyword("else")) {
++position;
falsePart = parseStatements(true);
} else {
falsePart = null;
}
statements.add(new Conditional(current.getPosition(), condition, truePart, falsePart));
break;
}
case 'w': { // while
++position;
final RValue condition = parseBracket();
final RValue body = parseStatements(true);
statements.add(new While(current.getPosition(), condition, body, false));
break;
}
case 'd': { // do/default
if (hasKeyword("default")) {
break loop;
}
++position;
final RValue body = parseStatements(true);
consumeKeyword("while");
final RValue condition = parseBracket();
statements.add(new While(current.getPosition(), condition, body, true));
expectSemicolon = true;
break;
}
case 'f': { // for
++position;
consumeCharacter('(');
int oldPosition = position;
final RValue init = parseExpression(true);
//if ((init instanceof LValue) && )
if (peek().id() == ';') {
++position;
final RValue condition = parseExpression(true);
consumeCharacter(';');
final RValue increment = parseExpression(true);
consumeCharacter(')');
final RValue body = parseStatements(true);
statements.add(new For(current.getPosition(), init, condition, increment, body));
} else {
position = oldPosition;
final Token variableToken = peek();
if (!(variableToken instanceof IdentifierToken)) {
throw new ParserException(variableToken.getPosition(), "Expected identifier");
}
RValue variable = expression.getVariable(((IdentifierToken) variableToken).value, true);
if (!(variable instanceof LValue)) {
throw new ParserException(variableToken.getPosition(), "Expected variable");
}
++position;
final Token equalsToken = peek();
if (!(equalsToken instanceof OperatorToken) || !((OperatorToken) equalsToken).operator.equals("=")) {
throw new ParserException(variableToken.getPosition(), "Expected '=' or a term and ';'");
}
++position;
final RValue first = parseExpression(true);
consumeCharacter(',');
final RValue last = parseExpression(true);
consumeCharacter(')');
final RValue body = parseStatements(true);
statements.add(new SimpleFor(current.getPosition(), (LValue) variable, first, last, body));
} // switch (keyword.charAt(0))
break;
}
case 'b': // break
++position;
statements.add(new Break(current.getPosition(), false));
break;
case 'c': // continue/case
if (hasKeyword("case")) {
break loop;
}
++position;
statements.add(new Break(current.getPosition(), true));
break;
case 'r': // return
++position;
statements.add(new Return(current.getPosition(), parseExpression(true)));
expectSemicolon = true;
break;
case 's': // switch
++position;
final RValue parameter = parseBracket();
final List<Double> values = new ArrayList<Double>();
final List<RValue> caseStatements = new ArrayList<RValue>();
RValue defaultCase = null;
consumeCharacter('{');
while (peek().id() != '}') {
if (position >= tokens.size()) {
throw new ParserException(current.getPosition(), "Expected '}' instead of EOF");
}
if (defaultCase != null) {
throw new ParserException(current.getPosition(), "Expected '}' instead of " + peek());
}
if (hasKeyword("case")) {
++position;
final Token valueToken = peek();
if (!(valueToken instanceof NumberToken)) {
throw new ParserException(current.getPosition(), "Expected number instead of " + peek());
}
++position;