allow grouping binary operations

This commit is contained in:
dfsek
2020-12-21 20:55:51 -07:00
parent fe17683d27
commit be8ed913e5
2 changed files with 11 additions and 4 deletions

View File

@@ -102,8 +102,10 @@ public class Parser {
@SuppressWarnings("unchecked")
private Returnable<?> parseExpression(List<Token> tokens, boolean full) throws ParseException {
System.out.println(tokens.get(0));
Token first = tokens.get(0);
if(first.getType().equals(Token.Type.GROUP_BEGIN)) return parseGroup(tokens);
checkType(first, Token.Type.IDENTIFIER, Token.Type.BOOLEAN, Token.Type.STRING, Token.Type.NUMBER, Token.Type.BOOLEAN_NOT, Token.Type.GROUP_BEGIN);
boolean not = false;
@@ -143,6 +145,13 @@ public class Parser {
return expression;
}
private Returnable<?> parseGroup(List<Token> tokens) throws ParseException {
checkType(tokens.remove(0), Token.Type.GROUP_BEGIN);
Returnable<?> expression = parseExpression(tokens, true);
checkType(tokens.remove(0), Token.Type.GROUP_END);
return expression;
}
private BinaryOperation<?, ?> parseBinaryOperation(Returnable<?> left, List<Token> tokens) throws ParseException {
Token binaryOperator = tokens.remove(0);
@@ -187,8 +196,6 @@ public class Parser {
return new GreaterOrEqualsThanStatement((Returnable<Number>) left, (Returnable<Number>) right, binaryOperator.getPosition());
case LESS_THAN_OR_EQUALS_OPERATOR:
return new LessThanOrEqualsStatement((Returnable<Number>) left, (Returnable<Number>) right, binaryOperator.getPosition());
default:
throw new UnsupportedOperationException("Unsupported binary operator: " + binaryOperator.getType());
}

View File

@@ -1,4 +1,4 @@
test("hello" + 3 + "gdfg", 2);
test("hello" + 3 + "gdfg", (2 * (3+1)));
if(true == test("hello" + 3 + "gdfg", 3 + 8*2 - 1)) {
test("fdsgdf" + 2, 3.4);