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

View File

@ -90,7 +90,7 @@ public class ExpressionTest {
@Test @Test
public void testWhile() throws ExpressionException { 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; 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 @Test