Skip to content

Commit

Permalink
Merge pull request #6 from LunarClient/fix-order-of-operations
Browse files Browse the repository at this point in the history
Fix Operator Precedence
  • Loading branch information
bernie-g authored Feb 28, 2024
2 parents 55f338b + 576c2bc commit 1fc5c7c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ plugins {

apply plugin: 'maven-publish'
apply plugin: 'com.google.cloud.artifactregistry.gradle-plugin'
version = "1.0.9"
version = "1.0.10"
group = "com.eliotlash.molang"
archivesBaseName = "particleman"

Expand Down
31 changes: 18 additions & 13 deletions src/main/java/com/eliotlash/molang/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private Expr expression() {
}

private Expr assignment() {
Expr expr = disjunction();
Expr expr = coalesce();

if (match(EQUALS)) {
Token equals = previous();
Expand Down Expand Up @@ -230,22 +230,22 @@ private Expr equality() {
}

private Expr comparison() {
var expr = coalesce();
var expr = term();

while (match(GREATER_THAN, GREATER_EQUAL, LESS_THAN, LESS_EQUAL)) {
Token operator = previous();
Expr right = coalesce();
Expr right = term();
expr = new Expr.BinOp(Operator.from(operator), expr, right);
}

return expr;
}

private Expr coalesce() {
var expr = term();
var expr = ternary();

while (match(COALESCE)) {
Expr right = term();
Expr right = ternary();
expr = new Expr.Coalesce(expr, right);
}

Expand Down Expand Up @@ -283,14 +283,8 @@ private Expr exponentiation() {
return expr;
}

private Expr unary() {
if (match(NOT)) {
return new Expr.Not(unary());
}
if (match(MINUS)) {
return new Expr.Negate(unary());
}
Expr access = access();
private Expr ternary() {
Expr access = disjunction();
if (match(QUESTION)) {
Expr left = expression();
if (match(COLON)) {
Expand All @@ -300,6 +294,17 @@ private Expr unary() {
return new Expr.Conditional(access, left);
}
}
return access;
}

private Expr unary() {
if (match(NOT)) {
return new Expr.Not(unary());
}
if (match(MINUS)) {
return new Expr.Negate(unary());
}
Expr access = access();
if (match(ARROW)) {
Expr right = expression();
if (access instanceof Expr.Access acc) {
Expand Down
31 changes: 29 additions & 2 deletions src/test/java/com/eliotlash/molang/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ void testPrecedence() {
assertEquals(op(op(one, Operator.GT, one), Operator.EQ, one), e("1 > 1 == 1"));

// comparison/coalesce
assertEquals(op(one, Operator.LT, new Expr.Coalesce(a, one)), e("1 < a ?? 1"));
assertEquals(op(new Expr.Coalesce(a, one), Operator.LT, one), e("a ?? 1 < 1"));
assertEquals(new Expr.Coalesce(op(one, Operator.LT, a), one), e("1 < a ?? 1"));
assertEquals(new Expr.Coalesce(a, op(one, Operator.LT, one)), e("a ?? 1 < 1"));

// coalesce/term
assertEquals(new Expr.Coalesce(a, op(one, Operator.SUB, one)), e("a ?? 1 - 1"));
Expand All @@ -143,6 +143,33 @@ void testPrecedence() {
assertEquals(op(one, Operator.POW, new Expr.Negate(one)), e("1 ^ -1"));
assertEquals(op(new Expr.Negate(one), Operator.POW, new Expr.Negate(one)), e("-1 ^ -1"));
assertEquals(op(new Expr.Negate(one), Operator.POW, one), e("-1 ^ 1"));

// ternary
assertEquals(
new Expr.Ternary(
op(a, Operator.GT, c(1)),
c(1),
c(0)
),
e("a > 1 ? 1 : 0")
);
assertEquals(
new Expr.Ternary(
op(a, Operator.GT, c(1)),
new Expr.Ternary(
op(a, Operator.LT, c(1)),
c(0),
c(1)
),
new Expr.Ternary(
op(a, Operator.LT, c(1)),
c(0),
c(1)
)
),
e("a > 1 ? a < 1 ? 0 : 1 : a < 1 ? 0 : 1")
);

}

@Test
Expand Down

0 comments on commit 1fc5c7c

Please sign in to comment.