Поддержка логических операций И ИЛИ

This commit is contained in:
Victor 2015-04-16 17:31:15 +03:00
parent 8954e36007
commit d7c85173b1
4 changed files with 36 additions and 1 deletions

View File

@ -54,6 +54,8 @@ public final class Lexer {
KEYWORDS.put("if", TokenType.IF);
KEYWORDS.put("else", TokenType.ELSE);
KEYWORDS.put("endif", TokenType.ENDIF);
KEYWORDS.put("or", TokenType.OR);
KEYWORDS.put("and", TokenType.AND);
KEYWORDS.put("not", TokenType.NOT);
KEYWORDS.put("renpy.pause", TokenType.RENPY_PAUSE);

View File

@ -412,7 +412,35 @@ public final class Parser {
}
private Expression expression() {
return notTest();
return orTest();
}
private Expression orTest() {
Expression expression = andTest();
while (true) {
if (match(TokenType.OR)) {
expression = new BinaryExpression(Operator.BOOLEAN_OR, expression, andTest());
continue;
}
break;
}
return expression;
}
private Expression andTest() {
Expression expression = notTest();
while (true) {
if (match(TokenType.AND)) {
expression = new BinaryExpression(Operator.BOOLEAN_AND, expression, notTest());
continue;
}
break;
}
return expression;
}
private Expression notTest() {

View File

@ -52,6 +52,8 @@ public enum TokenType {
IF,
ELSE,
ENDIF,
OR,
AND,
NOT,
// команды

View File

@ -7,6 +7,7 @@ public class BinaryExpression implements Expression {
public static enum Operator {
ADD, SUBTRACT,
BOOLEAN_OR, BOOLEAN_AND,
EQUALS, NOTEQUALS,
}
@ -28,6 +29,8 @@ public class BinaryExpression implements Expression {
case SUBTRACT: return leftVal - rightVal;
case EQUALS: return (leftVal == rightVal) ? 1 : 0;
case NOTEQUALS: return (leftVal != rightVal) ? 1 : 0;
case BOOLEAN_OR: return ((leftVal != 0) || (rightVal != 0)) ? 1 : 0;
case BOOLEAN_AND: return ((leftVal != 0) && (rightVal != 0)) ? 1 : 0;
default:
throw new RuntimeException("Неизвестный оператор " + operator.name());
}