From 1cd89a7d8bb31d790981104bb683f4ea6a454c0a Mon Sep 17 00:00:00 2001 From: Sam Cao Date: Thu, 31 Oct 2024 13:48:26 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20Add=20number?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../buddy/ts2java/ast/Ts2JavaAstNumber.java | 64 +++++++++++++++++++ .../buddy/ts2java/ast/Ts2JavaAstVarDecl.java | 23 +------ 2 files changed, 66 insertions(+), 21 deletions(-) create mode 100644 src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstNumber.java 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 new file mode 100644 index 0000000..1d1cc6b --- /dev/null +++ b/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstNumber.java @@ -0,0 +1,64 @@ +/* + * 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.exceptions.Ts2JavaAstException; +import com.caoccao.javet.swc4j.ast.expr.lit.Swc4jAstNumber; +import com.caoccao.javet.utils.SimpleFreeMarkerFormat; +import com.caoccao.javet.utils.SimpleMap; +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 { + private final TypeDescription valueType; + + public Ts2JavaAstNumber(TypeDescription valueType) { + this.valueType = Objects.requireNonNull(valueType); + } + + public TypeDescription getValueType() { + return valueType; + } + + @Override + public TypeDescription manipulate(JavaFunctionContext functionContext, Swc4jAstNumber ast) { + StackManipulation stackManipulation; + if (valueType.represents(int.class) || valueType.represents(short.class) || valueType.represents(byte.class)) { + stackManipulation = IntegerConstant.forValue(ast.asInt()); + } else if (valueType.represents(long.class)) { + stackManipulation = LongConstant.forValue(ast.asLong()); + } else if (valueType.represents(float.class)) { + stackManipulation = FloatConstant.forValue(ast.asFloat()); + } else if (valueType.represents(double.class)) { + stackManipulation = DoubleConstant.forValue(ast.asDouble()); + } else { + throw new Ts2JavaAstException( + ast, + SimpleFreeMarkerFormat.format("Number type ${type} is not supported.", + SimpleMap.of("type", valueType.getName()))); + } + functionContext.getStackManipulations().add(stackManipulation); + return valueType; + } +} diff --git a/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstVarDecl.java b/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstVarDecl.java index 9ae7622..4d858a7 100644 --- a/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstVarDecl.java +++ b/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstVarDecl.java @@ -32,10 +32,6 @@ import com.caoccao.javet.utils.SimpleMap; 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 net.bytebuddy.implementation.bytecode.member.MethodVariableAccess; public final class Ts2JavaAstVarDecl implements ITs2JavaAstStackManipulation { @@ -55,23 +51,8 @@ public TypeDescription manipulate(JavaFunctionContext functionContext, Swc4jAstV ISwc4jAstExpr expression = varDeclarator.getInit().get(); switch (expression.getType()) { case Number: { - Swc4jAstNumber value = expression.as(Swc4jAstNumber.class); - StackManipulation stackManipulation; - if (variableType.represents(int.class)) { - stackManipulation = IntegerConstant.forValue(value.asInt()); - } else if (variableType.represents(long.class)) { - stackManipulation = LongConstant.forValue(value.asLong()); - } else if (variableType.represents(float.class)) { - stackManipulation = FloatConstant.forValue(value.asFloat()); - } else if (variableType.represents(double.class)) { - stackManipulation = DoubleConstant.forValue(value.asDouble()); - } else { - throw new Ts2JavaAstException( - expression, - SimpleFreeMarkerFormat.format("VarDecl init type ${type} is not supported.", - SimpleMap.of("type", expression.getType().name()))); - } - functionContext.getStackManipulations().add(stackManipulation); + new Ts2JavaAstNumber(variableType) + .manipulate(functionContext, expression.as(Swc4jAstNumber.class)); break; } case Ident: {