From a68841e51007afcf4466d9aa9e168d826b3bb38b Mon Sep 17 00:00:00 2001 From: Sam Cao Date: Tue, 19 Nov 2024 14:23:17 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20Add=20isLogicalOperator(),?= =?UTF-8?q?=20isLogicalCompareOperator(),=20isLogicalConditionOperator()?= =?UTF-8?q?=20to=20Swc4jAstBinaryOp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/release_notes.md | 2 +- .../swc4j/ast/enums/Swc4jAstBinaryOp.java | 47 ++++++++++++------- .../swc4j/ast/enums/TestSwc4jAstBinaryOp.java | 38 ++++++++++++++- 3 files changed, 68 insertions(+), 19 deletions(-) diff --git a/docs/release_notes.md b/docs/release_notes.md index cb12f91..00f9b14 100644 --- a/docs/release_notes.md +++ b/docs/release_notes.md @@ -5,7 +5,7 @@ * Added `find()` to `ISwc4jAst` * Added `getMinusCount()` to `Swc4jAstNumber` * Added `getBangCount()` to `Swc4jAstBinExpr` -* Added `getOppositeOperator()` to `Swc4jAstBinaryOp` +* Added `getOppositeOperator()`, `isLogicalOperator()`, `isLogicalCompareOperator()`, `isLogicalConditionOperator()` to `Swc4jAstBinaryOp` ## 1.2.0 diff --git a/src/main/java/com/caoccao/javet/swc4j/ast/enums/Swc4jAstBinaryOp.java b/src/main/java/com/caoccao/javet/swc4j/ast/enums/Swc4jAstBinaryOp.java index 2765980..87a075b 100644 --- a/src/main/java/com/caoccao/javet/swc4j/ast/enums/Swc4jAstBinaryOp.java +++ b/src/main/java/com/caoccao/javet/swc4j/ast/enums/Swc4jAstBinaryOp.java @@ -57,22 +57,22 @@ public enum Swc4jAstBinaryOp implements ISwc4jEnumIdName { BitOr.setBitOperator().setOppositeOperator(BitAnd); BitXor.setBitOperator(); Div.setArithmeticOperator().setOppositeOperator(Mul); - EqEq.setLogicalOperator().setOppositeOperator(NotEq); - EqEqEq.setLogicalOperator().setOppositeOperator(NotEqEq); + EqEq.setLogicalCompareOperator().setOppositeOperator(NotEq); + EqEqEq.setLogicalCompareOperator().setOppositeOperator(NotEqEq); Exp.setArithmeticOperator(); - Gt.setLogicalOperator().setOppositeOperator(LtEq); - GtEq.setLogicalOperator().setOppositeOperator(Lt); + Gt.setLogicalCompareOperator().setOppositeOperator(LtEq); + GtEq.setLogicalCompareOperator().setOppositeOperator(Lt); In.setSpaceRequired(); InstanceOf.setSpaceRequired(); - LogicalAnd.setLogicalOperator().setOppositeOperator(LogicalOr); - LogicalOr.setLogicalOperator().setOppositeOperator(LogicalAnd); + LogicalAnd.setLogicalConditionOperator().setOppositeOperator(LogicalOr); + LogicalOr.setLogicalConditionOperator().setOppositeOperator(LogicalAnd); LShift.setArithmeticOperator().setOppositeOperator(RShift); - Lt.setLogicalOperator().setOppositeOperator(GtEq); - LtEq.setLogicalOperator().setOppositeOperator(Gt); + Lt.setLogicalCompareOperator().setOppositeOperator(GtEq); + LtEq.setLogicalCompareOperator().setOppositeOperator(Gt); Mod.setArithmeticOperator(); Mul.setArithmeticOperator().setOppositeOperator(Div); - NotEq.setLogicalOperator().setOppositeOperator(EqEq); - NotEqEq.setLogicalOperator().setOppositeOperator(EqEqEq); + NotEq.setLogicalCompareOperator().setOppositeOperator(EqEq); + NotEqEq.setLogicalCompareOperator().setOppositeOperator(EqEqEq); RShift.setArithmeticOperator().setOppositeOperator(LShift); Sub.setArithmeticOperator().setOppositeOperator(Add); ZeroFillRShift.setArithmeticOperator(); @@ -83,7 +83,8 @@ public enum Swc4jAstBinaryOp implements ISwc4jEnumIdName { private final String name; private boolean arithmeticOperator; private boolean bitOperator; - private boolean logicalOperator; + private boolean logicalCompareOperator; + private boolean logicalConditionOperator; private Swc4jAstBinaryOp oppositeOperator; private boolean spaceRequired; @@ -91,7 +92,8 @@ public enum Swc4jAstBinaryOp implements ISwc4jEnumIdName { arithmeticOperator = false; bitOperator = false; this.id = id; - logicalOperator = false; + logicalCompareOperator = false; + logicalConditionOperator = false; this.name = name; oppositeOperator = null; spaceRequired = false; @@ -123,8 +125,16 @@ public boolean isBitOperator() { return bitOperator; } + public boolean isLogicalCompareOperator() { + return logicalCompareOperator; + } + + public boolean isLogicalConditionOperator() { + return logicalConditionOperator; + } + public boolean isLogicalOperator() { - return logicalOperator; + return isLogicalConditionOperator() || isLogicalCompareOperator(); } public boolean isSpaceRequired() { @@ -141,8 +151,13 @@ private Swc4jAstBinaryOp setBitOperator() { return this; } - private Swc4jAstBinaryOp setLogicalOperator() { - this.logicalOperator = true; + private Swc4jAstBinaryOp setLogicalCompareOperator() { + logicalCompareOperator = true; + return this; + } + + private Swc4jAstBinaryOp setLogicalConditionOperator() { + logicalConditionOperator = true; return this; } @@ -152,7 +167,7 @@ private Swc4jAstBinaryOp setOppositeOperator(Swc4jAstBinaryOp oppositeOperator) } private Swc4jAstBinaryOp setSpaceRequired() { - this.spaceRequired = true; + spaceRequired = true; return this; } } diff --git a/src/test/java/com/caoccao/javet/swc4j/ast/enums/TestSwc4jAstBinaryOp.java b/src/test/java/com/caoccao/javet/swc4j/ast/enums/TestSwc4jAstBinaryOp.java index 9b6c946..7885d16 100644 --- a/src/test/java/com/caoccao/javet/swc4j/ast/enums/TestSwc4jAstBinaryOp.java +++ b/src/test/java/com/caoccao/javet/swc4j/ast/enums/TestSwc4jAstBinaryOp.java @@ -16,8 +16,12 @@ package com.caoccao.javet.swc4j.ast.enums; +import com.caoccao.javet.utils.SimpleSet; import org.junit.jupiter.api.Test; +import java.util.Set; +import java.util.stream.Stream; + import static org.junit.jupiter.api.Assertions.assertEquals; public class TestSwc4jAstBinaryOp { @@ -27,10 +31,10 @@ public void testGetOppositeOperator() { assertEquals(Swc4jAstBinaryOp.BitAnd, Swc4jAstBinaryOp.BitOr.getOppositeOperator()); assertEquals(Swc4jAstBinaryOp.BitOr, Swc4jAstBinaryOp.BitAnd.getOppositeOperator()); assertEquals(Swc4jAstBinaryOp.Div, Swc4jAstBinaryOp.Mul.getOppositeOperator()); - assertEquals(Swc4jAstBinaryOp.Gt, Swc4jAstBinaryOp.LtEq.getOppositeOperator()); - assertEquals(Swc4jAstBinaryOp.GtEq, Swc4jAstBinaryOp.Lt.getOppositeOperator()); assertEquals(Swc4jAstBinaryOp.EqEq, Swc4jAstBinaryOp.NotEq.getOppositeOperator()); assertEquals(Swc4jAstBinaryOp.EqEqEq, Swc4jAstBinaryOp.NotEqEq.getOppositeOperator()); + assertEquals(Swc4jAstBinaryOp.Gt, Swc4jAstBinaryOp.LtEq.getOppositeOperator()); + assertEquals(Swc4jAstBinaryOp.GtEq, Swc4jAstBinaryOp.Lt.getOppositeOperator()); assertEquals(Swc4jAstBinaryOp.LShift, Swc4jAstBinaryOp.RShift.getOppositeOperator()); assertEquals(Swc4jAstBinaryOp.LogicalAnd, Swc4jAstBinaryOp.LogicalOr.getOppositeOperator()); assertEquals(Swc4jAstBinaryOp.LogicalOr, Swc4jAstBinaryOp.LogicalAnd.getOppositeOperator()); @@ -42,4 +46,34 @@ public void testGetOppositeOperator() { assertEquals(Swc4jAstBinaryOp.RShift, Swc4jAstBinaryOp.LShift.getOppositeOperator()); assertEquals(Swc4jAstBinaryOp.Sub, Swc4jAstBinaryOp.Add.getOppositeOperator()); } + + @Test + public void testIsLogicalOperator() { + Set logicalConditionOperatorSet = SimpleSet.of( + Swc4jAstBinaryOp.LogicalAnd, + Swc4jAstBinaryOp.LogicalOr); + Set logicalCompareOperatorSet = SimpleSet.of( + Swc4jAstBinaryOp.EqEq, + Swc4jAstBinaryOp.EqEqEq, + Swc4jAstBinaryOp.Gt, + Swc4jAstBinaryOp.GtEq, + Swc4jAstBinaryOp.Lt, + Swc4jAstBinaryOp.LtEq, + Swc4jAstBinaryOp.NotEq, + Swc4jAstBinaryOp.NotEqEq); + Stream.of(Swc4jAstBinaryOp.values()).forEach(binaryOp -> { + assertEquals( + logicalCompareOperatorSet.contains(binaryOp), + binaryOp.isLogicalCompareOperator(), + binaryOp.name()); + assertEquals( + logicalConditionOperatorSet.contains(binaryOp), + binaryOp.isLogicalConditionOperator(), + binaryOp.name()); + assertEquals( + logicalCompareOperatorSet.contains(binaryOp) || logicalConditionOperatorSet.contains(binaryOp), + binaryOp.isLogicalOperator(), + binaryOp.name()); + }); + } }