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

Dieser Commit ist enthalten in:
TomyLobo 2011-11-30 08:55:35 +01:00
Ursprung 9cdac001e3
Commit af9e2da6d3
2 geänderte Dateien mit 18 neuen und 25 gelöschten Zeilen

Datei anzeigen

@ -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:

Datei anzeigen

@ -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