Skip to content

Commit

Permalink
✨ feat: Add number
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Oct 31, 2024
1 parent 90ad4d0 commit 1cd89a7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -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<Swc4jAstNumber> {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Swc4jAstVarDecl> {
Expand All @@ -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: {
Expand Down

0 comments on commit 1cd89a7

Please sign in to comment.