Skip to content

Commit

Permalink
✨ feat: Add - for number
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Nov 2, 2024
1 parent 6dd8d3e commit 3af82ed
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 12 deletions.
12 changes: 11 additions & 1 deletion scripts/ts/test/test.basic.operations.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { type int, long } from "./test.type.aliases.ts";
import { double, float, type int, long } from "./test.type.aliases.ts";

class Test {
public add_DD_I(a: double, b: double): double {
return a + b + (-1);
}

public add_FF_I(a: float, b: float): float {
return a + b + (-1);
}

public add_II_I(a: int, b: int): int {
return a + b;
}
Expand Down Expand Up @@ -42,6 +50,8 @@ class Test {
}
}

console.log(new Test().add_DD_I(1, 2));
console.log(new Test().add_FF_I(1, 2));
console.log(new Test().add_II_I(1, 2));
console.log(new Test().add_II_L(1, 2));
console.log(new Test().add_IL_L(1, 2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaAstException;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstBinExpr;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstIdent;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstUnaryExpr;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstExpr;
import com.caoccao.javet.utils.SimpleFreeMarkerFormat;
import com.caoccao.javet.utils.SimpleMap;
Expand All @@ -43,9 +44,11 @@ public boolean isLogicalNot() {
@Override
public TypeDescription manipulate(JavaFunctionContext functionContext, Swc4jAstBinExpr ast) {
final List<StackManipulation> stackManipulations = functionContext.getStackManipulations();
final TypeDescription leftType = manipulateExpression(functionContext, ast.getLeft());
final ISwc4jAstExpr leftExpression = ast.getLeft().unParenExpr();
final ISwc4jAstExpr rightExpression = ast.getRight().unParenExpr();
final TypeDescription leftType = manipulateExpression(functionContext, leftExpression);
final int stackManipulationSize = stackManipulations.size();
final TypeDescription rightType = manipulateExpression(functionContext, ast.getRight());
final TypeDescription rightType = manipulateExpression(functionContext, rightExpression);
TypeDescription upCaseType = JavaClassCast.getUpCastTypeForMathOp(leftType, rightType);
// Insert the type cast for left expression if possible.
JavaClassCast.getUpCastStackManipulation(leftType, upCaseType)
Expand Down Expand Up @@ -108,6 +111,8 @@ private TypeDescription manipulateExpression(JavaFunctionContext functionContext
return new Ts2JavaAstBinExpr().manipulate(functionContext, expression.as(Swc4jAstBinExpr.class));
case Ident:
return new Ts2JavaAstIdent().manipulate(functionContext, expression.as(Swc4jAstIdent.class));
case UnaryExpr:
return new Ts2JavaAstUnaryExpr().manipulate(functionContext, expression.as(Swc4jAstUnaryExpr.class));
default:
throw new Ts2JavaAstException(
expression,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,65 @@
import com.caoccao.javet.swc4j.ast.expr.lit.Swc4jAstNumber;
import com.caoccao.javet.utils.SimpleFreeMarkerFormat;
import com.caoccao.javet.utils.SimpleMap;
import com.caoccao.javet.utils.StringUtils;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.implementation.bytecode.StackManipulation;
import net.bytebuddy.implementation.bytecode.constant.DoubleConstant;
import net.bytebuddy.implementation.bytecode.constant.FloatConstant;
import net.bytebuddy.implementation.bytecode.constant.IntegerConstant;
import net.bytebuddy.implementation.bytecode.constant.LongConstant;

import java.util.Objects;

public final class Ts2JavaAstNumber implements ITs2JavaAstStackManipulation<Swc4jAstNumber> {
private final TypeDescription valueType;
private boolean negative;
private TypeDescription valueType;

public Ts2JavaAstNumber() {
this(null);
}

public Ts2JavaAstNumber(TypeDescription valueType) {
this.valueType = Objects.requireNonNull(valueType);
this.valueType = valueType;
}

public TypeDescription getValueType() {
return valueType;
}

public boolean isNegative() {
return negative;
}

@Override
public TypeDescription manipulate(JavaFunctionContext functionContext, Swc4jAstNumber ast) {
StackManipulation stackManipulation;
if (valueType == null) {
valueType = ast.getRaw()
.map(raw -> {
if (StringUtils.isNotEmpty(raw)) {
if (raw.contains(".")) {
return TypeDescription.ForLoadedType.of(double.class);
} else {
long value = Long.parseLong(raw);
if (value <= Integer.MAX_VALUE && value >= Integer.MIN_VALUE) {
return TypeDescription.ForLoadedType.of(int.class);
} else {
return TypeDescription.ForLoadedType.of(long.class);
}
}
} else {
return null;
}
})
.orElse(TypeDescription.ForLoadedType.of(int.class));
}
if (valueType.represents(int.class) || valueType.represents(short.class) || valueType.represents(byte.class)) {
stackManipulation = IntegerConstant.forValue(ast.asInt());
stackManipulation = IntegerConstant.forValue(negative ? -ast.asInt() : ast.asInt());
} else if (valueType.represents(long.class)) {
stackManipulation = LongConstant.forValue(ast.asLong());
stackManipulation = LongConstant.forValue(negative ? -ast.asLong() : ast.asLong());
} else if (valueType.represents(float.class)) {
stackManipulation = FloatConstant.forValue(ast.asFloat());
stackManipulation = FloatConstant.forValue(negative ? -ast.asFloat() : ast.asFloat());
} else if (valueType.represents(double.class)) {
stackManipulation = DoubleConstant.forValue(ast.asDouble());
stackManipulation = DoubleConstant.forValue(negative ? -ast.asDouble() : ast.asDouble());
} else {
throw new Ts2JavaAstException(
ast,
Expand All @@ -61,4 +89,9 @@ public TypeDescription manipulate(JavaFunctionContext functionContext, Swc4jAstN
functionContext.getStackManipulations().add(stackManipulation);
return valueType;
}

public Ts2JavaAstNumber setNegative(boolean negative) {
this.negative = negative;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaAstException;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstBinExpr;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstUnaryExpr;
import com.caoccao.javet.swc4j.ast.expr.lit.Swc4jAstNumber;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstExpr;
import com.caoccao.javet.utils.SimpleFreeMarkerFormat;
import com.caoccao.javet.utils.SimpleMap;
Expand All @@ -38,7 +39,18 @@ public TypeDescription manipulate(JavaFunctionContext functionContext, Swc4jAstU
default:
throw new Ts2JavaAstException(
arg,
SimpleFreeMarkerFormat.format("UnaryExpr arg type ${argType} is not supported.",
SimpleFreeMarkerFormat.format("UnaryExpr arg type ${argType} for ! is not supported.",
SimpleMap.of("argType", arg.getType().name())));
}
case Minus:
switch (arg.getType()) {
case Number:
return new Ts2JavaAstNumber().setNegative(true).manipulate(
functionContext, arg.as(Swc4jAstNumber.class));
default:
throw new Ts2JavaAstException(
arg,
SimpleFreeMarkerFormat.format("UnaryExpr arg type ${argType} for - is not supported.",
SimpleMap.of("argType", arg.getType().name())));
}
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ protected void init() {
}
}

@Test
public void testAdd_DD_I() throws Exception {
Method method = clazz.getMethod("add_DD_I", double.class, double.class);
assertNotNull(method);
assertEquals(double.class, method.getReturnType());
assertEquals(2, method.getParameterCount());
assertEquals(double.class, method.getParameters()[0].getType());
assertEquals(double.class, method.getParameters()[1].getType());
Object object = clazz.getConstructor().newInstance();
assertEquals(1D + 2D - 1D, (double) method.invoke(object, 1D, 2D), 0.001D);
assertEquals(1.23D + 2D - 1D, (double) method.invoke(object, 1.23D, 2D), 0.001D);
}

@Test
public void testAdd_II_I() throws Exception {
assertEquals(3, add(1, 2));
Expand All @@ -96,6 +109,7 @@ public void testAdd_II_I() throws Exception {
assertEquals(int.class, method.getParameters()[1].getType());
Object object = clazz.getConstructor().newInstance();
assertEquals(1 + 2, method.invoke(object, 1, 2));
assertEquals(-1 + -2, method.invoke(object, -1, -2));
}

@Test
Expand Down

0 comments on commit 3af82ed

Please sign in to comment.