Skip to content

Commit

Permalink
✨ feat: Support ident in minus
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Nov 4, 2024
1 parent 644517f commit 1056d0a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,12 @@
import net.bytebuddy.implementation.bytecode.constant.LongConstant;

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 = valueType;
negative = false;
valueType = null;
}

public TypeDescription getValueType() {
Expand Down Expand Up @@ -95,4 +92,9 @@ public Ts2JavaAstNumber setNegative(boolean negative) {
this.negative = negative;
return this;
}

public Ts2JavaAstNumber setValueType(TypeDescription valueType) {
this.valueType = valueType;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.caoccao.javet.buddy.ts2java.compiler.JavaFunctionContext;
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.expr.lit.Swc4jAstNumber;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstExpr;
Expand Down Expand Up @@ -73,15 +74,22 @@ public TypeDescription manipulate(JavaFunctionContext functionContext, Swc4jAstU
break;
}
case Minus: {
boolean opcodeNegativeRequired = true;
switch (arg.getType()) {
case BinExpr:
returnType = new Ts2JavaAstBinExpr()
.manipulate(functionContext, arg.as(Swc4jAstBinExpr.class));
break;
case Ident:
returnType = new Ts2JavaAstIdent()
.manipulate(functionContext, arg.as(Swc4jAstIdent.class));
break;
case Number:
return new Ts2JavaAstNumber()
opcodeNegativeRequired = false;
returnType = new Ts2JavaAstNumber()
.setNegative(true)
.manipulate(functionContext, arg.as(Swc4jAstNumber.class));
break;
case UnaryExpr:
returnType = new Ts2JavaAstUnaryExpr()
.manipulate(functionContext, arg.as(Swc4jAstUnaryExpr.class));
Expand All @@ -92,14 +100,16 @@ public TypeDescription manipulate(JavaFunctionContext functionContext, Swc4jAstU
SimpleFreeMarkerFormat.format("UnaryExpr arg type ${argType} for - is not supported.",
SimpleMap.of("argType", arg.getType().name())));
}
final int opcode = getOpcodeNegative(ast, returnType);
StackManipulation stackManipulation = new StackManipulation.Simple((
MethodVisitor methodVisitor,
Implementation.Context implementationContext) -> {
methodVisitor.visitInsn(opcode);
return StackManipulation.Size.ZERO;
});
functionContext.getStackManipulations().add(stackManipulation);
if (opcodeNegativeRequired) {
final int opcode = getOpcodeNegative(ast, returnType);
StackManipulation stackManipulation = new StackManipulation.Simple((
MethodVisitor methodVisitor,
Implementation.Context implementationContext) -> {
methodVisitor.visitInsn(opcode);
return StackManipulation.Size.ZERO;
});
functionContext.getStackManipulations().add(stackManipulation);
}
break;
}
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,19 @@ public TypeDescription manipulate(JavaFunctionContext functionContext, Swc4jAstV
ISwc4jAstExpr expression = varDeclarator.getInit().get().unParenExpr();
TypeDescription valueType;
switch (expression.getType()) {
case BinExpr: {
valueType = new Ts2JavaAstBinExpr().manipulate(
functionContext, expression.as(Swc4jAstBinExpr.class));
case BinExpr:
valueType = new Ts2JavaAstBinExpr()
.manipulate(functionContext, expression.as(Swc4jAstBinExpr.class));
break;
}
case Number: {
valueType = new Ts2JavaAstNumber(variableType).manipulate(
functionContext, expression.as(Swc4jAstNumber.class));
case Number:
valueType = new Ts2JavaAstNumber()
.setValueType(variableType)
.manipulate(functionContext, expression.as(Swc4jAstNumber.class));
break;
}
case Ident: {
valueType = new Ts2JavaAstIdent().manipulate(
functionContext, expression.as(Swc4jAstIdent.class));
case Ident:
valueType = new Ts2JavaAstIdent()
.manipulate(functionContext, expression.as(Swc4jAstIdent.class));
break;
}
default:
throw new Ts2JavaAstException(
expression,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,15 @@ public void testMinus_I_I() throws Exception {
assertEquals(-2, tsClass.invoke(3));
}

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

@Test
public void testMinus_Minus_II_I() throws Exception {
TsClass tsClass = new TsClass(
Expand Down

0 comments on commit 1056d0a

Please sign in to comment.