Skip to content

Commit

Permalink
✨ feat: Add GE, GT, LE, LT for long
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Nov 1, 2024
1 parent 096171a commit 2fdc250
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 25 deletions.
21 changes: 21 additions & 0 deletions scripts/ts/test/test.logical.operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,41 @@ class Test {
return a >= b;
}

public logicalGE_IL_Z(a: int, b: long): boolean {
const c: boolean = a >= b;
return c;
}

public logicalGT_II_Z(a: int, b: int): boolean {
return a > b;
}

public logicalGT_IL_Z(a: int, b: long): boolean {
return a > b;
}

public logicalLE_II_Z(a: int, b: int): boolean {
return a <= b;
}

public logicalLE_IL_Z(a: int, b: long): boolean {
return a <= b;
}

public logicalLT_II_Z(a: int, b: int): boolean {
return a < b;
}

public logicalLT_IL_Z(a: int, b: long): boolean {
return a < b;
}
}

console.log(new Test().logicalGE_II_Z(1, 2));
console.log(new Test().logicalGE_IL_Z(1, 2));
console.log(new Test().logicalGT_II_Z(1, 2));
console.log(new Test().logicalGT_IL_Z(1, 2));
console.log(new Test().logicalLE_II_Z(1, 2));
console.log(new Test().logicalLE_IL_Z(1, 2));
console.log(new Test().logicalLT_II_Z(1, 2));
console.log(new Test().logicalLT_IL_Z(1, 2));
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.caoccao.javet.buddy.ts2java.compiler.JavaClassCast;
import com.caoccao.javet.buddy.ts2java.compiler.JavaFunctionContext;
import com.caoccao.javet.buddy.ts2java.compiler.JavaLocalVariable;
import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaAstException;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstBinExpr;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstIdent;
Expand All @@ -27,7 +26,6 @@
import com.caoccao.javet.utils.SimpleMap;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.implementation.bytecode.StackManipulation;
import net.bytebuddy.implementation.bytecode.member.MethodVariableAccess;

import java.util.List;

Expand Down Expand Up @@ -92,11 +90,7 @@ private TypeDescription manipulateExpression(JavaFunctionContext functionContext
case BinExpr:
return new Ts2JavaAstBinExpr().manipulate(functionContext, expression.as(Swc4jAstBinExpr.class));
case Ident: {
String name = expression.as(Swc4jAstIdent.class).getSym();
JavaLocalVariable localVariable = functionContext.getLocalVariable(name);
functionContext.getStackManipulations().add(
MethodVariableAccess.of(localVariable.getType()).loadFrom(localVariable.getOffset()));
return localVariable.getType();
return new Ts2JavaAstIdent().manipulate(functionContext, expression.as(Swc4jAstIdent.class));
}
default:
throw new Ts2JavaAstException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import net.bytebuddy.jar.asm.MethodVisitor;
import net.bytebuddy.jar.asm.Opcodes;

import java.util.ArrayList;
import java.util.List;

public final class Ts2JavaAstBinaryOp {
private Ts2JavaAstBinaryOp() {
}
Expand Down Expand Up @@ -64,8 +67,9 @@ public static Division getDivision(TypeDescription type) {
public static StackManipulation getLogicalStackManipulation(Swc4jAstBinaryOp binaryOp, TypeDescription type) {
Label labelFalse = new Label();
Label labelTrue = new Label();
int opcodeCompare;
List<StackManipulation> stackManipulations = new ArrayList<>();
if (type.isAssignableTo(int.class)) {
int opcodeCompare;
switch (binaryOp) {
case Gt:
opcodeCompare = Opcodes.IF_ICMPLE;
Expand All @@ -88,15 +92,51 @@ public static StackManipulation getLogicalStackManipulation(Swc4jAstBinaryOp bin
SimpleFreeMarkerFormat.format("Unsupported binary operation ${binaryOp} for type ${type} in logical operation.",
SimpleMap.of("binaryOp", binaryOp.name(), "type", type.getName())));
}
stackManipulations.add(new StackManipulation.Simple((
MethodVisitor methodVisitor,
Implementation.Context implementationContext) -> {
methodVisitor.visitJumpInsn(opcodeCompare, labelFalse);
return new StackManipulation.Size(-1, 0);
}));
} else if (type.isAssignableTo(long.class)) {
int opcodeCompare;
switch (binaryOp) {
case Gt:
opcodeCompare = Opcodes.IFLE;
break;
case GtEq:
opcodeCompare = Opcodes.IFLT;
break;
case Lt:
opcodeCompare = Opcodes.IFGE;
break;
case LtEq:
opcodeCompare = Opcodes.IFGT;
break;
case EqEq:
case EqEqEq:
case NotEq:
case NotEqEq:
default:
throw new Ts2JavaException(
SimpleFreeMarkerFormat.format("Unsupported binary operation ${binaryOp} for type ${type} in logical operation.",
SimpleMap.of("binaryOp", binaryOp.name(), "type", type.getName())));
}
stackManipulations.add(new StackManipulation.Simple((
MethodVisitor methodVisitor,
Implementation.Context implementationContext) -> {
methodVisitor.visitInsn(Opcodes.LCMP);
methodVisitor.visitJumpInsn(opcodeCompare, labelFalse);
return new StackManipulation.Size(-2, 0);
}));
} else {
throw new Ts2JavaException(
SimpleFreeMarkerFormat.format("Unsupported type ${type} in logical operation.",
SimpleMap.of("type", type.getName())));
}
return new StackManipulation.Simple((
stackManipulations.add(new StackManipulation.Simple((
MethodVisitor methodVisitor,
Implementation.Context implementationContext) -> {
methodVisitor.visitJumpInsn(opcodeCompare, labelFalse);
methodVisitor.visitInsn(Opcodes.ICONST_1);
methodVisitor.visitJumpInsn(Opcodes.GOTO, labelTrue);
methodVisitor.visitLabel(labelFalse);
Expand All @@ -105,7 +145,8 @@ public static StackManipulation getLogicalStackManipulation(Swc4jAstBinaryOp bin
methodVisitor.visitLabel(labelTrue);
methodVisitor.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[]{Opcodes.INTEGER});
return StackManipulation.Size.ZERO;
});
}));
return new StackManipulation.Compound(stackManipulations);
}

public static Multiplication getMultiplication(TypeDescription type) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2024-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.buddy.ts2java.ast;

import com.caoccao.javet.buddy.ts2java.compiler.JavaFunctionContext;
import com.caoccao.javet.buddy.ts2java.compiler.JavaLocalVariable;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstIdent;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.implementation.bytecode.StackManipulation;
import net.bytebuddy.implementation.bytecode.member.MethodVariableAccess;

public final class Ts2JavaAstIdent implements ITs2JavaAstStackManipulation<Swc4jAstIdent> {
@Override
public TypeDescription manipulate(JavaFunctionContext functionContext, Swc4jAstIdent ast) {
String name = ast.getSym();
JavaLocalVariable localVariable = functionContext.getLocalVariable(name);
MethodVariableAccess methodVariableAccess = MethodVariableAccess.of(localVariable.getType());
StackManipulation stackManipulation = methodVariableAccess.loadFrom(localVariable.getOffset());
functionContext.getStackManipulations().add(stackManipulation);
return localVariable.getType();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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.interfaces.ISwc4jAstExpr;
import com.caoccao.javet.swc4j.ast.stmt.Swc4jAstReturnStmt;
import com.caoccao.javet.utils.SimpleFreeMarkerFormat;
Expand All @@ -38,6 +39,9 @@ public TypeDescription manipulate(JavaFunctionContext functionContext, Swc4jAstR
case BinExpr:
fromType = new Ts2JavaAstBinExpr().manipulate(functionContext, arg.as(Swc4jAstBinExpr.class));
break;
case Ident:
fromType = new Ts2JavaAstIdent().manipulate(functionContext, arg.as(Swc4jAstIdent.class));
break;
default:
throw new Ts2JavaAstException(
arg,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public final class Ts2JavaAstTsTypeAnn {
private static final Map<String, TypeDescription> TS_KEYWORD_TYPE_MAP = SimpleMap.of(
"boolean", TypeDescription.ForLoadedType.of(boolean.class),
"string", TypeDescription.ForLoadedType.of(String.class));

private static final Map<String, TypeDescription> TS_TYPE_REF_MAP = SimpleMap.of(
"byte", TypeDescription.ForLoadedType.of(byte.class),
"char", TypeDescription.ForLoadedType.of(char.class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.caoccao.javet.buddy.ts2java.compiler.JavaFunctionContext;
import com.caoccao.javet.buddy.ts2java.compiler.JavaLocalVariable;
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.lit.Swc4jAstNumber;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstExpr;
Expand Down Expand Up @@ -49,21 +50,21 @@ public TypeDescription manipulate(JavaFunctionContext functionContext, Swc4jAstV
TypeDescription variableType = Ts2JavaAstTsTypeAnn.getTypeDescription(tsTypeAnn);
if (varDeclarator.getInit().isPresent()) {
ISwc4jAstExpr expression = varDeclarator.getInit().get();
TypeDescription valueType;
switch (expression.getType()) {
case BinExpr: {
valueType = new Ts2JavaAstBinExpr().manipulate(
functionContext, expression.as(Swc4jAstBinExpr.class));
break;
}
case Number: {
new Ts2JavaAstNumber(variableType)
.manipulate(functionContext, expression.as(Swc4jAstNumber.class));
valueType = new Ts2JavaAstNumber(variableType).manipulate(
functionContext, expression.as(Swc4jAstNumber.class));
break;
}
case Ident: {
String valueName = expression.as(Swc4jAstIdent.class).getSym();
JavaLocalVariable localVariable = functionContext.getLocalVariable(valueName);
MethodVariableAccess methodVariableAccess = MethodVariableAccess.of(localVariable.getType());
StackManipulation stackManipulation = methodVariableAccess.loadFrom(localVariable.getOffset());
functionContext.getStackManipulations().add(stackManipulation);
TypeDescription valueType = localVariable.getType();
JavaClassCast.getUpCastStackManipulation(valueType, variableType)
.ifPresent(functionContext.getStackManipulations()::add);
valueType = new Ts2JavaAstIdent().manipulate(
functionContext, expression.as(Swc4jAstIdent.class));
break;
}
default:
Expand All @@ -72,6 +73,8 @@ public TypeDescription manipulate(JavaFunctionContext functionContext, Swc4jAstV
SimpleFreeMarkerFormat.format("VarDecl init type ${type} is not supported.",
SimpleMap.of("type", expression.getType().name())));
}
JavaClassCast.getUpCastStackManipulation(valueType, variableType)
.ifPresent(functionContext.getStackManipulations()::add);
}
JavaLocalVariable localVariable = new JavaLocalVariable(variableName, variableType);
MethodVariableAccess methodVariableAccess = MethodVariableAccess.of(variableType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ public static TypeDescription getUpCastTypeForMathOp(TypeDescription... types) {
SimpleMap.of("class", type.getName())));
}
}
TypeDescription returnType = types[0];
TypeDescription upCaseType = types[0];
for (int i = 1; i < length; i++) {
TypeDescription type = types[i];
StackManipulation stackManipulation = PrimitiveWideningDelegate.forPrimitive(returnType).widenTo(type);
StackManipulation stackManipulation = PrimitiveWideningDelegate.forPrimitive(upCaseType).widenTo(type);
if (stackManipulation.isValid() && stackManipulation != StackManipulation.Trivial.INSTANCE) {
returnType = type;
upCaseType = type;
}
}
return returnType;
return upCaseType;
}
}
Loading

0 comments on commit 2fdc250

Please sign in to comment.