Skip to content

Commit

Permalink
✨ feat: Add getOppositeOperator() to Swc4jAstBinaryOp
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Nov 18, 2024
1 parent 0017881 commit 447126a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 18 deletions.
1 change: 1 addition & 0 deletions docs/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Added `find()` to `ISwc4jAst`
* Added `getMinusCount()` to `Swc4jAstNumber`
* Added `getBangCount()` to `Swc4jAstBinExpr`
* Added `getOppositeOperator()` to `Swc4jAstBinaryOp`

## 1.2.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,29 @@ public enum Swc4jAstBinaryOp implements ISwc4jEnumIdName {
private static final Swc4jAstBinaryOp[] TYPES = new Swc4jAstBinaryOp[LENGTH];

static {
Add.setArithmeticOperator();
BitAnd.setBitOperator();
BitOr.setBitOperator();
Add.setArithmeticOperator().setOppositeOperator(Sub);
BitAnd.setBitOperator().setOppositeOperator(BitOr);
BitOr.setBitOperator().setOppositeOperator(BitAnd);
BitXor.setBitOperator();
Div.setArithmeticOperator();
EqEq.setLogicalOperator();
EqEqEq.setLogicalOperator();
Div.setArithmeticOperator().setOppositeOperator(Mul);
EqEq.setLogicalOperator().setOppositeOperator(NotEq);
EqEqEq.setLogicalOperator().setOppositeOperator(NotEqEq);
Exp.setArithmeticOperator();
Gt.setLogicalOperator();
GtEq.setLogicalOperator();
Gt.setLogicalOperator().setOppositeOperator(LtEq);
GtEq.setLogicalOperator().setOppositeOperator(Lt);
In.setSpaceRequired();
InstanceOf.setSpaceRequired();
LogicalAnd.setLogicalOperator();
LogicalOr.setLogicalOperator();
LShift.setArithmeticOperator();
Lt.setLogicalOperator();
LtEq.setLogicalOperator();
LogicalAnd.setLogicalOperator().setOppositeOperator(LogicalOr);
LogicalOr.setLogicalOperator().setOppositeOperator(LogicalAnd);
LShift.setArithmeticOperator().setOppositeOperator(RShift);
Lt.setLogicalOperator().setOppositeOperator(GtEq);
LtEq.setLogicalOperator().setOppositeOperator(Gt);
Mod.setArithmeticOperator();
Mul.setArithmeticOperator();
NotEq.setLogicalOperator();
NotEqEq.setLogicalOperator();
RShift.setArithmeticOperator();
Sub.setArithmeticOperator();
Mul.setArithmeticOperator().setOppositeOperator(Div);
NotEq.setLogicalOperator().setOppositeOperator(EqEq);
NotEqEq.setLogicalOperator().setOppositeOperator(EqEqEq);
RShift.setArithmeticOperator().setOppositeOperator(LShift);
Sub.setArithmeticOperator().setOppositeOperator(Add);
ZeroFillRShift.setArithmeticOperator();
Stream.of(values()).forEach(v -> TYPES[v.getId()] = v);
}
Expand All @@ -84,6 +84,7 @@ public enum Swc4jAstBinaryOp implements ISwc4jEnumIdName {
private boolean arithmeticOperator;
private boolean bitOperator;
private boolean logicalOperator;
private Swc4jAstBinaryOp oppositeOperator;
private boolean spaceRequired;

Swc4jAstBinaryOp(int id, String name) {
Expand All @@ -92,6 +93,7 @@ public enum Swc4jAstBinaryOp implements ISwc4jEnumIdName {
this.id = id;
logicalOperator = false;
this.name = name;
oppositeOperator = null;
spaceRequired = false;
}

Expand All @@ -109,6 +111,10 @@ public String getName() {
return name;
}

public Swc4jAstBinaryOp getOppositeOperator() {
return oppositeOperator;
}

public boolean isArithmeticOperator() {
return arithmeticOperator;
}
Expand Down Expand Up @@ -140,6 +146,11 @@ private Swc4jAstBinaryOp setLogicalOperator() {
return this;
}

private Swc4jAstBinaryOp setOppositeOperator(Swc4jAstBinaryOp oppositeOperator) {
this.oppositeOperator = oppositeOperator;
return this;
}

private Swc4jAstBinaryOp setSpaceRequired() {
this.spaceRequired = true;
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2024. caoccao.com Sam Cao
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.caoccao.javet.swc4j.ast.enums;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class TestSwc4jAstBinaryOp {
@Test
public void testGetOppositeOperator() {
assertEquals(Swc4jAstBinaryOp.Add, Swc4jAstBinaryOp.Sub.getOppositeOperator());
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.LShift, Swc4jAstBinaryOp.RShift.getOppositeOperator());
assertEquals(Swc4jAstBinaryOp.LogicalAnd, Swc4jAstBinaryOp.LogicalOr.getOppositeOperator());
assertEquals(Swc4jAstBinaryOp.LogicalOr, Swc4jAstBinaryOp.LogicalAnd.getOppositeOperator());
assertEquals(Swc4jAstBinaryOp.Lt, Swc4jAstBinaryOp.GtEq.getOppositeOperator());
assertEquals(Swc4jAstBinaryOp.LtEq, Swc4jAstBinaryOp.Gt.getOppositeOperator());
assertEquals(Swc4jAstBinaryOp.Mul, Swc4jAstBinaryOp.Div.getOppositeOperator());
assertEquals(Swc4jAstBinaryOp.NotEq, Swc4jAstBinaryOp.EqEq.getOppositeOperator());
assertEquals(Swc4jAstBinaryOp.NotEqEq, Swc4jAstBinaryOp.EqEqEq.getOppositeOperator());
assertEquals(Swc4jAstBinaryOp.RShift, Swc4jAstBinaryOp.LShift.getOppositeOperator());
assertEquals(Swc4jAstBinaryOp.Sub, Swc4jAstBinaryOp.Add.getOppositeOperator());
}
}

0 comments on commit 447126a

Please sign in to comment.