Skip to content

Commit

Permalink
✨ feat: Add isLogicalOperator(), isLogicalCompareOperator(), isLogica…
Browse files Browse the repository at this point in the history
…lConditionOperator() to Swc4jAstBinaryOp
  • Loading branch information
caoccao committed Nov 19, 2024
1 parent 447126a commit a68841e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 19 deletions.
2 changes: 1 addition & 1 deletion docs/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -83,15 +83,17 @@ 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;

Swc4jAstBinaryOp(int id, String name) {
arithmeticOperator = false;
bitOperator = false;
this.id = id;
logicalOperator = false;
logicalCompareOperator = false;
logicalConditionOperator = false;
this.name = name;
oppositeOperator = null;
spaceRequired = false;
Expand Down Expand Up @@ -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() {
Expand All @@ -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;
}

Expand All @@ -152,7 +167,7 @@ private Swc4jAstBinaryOp setOppositeOperator(Swc4jAstBinaryOp oppositeOperator)
}

private Swc4jAstBinaryOp setSpaceRequired() {
this.spaceRequired = true;
spaceRequired = true;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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());
Expand All @@ -42,4 +46,34 @@ public void testGetOppositeOperator() {
assertEquals(Swc4jAstBinaryOp.RShift, Swc4jAstBinaryOp.LShift.getOppositeOperator());
assertEquals(Swc4jAstBinaryOp.Sub, Swc4jAstBinaryOp.Add.getOppositeOperator());
}

@Test
public void testIsLogicalOperator() {
Set<Swc4jAstBinaryOp> logicalConditionOperatorSet = SimpleSet.of(
Swc4jAstBinaryOp.LogicalAnd,
Swc4jAstBinaryOp.LogicalOr);
Set<Swc4jAstBinaryOp> 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());
});
}
}

0 comments on commit a68841e

Please sign in to comment.