Skip to content

Commit

Permalink
🦄 refactor: Redesign ast 16
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Nov 30, 2024
1 parent 90c4565 commit c0ad1ff
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

public class Ts2JavaAstUnaryExpr
extends BaseTs2JavaAst<Swc4jAstUnaryExpr, Ts2JavaMemoFunction>
implements ITs2JavaMinusFlippable, ITs2JavaBangFlippable,
implements ITs2JavaBangFlippable, ITs2JavaMinusFlippable,
ITs2JavaAstExpr<Swc4jAstUnaryExpr, Ts2JavaMemoFunction> {
protected final ITs2JavaAstExpr<?, ?> arg;
protected Swc4jAstUnaryOp op;
Expand All @@ -56,13 +56,13 @@ public Size apply(MethodVisitor methodVisitor, Implementation.Context context) {
Size size = arg.apply(methodVisitor, context);
switch (op) {
case Bang:
if (!(arg instanceof ITs2JavaBangFlippable)) {
if (!isBangFlippable()) {
final int opcode = getOpcodeNegative();
methodVisitor.visitInsn(opcode);
}
break;
case Minus: {
if (!(arg instanceof ITs2JavaMinusFlippable)) {
if (!isMinusFlippable()) {
final int opcode = getOpcodeNegative();
methodVisitor.visitInsn(opcode);
}
Expand Down Expand Up @@ -107,14 +107,14 @@ public void compile() {

@Override
public void flipBang() {
if (arg instanceof ITs2JavaBangFlippable) {
if (op == Swc4jAstUnaryOp.Bang && arg instanceof ITs2JavaBangFlippable) {
((ITs2JavaBangFlippable) arg).flipBang();
}
}

@Override
public void flipMinus() {
if (arg instanceof ITs2JavaMinusFlippable) {
if (op == Swc4jAstUnaryOp.Minus && arg instanceof ITs2JavaMinusFlippable) {
((ITs2JavaMinusFlippable) arg).flipMinus();
}
}
Expand Down Expand Up @@ -147,4 +147,20 @@ protected int getOpcodeNegative() {
SimpleMap.of("type", arg.getType().getName())));
}
}

@Override
public boolean isBangFlippable() {
if (op == Swc4jAstUnaryOp.Bang && arg instanceof ITs2JavaBangFlippable) {
return ((ITs2JavaBangFlippable) arg).isBangFlippable();
}
return false;
}

@Override
public boolean isMinusFlippable() {
if (op == Swc4jAstUnaryOp.Minus && arg instanceof ITs2JavaMinusFlippable) {
return ((ITs2JavaMinusFlippable) arg).isMinusFlippable();
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public void flipBang() {
value = !value;
}

@Override
public boolean isBangFlippable() {
return true;
}

public boolean isValue() {
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ public void flipMinus() {
negative = !negative;
}

@Override
public boolean isMinusFlippable() {
return true;
}

public boolean isNegative() {
return negative;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@

public interface ITs2JavaBangFlippable {
void flipBang();

boolean isBangFlippable();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@

public interface ITs2JavaMinusFlippable {
void flipMinus();

boolean isMinusFlippable();
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,55 +63,6 @@ public double pow(double a, double b) {
return Math.pow(a, b);
}

@Test
public void testDivide_II_I() throws Exception {
TsClassX tsClass = new TsClassX(
"return a / b;",
int.class,
TsMethodArgument.of("a", int.class),
TsMethodArgument.of("b", int.class));
assertEquals(3 / 2, tsClass.invoke(3, 2));
}

@Test
public void testMinus_II_I() throws Exception {
assertEquals(-5, minus(3, 2));
TsClassX tsClass = new TsClassX(
"return -(a + b);",
int.class,
TsMethodArgument.of("a", int.class),
TsMethodArgument.of("b", int.class));
assertEquals(-5, tsClass.invoke(3, 2));
}

@Test
public void testMinus_I_I() throws Exception {
TsClassX tsClass = new TsClassX(
"return -(a + (-1));",
int.class,
TsMethodArgument.of("a", int.class));
assertEquals(-2, tsClass.invoke(3));
}

@Test
public void testMinus_L_L() throws Exception {
TsClassX tsClass = new TsClassX(
"return -a;",
long.class,
TsMethodArgument.of("a", long.class));
assertEquals(-2L, tsClass.invoke(2L));
}

@Test
public void testMinus_Minus_II_I() throws Exception {
TsClassX tsClass = new TsClassX(
"return -(-(a + b));",
int.class,
TsMethodArgument.of("a", int.class),
TsMethodArgument.of("b", int.class));
assertEquals(5, tsClass.invoke(3, 2));
}

@Test
public void testMod_II_I() throws Exception {
TsClassX tsClass = new TsClassX(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,44 @@ public void testAdd_JI_J() throws Exception {
TsMethodArgument.of("b", int.class));
assertEquals(1L + 2, tsClass.invoke(1L, 2));
}

@Test
public void testDivide_FF_F() throws Exception {
tsClass = new TsClass(
"return a / b;",
float.class,
TsMethodArgument.of("a", float.class),
TsMethodArgument.of("b", float.class));
assertEquals(3F / 2F, (float) tsClass.invoke(3F, 2F), 0.001F);
}

@Test
public void testDivide_DD_D() throws Exception {
tsClass = new TsClass(
"return a / b;",
double.class,
TsMethodArgument.of("a", double.class),
TsMethodArgument.of("b", double.class));
assertEquals(3D / 2D, (double) tsClass.invoke(3D, 2D), 0.001D);
}

@Test
public void testDivide_II_I() throws Exception {
tsClass = new TsClass(
"return a / b;",
int.class,
TsMethodArgument.of("a", int.class),
TsMethodArgument.of("b", int.class));
assertEquals(3 / 2, tsClass.invoke(3, 2));
}

@Test
public void testDivide_LL_L() throws Exception {
tsClass = new TsClass(
"return a / b;",
long.class,
TsMethodArgument.of("a", long.class),
TsMethodArgument.of("b", long.class));
assertEquals(3L / 2L, tsClass.invoke(3L, 2L));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@

import com.caoccao.javet.buddy.ts2java.BaseTestTs2Java;
import com.caoccao.javet.buddy.ts2java.TsClass;
import com.caoccao.javet.buddy.ts2java.TsMethodArgument;
import org.junit.jupiter.api.Test;

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

public class TestTs2JavaAstUnaryExpr extends BaseTestTs2Java {
boolean a() {
return !(!true);
}

@Test
public void testBang() throws Exception {
tsClass = new TsClass("return !true;", boolean.class);
Expand All @@ -43,6 +48,28 @@ public void testMinus() throws Exception {
assertEquals(-1, tsClass.invoke());
tsClass = new TsClass("return -(-1);", int.class);
assertEquals(1, tsClass.invoke());
tsClass = new TsClass(
"return -a;",
long.class,
TsMethodArgument.of("a", long.class));
assertEquals(-2L, tsClass.invoke(2L));
tsClass = new TsClass(
"return -(a + b);",
int.class,
TsMethodArgument.of("a", int.class),
TsMethodArgument.of("b", int.class));
assertEquals(-5, tsClass.invoke(3, 2));
tsClass = new TsClass(
"return -(a + (-1));",
int.class,
TsMethodArgument.of("a", int.class));
assertEquals(-2, tsClass.invoke(3));
tsClass = new TsClass(
"return -(-(a + b));",
int.class,
TsMethodArgument.of("a", int.class),
TsMethodArgument.of("b", int.class));
assertEquals(5, tsClass.invoke(3, 2));
}

@Test
Expand Down

0 comments on commit c0ad1ff

Please sign in to comment.