From be8ed913e558efa1f7006549ab2e790475f136e5 Mon Sep 17 00:00:00 2001 From: dfsek Date: Mon, 21 Dec 2020 20:55:51 -0700 Subject: [PATCH] allow grouping binary operations --- .../dfsek/terra/api/structures/parser/Parser.java | 13 ++++++++++--- common/src/test/resources/test.tesf | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/common/src/main/java/com/dfsek/terra/api/structures/parser/Parser.java b/common/src/main/java/com/dfsek/terra/api/structures/parser/Parser.java index 2e01ee82e..19fcf8c4e 100644 --- a/common/src/main/java/com/dfsek/terra/api/structures/parser/Parser.java +++ b/common/src/main/java/com/dfsek/terra/api/structures/parser/Parser.java @@ -102,8 +102,10 @@ public class Parser { @SuppressWarnings("unchecked") private Returnable parseExpression(List 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 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 tokens) throws ParseException { Token binaryOperator = tokens.remove(0); @@ -187,8 +196,6 @@ public class Parser { return new GreaterOrEqualsThanStatement((Returnable) left, (Returnable) right, binaryOperator.getPosition()); case LESS_THAN_OR_EQUALS_OPERATOR: return new LessThanOrEqualsStatement((Returnable) left, (Returnable) right, binaryOperator.getPosition()); - - default: throw new UnsupportedOperationException("Unsupported binary operator: " + binaryOperator.getType()); } diff --git a/common/src/test/resources/test.tesf b/common/src/test/resources/test.tesf index 4eabb6a5f..35aa873b3 100644 --- a/common/src/test/resources/test.tesf +++ b/common/src/test/resources/test.tesf @@ -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);