Simplified the statement parser, fixed a few quirks and adjusted a test case.

This commit is contained in:
TomyLobo 2011-11-30 08:55:35 +01:00
parent 9cdac001e3
commit af9e2da6d3
2 changed files with 18 additions and 25 deletions

View File

@ -88,10 +88,8 @@ public class Parser {
private RValue parseStatements(boolean singleStatement) throws ParserException {
List<RValue> statements = new ArrayList<RValue>();
loop: while (true) {
if (position >= tokens.size()) {
break;
}
loop: while (position < tokens.size()) {
boolean expectSemicolon = false;
final Token current = peek();
switch (current.id()) {
@ -102,9 +100,6 @@ public class Parser {
consumeCharacter('}');
if (singleStatement) {
break loop;
}
break;
case '}':
@ -148,6 +143,8 @@ public class Parser {
final RValue condition = parseBracket();
statements.add(new While(current.getPosition(), condition, body, true));
expectSemicolon = true;
break;
}
@ -178,7 +175,6 @@ public class Parser {
if (!(variable instanceof LValue)) {
throw new ParserException(variableToken.getPosition(), "Expected variable");
}
++position;
final Token equalsToken = peek();
@ -194,7 +190,7 @@ public class Parser {
final RValue body = parseStatements(true);
statements.add(new SimpleFor(current.getPosition(), (LValue) variable, first, last, body));
}
} // switch (keyword.charAt(0))
break;
}
@ -212,36 +208,33 @@ public class Parser {
++position;
statements.add(new Return(current.getPosition(), parseExpression(true)));
if (peek().id() == ';') {
++position;
break;
} else {
break loop;
}
expectSemicolon = true;
break;
default:
throw new ParserException(current.getPosition(), "Unimplemented keyword '" + keyword + "'");
}
if (singleStatement) {
break loop;
}
break;
default:
statements.add(parseExpression(true));
expectSemicolon = true;
} // switch (current.id())
if (expectSemicolon) {
if (peek().id() == ';') {
++position;
if (singleStatement) {
break loop;
}
break;
} else {
break loop;
break;
}
}
}
if (singleStatement) {
break;
}
} // while (position < tokens.size())
switch (statements.size()) {
case 0:

View File

@ -90,7 +90,7 @@ public class ExpressionTest {
@Test
public void testWhile() throws ExpressionException {
assertEquals(5, simpleEval("c=5; a=0; while (c > 0) { ++a; --c; } a"), 0);
assertEquals(5, simpleEval("c=5; a=0; do { ++a; --c; } while (c > 0) a"), 0);
assertEquals(5, simpleEval("c=5; a=0; do { ++a; --c; } while (c > 0); a"), 0);
}
@Test