diff --git a/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstBinExpr.java b/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstBinExpr.java deleted file mode 100644 index 9c17177..0000000 --- a/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstBinExpr.java +++ /dev/null @@ -1,143 +0,0 @@ -/* - * 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.buddy.ts2java.ast; - -import com.caoccao.javet.buddy.ts2java.compiler.JavaByteCodeHint; -import com.caoccao.javet.buddy.ts2java.compiler.JavaClassCast; -import com.caoccao.javet.buddy.ts2java.compiler.JavaFunctionContext; -import com.caoccao.javet.buddy.ts2java.compiler.JavaLogicalLabels; -import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaAstException; -import com.caoccao.javet.swc4j.ast.enums.Swc4jAstBinaryOp; -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; -import com.caoccao.javet.utils.SimpleFreeMarkerFormat; -import com.caoccao.javet.utils.SimpleMap; -import net.bytebuddy.description.type.TypeDescription; -import net.bytebuddy.implementation.Implementation; -import net.bytebuddy.implementation.bytecode.StackManipulation; -import net.bytebuddy.jar.asm.Label; -import net.bytebuddy.jar.asm.MethodVisitor; -import net.bytebuddy.jar.asm.Opcodes; - -import java.util.List; -import java.util.Optional; - -public final class Ts2JavaAstBinExpr implements ITs2JavaAstStackManipulation { - private static StackManipulation getLogicalClose(JavaLogicalLabels logicalLabels) { - final Label labelFalse = logicalLabels.get(1); - final Label labelClose = logicalLabels.get(0); - return new StackManipulation.Simple(( - MethodVisitor methodVisitor, - Implementation.Context implementationContext) -> { - methodVisitor.visitInsn(Opcodes.ICONST_1); - methodVisitor.visitJumpInsn(Opcodes.GOTO, labelClose); - methodVisitor.visitLabel(labelFalse); - methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null); - methodVisitor.visitInsn(Opcodes.ICONST_0); - methodVisitor.visitLabel(labelClose); - methodVisitor.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[]{Opcodes.INTEGER}); - return StackManipulation.Size.ZERO; - }); - } - - @Override - public JavaByteCodeHint manipulate(JavaFunctionContext functionContext, Swc4jAstBinExpr ast) { - Ts2JavaAst.manipulateLineNumber(functionContext, ast); - Swc4jAstBinaryOp binaryOp = ast.getOp(); - if (binaryOp.isLogicalOperator()) { - if (ast.getBangCount() % 2 == 1) { - binaryOp = binaryOp.getOppositeOperator(); - } - if (binaryOp == Swc4jAstBinaryOp.LogicalOr) { - functionContext.getLogicalLabels().append(); - } - } - final List stackManipulations = functionContext.getStackManipulations(); - final ISwc4jAstExpr leftExpression = ast.getLeft().unParenExpr(); - final ISwc4jAstExpr rightExpression = ast.getRight().unParenExpr(); - final JavaByteCodeHint leftHint = manipulateExpression(functionContext, leftExpression); - int leftEndIndex = stackManipulations.size(); - final JavaByteCodeHint rightHint = manipulateExpression(functionContext, rightExpression); - final JavaByteCodeHint hint = new JavaByteCodeHint(); - switch (binaryOp) { - case Exp: - hint.setType(TypeDescription.ForLoadedType.of(double.class)); - break; - default: - hint.setType(JavaClassCast.getUpCastTypeForMathOp(leftHint.getType(), rightHint.getType())); - break; - } - // Insert the type cast for left expression if possible. - Optional optionalUpCastStackManipulation = - JavaClassCast.getUpCastStackManipulation(leftHint.getType(), hint.getType()); - if (optionalUpCastStackManipulation.isPresent()) { - stackManipulations.add(leftEndIndex, optionalUpCastStackManipulation.get()); - ++leftEndIndex; - } - // Add the type cast for right expression if possible. - JavaClassCast.getUpCastStackManipulation(rightHint.getType(), hint.getType()).ifPresent(stackManipulations::add); - if (binaryOp.isArithmeticOperator()) { - Ts2JavaAstBinaryOp.manipulateArithmetic(stackManipulations, binaryOp, hint); - } else if (binaryOp.isLogicalOperator()) { - switch (binaryOp) { - case LogicalAnd: - Ts2JavaAstBinaryOp.manipulateLogicalAnd(functionContext, ast, leftEndIndex, leftHint, rightHint); - break; - case LogicalOr: - Ts2JavaAstBinaryOp.manipulateLogicalOr(functionContext, ast, leftEndIndex, leftHint, rightHint); - break; - default: - Ts2JavaAstBinaryOp.manipulateLogicalCompare(functionContext, ast, binaryOp, hint); - break; - } - hint.setType(TypeDescription.ForLoadedType.of(boolean.class)); -// } else if (binaryOp.isBitOperator()) { - } else { - throw new Ts2JavaAstException( - ast, - SimpleFreeMarkerFormat.format("BinExpr op ${op} is not supported.", - SimpleMap.of("op", binaryOp.name()))); - } - if (binaryOp.isLogicalOperator()) { - if (ast.getLogicalOperatorCount() == 0) { - stackManipulations.add(getLogicalClose(functionContext.getLogicalLabels())); - } - } - return hint; - } - - private JavaByteCodeHint manipulateExpression(JavaFunctionContext functionContext, ISwc4jAstExpr expression) { - switch (expression.getType()) { - case BinExpr: - return new Ts2JavaAstBinExpr().manipulate(functionContext, expression.as(Swc4jAstBinExpr.class)); - case Ident: - return new Ts2JavaAstIdent().manipulate(functionContext, expression.as(Swc4jAstIdent.class)); - case Number: - return new Ts2JavaAstNumber().manipulate(functionContext, expression.as(Swc4jAstNumber.class)); - case UnaryExpr: - return new Ts2JavaAstUnaryExpr().manipulate(functionContext, expression.as(Swc4jAstUnaryExpr.class)); - default: - throw new Ts2JavaAstException( - expression, - SimpleFreeMarkerFormat.format("BinExpr left expr type ${exprType} is not supported.", - SimpleMap.of("exprType", expression.getType().name()))); - } - } -} diff --git a/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstBinaryOp.java b/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstBinaryOp.java deleted file mode 100644 index 9541fdd..0000000 --- a/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstBinaryOp.java +++ /dev/null @@ -1,326 +0,0 @@ -/* - * 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.buddy.ts2java.ast; - -import com.caoccao.javet.buddy.ts2java.compiler.JavaByteCodeHint; -import com.caoccao.javet.buddy.ts2java.compiler.JavaFunctionContext; -import com.caoccao.javet.buddy.ts2java.compiler.JavaLogicalLabels; -import com.caoccao.javet.buddy.ts2java.compiler.instructions.IJavaInstructionLogical; -import com.caoccao.javet.buddy.ts2java.compiler.instructions.JavaInstructionLogicalCompare; -import com.caoccao.javet.buddy.ts2java.compiler.instructions.JavaInstructionLogicalCondition; -import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaAstException; -import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaException; -import com.caoccao.javet.swc4j.ast.enums.Swc4jAstBinaryOp; -import com.caoccao.javet.swc4j.ast.expr.Swc4jAstBinExpr; -import com.caoccao.javet.utils.SimpleFreeMarkerFormat; -import com.caoccao.javet.utils.SimpleMap; -import net.bytebuddy.description.type.TypeDescription; -import net.bytebuddy.implementation.Implementation; -import net.bytebuddy.implementation.bytecode.*; -import net.bytebuddy.jar.asm.Label; -import net.bytebuddy.jar.asm.MethodVisitor; -import net.bytebuddy.jar.asm.Opcodes; - -import java.util.List; - -public final class Ts2JavaAstBinaryOp { - public static final String JAVA_LANG_MATH = "java/lang/Math"; - - private Ts2JavaAstBinaryOp() { - } - - private static Addition getAddition(TypeDescription type) { - if (type.represents(int.class)) { - return Addition.INTEGER; - } else if (type.represents(long.class)) { - return Addition.LONG; - } else if (type.represents(float.class)) { - return Addition.FLOAT; - } else if (type.represents(double.class)) { - return Addition.DOUBLE; - } - throw new Ts2JavaException( - SimpleFreeMarkerFormat.format("Unsupported type ${type} in addition.", - SimpleMap.of("type", type.getName()))); - } - - public static StackManipulation getBitAndStackManipulation(JavaFunctionContext functionContext) { - return new StackManipulation.Simple(( - MethodVisitor methodVisitor, - Implementation.Context implementationContext) -> { - // TODO - return new StackManipulation.Size(-1, 0); - }); - } - - public static StackManipulation getBitOrStackManipulation(JavaFunctionContext functionContext) { - return new StackManipulation.Simple(( - MethodVisitor methodVisitor, - Implementation.Context implementationContext) -> { - // TODO - return new StackManipulation.Size(-1, 0); - }); - } - - private static Division getDivision(TypeDescription type) { - if (type.represents(int.class)) { - return Division.INTEGER; - } else if (type.represents(long.class)) { - return Division.LONG; - } else if (type.represents(float.class)) { - return Division.FLOAT; - } else if (type.represents(double.class)) { - return Division.DOUBLE; - } - throw new Ts2JavaException( - SimpleFreeMarkerFormat.format("Unsupported type ${type} in division.", - SimpleMap.of("type", type.getName()))); - } - - private static StackManipulation getExp() { - return new StackManipulation.Simple(( - MethodVisitor methodVisitor, - Implementation.Context implementationContext) -> { - methodVisitor.visitMethodInsn( - Opcodes.INVOKESTATIC, - JAVA_LANG_MATH, - "pow", - "(DD)D", - false); - return new StackManipulation.Size(-2, 0); - }); - } - - private static Multiplication getMultiplication(TypeDescription type) { - if (type.represents(int.class)) { - return Multiplication.INTEGER; - } else if (type.represents(long.class)) { - return Multiplication.LONG; - } else if (type.represents(float.class)) { - return Multiplication.FLOAT; - } else if (type.represents(double.class)) { - return Multiplication.DOUBLE; - } - throw new Ts2JavaException( - SimpleFreeMarkerFormat.format("Unsupported type ${type} in multiplication.", - SimpleMap.of("type", type.getName()))); - } - - private static Remainder getRemainder(TypeDescription type) { - if (type.represents(int.class)) { - return Remainder.INTEGER; - } else if (type.represents(long.class)) { - return Remainder.LONG; - } else if (type.represents(float.class)) { - return Remainder.FLOAT; - } else if (type.represents(double.class)) { - return Remainder.DOUBLE; - } - throw new Ts2JavaException( - SimpleFreeMarkerFormat.format("Unsupported type ${type} in mod.", - SimpleMap.of("type", type.getName()))); - } - - private static ShiftLeft getShiftLeft(TypeDescription type) { - if (type.represents(int.class)) { - return ShiftLeft.INTEGER; - } else if (type.represents(long.class)) { - return ShiftLeft.LONG; - } - throw new Ts2JavaException( - SimpleFreeMarkerFormat.format("Unsupported type ${type} in left shift.", - SimpleMap.of("type", type.getName()))); - } - - private static ShiftRight getShiftRight(TypeDescription type) { - if (type.represents(int.class)) { - return ShiftRight.INTEGER; - } else if (type.represents(long.class)) { - return ShiftRight.LONG; - } - throw new Ts2JavaException( - SimpleFreeMarkerFormat.format("Unsupported type ${type} in right shift.", - SimpleMap.of("type", type.getName()))); - } - - private static Subtraction getSubtraction(TypeDescription type) { - if (type.represents(int.class)) { - return Subtraction.INTEGER; - } else if (type.represents(long.class)) { - return Subtraction.LONG; - } else if (type.represents(float.class)) { - return Subtraction.FLOAT; - } else if (type.represents(double.class)) { - return Subtraction.DOUBLE; - } - throw new Ts2JavaException( - SimpleFreeMarkerFormat.format("Unsupported type ${type} in subtraction.", - SimpleMap.of("type", type.getName()))); - } - - private static StackManipulation getZeroFillShiftRight(TypeDescription type) { - if (type.represents(int.class)) { - return new StackManipulation.Simple( - (MethodVisitor methodVisitor, Implementation.Context implementationContext) -> { - methodVisitor.visitInsn(Opcodes.IUSHR); - return new StackManipulation.Size(-1, 0); - }); - } else if (type.represents(long.class)) { - return new StackManipulation.Simple( - (MethodVisitor methodVisitor, Implementation.Context implementationContext) -> { - methodVisitor.visitInsn(Opcodes.L2I); - methodVisitor.visitInsn(Opcodes.LUSHR); - return new StackManipulation.Size(-2, 0); - }); - } - throw new Ts2JavaException( - SimpleFreeMarkerFormat.format("Unsupported type ${type} in zero fill right shift.", - SimpleMap.of("type", type.getName()))); - } - - public static void manipulateArithmetic( - List stackManipulations, - Swc4jAstBinaryOp binaryOp, - JavaByteCodeHint hint) { - switch (binaryOp) { - case Add: - stackManipulations.add(getAddition(hint.getType())); - break; - case Div: - stackManipulations.add(getDivision(hint.getType())); - break; - case LShift: - stackManipulations.add(getShiftLeft(hint.getType())); - break; - case Mod: - stackManipulations.add(getRemainder(hint.getType())); - break; - case Mul: - stackManipulations.add(getMultiplication(hint.getType())); - break; - case RShift: - stackManipulations.add(getShiftRight(hint.getType())); - break; - case Sub: - stackManipulations.add(getSubtraction(hint.getType())); - break; - case ZeroFillRShift: - stackManipulations.add(getZeroFillShiftRight(hint.getType())); - break; - case Exp: - stackManipulations.add(getExp()); - break; - default: - throw new Ts2JavaException( - SimpleFreeMarkerFormat.format("Binary op ${op} is not supported.", - SimpleMap.of("op", binaryOp.name()))); - } - } - - public static void manipulateLogicalAnd( - JavaFunctionContext functionContext, - Swc4jAstBinExpr binExpr, - int leftEndIndex, - JavaByteCodeHint leftHint, - JavaByteCodeHint rightHint) { - if (!leftHint.getType().represents(boolean.class)) { - throw new Ts2JavaAstException( - binExpr.getLeft(), - SimpleFreeMarkerFormat.format("Unsupported left type ${type} in logical AND (&&).", - SimpleMap.of("type", leftHint.getType().getName()))); - } - if (!rightHint.getType().represents(boolean.class)) { - throw new Ts2JavaAstException( - binExpr.getRight(), - SimpleFreeMarkerFormat.format("Unsupported right type ${type} in logical AND (&&).", - SimpleMap.of("type", rightHint.getType().getName()))); - } - final List stackManipulations = functionContext.getStackManipulations(); - final Label labelFalse = functionContext.getLogicalLabels().getLastLabel(); - if (!(stackManipulations.get(leftEndIndex - 1) instanceof IJavaInstructionLogical)) { - stackManipulations.add(leftEndIndex, new JavaInstructionLogicalCondition(Opcodes.IFEQ, labelFalse)); - ++leftEndIndex; - } - if (!(stackManipulations.get(stackManipulations.size() - 1) instanceof IJavaInstructionLogical)) { - final Label label = functionContext.getLogicalLabels().size() > 2 - ? functionContext.getLogicalLabels().getByReverseIndex(1) - : labelFalse; - stackManipulations.add(new JavaInstructionLogicalCondition(Opcodes.IFEQ, label)); - } - } - - public static void manipulateLogicalCompare( - JavaFunctionContext functionContext, - Swc4jAstBinExpr binExpr, - Swc4jAstBinaryOp binaryOp, - JavaByteCodeHint hint) { - JavaInstructionLogicalCompare instruction = new JavaInstructionLogicalCompare( - binExpr, - binaryOp, - hint.getType(), - functionContext.getLogicalLabels().getLastLabel()); - functionContext.getStackManipulations().add(instruction); - } - - public static void manipulateLogicalOr( - JavaFunctionContext functionContext, - Swc4jAstBinExpr binExpr, - int leftEndIndex, - JavaByteCodeHint leftHint, - JavaByteCodeHint rightHint) { - if (!leftHint.getType().represents(boolean.class)) { - throw new Ts2JavaAstException( - binExpr.getLeft(), - SimpleFreeMarkerFormat.format("Unsupported left type ${type} in logical OR (||).", - SimpleMap.of("type", leftHint.getType().getName()))); - } - if (!rightHint.getType().represents(boolean.class)) { - throw new Ts2JavaAstException( - binExpr.getRight(), - SimpleFreeMarkerFormat.format("Unsupported right type ${type} in logical OR (||).", - SimpleMap.of("type", rightHint.getType().getName()))); - } - final List stackManipulations = functionContext.getStackManipulations(); - final JavaLogicalLabels logicalLabels = functionContext.getLogicalLabels(); - final Label labelFalse = logicalLabels.getByReverseIndex(1); - final Label labelTrue = logicalLabels.getLastLabel(); - StackManipulation leftStackManipulation = stackManipulations.get(leftEndIndex - 1); - if (leftStackManipulation instanceof IJavaInstructionLogical) { - ((IJavaInstructionLogical) leftStackManipulation).flip().setLabel(labelTrue); - } else { - stackManipulations.add( - leftEndIndex, - new JavaInstructionLogicalCondition(Opcodes.IFGT, labelTrue)); - ++leftEndIndex; - } - StackManipulation rightStackManipulation = stackManipulations.get(stackManipulations.size() - 1); - if (rightStackManipulation instanceof IJavaInstructionLogical) { - ((IJavaInstructionLogical) rightStackManipulation).setLabel(labelFalse); - } else { - stackManipulations.add(new JavaInstructionLogicalCondition(Opcodes.IFLE, labelFalse)); - } - final int logicalLabelSize = logicalLabels.size(); - if (logicalLabelSize == 3) { - stackManipulations.add(new StackManipulation.Simple( - (MethodVisitor methodVisitor, Implementation.Context implementationContext) -> { - methodVisitor.visitLabel(labelTrue); - methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null); - return StackManipulation.Size.ZERO; - })); - } - } -} diff --git a/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstIdent.java b/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstIdent.java deleted file mode 100644 index 4935dfd..0000000 --- a/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstIdent.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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.buddy.ts2java.ast; - -import com.caoccao.javet.buddy.ts2java.compiler.JavaByteCodeHint; -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.implementation.bytecode.StackManipulation; -import net.bytebuddy.implementation.bytecode.member.MethodVariableAccess; - -public final class Ts2JavaAstIdent implements ITs2JavaAstStackManipulation { - @Override - public JavaByteCodeHint manipulate(JavaFunctionContext functionContext, Swc4jAstIdent ast) { - Ts2JavaAst.manipulateLineNumber(functionContext, 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 new JavaByteCodeHint(localVariable.getType()); - } -} diff --git a/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstNumber.java b/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstNumber.java deleted file mode 100644 index 74949ec..0000000 --- a/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstNumber.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * 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.buddy.ts2java.ast; - -import com.caoccao.javet.buddy.ts2java.compiler.JavaByteCodeHint; -import com.caoccao.javet.buddy.ts2java.compiler.JavaFunctionContext; -import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaAstException; -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; - -public final class Ts2JavaAstNumber implements ITs2JavaAstStackManipulation { - private TypeDescription valueType; - - public Ts2JavaAstNumber() { - valueType = null; - } - - public TypeDescription getValueType() { - return valueType; - } - - @Override - public JavaByteCodeHint manipulate(JavaFunctionContext functionContext, Swc4jAstNumber ast) { - Ts2JavaAst.manipulateLineNumber(functionContext, ast); - StackManipulation stackManipulation; - TypeDescription type = valueType; - if (type == null) { - type = 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)); - } - final boolean isNegative = ast.getMinusCount() % 2 == 1; - if (type.represents(int.class) || type.represents(short.class) || type.represents(byte.class)) { - stackManipulation = IntegerConstant.forValue(isNegative ? -ast.asInt() : ast.asInt()); - } else if (type.represents(long.class)) { - stackManipulation = LongConstant.forValue(isNegative ? -ast.asLong() : ast.asLong()); - } else if (type.represents(float.class)) { - stackManipulation = FloatConstant.forValue(isNegative ? -ast.asFloat() : ast.asFloat()); - } else if (type.represents(double.class)) { - stackManipulation = DoubleConstant.forValue(isNegative ? -ast.asDouble() : ast.asDouble()); - } else { - throw new Ts2JavaAstException( - ast, - SimpleFreeMarkerFormat.format("Number type ${type} is not supported.", - SimpleMap.of("type", type.getName()))); - } - functionContext.getStackManipulations().add(stackManipulation); - return new JavaByteCodeHint(type); - } - - public Ts2JavaAstNumber setValueType(TypeDescription valueType) { - this.valueType = valueType; - return this; - } -} diff --git a/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstUnaryExpr.java b/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstUnaryExpr.java deleted file mode 100644 index c8a90ef..0000000 --- a/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstUnaryExpr.java +++ /dev/null @@ -1,130 +0,0 @@ -/* - * 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.buddy.ts2java.ast; - -import com.caoccao.javet.buddy.ts2java.compiler.JavaByteCodeHint; -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; -import com.caoccao.javet.utils.SimpleFreeMarkerFormat; -import com.caoccao.javet.utils.SimpleMap; -import net.bytebuddy.description.type.TypeDescription; -import net.bytebuddy.implementation.Implementation; -import net.bytebuddy.implementation.bytecode.StackManipulation; -import net.bytebuddy.jar.asm.MethodVisitor; -import net.bytebuddy.jar.asm.Opcodes; - -public final class Ts2JavaAstUnaryExpr implements ITs2JavaAstStackManipulation { - private int getOpcodeNegative(Swc4jAstUnaryExpr ast, TypeDescription type) { - if (type.represents(int.class) - || type.represents(byte.class) - || type.represents(char.class) - || type.represents(short.class)) { - return Opcodes.INEG; - } else if (type.represents(long.class)) { - return Opcodes.LNEG; - } else if (type.represents(float.class)) { - return Opcodes.FNEG; - } else if (type.represents(double.class)) { - return Opcodes.DNEG; - } else { - throw new Ts2JavaAstException( - ast, - SimpleFreeMarkerFormat.format("Minus cannot be applied to type ${type}.", - SimpleMap.of("type", type.getName()))); - } - } - - @Override - public JavaByteCodeHint manipulate(JavaFunctionContext functionContext, Swc4jAstUnaryExpr ast) { - Ts2JavaAst.manipulateLineNumber(functionContext, ast); - JavaByteCodeHint hint; - ISwc4jAstExpr arg = ast.getArg().unParenExpr(); - switch (ast.getOp()) { - case Bang: { - switch (arg.getType()) { - case BinExpr: - hint = new Ts2JavaAstBinExpr() - .manipulate(functionContext, arg.as(Swc4jAstBinExpr.class)); - break; - case Ident: - hint = new Ts2JavaAstIdent() - .manipulate(functionContext, arg.as(Swc4jAstIdent.class)); - break; - case UnaryExpr: - hint = new Ts2JavaAstUnaryExpr() - .manipulate(functionContext, arg.as(Swc4jAstUnaryExpr.class)); - break; - default: - throw new Ts2JavaAstException( - arg, - SimpleFreeMarkerFormat.format("UnaryExpr arg type ${argType} for ! is not supported.", - SimpleMap.of("argType", arg.getType().name()))); - } - break; - } - case Minus: { - boolean opcodeNegativeRequired = true; - switch (arg.getType()) { - case BinExpr: - hint = new Ts2JavaAstBinExpr() - .manipulate(functionContext, arg.as(Swc4jAstBinExpr.class)); - break; - case Ident: - hint = new Ts2JavaAstIdent() - .manipulate(functionContext, arg.as(Swc4jAstIdent.class)); - break; - case Number: - opcodeNegativeRequired = false; - hint = new Ts2JavaAstNumber() - .manipulate(functionContext, arg.as(Swc4jAstNumber.class)); - break; - case UnaryExpr: - hint = new Ts2JavaAstUnaryExpr() - .manipulate(functionContext, arg.as(Swc4jAstUnaryExpr.class)); - break; - default: - throw new Ts2JavaAstException( - arg, - SimpleFreeMarkerFormat.format("UnaryExpr arg type ${argType} for - is not supported.", - SimpleMap.of("argType", arg.getType().name()))); - } - if (opcodeNegativeRequired) { - final int opcode = getOpcodeNegative(ast, hint.getType()); - StackManipulation stackManipulation = new StackManipulation.Simple(( - MethodVisitor methodVisitor, - Implementation.Context implementationContext) -> { - methodVisitor.visitInsn(opcode); - return StackManipulation.Size.ZERO; - }); - functionContext.getStackManipulations().add(stackManipulation); - } - break; - } - default: - throw new Ts2JavaAstException( - ast, - SimpleFreeMarkerFormat.format("UnaryExpr op ${op} is not supported.", - SimpleMap.of("op", ast.getOp().name()))); - } - return hint; - } -}